2020-02-17 12:35:50 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
no-undef,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:35:39 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS202: Simplify dynamic range loops
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:35:59 -05:00
|
|
|
const sinon = require('sinon')
|
2021-03-23 15:08:32 -04:00
|
|
|
const { expect } = require('chai')
|
2020-09-10 11:48:09 -04:00
|
|
|
const { db, ObjectId } = require('../../../app/js/mongodb')
|
2021-07-12 12:47:16 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-02-17 12:35:59 -05:00
|
|
|
const request = require('request')
|
|
|
|
const rclient = require('redis').createClient(Settings.redis.history) // Only works locally for now
|
|
|
|
|
|
|
|
const TrackChangesApp = require('./helpers/TrackChangesApp')
|
|
|
|
const TrackChangesClient = require('./helpers/TrackChangesClient')
|
|
|
|
const MockDocStoreApi = require('./helpers/MockDocStoreApi')
|
|
|
|
const MockWebApi = require('./helpers/MockWebApi')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('Archiving updates', function () {
|
|
|
|
before(function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
if (
|
|
|
|
__guard__(
|
|
|
|
__guard__(
|
|
|
|
Settings != null ? Settings.trackchanges : undefined,
|
2021-07-13 07:04:43 -04:00
|
|
|
x1 => x1.s3
|
2020-02-17 12:35:59 -05:00
|
|
|
),
|
2021-07-13 07:04:43 -04:00
|
|
|
x => x.key.length
|
2020-02-17 12:35:59 -05:00
|
|
|
) < 1
|
|
|
|
) {
|
|
|
|
const message = new Error('s3 keys not setup, this test setup will fail')
|
|
|
|
return done(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return TrackChangesClient.waitForS3(done)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
before(function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
this.now = Date.now()
|
|
|
|
this.to = this.now
|
|
|
|
this.user_id = ObjectId().toString()
|
2021-02-25 04:52:54 -05:00
|
|
|
this.user_id_2 = ObjectId().toString()
|
2020-02-17 12:35:59 -05:00
|
|
|
this.doc_id = ObjectId().toString()
|
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
|
|
|
|
this.minutes = 60 * 1000
|
|
|
|
this.hours = 60 * this.minutes
|
|
|
|
|
|
|
|
MockWebApi.projects[this.project_id] = {
|
|
|
|
features: {
|
2021-07-13 07:04:43 -04:00
|
|
|
versioning: true,
|
|
|
|
},
|
2020-02-17 12:35:59 -05:00
|
|
|
}
|
|
|
|
sinon.spy(MockWebApi, 'getProjectDetails')
|
|
|
|
|
|
|
|
MockWebApi.users[this.user_id] = this.user = {
|
|
|
|
email: 'user@sharelatex.com',
|
|
|
|
first_name: 'Leo',
|
|
|
|
last_name: 'Lion',
|
2021-07-13 07:04:43 -04:00
|
|
|
id: this.user_id,
|
2020-02-17 12:35:59 -05:00
|
|
|
}
|
|
|
|
sinon.spy(MockWebApi, 'getUserInfo')
|
|
|
|
|
|
|
|
MockDocStoreApi.docs[this.doc_id] = this.doc = {
|
|
|
|
_id: this.doc_id,
|
2021-07-13 07:04:43 -04:00
|
|
|
project_id: this.project_id,
|
2020-02-17 12:35:59 -05:00
|
|
|
}
|
|
|
|
sinon.spy(MockDocStoreApi, 'getAllDoc')
|
|
|
|
|
|
|
|
this.updates = []
|
|
|
|
for (
|
|
|
|
let i = 0, end = 512 + 10, asc = end >= 0;
|
|
|
|
asc ? i <= end : i >= end;
|
|
|
|
asc ? i++ : i--
|
|
|
|
) {
|
|
|
|
this.updates.push({
|
|
|
|
op: [{ i: 'a', p: 0 }],
|
|
|
|
meta: { ts: this.now + (i - 2048) * this.hours, user_id: this.user_id },
|
2021-07-13 07:04:43 -04:00
|
|
|
v: 2 * i + 1,
|
2020-02-17 12:35:59 -05:00
|
|
|
})
|
|
|
|
this.updates.push({
|
|
|
|
op: [{ i: 'b', p: 0 }],
|
|
|
|
meta: {
|
|
|
|
ts: this.now + (i - 2048) * this.hours + 10 * this.minutes,
|
2021-07-13 07:04:43 -04:00
|
|
|
user_id: this.user_id_2,
|
2020-02-17 12:35:59 -05:00
|
|
|
},
|
2021-07-13 07:04:43 -04:00
|
|
|
v: 2 * i + 2,
|
2020-02-17 12:35:59 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
TrackChangesApp.ensureRunning(() => {
|
|
|
|
return TrackChangesClient.pushRawUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.updates,
|
2021-07-13 07:04:43 -04:00
|
|
|
error => {
|
2020-02-17 12:35:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return TrackChangesClient.flushDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2021-07-13 07:04:43 -04:00
|
|
|
error => {
|
2020-02-17 12:35:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
after(function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
MockWebApi.getUserInfo.restore()
|
2020-09-10 11:48:09 -04:00
|
|
|
return db.docHistory.deleteMany(
|
2020-02-17 12:35:59 -05:00
|
|
|
{ project_id: ObjectId(this.project_id) },
|
|
|
|
() => {
|
|
|
|
return db.docHistoryIndex.remove(
|
|
|
|
{ project_id: ObjectId(this.project_id) },
|
|
|
|
() => {
|
|
|
|
return TrackChangesClient.removeS3Doc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
done
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-02-23 08:57:27 -05:00
|
|
|
function testExportFeature() {
|
|
|
|
describe('exporting the project', function () {
|
|
|
|
before('fetch export', function (done) {
|
2021-02-25 04:52:54 -05:00
|
|
|
TrackChangesClient.exportProject(
|
|
|
|
this.project_id,
|
|
|
|
(error, updates, userIds) => {
|
|
|
|
if (error) {
|
|
|
|
return done(error)
|
|
|
|
}
|
|
|
|
this.exportedUpdates = updates
|
|
|
|
this.exportedUserIds = userIds
|
|
|
|
done()
|
2021-02-23 08:57:27 -05:00
|
|
|
}
|
2021-02-25 04:52:54 -05:00
|
|
|
)
|
2021-02-23 08:57:27 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should include all the imported updates, with ids, sorted by timestamp', function () {
|
|
|
|
// Add a safe guard for an empty array matching an empty export.
|
|
|
|
expect(this.updates).to.have.length(1024 + 22)
|
|
|
|
|
|
|
|
const expectedExportedUpdates = this.updates
|
|
|
|
.slice()
|
|
|
|
.reverse()
|
2021-07-13 07:04:43 -04:00
|
|
|
.map(update => {
|
2021-02-23 08:57:27 -05:00
|
|
|
// clone object, updates are created once in before handler
|
|
|
|
const exportedUpdate = Object.assign({}, update)
|
|
|
|
exportedUpdate.meta = Object.assign({}, update.meta)
|
|
|
|
|
|
|
|
exportedUpdate.doc_id = this.doc_id
|
|
|
|
exportedUpdate.project_id = this.project_id
|
|
|
|
|
|
|
|
// This is for merged updates, which does not apply here.
|
|
|
|
exportedUpdate.meta.start_ts = exportedUpdate.meta.end_ts =
|
|
|
|
exportedUpdate.meta.ts
|
|
|
|
delete exportedUpdate.meta.ts
|
|
|
|
return exportedUpdate
|
|
|
|
})
|
|
|
|
expect(this.exportedUpdates).to.deep.equal(expectedExportedUpdates)
|
2021-02-25 04:52:54 -05:00
|
|
|
expect(this.exportedUserIds).to.deep.equal([
|
|
|
|
this.user_id,
|
2021-07-13 07:04:43 -04:00
|
|
|
this.user_id_2,
|
2021-02-25 04:52:54 -05:00
|
|
|
])
|
2021-02-23 08:57:27 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("before archiving a doc's updates", function () {
|
|
|
|
testExportFeature()
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe("archiving a doc's updates", function () {
|
|
|
|
before(function (done) {
|
2021-07-13 07:04:43 -04:00
|
|
|
TrackChangesClient.pushDocHistory(this.project_id, this.doc_id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-02-17 12:35:59 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
return done()
|
|
|
|
})
|
2020-02-17 12:35:59 -05:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should have one cached pack', function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
return db.docHistory.count(
|
|
|
|
{ doc_id: ObjectId(this.doc_id), expiresAt: { $exists: true } },
|
|
|
|
(error, count) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
count.should.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should have one remaining pack after cache is expired', function (done) {
|
2020-09-10 11:48:09 -04:00
|
|
|
return db.docHistory.deleteMany(
|
2020-02-17 12:35:59 -05:00
|
|
|
{
|
|
|
|
doc_id: ObjectId(this.doc_id),
|
2021-07-13 07:04:43 -04:00
|
|
|
expiresAt: { $exists: true },
|
2020-02-17 12:35:59 -05:00
|
|
|
},
|
|
|
|
(err, result) => {
|
|
|
|
if (typeof error !== 'undefined' && error !== null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return db.docHistory.count(
|
|
|
|
{ doc_id: ObjectId(this.doc_id) },
|
|
|
|
(error, count) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
count.should.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should have a docHistoryIndex entry marked as inS3', function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
return db.docHistoryIndex.findOne(
|
|
|
|
{ _id: ObjectId(this.doc_id) },
|
|
|
|
(error, index) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
index.packs[0].inS3.should.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should have a docHistoryIndex entry with the last version', function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
return db.docHistoryIndex.findOne(
|
|
|
|
{ _id: ObjectId(this.doc_id) },
|
|
|
|
(error, index) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
index.packs[0].v_end.should.equal(1024)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-02-23 08:57:27 -05:00
|
|
|
it('should store 1024 doc changes in S3 in one pack', function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
return db.docHistoryIndex.findOne(
|
|
|
|
{ _id: ObjectId(this.doc_id) },
|
|
|
|
(error, index) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const pack_id = index.packs[0]._id
|
|
|
|
return TrackChangesClient.getS3Doc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
pack_id,
|
|
|
|
(error, doc) => {
|
|
|
|
doc.n.should.equal(1024)
|
|
|
|
doc.pack.length.should.equal(1024)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2021-02-23 08:57:27 -05:00
|
|
|
|
|
|
|
testExportFeature()
|
2020-02-17 12:35:59 -05:00
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe("unarchiving a doc's updates", function () {
|
|
|
|
before(function (done) {
|
2021-07-13 07:04:43 -04:00
|
|
|
TrackChangesClient.pullDocHistory(this.project_id, this.doc_id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-02-17 12:35:59 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
return done()
|
|
|
|
})
|
2020-02-17 12:35:59 -05:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should restore both packs', function (done) {
|
2020-02-17 12:35:59 -05:00
|
|
|
return db.docHistory.count(
|
|
|
|
{ doc_id: ObjectId(this.doc_id) },
|
|
|
|
(error, count) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
count.should.equal(2)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-02-17 12:35:39 -05:00
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
2020-02-17 12:35:59 -05:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|