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

Promisify ReferalFeatures and ReferalFeaturesTests

GitOrigin-RevId: 25c1d80c5aea2883f62b95b8646a5670340c5cf2
This commit is contained in:
David 2024-02-08 11:13:25 +00:00 committed by Copybot
parent 144e22c209
commit d4e9d1bd89
2 changed files with 40 additions and 39 deletions

View file

@ -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) {
module.exports = ReferalFeatures = {
getBonusFeatures(userId, callback) {
if (callback == null) {
callback = function () {}
}
const query = { _id: userId } const query = { _id: userId }
User.findOne(query, { refered_user_count: 1 }, function (error, user) { const user = await User.findOne(query, { refered_user_count: 1 }).exec()
if (error) {
return callback(error)
}
if (user == null) { if (user == null) {
return callback(new Error(`user not found ${userId} for assignBonus`)) throw new Error(`user not found ${userId} for assignBonus`)
} }
if (user.refered_user_count != null && user.refered_user_count > 0) { if (user.refered_user_count != null && user.refered_user_count > 0) {
const newFeatures = ReferalFeatures._calculateFeatures(user) const newFeatures = ReferalFeatures._calculateFeatures(user)
callback(null, newFeatures) return newFeatures
} else {
callback(null, {})
} }
})
return {}
}, },
_calculateFeatures(user) { _calculateFeatures(user) {
@ -49,6 +42,7 @@ module.exports = ReferalFeatures = {
}, },
} }
module.exports.promises = { module.exports = {
getBonusFeatures: promisify(module.exports.getBonusFeatures), getBonusFeatures: callbackify(ReferalFeatures.getBonusFeatures),
promises: ReferalFeatures,
} }

View file

@ -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.User.findOne = sinon.stub().returns({
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback) 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 () { it('should return the features', function () {
this.callback expect(this.features).to.equal(this.Settings.bonus_features[3])
.calledWith(null, this.Settings.bonus_features[3])
.should.equal(true)
}) })
}) })
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 this.User.findOne = sinon.stub().returns({
exec: sinon
.stub() .stub()
.callsArgWith(2, null, { refered_user_count: this.refered_user_count }) .resolves({ refered_user_count: this.refered_user_count }),
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback) })
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 () { it('should return an empty feature set', function () {
this.callback.calledWith(null, {}).should.equal(true) expect(this.features).to.be.empty
}) })
}) })
}) })