From 144e22c20922a9f2e4e6baa37a2eb2feb11681c7 Mon Sep 17 00:00:00 2001 From: David <33458145+davidmcpowell@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:13:14 +0000 Subject: [PATCH] Merge pull request #16918 from overleaf/dp-mongoose-callback-referal-handler Promisify ReferalHandler and ReferalHandlerTests GitOrigin-RevId: b73eda0063b04227a0d58d8e9bd0dea1ff6f0af1 --- .../src/Features/Referal/ReferalController.js | 2 +- .../src/Features/Referal/ReferalHandler.js | 22 +-- .../unit/src/Referal/ReferalHandlerTests.js | 125 +++++++++--------- 3 files changed, 75 insertions(+), 74 deletions(-) diff --git a/services/web/app/src/Features/Referal/ReferalController.js b/services/web/app/src/Features/Referal/ReferalController.js index 8ca733305d..28160fa482 100644 --- a/services/web/app/src/Features/Referal/ReferalController.js +++ b/services/web/app/src/Features/Referal/ReferalController.js @@ -6,7 +6,7 @@ module.exports = { const userId = SessionManager.getLoggedInUserId(req.session) ReferalHandler.getReferedUsers( userId, - (err, referedUsers, referedUserCount) => { + (err, { referedUsers, referedUserCount }) => { if (err) { next(err) } else { diff --git a/services/web/app/src/Features/Referal/ReferalHandler.js b/services/web/app/src/Features/Referal/ReferalHandler.js index bcc60815dd..3a6092dd2b 100644 --- a/services/web/app/src/Features/Referal/ReferalHandler.js +++ b/services/web/app/src/Features/Referal/ReferalHandler.js @@ -1,15 +1,17 @@ +const { callbackify } = require('@overleaf/promise-utils') const { User } = require('../../models/User') +async function getReferedUsers(userId) { + const projection = { refered_users: 1, refered_user_count: 1 } + const user = await User.findById(userId, projection).exec() + const referedUsers = user.refered_users || [] + const referedUserCount = user.refered_user_count || referedUsers.length + return { referedUsers, referedUserCount } +} + module.exports = { - getReferedUsers(userId, callback) { - const projection = { refered_users: 1, refered_user_count: 1 } - User.findById(userId, projection, function (err, user) { - if (err) { - return callback(err) - } - const referedUsers = user.refered_users || [] - const referedUserCount = user.refered_user_count || referedUsers.length - callback(null, referedUsers, referedUserCount) - }) + getReferedUsers: callbackify(getReferedUsers), + promises: { + getReferedUsers, }, } diff --git a/services/web/test/unit/src/Referal/ReferalHandlerTests.js b/services/web/test/unit/src/Referal/ReferalHandlerTests.js index 87caead125..3efd677e88 100644 --- a/services/web/test/unit/src/Referal/ReferalHandlerTests.js +++ b/services/web/test/unit/src/Referal/ReferalHandlerTests.js @@ -8,7 +8,11 @@ const modulePath = require('path').join( describe('Referal handler', function () { beforeEach(function () { - this.User = { findById: sinon.stub() } + this.User = { + findById: sinon.stub().returns({ + exec: sinon.stub(), + }), + } this.handler = SandboxedModule.require(modulePath, { requires: { '../../models/User': { @@ -20,73 +24,68 @@ describe('Referal handler', function () { }) describe('getting refered user_ids', function () { - it('should get the user from mongo and return the refered users array', function (done) { + it('should get the user from mongo and return the refered users array', async function () { const user = { refered_users: ['1234', '312312', '3213129'], refered_user_count: 3, } - this.User.findById.callsArgWith(2, null, user) - - this.handler.getReferedUsers( - this.user_id, - (err, passedReferedUserIds, passedReferedUserCount) => { - expect(err).not.to.exist - passedReferedUserIds.should.deep.equal(user.refered_users) - passedReferedUserCount.should.equal(3) - done() - } - ) - }) - - it('should return an empty array if it is not set', function (done) { - const user = {} - this.User.findById.callsArgWith(2, null, user) - - this.handler.getReferedUsers( - this.user_id, - (err, passedReferedUserIds, passedReferedUserCount) => { - expect(err).not.to.exist - passedReferedUserIds.length.should.equal(0) - done() - } - ) - }) - - it('should return a zero count if neither it or the array are set', function (done) { - const user = {} - this.User.findById.callsArgWith(2, null, user) - - this.handler.getReferedUsers( - this.user_id, - (err, passedReferedUserIds, passedReferedUserCount) => { - expect(err).not.to.exist - passedReferedUserCount.should.equal(0) - done() - } - ) - }) - - it('should return the array length if count is not set', function (done) { - const user = { refered_users: ['1234', '312312', '3213129'] } - this.User.findById.callsArgWith(2, null, user) - - this.handler.getReferedUsers( - this.user_id, - (err, passedReferedUserIds, passedReferedUserCount) => { - expect(err).not.to.exist - passedReferedUserCount.should.equal(3) - done() - } - ) - }) - - it('should error if finding the user fails', function (done) { - this.User.findById.callsArgWith(2, new Error('user not found')) - - this.handler.getReferedUsers(this.user_id, err => { - err.should.match(/user not found/) - done() + this.User.findById.returns({ + exec: sinon.stub().resolves(user), }) + + const { + referedUsers: passedReferedUserIds, + referedUserCount: passedReferedUserCount, + } = await this.handler.promises.getReferedUsers(this.user_id) + + passedReferedUserIds.should.deep.equal(user.refered_users) + passedReferedUserCount.should.equal(3) + }) + + it('should return an empty array if it is not set', async function () { + const user = {} + this.User.findById.returns({ + exec: sinon.stub().resolves(user), + }) + + const { referedUsers: passedReferedUserIds } = + await this.handler.promises.getReferedUsers(this.user_id) + + passedReferedUserIds.length.should.equal(0) + }) + + it('should return a zero count if neither it or the array are set', async function () { + const user = {} + this.User.findById.returns({ + exec: sinon.stub().resolves(user), + }) + + const { referedUserCount: passedReferedUserCount } = + await this.handler.promises.getReferedUsers(this.user_id) + + passedReferedUserCount.should.equal(0) + }) + + it('should return the array length if count is not set', async function () { + const user = { refered_users: ['1234', '312312', '3213129'] } + this.User.findById.returns({ + exec: sinon.stub().resolves(user), + }) + + const { referedUserCount: passedReferedUserCount } = + await this.handler.promises.getReferedUsers(this.user_id) + + passedReferedUserCount.should.equal(3) + }) + + it('should error if finding the user fails', async function () { + this.User.findById.returns({ + exec: sinon.stub().rejects(new Error('user not found')), + }) + + expect( + this.handler.promises.getReferedUsers(this.user_id) + ).to.be.rejectedWith('user not found') }) }) })