| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- /* 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.
- /*
- * 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
- */
- const sinon = require('sinon')
- const chai = require('chai')
- chai.should()
- const { expect } = chai
- const { db, ObjectId } = require('../../../app/js/mongodb')
- const Settings = require('settings-sharelatex')
- 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')
- describe('Archiving updates', function () {
- before(function (done) {
- if (
- __guard__(
- __guard__(
- Settings != null ? Settings.trackchanges : undefined,
- (x1) => x1.s3
- ),
- (x) => x.key.length
- ) < 1
- ) {
- const message = new Error('s3 keys not setup, this test setup will fail')
- return done(message)
- }
- return TrackChangesClient.waitForS3(done)
- })
- before(function (done) {
- this.now = Date.now()
- this.to = this.now
- this.user_id = ObjectId().toString()
- this.user_id_2 = ObjectId().toString()
- 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: {
- versioning: true
- }
- }
- sinon.spy(MockWebApi, 'getProjectDetails')
- MockWebApi.users[this.user_id] = this.user = {
- email: 'user@sharelatex.com',
- first_name: 'Leo',
- last_name: 'Lion',
- id: this.user_id
- }
- sinon.spy(MockWebApi, 'getUserInfo')
- MockDocStoreApi.docs[this.doc_id] = this.doc = {
- _id: this.doc_id,
- project_id: this.project_id
- }
- 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 },
- v: 2 * i + 1
- })
- this.updates.push({
- op: [{ i: 'b', p: 0 }],
- meta: {
- ts: this.now + (i - 2048) * this.hours + 10 * this.minutes,
- user_id: this.user_id_2
- },
- v: 2 * i + 2
- })
- }
- TrackChangesApp.ensureRunning(() => {
- return TrackChangesClient.pushRawUpdates(
- this.project_id,
- this.doc_id,
- this.updates,
- (error) => {
- if (error != null) {
- throw error
- }
- return TrackChangesClient.flushDoc(
- this.project_id,
- this.doc_id,
- (error) => {
- if (error != null) {
- throw error
- }
- return done()
- }
- )
- }
- )
- })
- return null
- })
- after(function (done) {
- MockWebApi.getUserInfo.restore()
- return db.docHistory.deleteMany(
- { 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
- )
- }
- )
- }
- )
- })
- function testExportFeature() {
- describe('exporting the project', function () {
- before('fetch export', function (done) {
- TrackChangesClient.exportProject(
- this.project_id,
- (error, updates, userIds) => {
- if (error) {
- return done(error)
- }
- this.exportedUpdates = updates
- this.exportedUserIds = userIds
- done()
- }
- )
- })
- 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()
- .map((update) => {
- // 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)
- expect(this.exportedUserIds).to.deep.equal([
- this.user_id,
- this.user_id_2
- ])
- })
- })
- }
- describe("before archiving a doc's updates", function () {
- testExportFeature()
- })
- describe("archiving a doc's updates", function () {
- before(function (done) {
- TrackChangesClient.pushDocHistory(
- this.project_id,
- this.doc_id,
- (error) => {
- if (error != null) {
- throw error
- }
- return done()
- }
- )
- return null
- })
- it('should have one cached pack', function (done) {
- 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()
- }
- )
- })
- it('should have one remaining pack after cache is expired', function (done) {
- return db.docHistory.deleteMany(
- {
- doc_id: ObjectId(this.doc_id),
- expiresAt: { $exists: true }
- },
- (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()
- }
- )
- }
- )
- })
- it('should have a docHistoryIndex entry marked as inS3', function (done) {
- 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()
- }
- )
- })
- it('should have a docHistoryIndex entry with the last version', function (done) {
- 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()
- }
- )
- })
- it('should store 1024 doc changes in S3 in one pack', function (done) {
- 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()
- }
- )
- }
- )
- })
- testExportFeature()
- })
- return describe("unarchiving a doc's updates", function () {
- before(function (done) {
- TrackChangesClient.pullDocHistory(
- this.project_id,
- this.doc_id,
- (error) => {
- if (error != null) {
- throw error
- }
- return done()
- }
- )
- return null
- })
- return it('should restore both packs', function (done) {
- return db.docHistory.count(
- { doc_id: ObjectId(this.doc_id) },
- (error, count) => {
- if (error != null) {
- throw error
- }
- count.should.equal(2)
- return done()
- }
- )
- })
- })
- })
- function __guard__(value, transform) {
- return typeof value !== 'undefined' && value !== null
- ? transform(value)
- : undefined
- }
|