overleaf/services/track-changes/test/unit/js/MongoManager/MongoManagerTests.js

240 lines
7.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable
no-return-assign,
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
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../../app/js/MongoManager.js'
const packModulePath = '../../../../app/js/PackManager.js'
const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongodb')
const tk = require('timekeeper')
2020-06-04 04:24:21 -04:00
describe('MongoManager', function () {
beforeEach(function () {
tk.freeze(new Date())
this.MongoManager = SandboxedModule.require(modulePath, {
requires: {
'./mongodb': { db: (this.db = {}), ObjectId },
'./PackManager': (this.PackManager = {}),
2021-07-13 07:04:43 -04:00
'@overleaf/metrics': { timeAsyncMethod() {} },
},
})
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 () {
return tk.reset()
})
2020-06-04 04:24:21 -04:00
describe('getLastCompressedUpdate', function () {
beforeEach(function () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
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 () {
this.db.docHistory = { updateMany: sinon.stub().yields() }
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 () {
return this.db.docHistory.updateMany
.calledWith(
{
doc_id: ObjectId(this.doc_id),
2021-07-13 07:04:43 -04:00
project_id: { $exists: false },
},
{
2021-07-13 07:04:43 -04:00
$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 () {
return this.callback.called.should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
describe('getProjectMetaData', function () {
beforeEach(function () {
this.metadata = { mock: 'metadata' }
this.db.projectHistoryMetaData = {
2021-07-13 07:04:43 -04:00
findOne: sinon.stub().callsArgWith(1, null, this.metadata),
}
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 () {
return this.db.projectHistoryMetaData.findOne
.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 () {
return this.callback.calledWith(null, this.metadata).should.equal(true)
})
})
2020-06-04 04:24:21 -04:00
return describe('setProjectMetaData', function () {
beforeEach(function () {
this.metadata = { mock: 'metadata' }
this.db.projectHistoryMetaData = {
2021-07-13 07:04:43 -04:00
updateOne: sinon.stub().yields(),
}
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 () {
return this.db.projectHistoryMetaData.updateOne
.calledWith(
{
2021-07-13 07:04:43 -04:00
project_id: ObjectId(this.project_id),
},
{
2021-07-13 07:04:43 -04:00
$set: this.metadata,
},
{
2021-07-13 07:04:43 -04:00
upsert: true,
}
)
.should.equal(true)
})
2020-06-04 04:24:21 -04:00
return it('should call the callback', function () {
return this.callback.called.should.equal(true)
})
})
})