2020-02-16 09:02:49 -05:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-16 09:02:48 -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-16 09:02:55 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/MongoManager'
|
|
|
|
)
|
2020-08-21 12:50:01 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-02-16 09:02:55 -05:00
|
|
|
const { assert } = require('chai')
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('MongoManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.MongoManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-08-21 12:50:01 -04:00
|
|
|
'./mongodb': {
|
2020-02-16 09:02:55 -05:00
|
|
|
db: (this.db = { docs: {}, docOps: {} }),
|
|
|
|
ObjectId
|
|
|
|
},
|
2020-09-10 13:07:14 -04:00
|
|
|
'@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
|
2020-02-16 09:02:55 -05:00
|
|
|
'logger-sharelatex': { log() {} }
|
2020-07-31 11:20:07 -04:00
|
|
|
},
|
|
|
|
globals: {
|
|
|
|
console
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
this.doc_id = ObjectId().toString()
|
2020-08-28 08:13:19 -04:00
|
|
|
this.callback = sinon.stub()
|
2020-02-16 09:02:55 -05:00
|
|
|
return (this.stubbedErr = new Error('hello world'))
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('findDoc', function () {
|
2020-08-28 08:13:19 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.doc = { name: 'mock-doc' }
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docs.findOne = sinon.stub().callsArgWith(2, null, this.doc)
|
2020-02-16 09:02:55 -05:00
|
|
|
this.filter = { lines: true }
|
|
|
|
return this.MongoManager.findDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.filter,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should find the doc', function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docs.findOne
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith(
|
|
|
|
{
|
|
|
|
_id: ObjectId(this.doc_id),
|
|
|
|
project_id: ObjectId(this.project_id)
|
|
|
|
},
|
2020-09-07 05:16:12 -04:00
|
|
|
{
|
|
|
|
projection: this.filter
|
|
|
|
}
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should call the callback with the doc', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.callback.calledWith(null, this.doc).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-15 08:13:48 -05:00
|
|
|
describe('patchDoc', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.db.docs.updateOne = sinon.stub().yields(null)
|
|
|
|
this.meta = { name: 'foo.tex' }
|
|
|
|
this.callback.callsFake(done)
|
|
|
|
this.MongoManager.patchDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.meta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should pass the parameter along', function () {
|
|
|
|
this.db.docs.updateOne.should.have.been.calledWith(
|
|
|
|
{
|
|
|
|
_id: ObjectId(this.doc_id),
|
|
|
|
project_id: ObjectId(this.project_id)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: this.meta
|
|
|
|
},
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getProjectsDocs', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05: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 12:50:01 -04:00
|
|
|
this.db.docs.find = sinon.stub().returns({
|
|
|
|
toArray: sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, null, [this.doc, this.doc3, this.doc4])
|
|
|
|
})
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('with included_deleted = false', function () {
|
2020-08-28 08:13:19 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.getProjectsDocs(
|
|
|
|
this.project_id,
|
|
|
|
{ include_deleted: false },
|
|
|
|
this.filter,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should find the non-deleted docs via the project_id', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.db.docs.find
|
|
|
|
.calledWith(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(this.project_id),
|
|
|
|
deleted: { $ne: true }
|
|
|
|
},
|
2020-09-07 05:16:12 -04:00
|
|
|
{
|
|
|
|
projection: this.filter
|
|
|
|
}
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should call the callback with the docs', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, [this.doc, this.doc3, this.doc4])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return describe('with included_deleted = true', function () {
|
2020-08-28 08:13:19 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.getProjectsDocs(
|
|
|
|
this.project_id,
|
|
|
|
{ include_deleted: true },
|
|
|
|
this.filter,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should find all via the project_id', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.db.docs.find
|
|
|
|
.calledWith(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(this.project_id)
|
|
|
|
},
|
2020-09-07 05:16:12 -04:00
|
|
|
{
|
|
|
|
projection: this.filter
|
|
|
|
}
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should call the callback with the docs', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, [this.doc, this.doc3, this.doc4])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('upsertIntoDocCollection', function () {
|
|
|
|
beforeEach(function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docs.updateOne = sinon.stub().callsArgWith(3, this.stubbedErr)
|
2020-02-16 09:02:55 -05:00
|
|
|
return (this.oldRev = 77)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should upsert the document', function (done) {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.upsertIntoDocCollection(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
{ lines: this.lines },
|
2020-05-28 09:20:54 -04:00
|
|
|
(err) => {
|
2020-08-21 12:50:01 -04:00
|
|
|
const args = this.db.docs.updateOne.args[0]
|
2020-02-16 09:02:55 -05:00
|
|
|
assert.deepEqual(args[0], { _id: ObjectId(this.doc_id) })
|
|
|
|
assert.equal(args[1].$set.lines, this.lines)
|
|
|
|
assert.equal(args[1].$inc.rev, 1)
|
|
|
|
assert.deepEqual(args[1].$set.project_id, ObjectId(this.project_id))
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should return the error', function (done) {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.upsertIntoDocCollection(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
{ lines: this.lines },
|
2020-05-28 09:20:54 -04:00
|
|
|
(err) => {
|
2020-02-16 09:02:55 -05:00
|
|
|
err.should.equal(this.stubbedErr)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('markDocAsDeleted', function () {
|
|
|
|
beforeEach(function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docs.updateOne = sinon.stub().callsArgWith(2, this.stubbedErr)
|
2020-02-16 09:02:55 -05:00
|
|
|
return (this.oldRev = 77)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should process the update', function (done) {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.markDocAsDeleted(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2020-05-28 09:20:54 -04:00
|
|
|
(err) => {
|
2020-08-21 12:50:01 -04:00
|
|
|
const args = this.db.docs.updateOne.args[0]
|
2020-02-16 09:02:55 -05:00
|
|
|
assert.deepEqual(args[0], {
|
|
|
|
_id: ObjectId(this.doc_id),
|
|
|
|
project_id: ObjectId(this.project_id)
|
|
|
|
})
|
|
|
|
assert.equal(args[1].$set.deleted, true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should return the error', function (done) {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.markDocAsDeleted(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2020-05-28 09:20:54 -04:00
|
|
|
(err) => {
|
2020-02-16 09:02:55 -05:00
|
|
|
err.should.equal(this.stubbedErr)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('destroyDoc', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docs.deleteOne = sinon.stub().yields()
|
|
|
|
this.db.docOps.deleteOne = sinon.stub().yields()
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.destroyDoc('123456789012', done)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should destroy the doc', function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
return sinon.assert.calledWith(this.db.docs.deleteOne, {
|
2020-02-16 09:02:55 -05:00
|
|
|
_id: ObjectId('123456789012')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should destroy the docOps', function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
return sinon.assert.calledWith(this.db.docOps.deleteOne, {
|
2020-02-16 09:02:55 -05:00
|
|
|
doc_id: ObjectId('123456789012')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getDocVersion', function () {
|
|
|
|
describe('when the doc exists', function () {
|
2020-08-28 08:13:19 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.doc = { version: (this.version = 42) }
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.getDocVersion(this.doc_id, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should look for the doc in the database', function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
return this.db.docOps.findOne
|
2020-09-07 05:16:12 -04:00
|
|
|
.calledWith(
|
|
|
|
{ doc_id: ObjectId(this.doc_id) },
|
|
|
|
{
|
|
|
|
projection: { version: 1 }
|
|
|
|
}
|
|
|
|
)
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should call the callback with the version', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.callback.calledWith(null, this.version).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return describe("when the doc doesn't exist", function () {
|
2020-08-28 08:13:19 -04:00
|
|
|
beforeEach(function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.getDocVersion(this.doc_id, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should call the callback with 0', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.callback.calledWith(null, 0).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return describe('setDocVersion', function () {
|
2020-08-28 08:13:19 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.version = 42
|
2020-08-21 12:50:01 -04:00
|
|
|
this.db.docOps.updateOne = sinon.stub().callsArg(3)
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.MongoManager.setDocVersion(
|
|
|
|
this.doc_id,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should update the doc version', function () {
|
2020-08-21 12:50:01 -04:00
|
|
|
return this.db.docOps.updateOne
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith(
|
|
|
|
{
|
|
|
|
doc_id: ObjectId(this.doc_id)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
version: this.version
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
upsert: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|