2020-02-16 09:02:55 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2023-11-08 09:17:19 -05:00
|
|
|
const { expect } = require('chai')
|
2020-02-16 09:02:55 -05:00
|
|
|
const modulePath = require('path').join(__dirname, '../../../app/js/DocManager')
|
2024-07-15 07:10:48 -04:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
2020-02-16 09:02:55 -05:00
|
|
|
const Errors = require('../../../app/js/Errors')
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('DocManager', function () {
|
|
|
|
beforeEach(function () {
|
2023-12-13 04:38:54 -05:00
|
|
|
this.doc_id = new ObjectId().toString()
|
|
|
|
this.project_id = new ObjectId().toString()
|
|
|
|
this.another_project_id = new ObjectId().toString()
|
2023-11-13 08:59:10 -05:00
|
|
|
this.stubbedError = new Error('blew up')
|
|
|
|
this.version = 42
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager = {
|
|
|
|
promises: {
|
|
|
|
findDoc: sinon.stub(),
|
|
|
|
getProjectsDocs: sinon.stub(),
|
|
|
|
patchDoc: sinon.stub().resolves(),
|
|
|
|
upsertIntoDocCollection: sinon.stub().resolves(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.DocArchiveManager = {
|
|
|
|
promises: {
|
|
|
|
unarchiveDoc: sinon.stub(),
|
|
|
|
unArchiveAllDocs: sinon.stub(),
|
|
|
|
archiveDoc: sinon.stub().resolves(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.RangeManager = {
|
|
|
|
jsonRangesToMongo(r) {
|
|
|
|
return r
|
|
|
|
},
|
|
|
|
shouldUpdateRanges: sinon.stub().returns(false),
|
|
|
|
}
|
|
|
|
this.settings = { docstore: {} }
|
|
|
|
|
2020-02-16 09:02:55 -05:00
|
|
|
this.DocManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2023-11-08 09:17:19 -05:00
|
|
|
'./MongoManager': this.MongoManager,
|
|
|
|
'./DocArchiveManager': this.DocArchiveManager,
|
|
|
|
'./RangeManager': this.RangeManager,
|
|
|
|
'@overleaf/settings': this.settings,
|
2021-07-13 07:04:48 -04:00
|
|
|
'./Errors': Errors,
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getFullDoc', function () {
|
|
|
|
beforeEach(function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.DocManager.promises._getDoc = sinon.stub()
|
|
|
|
this.doc = {
|
2020-02-16 09:02:55 -05:00
|
|
|
_id: this.doc_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
lines: ['2134'],
|
2023-11-08 09:17:19 -05:00
|
|
|
}
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should call get doc with a quick filter', async function () {
|
|
|
|
this.DocManager.promises._getDoc.resolves(this.doc)
|
|
|
|
const doc = await this.DocManager.promises.getFullDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.doc_id
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
doc.should.equal(this.doc)
|
|
|
|
this.DocManager.promises._getDoc
|
|
|
|
.calledWith(this.project_id, this.doc_id, {
|
|
|
|
lines: true,
|
|
|
|
rev: true,
|
|
|
|
deleted: true,
|
|
|
|
version: true,
|
|
|
|
ranges: true,
|
|
|
|
inS3: true,
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return error when get doc errors', async function () {
|
|
|
|
this.DocManager.promises._getDoc.rejects(this.stubbedError)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.getFullDoc(this.project_id, this.doc_id)
|
|
|
|
).to.be.rejectedWith(this.stubbedError)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getRawDoc', function () {
|
|
|
|
beforeEach(function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.DocManager.promises._getDoc = sinon.stub()
|
|
|
|
this.doc = { lines: ['2134'] }
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should call get doc with a quick filter', async function () {
|
|
|
|
this.DocManager.promises._getDoc.resolves(this.doc)
|
|
|
|
const doc = await this.DocManager.promises.getDocLines(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.doc_id
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
doc.should.equal(this.doc)
|
|
|
|
this.DocManager.promises._getDoc
|
|
|
|
.calledWith(this.project_id, this.doc_id, {
|
|
|
|
lines: true,
|
|
|
|
inS3: true,
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return error when get doc errors', async function () {
|
|
|
|
this.DocManager.promises._getDoc.rejects(this.stubbedError)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.getDocLines(this.project_id, this.doc_id)
|
|
|
|
).to.be.rejectedWith(this.stubbedError)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getDoc', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project = { name: 'mock-project' }
|
|
|
|
this.doc = {
|
|
|
|
_id: this.doc_id,
|
|
|
|
project_id: this.project_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
lines: ['mock-lines'],
|
2024-01-22 09:45:18 -05:00
|
|
|
version: this.version,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when using a filter', function () {
|
|
|
|
beforeEach(function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.findDoc.resolves(this.doc)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should error if inS3 is not set to true', async function () {
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises._getDoc(this.project_id, this.doc_id, {
|
|
|
|
inS3: false,
|
|
|
|
})
|
|
|
|
).to.be.rejected
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should always get inS3 even when no filter is passed', async function () {
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises._getDoc(this.project_id, this.doc_id)
|
|
|
|
).to.be.rejected
|
|
|
|
this.MongoManager.promises.findDoc.called.should.equal(false)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should not error if inS3 is set to true', async function () {
|
|
|
|
await this.DocManager.promises._getDoc(this.project_id, this.doc_id, {
|
|
|
|
inS3: true,
|
|
|
|
})
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc is in the doc collection', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.MongoManager.promises.findDoc.resolves(this.doc)
|
|
|
|
this.result = await this.DocManager.promises._getDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
{ version: true, inS3: true }
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get the doc from the doc collection', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.findDoc
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-11-13 08:59:10 -05:00
|
|
|
it('should return the doc with the version', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.result.lines.should.equal(this.doc.lines)
|
|
|
|
this.result.version.should.equal(this.version)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when MongoManager.findDoc errors', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return the error', async function () {
|
|
|
|
this.MongoManager.promises.findDoc.rejects(this.stubbedError)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises._getDoc(this.project_id, this.doc_id, {
|
|
|
|
version: true,
|
|
|
|
inS3: true,
|
|
|
|
})
|
|
|
|
).to.be.rejectedWith(this.stubbedError)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc is archived', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.doc = {
|
|
|
|
_id: this.doc_id,
|
|
|
|
project_id: this.project_id,
|
2024-01-22 09:45:18 -05:00
|
|
|
version: 2,
|
2021-07-13 07:04:48 -04:00
|
|
|
inS3: true,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
this.unarchivedDoc = {
|
|
|
|
_id: this.doc_id,
|
|
|
|
project_id: this.project_id,
|
|
|
|
lines: ['mock-lines'],
|
2024-01-22 09:45:18 -05:00
|
|
|
version: 2,
|
2023-11-08 09:17:19 -05:00
|
|
|
inS3: false,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.findDoc.resolves(this.doc)
|
|
|
|
this.DocArchiveManager.promises.unarchiveDoc.callsFake(
|
|
|
|
async (projectId, docId) => {
|
2023-11-13 08:59:10 -05:00
|
|
|
this.MongoManager.promises.findDoc.resolves({
|
|
|
|
...this.unarchivedDoc,
|
|
|
|
})
|
2023-11-08 09:17:19 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
this.result = await this.DocManager.promises._getDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
{
|
|
|
|
version: true,
|
|
|
|
inS3: true,
|
|
|
|
}
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should call the DocArchive to unarchive the doc', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.DocArchiveManager.promises.unarchiveDoc
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should look up the doc twice', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.findDoc.calledTwice.should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return the doc', function () {
|
2023-11-13 08:59:10 -05:00
|
|
|
expect(this.result).to.deep.equal({
|
|
|
|
...this.unarchivedDoc,
|
|
|
|
})
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
describe('when the doc does not exist in the docs collection', function () {
|
|
|
|
it('should return a NotFoundError', async function () {
|
|
|
|
this.MongoManager.promises.findDoc.resolves(null)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises._getDoc(this.project_id, this.doc_id, {
|
|
|
|
version: true,
|
|
|
|
inS3: true,
|
|
|
|
})
|
|
|
|
).to.be.rejectedWith(
|
|
|
|
`No such doc: ${this.doc_id} in project ${this.project_id}`
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getAllNonDeletedDocs', function () {
|
|
|
|
describe('when the project exists', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.docs = [
|
|
|
|
{
|
|
|
|
_id: this.doc_id,
|
|
|
|
project_id: this.project_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
lines: ['mock-lines'],
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
]
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.getProjectsDocs.resolves(this.docs)
|
|
|
|
this.DocArchiveManager.promises.unArchiveAllDocs.resolves(this.docs)
|
2020-02-16 09:02:55 -05:00
|
|
|
this.filter = { lines: true }
|
2023-11-08 09:17:19 -05:00
|
|
|
this.result = await this.DocManager.promises.getAllNonDeletedDocs(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
2023-11-13 08:59:10 -05:00
|
|
|
this.filter
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get the project from the database', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.getProjectsDocs.should.have.been.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
{ include_deleted: false },
|
|
|
|
this.filter
|
|
|
|
)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return the docs', function () {
|
|
|
|
expect(this.result).to.deep.equal(this.docs)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
describe('when there are no docs for the project', function () {
|
|
|
|
it('should return a NotFoundError', async function () {
|
|
|
|
this.MongoManager.promises.getProjectsDocs.resolves(null)
|
|
|
|
this.DocArchiveManager.promises.unArchiveAllDocs.resolves(null)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.getAllNonDeletedDocs(
|
|
|
|
this.project_id,
|
|
|
|
this.filter
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
).to.be.rejectedWith(`No docs for project ${this.project_id}`)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-15 08:13:48 -05:00
|
|
|
describe('patchDoc', function () {
|
|
|
|
describe('when the doc exists', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.lines = ['mock', 'doc', 'lines']
|
|
|
|
this.rev = 77
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.findDoc.resolves({
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(this.doc_id),
|
2023-11-08 09:17:19 -05:00
|
|
|
})
|
2021-02-15 08:13:48 -05:00
|
|
|
this.meta = {}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('standard path', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
await this.DocManager.promises.patchDoc(
|
2021-02-15 08:13:48 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.meta
|
2021-02-15 08:13:48 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the doc', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.MongoManager.promises.findDoc).to.have.been.calledWith(
|
2021-02-15 08:13:48 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should persist the meta', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.MongoManager.promises.patchDoc).to.have.been.calledWith(
|
2021-02-15 08:13:48 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.meta
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('background flush disabled and deleting a doc', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
2021-02-15 08:13:48 -05:00
|
|
|
this.settings.docstore.archiveOnSoftDelete = false
|
|
|
|
this.meta.deleted = true
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
await this.DocManager.promises.patchDoc(
|
2021-02-15 08:13:48 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.meta
|
2021-02-15 08:13:48 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush the doc out of mongo', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.DocArchiveManager.promises.archiveDoc).to.not.have.been
|
|
|
|
.called
|
2021-02-15 08:13:48 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('background flush enabled and not deleting a doc', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
2021-02-15 08:13:48 -05:00
|
|
|
this.settings.docstore.archiveOnSoftDelete = false
|
|
|
|
this.meta.deleted = false
|
2023-11-08 09:17:19 -05:00
|
|
|
await this.DocManager.promises.patchDoc(
|
2021-02-15 08:13:48 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.meta
|
2021-02-15 08:13:48 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush the doc out of mongo', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.DocArchiveManager.promises.archiveDoc).to.not.have.been
|
|
|
|
.called
|
2021-02-15 08:13:48 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('background flush enabled and deleting a doc', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.settings.docstore.archiveOnSoftDelete = true
|
|
|
|
this.meta.deleted = true
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the background flush succeeds', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
await this.DocManager.promises.patchDoc(
|
2021-02-15 08:13:48 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.meta
|
2021-02-15 08:13:48 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not log a warning', function () {
|
|
|
|
expect(this.logger.warn).to.not.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should flush the doc out of mongo', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(
|
|
|
|
this.DocArchiveManager.promises.archiveDoc
|
|
|
|
).to.have.been.calledWith(this.project_id, this.doc_id)
|
2021-02-15 08:13:48 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the background flush fails', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
2021-02-15 08:13:48 -05:00
|
|
|
this.err = new Error('foo')
|
2023-11-08 09:17:19 -05:00
|
|
|
this.DocArchiveManager.promises.archiveDoc.rejects(this.err)
|
|
|
|
await this.DocManager.promises.patchDoc(
|
2021-02-15 08:13:48 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.meta
|
2021-02-15 08:13:48 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log a warning', function () {
|
|
|
|
expect(this.logger.warn).to.have.been.calledWith(
|
|
|
|
sinon.match({
|
2023-03-16 07:58:12 -04:00
|
|
|
projectId: this.project_id,
|
|
|
|
docId: this.doc_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
err: this.err,
|
2021-02-15 08:13:48 -05:00
|
|
|
}),
|
|
|
|
'archiving a single doc in the background failed'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the doc does not exist', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return a NotFoundError', async function () {
|
|
|
|
this.MongoManager.promises.findDoc.resolves(null)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.patchDoc(this.project_id, this.doc_id, {})
|
|
|
|
).to.be.rejectedWith(
|
|
|
|
`No such project/doc to delete: ${this.project_id}/${this.doc_id}`
|
2021-02-15 08:13:48 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
describe('updateDoc', function () {
|
2020-05-28 09:20:54 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.oldDocLines = ['old', 'doc', 'lines']
|
|
|
|
this.newDocLines = ['new', 'doc', 'lines']
|
|
|
|
this.originalRanges = {
|
|
|
|
changes: [
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
id: new ObjectId().toString(),
|
2020-02-16 09:02:55 -05:00
|
|
|
op: { i: 'foo', p: 3 },
|
|
|
|
meta: {
|
2023-12-13 04:38:54 -05:00
|
|
|
user_id: new ObjectId().toString(),
|
2021-07-13 07:04:48 -04:00
|
|
|
ts: new Date().toString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
this.newRanges = {
|
|
|
|
changes: [
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
id: new ObjectId().toString(),
|
2020-02-16 09:02:55 -05:00
|
|
|
op: { i: 'bar', p: 6 },
|
|
|
|
meta: {
|
2023-12-13 04:38:54 -05:00
|
|
|
user_id: new ObjectId().toString(),
|
2021-07-13 07:04:48 -04:00
|
|
|
ts: new Date().toString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
this.version = 42
|
|
|
|
this.doc = {
|
|
|
|
_id: this.doc_id,
|
|
|
|
project_id: this.project_id,
|
|
|
|
lines: this.oldDocLines,
|
|
|
|
rev: (this.rev = 5),
|
|
|
|
version: this.version,
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: this.originalRanges,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
this.DocManager.promises._getDoc = sinon.stub()
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when only the doc lines have changed', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(this.doc)
|
|
|
|
this.result = await this.DocManager.promises.updateDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newDocLines,
|
|
|
|
this.version,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.originalRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get the existing doc', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.DocManager.promises._getDoc
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith(this.project_id, this.doc_id, {
|
|
|
|
version: true,
|
|
|
|
rev: true,
|
|
|
|
lines: true,
|
|
|
|
ranges: true,
|
2021-07-13 07:04:48 -04:00
|
|
|
inS3: true,
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should upsert the document to the doc collection', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection
|
2023-03-22 09:08:01 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id, this.rev, {
|
|
|
|
lines: this.newDocLines,
|
|
|
|
})
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return the new rev', function () {
|
|
|
|
expect(this.result).to.deep.equal({ modified: true, rev: this.rev + 1 })
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc ranges have changed', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(this.doc)
|
2020-02-16 09:02:55 -05:00
|
|
|
this.RangeManager.shouldUpdateRanges.returns(true)
|
2023-11-08 09:17:19 -05:00
|
|
|
this.result = await this.DocManager.promises.updateDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.oldDocLines,
|
|
|
|
this.version,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.newRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should upsert the ranges', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection
|
2023-03-22 09:08:01 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id, this.rev, {
|
|
|
|
ranges: this.newRanges,
|
|
|
|
})
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return the new rev', function () {
|
|
|
|
expect(this.result).to.deep.equal({ modified: true, rev: this.rev + 1 })
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when only the version has changed', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(this.doc)
|
|
|
|
this.result = await this.DocManager.promises.updateDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.oldDocLines,
|
|
|
|
this.version + 1,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.originalRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should update the version', function () {
|
2023-11-15 07:30:14 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection.should.have.been.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.rev,
|
|
|
|
{ version: this.version + 1 }
|
|
|
|
)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-13 08:59:10 -05:00
|
|
|
it('should return the old rev', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.result).to.deep.equal({ modified: true, rev: this.rev })
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc has not changed at all', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(this.doc)
|
|
|
|
this.result = await this.DocManager.promises.updateDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.oldDocLines,
|
|
|
|
this.version,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.originalRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-11-15 07:30:14 -05:00
|
|
|
it('should not update the ranges or lines or version', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection.called.should.equal(
|
2020-02-16 09:02:55 -05:00
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-11-13 08:59:10 -05:00
|
|
|
it('should return the old rev and modified == false', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.result).to.deep.equal({ modified: false, rev: this.rev })
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the version is null', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return an error', async function () {
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.updateDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newDocLines,
|
|
|
|
null,
|
|
|
|
this.originalRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
).to.be.rejectedWith('no lines, version or ranges provided')
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the lines are null', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return an error', async function () {
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.updateDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
null,
|
|
|
|
this.version,
|
2023-11-13 08:59:10 -05:00
|
|
|
this.originalRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
).to.be.rejectedWith('no lines, version or ranges provided')
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the ranges are null', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return an error', async function () {
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.updateDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newDocLines,
|
|
|
|
this.version,
|
|
|
|
null
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
).to.be.rejectedWith('no lines, version or ranges provided')
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when there is a generic error getting the doc', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.error = new Error('doc could not be found')
|
2023-11-08 09:17:19 -05:00
|
|
|
this.DocManager.promises._getDoc = sinon.stub().rejects(this.error)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.updateDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newDocLines,
|
|
|
|
this.version,
|
|
|
|
this.originalRanges
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(this.error)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should not upsert the document to the doc collection', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection.should.not.have.been
|
|
|
|
.called
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-03-21 09:18:16 -04:00
|
|
|
describe('when the version was decremented', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
it('should return an error', async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(this.doc)
|
|
|
|
await expect(
|
|
|
|
this.DocManager.promises.updateDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newDocLines,
|
|
|
|
this.version - 1,
|
|
|
|
this.originalRanges
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(Errors.DocVersionDecrementedError)
|
2023-03-21 09:18:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc lines have not changed', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(this.doc)
|
|
|
|
this.result = await this.DocManager.promises.updateDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.oldDocLines.slice(),
|
|
|
|
this.version,
|
2023-11-13 08:59:10 -05:00
|
|
|
this.originalRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should not update the doc', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection.called.should.equal(
|
2020-02-16 09:02:55 -05:00
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-11-13 08:59:10 -05:00
|
|
|
it('should return the existing rev', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.result).to.deep.equal({ modified: false, rev: this.rev })
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-03-22 09:08:01 -04:00
|
|
|
describe('when the doc does not exist', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(null)
|
|
|
|
this.result = await this.DocManager.promises.updateDoc(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newDocLines,
|
|
|
|
this.version,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.originalRanges
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should upsert the document to the doc collection', function () {
|
2023-11-15 07:30:14 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection.should.have.been.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
undefined,
|
|
|
|
{
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: this.newDocLines,
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: this.originalRanges,
|
2023-11-15 07:30:14 -05:00
|
|
|
version: this.version,
|
|
|
|
}
|
|
|
|
)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2023-11-13 08:59:10 -05:00
|
|
|
it('should return the new rev', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.result).to.deep.equal({ modified: true, rev: 1 })
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
2023-03-22 09:08:01 -04:00
|
|
|
|
|
|
|
describe('when another update is racing', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.DocManager.promises._getDoc = sinon.stub().resolves(this.doc)
|
|
|
|
this.MongoManager.promises.upsertIntoDocCollection
|
2023-03-22 09:08:01 -04:00
|
|
|
.onFirstCall()
|
2023-11-08 09:17:19 -05:00
|
|
|
.rejects(new Errors.DocRevValueError())
|
2023-03-22 09:08:01 -04:00
|
|
|
this.RangeManager.shouldUpdateRanges.returns(true)
|
2023-11-08 09:17:19 -05:00
|
|
|
this.result = await this.DocManager.promises.updateDoc(
|
2023-03-22 09:08:01 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newDocLines,
|
|
|
|
this.version + 1,
|
2023-11-08 09:17:19 -05:00
|
|
|
this.newRanges
|
2023-03-22 09:08:01 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should upsert the doc twice', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection.should.have.been.calledWith(
|
2023-03-22 09:08:01 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.rev,
|
|
|
|
{
|
|
|
|
ranges: this.newRanges,
|
|
|
|
lines: this.newDocLines,
|
2023-11-15 07:30:14 -05:00
|
|
|
version: this.version + 1,
|
2023-03-22 09:08:01 -04:00
|
|
|
}
|
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
this.MongoManager.promises.upsertIntoDocCollection.should.have.been
|
|
|
|
.calledTwice
|
2023-03-22 09:08:01 -04:00
|
|
|
})
|
|
|
|
|
2023-11-13 08:59:10 -05:00
|
|
|
it('should return the new rev', function () {
|
2023-11-08 09:17:19 -05:00
|
|
|
expect(this.result).to.deep.equal({ modified: true, rev: this.rev + 1 })
|
2023-03-22 09:08:01 -04:00
|
|
|
})
|
|
|
|
})
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|