| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- /* eslint-disable
- handle-callback-err,
- no-return-assign,
- */
- // 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 SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const modulePath = require('path').join(
- __dirname,
- '../../../app/js/MongoManager'
- )
- const { ObjectId } = require('mongodb')
- const { assert } = require('chai')
- describe('MongoManager', function () {
- beforeEach(function () {
- this.MongoManager = SandboxedModule.require(modulePath, {
- requires: {
- './mongodb': {
- db: (this.db = { docs: {}, docOps: {} }),
- ObjectId
- },
- '@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
- '@overleaf/settings': { max_deleted_docs: 42 }
- }
- })
- this.project_id = ObjectId().toString()
- this.doc_id = ObjectId().toString()
- this.callback = sinon.stub()
- return (this.stubbedErr = new Error('hello world'))
- })
- describe('findDoc', function () {
- beforeEach(function () {
- this.doc = { name: 'mock-doc' }
- this.db.docs.findOne = sinon.stub().callsArgWith(2, null, this.doc)
- this.filter = { lines: true }
- return this.MongoManager.findDoc(
- this.project_id,
- this.doc_id,
- this.filter,
- this.callback
- )
- })
- it('should find the doc', function () {
- this.db.docs.findOne
- .calledWith(
- {
- _id: ObjectId(this.doc_id),
- project_id: ObjectId(this.project_id)
- },
- {
- projection: this.filter
- }
- )
- .should.equal(true)
- })
- return it('should call the callback with the doc', function () {
- return this.callback.calledWith(null, this.doc).should.equal(true)
- })
- })
- 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
- )
- })
- })
- describe('getProjectsDocs', function () {
- beforeEach(function () {
- this.filter = { lines: true }
- this.doc1 = { name: 'mock-doc1' }
- this.doc2 = { name: 'mock-doc2' }
- this.doc3 = { name: 'mock-doc3' }
- this.doc4 = { name: 'mock-doc4' }
- this.db.docs.find = sinon.stub().returns({
- toArray: sinon
- .stub()
- .callsArgWith(0, null, [this.doc, this.doc3, this.doc4])
- })
- })
- describe('with included_deleted = false', function () {
- beforeEach(function () {
- return this.MongoManager.getProjectsDocs(
- this.project_id,
- { include_deleted: false },
- this.filter,
- this.callback
- )
- })
- it('should find the non-deleted docs via the project_id', function () {
- return this.db.docs.find
- .calledWith(
- {
- project_id: ObjectId(this.project_id),
- deleted: { $ne: true }
- },
- {
- projection: this.filter
- }
- )
- .should.equal(true)
- })
- return it('should call the callback with the docs', function () {
- return this.callback
- .calledWith(null, [this.doc, this.doc3, this.doc4])
- .should.equal(true)
- })
- })
- return describe('with included_deleted = true', function () {
- beforeEach(function () {
- return this.MongoManager.getProjectsDocs(
- this.project_id,
- { include_deleted: true },
- this.filter,
- this.callback
- )
- })
- it('should find all via the project_id', function () {
- return this.db.docs.find
- .calledWith(
- {
- project_id: ObjectId(this.project_id)
- },
- {
- projection: this.filter
- }
- )
- .should.equal(true)
- })
- return it('should call the callback with the docs', function () {
- return this.callback
- .calledWith(null, [this.doc, this.doc3, this.doc4])
- .should.equal(true)
- })
- })
- })
- 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({
- toArray: sinon.stub().yields(null, [this.doc1, this.doc2, this.doc3])
- })
- this.callback.callsFake(done)
- this.MongoManager.getProjectsDeletedDocs(
- this.project_id,
- this.filter,
- this.callback
- )
- })
- it('should find the deleted docs via the project_id', function () {
- this.db.docs.find
- .calledWith({
- project_id: ObjectId(this.project_id),
- deleted: true
- })
- .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 },
- limit: 42
- })
- .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)
- })
- })
- describe('upsertIntoDocCollection', function () {
- beforeEach(function () {
- this.db.docs.updateOne = sinon.stub().callsArgWith(3, this.stubbedErr)
- return (this.oldRev = 77)
- })
- it('should upsert the document', function (done) {
- return this.MongoManager.upsertIntoDocCollection(
- this.project_id,
- this.doc_id,
- { lines: this.lines },
- (err) => {
- const args = this.db.docs.updateOne.args[0]
- 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()
- }
- )
- })
- return it('should return the error', function (done) {
- return this.MongoManager.upsertIntoDocCollection(
- this.project_id,
- this.doc_id,
- { lines: this.lines },
- (err) => {
- err.should.equal(this.stubbedErr)
- return done()
- }
- )
- })
- })
- describe('destroyDoc', function () {
- beforeEach(function (done) {
- this.db.docs.deleteOne = sinon.stub().yields()
- this.db.docOps.deleteOne = sinon.stub().yields()
- return this.MongoManager.destroyDoc('123456789012', done)
- })
- it('should destroy the doc', function () {
- return sinon.assert.calledWith(this.db.docs.deleteOne, {
- _id: ObjectId('123456789012')
- })
- })
- return it('should destroy the docOps', function () {
- return sinon.assert.calledWith(this.db.docOps.deleteOne, {
- doc_id: ObjectId('123456789012')
- })
- })
- })
- describe('getDocVersion', function () {
- describe('when the doc exists', function () {
- beforeEach(function () {
- this.doc = { version: (this.version = 42) }
- this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
- return this.MongoManager.getDocVersion(this.doc_id, this.callback)
- })
- it('should look for the doc in the database', function () {
- return this.db.docOps.findOne
- .calledWith(
- { doc_id: ObjectId(this.doc_id) },
- {
- projection: { version: 1 }
- }
- )
- .should.equal(true)
- })
- return it('should call the callback with the version', function () {
- return this.callback.calledWith(null, this.version).should.equal(true)
- })
- })
- return describe("when the doc doesn't exist", function () {
- beforeEach(function () {
- this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
- return this.MongoManager.getDocVersion(this.doc_id, this.callback)
- })
- return it('should call the callback with 0', function () {
- return this.callback.calledWith(null, 0).should.equal(true)
- })
- })
- })
- return describe('setDocVersion', function () {
- beforeEach(function () {
- this.version = 42
- this.db.docOps.updateOne = sinon.stub().callsArg(3)
- return this.MongoManager.setDocVersion(
- this.doc_id,
- this.version,
- this.callback
- )
- })
- it('should update the doc version', function () {
- return this.db.docOps.updateOne
- .calledWith(
- {
- doc_id: ObjectId(this.doc_id)
- },
- {
- $set: {
- version: this.version
- }
- },
- {
- upsert: true
- }
- )
- .should.equal(true)
- })
- return it('should call the callback', function () {
- return this.callback.called.should.equal(true)
- })
- })
- })
|