2014-01-27 13:09:37 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/HttpController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "HttpController", ->
|
|
|
|
beforeEach ->
|
|
|
|
@HttpController = SandboxedModule.require modulePath, requires:
|
|
|
|
"logger-sharelatex": { log: sinon.stub() }
|
2014-03-05 10:59:40 -05:00
|
|
|
"./UpdatesManager": @UpdatesManager = {}
|
2014-03-04 10:27:03 -05:00
|
|
|
"./DiffManager": @DiffManager = {}
|
2014-03-10 12:58:26 -04:00
|
|
|
"./RestoreManager": @RestoreManager = {}
|
2014-01-27 13:09:37 -05:00
|
|
|
@doc_id = "doc-id-123"
|
2014-03-04 10:27:03 -05:00
|
|
|
@project_id = "project-id-123"
|
2014-01-27 13:09:37 -05:00
|
|
|
@next = sinon.stub()
|
2014-03-11 09:01:07 -04:00
|
|
|
@user_id = "mock-user-123"
|
2014-03-11 13:39:40 -04:00
|
|
|
@now = Date.now()
|
2014-01-27 13:09:37 -05:00
|
|
|
|
2014-02-26 06:34:56 -05:00
|
|
|
describe "flushUpdatesWithLock", ->
|
2014-01-27 13:09:37 -05:00
|
|
|
beforeEach ->
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
doc_id: @doc_id
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
2014-03-05 10:59:40 -05:00
|
|
|
@UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(1)
|
2014-02-26 06:34:56 -05:00
|
|
|
@HttpController.flushUpdatesWithLock @req, @res, @next
|
2014-01-27 13:09:37 -05:00
|
|
|
|
2014-02-26 06:34:56 -05:00
|
|
|
it "should process the updates", ->
|
2014-03-05 10:59:40 -05:00
|
|
|
@UpdatesManager.processUncompressedUpdatesWithLock
|
2014-02-26 06:34:56 -05:00
|
|
|
.calledWith(@doc_id)
|
2014-01-27 13:09:37 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a success code", ->
|
2014-03-04 10:27:03 -05:00
|
|
|
@res.send.calledWith(204).should.equal true
|
|
|
|
|
|
|
|
describe "getDiff", ->
|
|
|
|
beforeEach ->
|
2014-03-10 12:58:26 -04:00
|
|
|
@from = 42
|
|
|
|
@to = 45
|
2014-03-04 10:27:03 -05:00
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
doc_id: @doc_id
|
|
|
|
project_id: @project_id
|
|
|
|
query:
|
|
|
|
from: @from.toString()
|
|
|
|
to: @to.toString()
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
@diff = [ u: "mock-diff" ]
|
|
|
|
@DiffManager.getDiff = sinon.stub().callsArgWith(4, null, @diff)
|
|
|
|
@HttpController.getDiff @req, @res, @next
|
|
|
|
|
|
|
|
it "should get the diff", ->
|
|
|
|
@DiffManager.getDiff
|
|
|
|
.calledWith(@project_id, @doc_id, parseInt(@from, 10), parseInt(@to, 10))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the diff", ->
|
|
|
|
@res.send.calledWith(JSON.stringify(diff: @diff)).should.equal true
|
2014-03-05 10:59:40 -05:00
|
|
|
|
|
|
|
describe "getUpdates", ->
|
|
|
|
beforeEach ->
|
2014-03-10 12:58:26 -04:00
|
|
|
@to = 42
|
2014-03-05 10:59:40 -05:00
|
|
|
@limit = 10
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
doc_id: @doc_id
|
|
|
|
project_id: @project_id
|
|
|
|
query:
|
|
|
|
to: @to.toString()
|
|
|
|
limit: @limit.toString()
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
2014-03-11 13:39:40 -04:00
|
|
|
@rawUpdates = ["raw-updates"]
|
|
|
|
@updatesView = ["updates-view"]
|
|
|
|
@HttpController._buildUpdatesView = sinon.stub().returns(@updatesView)
|
2014-03-06 13:04:00 -05:00
|
|
|
@UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @rawUpdates)
|
2014-03-05 10:59:40 -05:00
|
|
|
@HttpController.getUpdates @req, @res, @next
|
|
|
|
|
|
|
|
it "should get the updates", ->
|
2014-03-06 13:04:00 -05:00
|
|
|
@UpdatesManager.getUpdatesWithUserInfo
|
2014-03-05 10:59:40 -05:00
|
|
|
.calledWith(@doc_id, to: @to, limit: @limit)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-03-11 13:39:40 -04:00
|
|
|
it "should build the updates view", ->
|
|
|
|
@HttpController._buildUpdatesView
|
|
|
|
.calledWith(@rawUpdates)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-03-06 13:04:00 -05:00
|
|
|
it "should return the formatted updates", ->
|
2014-03-11 13:39:40 -04:00
|
|
|
@res.send.calledWith(JSON.stringify(updates: @updatesView)).should.equal true
|
|
|
|
|
|
|
|
describe "_buildUpdatesView", ->
|
|
|
|
it "should concat updates that are close in time", ->
|
|
|
|
expect(@HttpController._buildUpdatesView [{
|
|
|
|
meta:
|
|
|
|
user: @user_2 = { id: "mock-user-2" }
|
|
|
|
start_ts: @now + 20
|
|
|
|
end_ts: @now + 30
|
|
|
|
v: 5
|
|
|
|
}, {
|
|
|
|
meta:
|
|
|
|
user: @user_1 = { id: "mock-user-1" }
|
|
|
|
start_ts: @now
|
|
|
|
end_ts: @now + 10
|
|
|
|
v: 4
|
|
|
|
}]).to.deep.equal [{
|
|
|
|
meta:
|
|
|
|
users: [@user_1, @user_2]
|
|
|
|
start_ts: @now
|
|
|
|
end_ts: @now + 30
|
|
|
|
fromV: 4
|
|
|
|
toV: 5
|
|
|
|
}]
|
|
|
|
|
|
|
|
it "should leave updates that are far apart in time", ->
|
|
|
|
oneDay = 1000 * 60 * 60 * 24
|
|
|
|
expect(@HttpController._buildUpdatesView [{
|
|
|
|
meta:
|
|
|
|
user: @user_2 = { id: "mock-user-2" }
|
|
|
|
start_ts: @now + oneDay
|
|
|
|
end_ts: @now + oneDay + 10
|
|
|
|
v: 5
|
|
|
|
}, {
|
|
|
|
meta:
|
|
|
|
user: @user_1 = { id: "mock-user-2" }
|
|
|
|
start_ts: @now
|
|
|
|
end_ts: @now + 10
|
|
|
|
v: 4
|
|
|
|
}]).to.deep.equal [{
|
|
|
|
meta:
|
|
|
|
users: [@user_2]
|
|
|
|
start_ts: @now + oneDay
|
|
|
|
end_ts: @now + oneDay + 10
|
|
|
|
fromV: 5
|
|
|
|
toV: 5
|
|
|
|
}, {
|
|
|
|
meta:
|
|
|
|
users: [@user_1]
|
|
|
|
start_ts: @now
|
|
|
|
end_ts: @now + 10
|
|
|
|
fromV: 4
|
|
|
|
toV: 4
|
|
|
|
}]
|
2014-03-10 12:58:26 -04:00
|
|
|
|
|
|
|
describe "RestoreManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@version = "42"
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
doc_id: @doc_id
|
|
|
|
project_id: @project_id
|
|
|
|
version: @version
|
2014-03-11 09:01:07 -04:00
|
|
|
headers:
|
|
|
|
"x-user-id": @user_id
|
2014-03-10 12:58:26 -04:00
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
|
2014-03-11 09:01:07 -04:00
|
|
|
@RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4)
|
2014-03-10 12:58:26 -04:00
|
|
|
@HttpController.restore @req, @res, @next
|
|
|
|
|
|
|
|
it "should restore the document", ->
|
|
|
|
@RestoreManager.restoreToBeforeVersion
|
2014-03-11 09:01:07 -04:00
|
|
|
.calledWith(@project_id, @doc_id, parseInt(@version, 10), @user_id)
|
2014-03-10 12:58:26 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a success code", ->
|
|
|
|
@res.send.calledWith(204).should.equal true
|