overleaf/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee

125 lines
3.8 KiB
CoffeeScript
Raw Normal View History

2015-09-02 17:47:34 -04: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"
request = require "request"
rclient = require("redis").createClient(Settings.redis.history) # Only works locally for now
2015-09-02 17:47:34 -04:00
TrackChangesApp = require "./helpers/TrackChangesApp"
2015-09-02 17:47:34 -04:00
TrackChangesClient = require "./helpers/TrackChangesClient"
MockDocStoreApi = require "./helpers/MockDocStoreApi"
MockWebApi = require "./helpers/MockWebApi"
describe "Archiving updates", ->
before (done) ->
@now = Date.now()
@to = @now
@user_id = ObjectId().toString()
@doc_id = ObjectId().toString()
@project_id = ObjectId().toString()
@minutes = 60 * 1000
@hours = 60 * @minutes
MockWebApi.projects[@project_id] =
features:
versioning: true
2015-09-16 18:33:23 -04:00
sinon.spy MockWebApi, "getProjectDetails"
2015-09-02 17:47:34 -04: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"
2015-09-02 17:47:34 -04:00
MockDocStoreApi.docs[@doc_id] = @doc =
_id: @doc_id
project_id: @project_id
sinon.spy MockDocStoreApi, "getAllDoc"
@updates = []
2016-03-10 10:15:57 -05:00
for i in [0..512+10]
2015-09-02 17:47:34 -04:00
@updates.push {
op: [{ i: "a", p: 0 }]
2016-03-10 10:15:57 -05:00
meta: { ts: @now + (i-2048) * @hours, user_id: @user_id }
2015-09-02 17:47:34 -04:00
v: 2 * i + 1
}
@updates.push {
op: [{ i: "b", p: 0 }]
2016-03-10 10:15:57 -05:00
meta: { ts: @now + (i-2048) * @hours + 10*@minutes, user_id: @user_id }
2015-09-02 17:47:34 -04:00
v: 2 * i + 2
}
TrackChangesApp.ensureRunning =>
TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) =>
2015-09-02 17:47:34 -04:00
throw error if error?
TrackChangesClient.flushDoc @project_id, @doc_id, (error) ->
throw error if error?
done()
2015-09-02 17:47:34 -04:00
2015-09-03 07:36:32 -04:00
after (done) ->
2015-09-16 18:33:23 -04:00
MockWebApi.getUserInfo.restore()
2016-03-10 10:15:57 -05:00
db.docHistory.remove {project_id: ObjectId(@project_id)}, () =>
db.docHistoryIndex.remove {project_id: ObjectId(@project_id)}, () =>
TrackChangesClient.removeS3Doc @project_id, @doc_id, done
2015-09-02 17:47:34 -04:00
describe "archiving a doc's updates", ->
before (done) ->
2016-03-09 11:56:49 -05:00
TrackChangesClient.pushDocHistory @project_id, @doc_id, (error) ->
2015-09-02 17:47:34 -04:00
throw error if error?
done()
2016-03-09 11:56:49 -05:00
it "should have one cached pack", (done) ->
db.docHistory.count { doc_id: ObjectId(@doc_id), expiresAt:{$exists:true}}, (error, count) ->
2015-09-02 17:47:34 -04:00
throw error if error?
2016-03-09 11:56:49 -05:00
count.should.equal 1
2015-09-02 17:47:34 -04:00
done()
2016-03-09 11:56:49 -05:00
it "should have one remaining pack after cache is expired", (done) ->
db.docHistory.remove {
doc_id: ObjectId(@doc_id),
expiresAt:{$exists:true}
}, (err, result) =>
2015-09-02 17:47:34 -04:00
throw error if error?
2016-03-09 11:56:49 -05:00
db.docHistory.count { doc_id: ObjectId(@doc_id)}, (error, count) ->
throw error if error?
count.should.equal 1
done()
2015-09-02 17:47:34 -04:00
2016-03-09 11:56:49 -05:00
it "should have a docHistoryIndex entry marked as inS3", (done) ->
db.docHistoryIndex.findOne { _id: ObjectId(@doc_id) }, (error, index) ->
2015-09-02 17:47:34 -04:00
throw error if error?
2016-03-09 11:56:49 -05:00
index.packs[0].inS3.should.equal true
2015-09-03 07:36:32 -04:00
done()
2016-03-09 11:56:49 -05:00
it "should have a docHistoryIndex entry with the last version", (done) ->
db.docHistoryIndex.findOne { _id: ObjectId(@doc_id) }, (error, index) ->
throw error if error?
2016-03-10 10:15:57 -05:00
index.packs[0].v_end.should.equal 1024
2015-09-03 07:36:32 -04:00
done()
2016-03-10 10:15:57 -05:00
it "should store 1024 doc changes in S3 in one pack", (done) ->
db.docHistoryIndex.findOne { _id: ObjectId(@doc_id) }, (error, index) =>
throw error if error?
pack_id = index.packs[0]._id
TrackChangesClient.getS3Doc @project_id, @doc_id, pack_id, (error, doc) =>
doc.n.should.equal 1024
doc.pack.length.should.equal 1024
done()
2016-03-09 11:56:49 -05:00
2015-09-03 07:36:32 -04:00
describe "unarchiving a doc's updates", ->
before (done) ->
2016-03-09 11:56:49 -05:00
TrackChangesClient.pullDocHistory @project_id, @doc_id, (error) ->
2015-09-03 07:36:32 -04:00
throw error if error?
done()
2016-03-09 11:56:49 -05:00
it "should restore both packs", (done) ->
2015-09-03 07:36:32 -04:00
db.docHistory.count { doc_id: ObjectId(@doc_id) }, (error, count) ->
throw error if error?
2016-03-09 11:56:49 -05:00
count.should.equal 2
2015-09-03 07:36:32 -04:00
done()