Procházet zdrojové kódy

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

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

GitOrigin-RevId: 856b871feba1df13ef163fa763274927d49b9105
Jakob Ackermann před 5 roky
rodič
revize
1505efc23d

+ 3 - 3
services/web/app/src/Features/User/UserUpdater.js

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

+ 27 - 11
services/web/test/unit/src/User/UserUpdaterTests.js

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