add unit test for withRevCheck method

This commit is contained in:
Brian Gough 2021-08-02 11:36:43 +01:00
parent 7b904d9a9d
commit 8afdc8cbd4
2 changed files with 36 additions and 4 deletions

View file

@ -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,

View file

@ -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()
}
)
})
})
})