2020-02-17 12:35:01 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:50 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:35:16 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const should = chai.should()
|
|
|
|
const { expect } = chai
|
|
|
|
const modulePath = '../../../../app/js/MongoManager.js'
|
|
|
|
const packModulePath = '../../../../app/js/PackManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-09-10 07:58:06 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-02-17 12:35:16 -05:00
|
|
|
const tk = require('timekeeper')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('MongoManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
tk.freeze(new Date())
|
|
|
|
this.MongoManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-09-10 07:58:06 -04:00
|
|
|
'./mongodb': { db: (this.db = {}), ObjectId },
|
2020-02-17 12:35:16 -05:00
|
|
|
'./PackManager': (this.PackManager = {}),
|
2020-11-25 06:57:20 -05:00
|
|
|
'@overleaf/metrics': { timeAsyncMethod() {} },
|
2020-02-17 12:35:16 -05:00
|
|
|
'logger-sharelatex': { log() {} }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.doc_id = ObjectId().toString()
|
|
|
|
return (this.project_id = ObjectId().toString())
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
afterEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return tk.reset()
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getLastCompressedUpdate', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.update = 'mock-update'
|
|
|
|
this.db.docHistory = {}
|
|
|
|
this.db.docHistory.find = sinon.stub().returns(this.db.docHistory)
|
|
|
|
this.db.docHistory.findOne = sinon.stub().returns(this.db.docHistory)
|
|
|
|
this.db.docHistory.sort = sinon.stub().returns(this.db.docHistory)
|
|
|
|
this.db.docHistory.limit = sinon.stub().returns(this.db.docHistory)
|
|
|
|
this.db.docHistory.toArray = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, null, [this.update])
|
|
|
|
|
|
|
|
return this.MongoManager.getLastCompressedUpdate(
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should find the updates for the doc', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.db.docHistory.find
|
|
|
|
.calledWith({ doc_id: ObjectId(this.doc_id) })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should limit to one result', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.db.docHistory.limit.calledWith(1).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should sort in descending version order', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.db.docHistory.sort.calledWith({ v: -1 }).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the call back with the update', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, this.update).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('peekLastCompressedUpdate', function () {
|
|
|
|
describe('when there is no last update', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.PackManager.getLastPackFromIndex = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, null)
|
|
|
|
this.MongoManager.getLastCompressedUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, null)
|
|
|
|
return this.MongoManager.peekLastCompressedUpdate(
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the last update', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.MongoManager.getLastCompressedUpdate
|
|
|
|
.calledWith(this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with no update', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, null).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when there is an update', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.update = { _id: Object() }
|
|
|
|
this.MongoManager.getLastCompressedUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.update)
|
|
|
|
return this.MongoManager.peekLastCompressedUpdate(
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the last update', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.MongoManager.getLastCompressedUpdate
|
|
|
|
.calledWith(this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the update', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, this.update).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when there is a last update in S3', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.update = { _id: Object(), v: 12345, v_end: 12345, inS3: true }
|
|
|
|
this.PackManager.getLastPackFromIndex = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.update)
|
|
|
|
this.MongoManager.getLastCompressedUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null)
|
|
|
|
return this.MongoManager.peekLastCompressedUpdate(
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the last update', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.MongoManager.getLastCompressedUpdate
|
|
|
|
.calledWith(this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with a null update and the correct version', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, null, this.update.v_end)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('backportProjectId', function () {
|
|
|
|
beforeEach(function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
this.db.docHistory = { updateMany: sinon.stub().yields() }
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.MongoManager.backportProjectId(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it("should insert the project_id into all entries for the doc_id which don't have it set", function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.updateMany
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWith(
|
|
|
|
{
|
|
|
|
doc_id: ObjectId(this.doc_id),
|
|
|
|
project_id: { $exists: false }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: { project_id: ObjectId(this.project_id) }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getProjectMetaData', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.metadata = { mock: 'metadata' }
|
|
|
|
this.db.projectHistoryMetaData = {
|
2020-09-10 09:54:18 -04:00
|
|
|
findOne: sinon.stub().callsArgWith(1, null, this.metadata)
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
return this.MongoManager.getProjectMetaData(
|
|
|
|
this.project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should look up the meta data in the db', function () {
|
2020-09-10 09:54:18 -04:00
|
|
|
return this.db.projectHistoryMetaData.findOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWith({ project_id: ObjectId(this.project_id) })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the metadata', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, this.metadata).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('setProjectMetaData', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.metadata = { mock: 'metadata' }
|
|
|
|
this.db.projectHistoryMetaData = {
|
2020-09-10 07:58:06 -04:00
|
|
|
updateOne: sinon.stub().yields()
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
return this.MongoManager.setProjectMetaData(
|
|
|
|
this.project_id,
|
|
|
|
this.metadata,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should upsert the metadata into the DB', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.projectHistoryMetaData.updateOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWith(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(this.project_id)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: this.metadata
|
|
|
|
},
|
|
|
|
{
|
|
|
|
upsert: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|