2014-03-05 10:59:40 -05:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
mongojs = require "../../../app/js/mongojs"
|
|
|
|
db = mongojs.db
|
|
|
|
ObjectId = mongojs.ObjectId
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
|
|
|
|
TrackChangesClient = require "./helpers/TrackChangesClient"
|
2014-03-06 13:04:00 -05:00
|
|
|
MockWebApi = require "./helpers/MockWebApi"
|
2014-03-05 10:59:40 -05:00
|
|
|
|
|
|
|
describe "Getting updates", ->
|
2014-03-18 14:09:25 -04:00
|
|
|
before (done) ->
|
2014-03-05 10:59:40 -05:00
|
|
|
@now = Date.now()
|
|
|
|
@to = @now
|
|
|
|
@user_id = ObjectId().toString()
|
2015-09-10 10:40:43 -04:00
|
|
|
@deleted_user_id = 'deleted_user'
|
2014-03-18 14:09:25 -04:00
|
|
|
@doc_id = ObjectId().toString()
|
|
|
|
@project_id = ObjectId().toString()
|
2014-03-05 10:59:40 -05:00
|
|
|
|
|
|
|
@minutes = 60 * 1000
|
2014-03-28 12:01:34 -04:00
|
|
|
@hours = 60 * @minutes
|
|
|
|
|
|
|
|
MockWebApi.projects[@project_id] =
|
|
|
|
features:
|
|
|
|
versioning: true
|
2014-03-05 10:59:40 -05:00
|
|
|
|
2014-03-06 13:04:00 -05:00
|
|
|
MockWebApi.users[@user_id] = @user =
|
|
|
|
email: "user@sharelatex.com"
|
|
|
|
first_name: "Leo"
|
|
|
|
last_name: "Lion"
|
|
|
|
id: @user_id
|
2015-09-16 18:33:23 -04:00
|
|
|
sinon.spy MockWebApi, "getUserInfo"
|
2014-03-06 13:04:00 -05:00
|
|
|
|
2014-03-18 14:09:25 -04:00
|
|
|
@updates = []
|
|
|
|
for i in [0..9]
|
|
|
|
@updates.push {
|
|
|
|
op: [{ i: "a", p: 0 }]
|
2014-03-28 12:01:34 -04:00
|
|
|
meta: { ts: @now - (9 - i) * @hours - 2 * @minutes, user_id: @user_id }
|
2014-03-18 14:09:25 -04:00
|
|
|
v: 2 * i + 1
|
|
|
|
}
|
|
|
|
@updates.push {
|
|
|
|
op: [{ i: "b", p: 0 }]
|
2014-03-28 12:01:34 -04:00
|
|
|
meta: { ts: @now - (9 - i) * @hours, user_id: @user_id }
|
2014-03-18 14:09:25 -04:00
|
|
|
v: 2 * i + 2
|
|
|
|
}
|
2015-09-10 10:40:43 -04:00
|
|
|
@updates[0].meta.user_id = @deleted_user_id
|
2014-03-18 14:09:25 -04:00
|
|
|
|
2014-03-21 09:17:58 -04:00
|
|
|
TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) =>
|
2014-03-18 14:09:25 -04:00
|
|
|
throw error if error?
|
2014-03-21 09:48:14 -04:00
|
|
|
done()
|
2014-03-18 14:09:25 -04:00
|
|
|
|
2014-03-11 13:39:40 -04:00
|
|
|
after: () ->
|
2015-09-16 18:33:23 -04:00
|
|
|
MockWebApi.getUserInfo.restore()
|
2014-03-05 10:59:40 -05:00
|
|
|
|
2014-03-18 14:09:25 -04:00
|
|
|
describe "getting updates up to the limit", ->
|
2014-03-11 13:39:40 -04:00
|
|
|
before (done) ->
|
2014-03-20 08:10:04 -04:00
|
|
|
TrackChangesClient.getUpdates @project_id, { before: @to + 1, min_count: 3 }, (error, body) =>
|
2014-03-05 10:59:40 -05:00
|
|
|
throw error if error?
|
2014-03-18 14:09:25 -04:00
|
|
|
@updates = body.updates
|
|
|
|
done()
|
2014-03-05 10:59:40 -05:00
|
|
|
|
2014-03-11 13:39:40 -04:00
|
|
|
it "should fetch the user details from the web api", ->
|
2015-09-16 18:33:23 -04:00
|
|
|
MockWebApi.getUserInfo
|
2014-03-11 13:39:40 -04:00
|
|
|
.calledWith(@user_id)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-03-20 08:10:04 -04:00
|
|
|
it "should return at least the min_count number of summarized updates", ->
|
2014-03-20 09:37:23 -04:00
|
|
|
docs1 = {}
|
|
|
|
docs1[@doc_id] = toV: 20, fromV: 19
|
|
|
|
docs2 = {}
|
|
|
|
docs2[@doc_id] = toV: 18, fromV: 17
|
|
|
|
docs3 = {}
|
|
|
|
docs3[@doc_id] = toV: 16, fromV: 15
|
2014-03-20 08:10:04 -04:00
|
|
|
expect(@updates.slice(0,3)).to.deep.equal [{
|
2014-03-20 09:37:23 -04:00
|
|
|
docs: docs1
|
2014-03-11 13:39:40 -04:00
|
|
|
meta:
|
2014-03-18 14:09:25 -04:00
|
|
|
start_ts: @to - 2 * @minutes
|
2014-03-11 13:39:40 -04:00
|
|
|
end_ts: @to
|
|
|
|
users: [@user]
|
|
|
|
}, {
|
2014-03-20 09:37:23 -04:00
|
|
|
docs: docs2
|
2014-03-11 13:39:40 -04:00
|
|
|
meta:
|
2014-03-28 12:01:34 -04:00
|
|
|
start_ts: @to - 1 * @hours - 2 * @minutes
|
|
|
|
end_ts: @to - 1 * @hours
|
2014-03-18 14:09:25 -04:00
|
|
|
users: [@user]
|
2014-03-11 13:39:40 -04:00
|
|
|
}, {
|
2014-03-20 09:37:23 -04:00
|
|
|
docs: docs3
|
2014-03-18 14:09:25 -04:00
|
|
|
meta:
|
2014-03-28 12:01:34 -04:00
|
|
|
start_ts: @to - 2 * @hours - 2 * @minutes
|
|
|
|
end_ts: @to - 2 * @hours
|
2014-03-18 14:09:25 -04:00
|
|
|
users: [@user]
|
2014-03-11 13:39:40 -04:00
|
|
|
}]
|
|
|
|
|
2014-03-18 14:09:25 -04:00
|
|
|
describe "getting updates beyond the end of the database", ->
|
|
|
|
before (done) ->
|
2014-03-28 12:01:34 -04:00
|
|
|
TrackChangesClient.getUpdates @project_id, { before: @to - 8 * @hours + 1, min_count: 30 }, (error, body) =>
|
2014-03-11 13:39:40 -04:00
|
|
|
throw error if error?
|
2014-03-18 14:09:25 -04:00
|
|
|
@updates = body.updates
|
|
|
|
done()
|
2014-03-06 13:04:00 -05:00
|
|
|
|
2014-03-18 14:09:25 -04:00
|
|
|
it "should return as many updates as it can", ->
|
2014-03-20 09:37:23 -04:00
|
|
|
docs1 = {}
|
|
|
|
docs1[@doc_id] = toV: 4, fromV: 3
|
|
|
|
docs2 = {}
|
|
|
|
docs2[@doc_id] = toV: 2, fromV: 1
|
2014-03-11 13:39:40 -04:00
|
|
|
expect(@updates).to.deep.equal [{
|
2014-03-20 09:37:23 -04:00
|
|
|
docs: docs1
|
2014-03-11 13:39:40 -04:00
|
|
|
meta:
|
2014-03-28 12:01:34 -04:00
|
|
|
start_ts: @to - 8 * @hours - 2 * @minutes
|
|
|
|
end_ts: @to - 8 * @hours
|
2014-03-18 14:09:25 -04:00
|
|
|
users: [@user]
|
|
|
|
}, {
|
2014-03-20 09:37:23 -04:00
|
|
|
docs: docs2
|
2014-03-18 14:09:25 -04:00
|
|
|
meta:
|
2014-03-28 12:01:34 -04:00
|
|
|
start_ts: @to - 9 * @hours - 2 * @minutes
|
|
|
|
end_ts: @to - 9 * @hours
|
2015-09-10 10:40:43 -04:00
|
|
|
users: [@user, null]
|
2014-03-11 13:39:40 -04:00
|
|
|
}]
|