From 8afdc8cbd4fb325fc53301e6d71a5529457ac008 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 2 Aug 2021 11:36:43 +0100 Subject: [PATCH] add unit test for withRevCheck method --- services/docstore/app/js/MongoManager.js | 4 +-- .../test/unit/js/MongoManagerTests.js | 36 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/services/docstore/app/js/MongoManager.js b/services/docstore/app/js/MongoManager.js index 456d8c7334..263a162516 100644 --- a/services/docstore/app/js/MongoManager.js +++ b/services/docstore/app/js/MongoManager.js @@ -15,7 +15,7 @@ const { db, ObjectId } = require('./mongodb') const logger = require('logger-sharelatex') const metrics = require('@overleaf/metrics') const Settings = require('@overleaf/settings') -const { DocModifiedError } = require('./Errors') +const Errors = require('./Errors') const { promisify } = require('util') module.exports = MongoManager = { @@ -206,7 +206,7 @@ module.exports = MongoManager = { if (err) return callback(err) if (doc.rev !== currentRev) { return callback( - new DocModifiedError('doc rev has changed', { + new Errors.DocModifiedError('doc rev has changed', { doc_id: doc._id, rev: doc.rev, currentRev, diff --git a/services/docstore/test/unit/js/MongoManagerTests.js b/services/docstore/test/unit/js/MongoManagerTests.js index eb209a24c2..5bfcb3c179 100644 --- a/services/docstore/test/unit/js/MongoManagerTests.js +++ b/services/docstore/test/unit/js/MongoManagerTests.js @@ -29,7 +29,7 @@ describe('MongoManager', function () { }, '@overleaf/metrics': { timeAsyncMethod: sinon.stub() }, '@overleaf/settings': { max_deleted_docs: 42 }, - './Errors': { Errors }, + './Errors': Errors, }, }) this.project_id = ObjectId().toString() @@ -307,7 +307,7 @@ describe('MongoManager', function () { }) }) - return describe('setDocVersion', function () { + describe('setDocVersion', function () { beforeEach(function () { this.version = 42 this.db.docOps.updateOne = sinon.stub().callsArg(3) @@ -340,4 +340,36 @@ describe('MongoManager', function () { return this.callback.called.should.equal(true) }) }) + + describe('withRevCheck', function () { + this.beforeEach(function () { + this.doc = { _id: ObjectId(), name: 'mock-doc', rev: 1 } + this.testFunction = sinon.stub().yields(null, 'foo') + }) + + it('should call the callback when the rev has not changed', function (done) { + this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 1 }) + this.MongoManager.withRevCheck( + this.doc, + this.testFunction, + (err, result) => { + result.should.equal('foo') + assert.isNull(err) + done() + } + ) + }) + + it('should return an error when the rev has changed', function (done) { + this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 2 }) + this.MongoManager.withRevCheck( + this.doc, + this.testFunction, + (err, result) => { + err.should.be.instanceof(Errors.DocModifiedError) + done() + } + ) + }) + }) })