2017-01-09 08:34:10 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
modulePath = "../../../../app/js/HttpController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
Errors = require "../../../../app/js/Errors.js"
|
|
|
|
|
|
|
|
describe "HttpController", ->
|
|
|
|
beforeEach ->
|
|
|
|
@HttpController = SandboxedModule.require modulePath, requires:
|
|
|
|
"./DocumentManager": @DocumentManager = {}
|
2018-02-22 05:01:05 -05:00
|
|
|
"./HistoryManager": @HistoryManager =
|
|
|
|
flushProjectChangesAsync: sinon.stub()
|
2017-01-09 08:34:10 -05:00
|
|
|
"./ProjectManager": @ProjectManager = {}
|
|
|
|
"logger-sharelatex" : @logger = { log: sinon.stub() }
|
|
|
|
"./Metrics": @Metrics = {}
|
2017-08-10 09:57:40 -04:00
|
|
|
"./Errors" : Errors
|
2017-01-09 08:34:10 -05:00
|
|
|
@Metrics.Timer = class Timer
|
|
|
|
done: sinon.stub()
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@next = sinon.stub()
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
2019-02-12 11:45:11 -05:00
|
|
|
json: sinon.stub()
|
2017-01-09 08:34:10 -05:00
|
|
|
|
|
|
|
describe "getDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@lines = ["one", "two", "three"]
|
|
|
|
@ops = ["mock-op-1", "mock-op-2"]
|
|
|
|
@version = 42
|
|
|
|
@fromVersion = 42
|
|
|
|
@ranges = { changes: "mock", comments: "mock" }
|
2017-11-06 12:18:28 -05:00
|
|
|
@pathname = '/a/b/c'
|
2017-01-09 08:34:10 -05:00
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
|
|
|
|
describe "when the document exists and no recent ops are requested", ->
|
|
|
|
beforeEach ->
|
2017-11-06 12:18:28 -05:00
|
|
|
@DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, @lines, @version, [], @ranges, @pathname)
|
2017-01-09 08:34:10 -05:00
|
|
|
@HttpController.getDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should get the doc", ->
|
|
|
|
@DocumentManager.getDocAndRecentOpsWithLock
|
|
|
|
.calledWith(@project_id, @doc_id, -1)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the doc as JSON", ->
|
2019-02-12 11:45:11 -05:00
|
|
|
@res.json
|
|
|
|
.calledWith({
|
2017-01-09 08:34:10 -05:00
|
|
|
id: @doc_id
|
|
|
|
lines: @lines
|
|
|
|
version: @version
|
|
|
|
ops: []
|
|
|
|
ranges: @ranges
|
2017-11-06 12:18:28 -05:00
|
|
|
pathname: @pathname
|
2019-02-12 11:45:11 -05:00
|
|
|
})
|
2017-01-09 08:34:10 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(doc_id: @doc_id, project_id: @project_id, "getting doc via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when recent ops are requested", ->
|
|
|
|
beforeEach ->
|
2019-02-12 11:45:11 -05:00
|
|
|
@DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, @lines, @version, @ops, @ranges, @pathname)
|
2017-01-09 08:34:10 -05:00
|
|
|
@req.query = fromVersion: "#{@fromVersion}"
|
|
|
|
@HttpController.getDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should get the doc", ->
|
|
|
|
@DocumentManager.getDocAndRecentOpsWithLock
|
|
|
|
.calledWith(@project_id, @doc_id, @fromVersion)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the doc as JSON", ->
|
2019-02-12 11:45:11 -05:00
|
|
|
@res.json
|
|
|
|
.calledWith({
|
2017-01-09 08:34:10 -05:00
|
|
|
id: @doc_id
|
|
|
|
lines: @lines
|
|
|
|
version: @version
|
|
|
|
ops: @ops
|
2019-02-12 11:45:11 -05:00
|
|
|
ranges: @ranges
|
|
|
|
pathname: @pathname
|
|
|
|
})
|
2017-01-09 08:34:10 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(doc_id: @doc_id, project_id: @project_id, "getting doc via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when the document does not exist", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, null, null, null)
|
|
|
|
@HttpController.getDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with NotFoundError", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Errors.NotFoundError("not found"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.getDocAndRecentOpsWithLock = sinon.stub().callsArgWith(3, new Error("oops"), null, null)
|
|
|
|
@HttpController.getDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "setDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@lines = ["one", "two", "three"]
|
|
|
|
@source = "dropbox"
|
|
|
|
@user_id = "user-id-123"
|
|
|
|
@req =
|
|
|
|
headers: {}
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
body:
|
|
|
|
lines: @lines
|
|
|
|
source: @source
|
|
|
|
user_id: @user_id
|
2017-03-21 07:20:38 -04:00
|
|
|
undoing: @undoing = true
|
2017-01-09 08:34:10 -05:00
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
2017-03-21 07:20:38 -04:00
|
|
|
@DocumentManager.setDocWithLock = sinon.stub().callsArgWith(6)
|
2017-01-09 08:34:10 -05:00
|
|
|
@HttpController.setDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should set the doc", ->
|
|
|
|
@DocumentManager.setDocWithLock
|
2017-03-21 07:20:38 -04:00
|
|
|
.calledWith(@project_id, @doc_id, @lines, @source, @user_id, @undoing)
|
2017-01-09 08:34:10 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
2017-03-21 07:20:38 -04:00
|
|
|
.calledWith(doc_id: @doc_id, project_id: @project_id, lines: @lines, source: @source, user_id: @user_id, undoing: @undoing, "setting doc via http")
|
2017-01-09 08:34:10 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
2017-03-21 07:20:38 -04:00
|
|
|
@DocumentManager.setDocWithLock = sinon.stub().callsArgWith(6, new Error("oops"))
|
2017-01-09 08:34:10 -05:00
|
|
|
@HttpController.setDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when the payload is too large", ->
|
|
|
|
beforeEach ->
|
|
|
|
lines = []
|
|
|
|
for _ in [0..200000]
|
|
|
|
lines.push "test test test"
|
|
|
|
@req.body.lines = lines
|
2017-03-21 07:20:38 -04:00
|
|
|
@DocumentManager.setDocWithLock = sinon.stub().callsArgWith(6)
|
2017-01-09 08:34:10 -05:00
|
|
|
@HttpController.setDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it 'should send back a 406 response', ->
|
|
|
|
@res.send.calledWith(406).should.equal true
|
|
|
|
|
|
|
|
it 'should not call setDocWithLock', ->
|
|
|
|
@DocumentManager.setDocWithLock.callCount.should.equal 0
|
|
|
|
|
|
|
|
describe "flushProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectManager.flushProjectWithLocks = sinon.stub().callsArgWith(1)
|
|
|
|
@HttpController.flushProject(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should flush the project", ->
|
|
|
|
@ProjectManager.flushProjectWithLocks
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(project_id: @project_id, "flushing project via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectManager.flushProjectWithLocks = sinon.stub().callsArgWith(1, new Error("oops"))
|
|
|
|
@HttpController.flushProject(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "flushDocIfLoaded", ->
|
|
|
|
beforeEach ->
|
|
|
|
@lines = ["one", "two", "three"]
|
|
|
|
@version = 42
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArgWith(2)
|
|
|
|
@HttpController.flushDocIfLoaded(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should flush the doc", ->
|
|
|
|
@DocumentManager.flushDocIfLoadedWithLock
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(doc_id: @doc_id, project_id: @project_id, "flushing doc via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArgWith(2, new Error("oops"))
|
|
|
|
@HttpController.flushDocIfLoaded(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "flushAndDeleteDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArgWith(2)
|
|
|
|
@HttpController.flushAndDeleteDoc(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should flush and delete the doc", ->
|
|
|
|
@DocumentManager.flushAndDeleteDocWithLock
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
2018-02-22 05:01:05 -05:00
|
|
|
it "should flush project history", ->
|
|
|
|
@HistoryManager.flushProjectChangesAsync
|
|
|
|
.calledWithExactly(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
2017-01-09 08:34:10 -05:00
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(doc_id: @doc_id, project_id: @project_id, "deleting doc via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArgWith(2, new Error("oops"))
|
|
|
|
@HttpController.flushAndDeleteDoc(@req, @res, @next)
|
|
|
|
|
2018-02-22 05:01:05 -05:00
|
|
|
it "should flush project history", ->
|
|
|
|
@HistoryManager.flushProjectChangesAsync
|
|
|
|
.calledWithExactly(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
2017-01-09 08:34:10 -05:00
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "deleteProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectManager.flushAndDeleteProjectWithLocks = sinon.stub().callsArgWith(1)
|
|
|
|
@HttpController.deleteProject(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should delete the project", ->
|
|
|
|
@ProjectManager.flushAndDeleteProjectWithLocks
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(project_id: @project_id, "deleting project via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectManager.flushAndDeleteProjectWithLocks = sinon.stub().callsArgWith(1, new Error("oops"))
|
|
|
|
@HttpController.deleteProject(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
2017-01-09 08:41:18 -05:00
|
|
|
.should.equal true
|
|
|
|
|
2017-05-08 11:34:17 -04:00
|
|
|
describe "acceptChanges", ->
|
2017-01-09 08:41:18 -05:00
|
|
|
beforeEach ->
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
change_id: @change_id = "mock-change-od-1"
|
|
|
|
|
2017-05-09 11:16:25 -04:00
|
|
|
describe "successfully with a single change", ->
|
2017-01-09 08:41:18 -05:00
|
|
|
beforeEach ->
|
2017-05-08 11:34:17 -04:00
|
|
|
@DocumentManager.acceptChangesWithLock = sinon.stub().callsArgWith(3)
|
|
|
|
@HttpController.acceptChanges(@req, @res, @next)
|
2017-01-09 08:41:18 -05:00
|
|
|
|
|
|
|
it "should accept the change", ->
|
2017-05-08 11:34:17 -04:00
|
|
|
@DocumentManager.acceptChangesWithLock
|
|
|
|
.calledWith(@project_id, @doc_id, [ @change_id ])
|
2017-01-09 08:41:18 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
2017-05-08 11:34:17 -04:00
|
|
|
.calledWith({@project_id, @doc_id}, "accepting 1 changes via http")
|
2017-01-09 08:41:18 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
2017-05-09 11:16:25 -04:00
|
|
|
describe "succesfully with with multiple changes", ->
|
|
|
|
beforeEach ->
|
|
|
|
@change_ids = [ "mock-change-od-1", "mock-change-od-2", "mock-change-od-3", "mock-change-od-4" ]
|
|
|
|
@req.body =
|
|
|
|
change_ids: @change_ids
|
|
|
|
@DocumentManager.acceptChangesWithLock = sinon.stub().callsArgWith(3)
|
|
|
|
@HttpController.acceptChanges(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should accept the changes in the body payload", ->
|
|
|
|
@DocumentManager.acceptChangesWithLock
|
|
|
|
.calledWith(@project_id, @doc_id, @change_ids)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request with the correct number of changes", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith({@project_id, @doc_id}, "accepting #{ @change_ids.length } changes via http")
|
|
|
|
.should.equal true
|
|
|
|
|
2017-01-09 08:41:18 -05:00
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
2017-05-08 11:34:17 -04:00
|
|
|
@DocumentManager.acceptChangesWithLock = sinon.stub().callsArgWith(3, new Error("oops"))
|
|
|
|
@HttpController.acceptChanges(@req, @res, @next)
|
2017-01-09 08:41:18 -05:00
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
2017-01-24 09:57:11 -05:00
|
|
|
|
|
|
|
describe "deleteComment", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
comment_id: @comment_id = "mock-comment-id"
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.deleteCommentWithLock = sinon.stub().callsArgWith(3)
|
|
|
|
@HttpController.deleteComment(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should accept the change", ->
|
|
|
|
@DocumentManager.deleteCommentWithLock
|
|
|
|
.calledWith(@project_id, @doc_id, @comment_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith({@project_id, @doc_id, @comment_id}, "deleting comment via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentManager.deleteCommentWithLock = sinon.stub().callsArgWith(3, new Error("oops"))
|
|
|
|
@HttpController.deleteComment(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
2017-08-10 09:57:40 -04:00
|
|
|
|
2017-10-11 10:29:57 -04:00
|
|
|
describe "getProjectDocsAndFlushIfOld", ->
|
2017-08-10 09:57:40 -04:00
|
|
|
beforeEach ->
|
|
|
|
@state = "01234567890abcdef"
|
|
|
|
@docs = [{_id: "1234", lines: "hello", v: 23}, {_id: "4567", lines: "world", v: 45}]
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
query:
|
|
|
|
state: @state
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
2017-10-11 10:29:57 -04:00
|
|
|
@ProjectManager.getProjectDocsAndFlushIfOld = sinon.stub().callsArgWith(3,null, @docs)
|
|
|
|
@HttpController.getProjectDocsAndFlushIfOld(@req, @res, @next)
|
2017-08-10 09:57:40 -04:00
|
|
|
|
|
|
|
it "should get docs from the project manager", ->
|
2017-10-11 10:29:57 -04:00
|
|
|
@ProjectManager.getProjectDocsAndFlushIfOld
|
2017-08-10 09:57:40 -04:00
|
|
|
.calledWith(@project_id, @state, {})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(@docs)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the request", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith({project_id: @project_id, exclude: []}, "getting docs via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log the response", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith({project_id: @project_id, result: ["1234:23", "4567:45"]}, "got docs via http")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when there is a conflict", ->
|
|
|
|
beforeEach ->
|
2017-10-11 10:29:57 -04:00
|
|
|
@ProjectManager.getProjectDocsAndFlushIfOld = sinon.stub().callsArgWith(3, new Errors.ProjectStateChangedError("project state changed"))
|
|
|
|
@HttpController.getProjectDocsAndFlushIfOld(@req, @res, @next)
|
2017-08-10 09:57:40 -04:00
|
|
|
|
|
|
|
it "should return an HTTP 409 Conflict response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(409)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when an error occurs", ->
|
|
|
|
beforeEach ->
|
2017-10-11 10:29:57 -04:00
|
|
|
@ProjectManager.getProjectDocsAndFlushIfOld = sinon.stub().callsArgWith(3, new Error("oops"))
|
|
|
|
@HttpController.getProjectDocsAndFlushIfOld(@req, @res, @next)
|
2017-08-10 09:57:40 -04:00
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
2017-11-01 15:16:49 -04:00
|
|
|
|
|
|
|
describe "updateProject", ->
|
|
|
|
beforeEach ->
|
2018-04-23 07:08:04 -04:00
|
|
|
@projectHistoryId = "history-id-123"
|
2017-11-01 15:16:49 -04:00
|
|
|
@userId = "user-id-123"
|
|
|
|
@docUpdates = sinon.stub()
|
2017-11-06 11:14:27 -05:00
|
|
|
@fileUpdates = sinon.stub()
|
2018-03-05 07:14:47 -05:00
|
|
|
@version = 1234567
|
2017-11-01 15:16:49 -04:00
|
|
|
@req =
|
2018-04-23 07:08:04 -04:00
|
|
|
body: {@projectHistoryId, @userId, @docUpdates, @fileUpdates, @version}
|
2017-11-01 15:16:49 -04:00
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
2018-04-23 07:08:04 -04:00
|
|
|
@ProjectManager.updateProjectWithLocks = sinon.stub().callsArgWith(6)
|
2017-11-01 15:16:49 -04:00
|
|
|
@HttpController.updateProject(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should accept the change", ->
|
|
|
|
@ProjectManager.updateProjectWithLocks
|
2018-04-23 07:08:04 -04:00
|
|
|
.calledWith(@project_id, @projectHistoryId, @userId, @docUpdates, @fileUpdates, @version)
|
2017-11-01 15:16:49 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
2018-04-23 07:08:04 -04:00
|
|
|
@ProjectManager.updateProjectWithLocks = sinon.stub().callsArgWith(6, new Error("oops"))
|
2017-11-01 15:16:49 -04:00
|
|
|
@HttpController.updateProject(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|
2018-03-07 08:29:53 -05:00
|
|
|
|
|
|
|
describe "resyncProjectHistory", ->
|
|
|
|
beforeEach ->
|
2018-04-23 07:08:04 -04:00
|
|
|
@projectHistoryId = "history-id-123"
|
2018-03-07 08:29:53 -05:00
|
|
|
@docs = sinon.stub()
|
|
|
|
@files = sinon.stub()
|
|
|
|
@fileUpdates = sinon.stub()
|
|
|
|
@req =
|
|
|
|
body:
|
2018-04-23 07:08:04 -04:00
|
|
|
{@projectHistoryId, @docs, @files}
|
2018-03-07 08:29:53 -05:00
|
|
|
params:
|
|
|
|
project_id: @project_id
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
2018-04-23 07:08:04 -04:00
|
|
|
@HistoryManager.resyncProjectHistory = sinon.stub().callsArgWith(4)
|
2018-03-07 08:29:53 -05:00
|
|
|
@HttpController.resyncProjectHistory(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should accept the change", ->
|
|
|
|
@HistoryManager.resyncProjectHistory
|
2018-04-23 07:08:04 -04:00
|
|
|
.calledWith(@project_id, @projectHistoryId, @docs, @files)
|
2018-03-07 08:29:53 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful No Content response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when an errors occurs", ->
|
|
|
|
beforeEach ->
|
2018-04-23 07:08:04 -04:00
|
|
|
@HistoryManager.resyncProjectHistory = sinon.stub().callsArgWith(4, new Error("oops"))
|
2018-03-07 08:29:53 -05:00
|
|
|
@HttpController.resyncProjectHistory(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the error", ->
|
|
|
|
@next
|
|
|
|
.calledWith(new Error("oops"))
|
|
|
|
.should.equal true
|