Merge pull request #5592 from overleaf/jpa-fix-mongodb-result-check

[misc] fix detection of matched mongodb filter for update operations

GitOrigin-RevId: 856b871feba1df13ef163fa763274927d49b9105
This commit is contained in:
Jakob Ackermann 2021-10-28 10:35:02 +02:00 committed by Copybot
parent aa846a6af2
commit 1505efc23d
2 changed files with 30 additions and 14 deletions

View file

@ -162,7 +162,7 @@ async function setDefaultEmailAddress(
const res = await UserUpdater.promises.updateUser(query, update)
// this should not happen
if (res.n === 0) {
if (res.matchedCount !== 1) {
throw new Error('email update error')
}
@ -226,7 +226,7 @@ async function confirmEmail(userId, email) {
const res = await UserUpdater.promises.updateUser(query, update)
if (res.n === 0) {
if (res.matchedCount !== 1) {
throw new Errors.NotFoundError('user id and email do no match')
}
await FeaturesUpdater.promises.refreshFeatures(userId, 'confirm-email')
@ -341,7 +341,7 @@ const UserUpdater = {
OError.tag(error, 'problem removing users email')
return callback(error)
}
if (res.n === 0) {
if (res.matchedCount !== 1) {
return callback(new Error('Cannot remove email'))
}
FeaturesUpdater.refreshFeatures(userId, 'remove-email', callback)

View file

@ -103,7 +103,7 @@ describe('UserUpdater', function () {
beforeEach(function () {
this.UserUpdater.updateUser = sinon
.stub()
.callsArgWith(2, null, { n: 1, nModified: 1, ok: 1 })
.yields(null, { matchedCount: 1, modifiedCount: 1 })
})
it('should not remove affiliationUnchecked flag if v1 returns an error', function (done) {
this.addAffiliation.yields(true)
@ -349,7 +349,7 @@ describe('UserUpdater', function () {
beforeEach(function () {
this.UserUpdater.updateUser = sinon
.stub()
.callsArgWith(2, null, { nMatched: 1 })
.yields(null, { matchedCount: 1 })
})
it('remove email', function (done) {
@ -412,7 +412,9 @@ describe('UserUpdater', function () {
})
it('handle missed update', function (done) {
this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
this.UserUpdater.updateUser = sinon
.stub()
.yields(null, { matchedCount: 0 })
this.UserUpdater.removeEmailAddress(
this.stubbedUser._id,
@ -463,7 +465,9 @@ describe('UserUpdater', function () {
})
it('set default', function (done) {
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ matchedCount: 1 })
this.UserUpdater.setDefaultEmailAddress(
this.stubbedUser._id,
@ -484,7 +488,9 @@ describe('UserUpdater', function () {
})
it('set changed the email in newsletter', function (done) {
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ matchedCount: 1 })
this.UserUpdater.setDefaultEmailAddress(
this.stubbedUser._id,
@ -520,7 +526,9 @@ describe('UserUpdater', function () {
})
it('handle missed update', function (done) {
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 0 })
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ matchedCount: 0 })
this.UserUpdater.setDefaultEmailAddress(
this.stubbedUser._id,
@ -548,7 +556,9 @@ describe('UserUpdater', function () {
})
it('updates audit log', function (done) {
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ matchedCount: 1 })
this.UserUpdater.setDefaultEmailAddress(
this.stubbedUser._id,
@ -647,7 +657,9 @@ describe('UserUpdater', function () {
describe('security alert', function () {
it('should be sent to old and new email when sendSecurityAlert=true', function (done) {
// this.UserGetter.promises.getUser.resolves(this.stubbedUser)
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ matchedCount: 1 })
this.UserUpdater.setDefaultEmailAddress(
this.stubbedUser._id,
@ -677,7 +689,7 @@ describe('UserUpdater', function () {
this.EmailHandler.promises.sendEmail.rejects(anError)
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ n: 1 })
.resolves({ matchedCount: 1 })
})
it('should log but not pass back the error', function (done) {
this.UserUpdater.setDefaultEmailAddress(
@ -707,7 +719,9 @@ describe('UserUpdater', function () {
describe('confirmEmail', function () {
beforeEach(function () {
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ matchedCount: 1 })
})
it('should update the email record', function (done) {
@ -773,7 +787,9 @@ describe('UserUpdater', function () {
})
it('handle missed update', function (done) {
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 0 })
this.UserUpdater.promises.updateUser = sinon
.stub()
.resolves({ matchedCount: 0 })
this.UserUpdater.confirmEmail(
this.stubbedUser._id,