2020-02-16 14:02:55 +00:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/MongoManager'
|
|
|
|
)
|
2020-08-21 16:50:01 +00:00
|
|
|
const { ObjectId } = require('mongodb')
|
2022-04-26 11:16:36 +00:00
|
|
|
const { assert, expect } = require('chai')
|
2021-07-30 15:06:16 +00:00
|
|
|
const Errors = require('../../../app/js/Errors')
|
2020-02-16 14:02:55 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('MongoManager', function () {
|
|
|
|
beforeEach(function () {
|
2022-04-26 11:16:36 +00:00
|
|
|
this.db = {
|
|
|
|
docs: {
|
2022-04-26 15:10:15 +00:00
|
|
|
updateOne: sinon.stub().yields(null, { matchedCount: 1 }),
|
2023-03-22 13:08:01 +00:00
|
|
|
insertOne: sinon.stub().yields(null),
|
2022-04-26 11:16:36 +00:00
|
|
|
},
|
|
|
|
docOps: {},
|
|
|
|
}
|
2020-02-16 14:02:55 +00:00
|
|
|
this.MongoManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-08-21 16:50:01 +00:00
|
|
|
'./mongodb': {
|
2022-04-26 11:16:36 +00:00
|
|
|
db: this.db,
|
2021-07-13 11:04:48 +00:00
|
|
|
ObjectId,
|
2020-02-16 14:02:55 +00:00
|
|
|
},
|
2020-09-10 17:07:14 +00:00
|
|
|
'@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
|
2022-05-17 11:49:13 +00:00
|
|
|
'@overleaf/settings': {
|
|
|
|
max_deleted_docs: 42,
|
|
|
|
docstore: { archivingLockDurationMs: 5000 },
|
|
|
|
},
|
2021-08-02 10:36:43 +00:00
|
|
|
'./Errors': Errors,
|
2021-07-13 11:04:48 +00:00
|
|
|
},
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId = ObjectId().toString()
|
|
|
|
this.docId = ObjectId().toString()
|
2023-03-22 13:08:01 +00:00
|
|
|
this.rev = 42
|
2020-08-28 12:13:19 +00:00
|
|
|
this.callback = sinon.stub()
|
2022-04-19 12:28:47 +00:00
|
|
|
this.stubbedErr = new Error('hello world')
|
2023-11-15 12:30:14 +00:00
|
|
|
this.lines = ['Three French hens', 'Two turtle doves']
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('findDoc', function () {
|
2020-08-28 12:13:19 +00:00
|
|
|
beforeEach(function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.doc = { name: 'mock-doc' }
|
2020-08-21 16:50:01 +00:00
|
|
|
this.db.docs.findOne = sinon.stub().callsArgWith(2, null, this.doc)
|
2020-02-16 14:02:55 +00:00
|
|
|
this.filter = { lines: true }
|
2022-04-19 12:28:47 +00:00
|
|
|
this.MongoManager.findDoc(
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
2020-02-16 14:02:55 +00:00
|
|
|
this.filter,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should find the doc', function () {
|
2020-08-21 16:50:01 +00:00
|
|
|
this.db.docs.findOne
|
2020-02-16 14:02:55 +00:00
|
|
|
.calledWith(
|
|
|
|
{
|
2022-04-26 11:16:36 +00:00
|
|
|
_id: ObjectId(this.docId),
|
|
|
|
project_id: ObjectId(this.projectId),
|
2020-02-16 14:02:55 +00:00
|
|
|
},
|
2020-09-07 09:16:12 +00:00
|
|
|
{
|
2021-07-13 11:04:48 +00:00
|
|
|
projection: this.filter,
|
2020-09-07 09:16:12 +00:00
|
|
|
}
|
2020-02-16 14:02:55 +00:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
it('should call the callback with the doc', function () {
|
|
|
|
this.callback.calledWith(null, this.doc).should.equal(true)
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-15 13:13:48 +00:00
|
|
|
describe('patchDoc', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.meta = { name: 'foo.tex' }
|
|
|
|
this.callback.callsFake(done)
|
|
|
|
this.MongoManager.patchDoc(
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
2021-02-15 13:13:48 +00:00
|
|
|
this.meta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should pass the parameter along', function () {
|
|
|
|
this.db.docs.updateOne.should.have.been.calledWith(
|
|
|
|
{
|
2022-04-26 11:16:36 +00:00
|
|
|
_id: ObjectId(this.docId),
|
|
|
|
project_id: ObjectId(this.projectId),
|
2021-02-15 13:13:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 11:04:48 +00:00
|
|
|
$set: this.meta,
|
2021-02-15 13:13:48 +00:00
|
|
|
},
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('getProjectsDocs', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.filter = { lines: true }
|
|
|
|
this.doc1 = { name: 'mock-doc1' }
|
|
|
|
this.doc2 = { name: 'mock-doc2' }
|
|
|
|
this.doc3 = { name: 'mock-doc3' }
|
|
|
|
this.doc4 = { name: 'mock-doc4' }
|
2020-08-21 16:50:01 +00:00
|
|
|
this.db.docs.find = sinon.stub().returns({
|
|
|
|
toArray: sinon
|
|
|
|
.stub()
|
2021-07-13 11:04:48 +00:00
|
|
|
.callsArgWith(0, null, [this.doc, this.doc3, this.doc4]),
|
2020-08-21 16:50:01 +00:00
|
|
|
})
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('with included_deleted = false', function () {
|
2020-08-28 12:13:19 +00:00
|
|
|
beforeEach(function () {
|
2022-04-19 12:28:47 +00:00
|
|
|
this.MongoManager.getProjectsDocs(
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId,
|
2020-02-16 14:02:55 +00:00
|
|
|
{ include_deleted: false },
|
|
|
|
this.filter,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should find the non-deleted docs via the project_id', function () {
|
2022-04-19 12:28:47 +00:00
|
|
|
this.db.docs.find
|
2020-02-16 14:02:55 +00:00
|
|
|
.calledWith(
|
|
|
|
{
|
2022-04-26 11:16:36 +00:00
|
|
|
project_id: ObjectId(this.projectId),
|
2021-07-13 11:04:48 +00:00
|
|
|
deleted: { $ne: true },
|
2020-02-16 14:02:55 +00:00
|
|
|
},
|
2020-09-07 09:16:12 +00:00
|
|
|
{
|
2021-07-13 11:04:48 +00:00
|
|
|
projection: this.filter,
|
2020-09-07 09:16:12 +00:00
|
|
|
}
|
2020-02-16 14:02:55 +00:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
it('should call the callback with the docs', function () {
|
|
|
|
this.callback
|
2020-02-16 14:02:55 +00:00
|
|
|
.calledWith(null, [this.doc, this.doc3, this.doc4])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
describe('with included_deleted = true', function () {
|
2020-08-28 12:13:19 +00:00
|
|
|
beforeEach(function () {
|
2022-04-19 12:28:47 +00:00
|
|
|
this.MongoManager.getProjectsDocs(
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId,
|
2020-02-16 14:02:55 +00:00
|
|
|
{ include_deleted: true },
|
|
|
|
this.filter,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should find all via the project_id', function () {
|
2022-04-19 12:28:47 +00:00
|
|
|
this.db.docs.find
|
2020-02-16 14:02:55 +00:00
|
|
|
.calledWith(
|
|
|
|
{
|
2022-04-26 11:16:36 +00:00
|
|
|
project_id: ObjectId(this.projectId),
|
2020-02-16 14:02:55 +00:00
|
|
|
},
|
2020-09-07 09:16:12 +00:00
|
|
|
{
|
2021-07-13 11:04:48 +00:00
|
|
|
projection: this.filter,
|
2020-09-07 09:16:12 +00:00
|
|
|
}
|
2020-02-16 14:02:55 +00:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
it('should call the callback with the docs', function () {
|
|
|
|
this.callback
|
2020-02-16 14:02:55 +00:00
|
|
|
.calledWith(null, [this.doc, this.doc3, this.doc4])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-18 10:10:14 +00:00
|
|
|
describe('getProjectsDeletedDocs', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.filter = { name: true }
|
|
|
|
this.doc1 = { _id: '1', name: 'mock-doc1.tex' }
|
|
|
|
this.doc2 = { _id: '2', name: 'mock-doc2.tex' }
|
|
|
|
this.doc3 = { _id: '3', name: 'mock-doc3.tex' }
|
|
|
|
this.db.docs.find = sinon.stub().returns({
|
2021-07-13 11:04:48 +00:00
|
|
|
toArray: sinon.stub().yields(null, [this.doc1, this.doc2, this.doc3]),
|
2021-02-18 10:10:14 +00:00
|
|
|
})
|
|
|
|
this.callback.callsFake(done)
|
|
|
|
this.MongoManager.getProjectsDeletedDocs(
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId,
|
2021-02-18 10:10:14 +00:00
|
|
|
this.filter,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should find the deleted docs via the project_id', function () {
|
|
|
|
this.db.docs.find
|
|
|
|
.calledWith({
|
2022-04-26 11:16:36 +00:00
|
|
|
project_id: ObjectId(this.projectId),
|
2021-07-13 11:04:48 +00:00
|
|
|
deleted: true,
|
2021-02-18 10:10:14 +00:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should filter, sort by deletedAt and limit', function () {
|
|
|
|
this.db.docs.find
|
|
|
|
.calledWith(sinon.match.any, {
|
|
|
|
projection: this.filter,
|
|
|
|
sort: { deletedAt: -1 },
|
2021-07-13 11:04:48 +00:00
|
|
|
limit: 42,
|
2021-02-18 10:10:14 +00:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the callback with the docs', function () {
|
|
|
|
this.callback
|
|
|
|
.calledWith(null, [this.doc1, this.doc2, this.doc3])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('upsertIntoDocCollection', function () {
|
|
|
|
beforeEach(function () {
|
2022-04-19 12:28:47 +00:00
|
|
|
this.oldRev = 77
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should upsert the document', function (done) {
|
2022-04-19 12:28:47 +00:00
|
|
|
this.MongoManager.upsertIntoDocCollection(
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
2023-03-22 13:08:01 +00:00
|
|
|
this.oldRev,
|
2020-02-16 14:02:55 +00:00
|
|
|
{ lines: this.lines },
|
2021-07-13 11:04:48 +00:00
|
|
|
err => {
|
2023-03-22 13:08:01 +00:00
|
|
|
assert.equal(err, null)
|
2020-08-21 16:50:01 +00:00
|
|
|
const args = this.db.docs.updateOne.args[0]
|
2023-03-22 13:08:01 +00:00
|
|
|
assert.deepEqual(args[0], {
|
|
|
|
_id: ObjectId(this.docId),
|
|
|
|
project_id: ObjectId(this.projectId),
|
|
|
|
rev: this.oldRev,
|
|
|
|
})
|
2020-02-16 14:02:55 +00:00
|
|
|
assert.equal(args[1].$set.lines, this.lines)
|
|
|
|
assert.equal(args[1].$inc.rev, 1)
|
2022-04-19 12:28:47 +00:00
|
|
|
done()
|
2020-02-16 14:02:55 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-03-22 13:08:01 +00:00
|
|
|
it('should handle update error', function (done) {
|
|
|
|
this.db.docs.updateOne.yields(this.stubbedErr)
|
2022-04-19 12:28:47 +00:00
|
|
|
this.MongoManager.upsertIntoDocCollection(
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
2023-03-22 13:08:01 +00:00
|
|
|
this.rev,
|
2020-02-16 14:02:55 +00:00
|
|
|
{ lines: this.lines },
|
2021-07-13 11:04:48 +00:00
|
|
|
err => {
|
2020-02-16 14:02:55 +00:00
|
|
|
err.should.equal(this.stubbedErr)
|
2022-04-19 12:28:47 +00:00
|
|
|
done()
|
2020-02-16 14:02:55 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2023-03-22 13:08:01 +00:00
|
|
|
|
|
|
|
it('should insert without a previous rev', function (done) {
|
|
|
|
this.MongoManager.upsertIntoDocCollection(
|
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
|
|
|
null,
|
|
|
|
{ lines: this.lines, ranges: this.ranges },
|
|
|
|
err => {
|
|
|
|
expect(this.db.docs.insertOne).to.have.been.calledWith({
|
|
|
|
_id: ObjectId(this.docId),
|
|
|
|
project_id: ObjectId(this.projectId),
|
|
|
|
rev: 1,
|
|
|
|
lines: this.lines,
|
|
|
|
ranges: this.ranges,
|
|
|
|
})
|
|
|
|
expect(err).to.not.exist
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should handle generic insert error', function (done) {
|
|
|
|
this.db.docs.insertOne.yields(this.stubbedErr)
|
|
|
|
this.MongoManager.upsertIntoDocCollection(
|
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
|
|
|
null,
|
|
|
|
{ lines: this.lines, ranges: this.ranges },
|
|
|
|
err => {
|
|
|
|
expect(err).to.equal(this.stubbedErr)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should handle duplicate insert error', function (done) {
|
|
|
|
this.db.docs.insertOne.yields({ code: 11000 })
|
|
|
|
this.MongoManager.upsertIntoDocCollection(
|
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
|
|
|
null,
|
|
|
|
{ lines: this.lines, ranges: this.ranges },
|
|
|
|
err => {
|
|
|
|
expect(err).to.be.instanceof(Errors.DocRevValueError)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
|
2022-04-26 11:16:36 +00:00
|
|
|
describe('destroyProject', function () {
|
2020-05-28 13:20:54 +00:00
|
|
|
beforeEach(function (done) {
|
2022-04-26 11:16:36 +00:00
|
|
|
this.projectId = ObjectId()
|
|
|
|
this.docIds = [ObjectId(), ObjectId()]
|
|
|
|
this.db.docs.deleteMany = sinon.stub().yields()
|
|
|
|
this.db.docOps.deleteMany = sinon.stub().yields()
|
|
|
|
this.db.docs.find = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs({ project_id: this.projectId })
|
|
|
|
.returns({
|
|
|
|
toArray: sinon.stub().yields(
|
|
|
|
null,
|
|
|
|
this.docIds.map(id => ({
|
|
|
|
_id: id,
|
|
|
|
}))
|
|
|
|
),
|
|
|
|
})
|
|
|
|
this.MongoManager.destroyProject(this.projectId, done)
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
|
2022-04-26 11:16:36 +00:00
|
|
|
it('should destroy all docs', function () {
|
|
|
|
sinon.assert.calledWith(this.db.docs.deleteMany, {
|
|
|
|
project_id: this.projectId,
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
it('should destroy the docOps', function () {
|
2022-04-26 11:16:36 +00:00
|
|
|
sinon.assert.calledWith(this.db.docOps.deleteMany, {
|
|
|
|
doc_id: { $in: this.docIds },
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
describe('getDocVersion', function () {
|
|
|
|
describe('when the doc exists', function () {
|
2020-08-28 12:13:19 +00:00
|
|
|
beforeEach(function () {
|
2020-02-16 14:02:55 +00:00
|
|
|
this.doc = { version: (this.version = 42) }
|
2020-08-21 16:50:01 +00:00
|
|
|
this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
|
2022-04-26 11:16:36 +00:00
|
|
|
this.MongoManager.getDocVersion(this.docId, this.callback)
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
it('should look for the doc in the database', function () {
|
2022-04-19 12:28:47 +00:00
|
|
|
this.db.docOps.findOne
|
2020-09-07 09:16:12 +00:00
|
|
|
.calledWith(
|
2022-04-26 11:16:36 +00:00
|
|
|
{ doc_id: ObjectId(this.docId) },
|
2020-09-07 09:16:12 +00:00
|
|
|
{
|
2021-07-13 11:04:48 +00:00
|
|
|
projection: { version: 1 },
|
2020-09-07 09:16:12 +00:00
|
|
|
}
|
|
|
|
)
|
2020-02-16 14:02:55 +00:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
it('should call the callback with the version', function () {
|
|
|
|
this.callback.calledWith(null, this.version).should.equal(true)
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
describe("when the doc doesn't exist", function () {
|
2020-08-28 12:13:19 +00:00
|
|
|
beforeEach(function () {
|
2020-08-21 16:50:01 +00:00
|
|
|
this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
|
2022-04-26 11:16:36 +00:00
|
|
|
this.MongoManager.getDocVersion(this.docId, this.callback)
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
|
2022-04-19 12:28:47 +00:00
|
|
|
it('should call the callback with 0', function () {
|
|
|
|
this.callback.calledWith(null, 0).should.equal(true)
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-08 14:17:19 +00:00
|
|
|
describe('checkRevUnchanged', function () {
|
2021-08-02 10:36:43 +00:00
|
|
|
this.beforeEach(function () {
|
|
|
|
this.doc = { _id: ObjectId(), name: 'mock-doc', rev: 1 }
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the callback when the rev has not changed', function (done) {
|
|
|
|
this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 1 })
|
2023-11-08 14:17:19 +00:00
|
|
|
this.MongoManager.checkRevUnchanged(this.doc, err => {
|
|
|
|
assert.isUndefined(err)
|
|
|
|
done()
|
|
|
|
})
|
2021-08-02 10:36:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return an error when the rev has changed', function (done) {
|
|
|
|
this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 2 })
|
2023-11-08 14:17:19 +00:00
|
|
|
this.MongoManager.checkRevUnchanged(this.doc, err => {
|
|
|
|
err.should.be.instanceof(Errors.DocModifiedError)
|
|
|
|
done()
|
|
|
|
})
|
2021-08-02 10:36:43 +00:00
|
|
|
})
|
2021-11-30 10:27:53 +00:00
|
|
|
|
|
|
|
it('should return a value error if incoming rev is NaN', function (done) {
|
|
|
|
this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 2 })
|
|
|
|
this.doc = { _id: ObjectId(), name: 'mock-doc', rev: NaN }
|
2023-11-08 14:17:19 +00:00
|
|
|
this.MongoManager.checkRevUnchanged(this.doc, err => {
|
|
|
|
err.should.be.instanceof(Errors.DocRevValueError)
|
|
|
|
done()
|
|
|
|
})
|
2021-11-30 10:27:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return a value error if checked doc rev is NaN', function (done) {
|
|
|
|
this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: NaN })
|
2023-11-08 14:17:19 +00:00
|
|
|
this.MongoManager.checkRevUnchanged(this.doc, err => {
|
|
|
|
err.should.be.instanceof(Errors.DocRevValueError)
|
|
|
|
done()
|
|
|
|
})
|
2021-11-30 10:27:53 +00:00
|
|
|
})
|
2021-08-02 10:36:43 +00:00
|
|
|
})
|
2022-04-26 11:16:36 +00:00
|
|
|
|
|
|
|
describe('restoreArchivedDoc', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.archivedDoc = {
|
|
|
|
lines: ['a', 'b', 'c'],
|
|
|
|
ranges: { some: 'ranges' },
|
|
|
|
rev: 2,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('complete doc', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.MongoManager.restoreArchivedDoc(
|
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
|
|
|
this.archivedDoc,
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('updates Mongo', function () {
|
|
|
|
expect(this.db.docs.updateOne).to.have.been.calledWith(
|
|
|
|
{
|
|
|
|
_id: ObjectId(this.docId),
|
|
|
|
project_id: ObjectId(this.projectId),
|
|
|
|
rev: this.archivedDoc.rev,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
lines: this.archivedDoc.lines,
|
|
|
|
ranges: this.archivedDoc.ranges,
|
|
|
|
},
|
|
|
|
$unset: {
|
|
|
|
inS3: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('without ranges', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
delete this.archivedDoc.ranges
|
|
|
|
this.MongoManager.restoreArchivedDoc(
|
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
|
|
|
this.archivedDoc,
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sets ranges to an empty object', function () {
|
|
|
|
expect(this.db.docs.updateOne).to.have.been.calledWith(
|
|
|
|
{
|
|
|
|
_id: ObjectId(this.docId),
|
|
|
|
project_id: ObjectId(this.projectId),
|
|
|
|
rev: this.archivedDoc.rev,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
lines: this.archivedDoc.lines,
|
|
|
|
ranges: {},
|
|
|
|
},
|
|
|
|
$unset: {
|
|
|
|
inS3: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("when the update doesn't succeed", function () {
|
|
|
|
it('throws a DocRevValueError', function (done) {
|
2022-04-26 15:10:15 +00:00
|
|
|
this.db.docs.updateOne.yields(null, { matchedCount: 0 })
|
2022-04-26 11:16:36 +00:00
|
|
|
this.MongoManager.restoreArchivedDoc(
|
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
|
|
|
this.archivedDoc,
|
|
|
|
err => {
|
|
|
|
expect(err).to.be.instanceof(Errors.DocRevValueError)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-02-16 14:02:55 +00:00
|
|
|
})
|