Просмотр исходного кода

Merge pull request #16912 from overleaf/dp-mongoose-callback-referal-features

Promisify ReferalFeatures and ReferalFeaturesTests

GitOrigin-RevId: 25c1d80c5aea2883f62b95b8646a5670340c5cf2
David 2 лет назад
Родитель
Сommit
d4e9d1bd89

+ 17 - 23
services/web/app/src/Features/Referal/ReferalFeatures.js

@@ -1,30 +1,23 @@
 const _ = require('lodash')
 const _ = require('lodash')
-const { promisify } = require('util')
+const { callbackify } = require('util')
 const { User } = require('../../models/User')
 const { User } = require('../../models/User')
 const Settings = require('@overleaf/settings')
 const Settings = require('@overleaf/settings')
 
 
-let ReferalFeatures
+const ReferalFeatures = {
+  async getBonusFeatures(userId) {
+    const query = { _id: userId }
+    const user = await User.findOne(query, { refered_user_count: 1 }).exec()
 
 
-module.exports = ReferalFeatures = {
-  getBonusFeatures(userId, callback) {
-    if (callback == null) {
-      callback = function () {}
+    if (user == null) {
+      throw new Error(`user not found ${userId} for assignBonus`)
     }
     }
-    const query = { _id: userId }
-    User.findOne(query, { refered_user_count: 1 }, function (error, user) {
-      if (error) {
-        return callback(error)
-      }
-      if (user == null) {
-        return callback(new Error(`user not found ${userId} for assignBonus`))
-      }
-      if (user.refered_user_count != null && user.refered_user_count > 0) {
-        const newFeatures = ReferalFeatures._calculateFeatures(user)
-        callback(null, newFeatures)
-      } else {
-        callback(null, {})
-      }
-    })
+
+    if (user.refered_user_count != null && user.refered_user_count > 0) {
+      const newFeatures = ReferalFeatures._calculateFeatures(user)
+      return newFeatures
+    }
+
+    return {}
   },
   },
 
 
   _calculateFeatures(user) {
   _calculateFeatures(user) {
@@ -49,6 +42,7 @@ module.exports = ReferalFeatures = {
   },
   },
 }
 }
 
 
-module.exports.promises = {
-  getBonusFeatures: promisify(module.exports.getBonusFeatures),
+module.exports = {
+  getBonusFeatures: callbackify(ReferalFeatures.getBonusFeatures),
+  promises: ReferalFeatures,
 }
 }

+ 22 - 15
services/web/test/unit/src/Referal/ReferalFeaturesTests.js

@@ -1,5 +1,6 @@
 const SandboxedModule = require('sandboxed-module')
 const SandboxedModule = require('sandboxed-module')
 const sinon = require('sinon')
 const sinon = require('sinon')
+const { expect } = require('chai')
 const modulePath = require('path').join(
 const modulePath = require('path').join(
   __dirname,
   __dirname,
   '../../../../app/src/Features/Referal/ReferalFeatures.js'
   '../../../../app/src/Features/Referal/ReferalFeatures.js'
@@ -15,7 +16,6 @@ describe('ReferalFeatures', function () {
         '@overleaf/settings': (this.Settings = {}),
         '@overleaf/settings': (this.Settings = {}),
       },
       },
     })
     })
-    this.callback = sinon.stub()
     this.referal_id = 'referal-id-123'
     this.referal_id = 'referal-id-123'
     this.referal_medium = 'twitter'
     this.referal_medium = 'twitter'
     this.user_id = 'user-id-123'
     this.user_id = 'user-id-123'
@@ -23,7 +23,7 @@ describe('ReferalFeatures', function () {
   })
   })
 
 
   describe('getBonusFeatures', function () {
   describe('getBonusFeatures', function () {
-    beforeEach(function () {
+    beforeEach(async function () {
       this.refered_user_count = 3
       this.refered_user_count = 3
       this.Settings.bonus_features = {
       this.Settings.bonus_features = {
         3: {
         3: {
@@ -37,23 +37,25 @@ describe('ReferalFeatures', function () {
         features: { collaborators: 1, dropbox: false, versioning: false },
         features: { collaborators: 1, dropbox: false, versioning: false },
       }
       }
 
 
-      this.User.findOne = sinon.stub().callsArgWith(2, null, stubbedUser)
-      this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
+      this.User.findOne = sinon.stub().returns({
+        exec: sinon.stub().resolves(stubbedUser),
+      })
+      this.features = await this.ReferalFeatures.promises.getBonusFeatures(
+        this.user_id
+      )
     })
     })
 
 
     it('should get the users number of refered user', function () {
     it('should get the users number of refered user', function () {
       this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
       this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
     })
     })
 
 
-    it('should call the callback with the features', function () {
-      this.callback
-        .calledWith(null, this.Settings.bonus_features[3])
-        .should.equal(true)
+    it('should return the features', function () {
+      expect(this.features).to.equal(this.Settings.bonus_features[3])
     })
     })
   })
   })
 
 
   describe('when the user is not at a bonus level', function () {
   describe('when the user is not at a bonus level', function () {
-    beforeEach(function () {
+    beforeEach(async function () {
       this.refered_user_count = 0
       this.refered_user_count = 0
       this.Settings.bonus_features = {
       this.Settings.bonus_features = {
         1: {
         1: {
@@ -62,18 +64,23 @@ describe('ReferalFeatures', function () {
           versioning: false,
           versioning: false,
         },
         },
       }
       }
-      this.User.findOne = sinon
-        .stub()
-        .callsArgWith(2, null, { refered_user_count: this.refered_user_count })
-      this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
+      this.User.findOne = sinon.stub().returns({
+        exec: sinon
+          .stub()
+          .resolves({ refered_user_count: this.refered_user_count }),
+      })
+
+      this.features = await this.ReferalFeatures.promises.getBonusFeatures(
+        this.user_id
+      )
     })
     })
 
 
     it('should get the users number of refered user', function () {
     it('should get the users number of refered user', function () {
       this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
       this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
     })
     })
 
 
-    it('should call the callback with no features', function () {
-      this.callback.calledWith(null, {}).should.equal(true)
+    it('should return an empty feature set', function () {
+      expect(this.features).to.be.empty
     })
     })
   })
   })
 })
 })