From 5b458e32ac2673b1a8b6af6c81c34365e8e1751b Mon Sep 17 00:00:00 2001 From: Alasdair Smith Date: Fri, 9 Aug 2019 10:38:58 +0100 Subject: [PATCH] Merge pull request #2057 from overleaf/spd-promisify-userhelper Add promisified version of User helper in acceptance tests GitOrigin-RevId: ce2c28dd0d8ec66edaef54a260a3944f4cdd55d6 --- .../web/test/acceptance/src/helpers/User.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/services/web/test/acceptance/src/helpers/User.js b/services/web/test/acceptance/src/helpers/User.js index 2ee2585779..539042aa01 100644 --- a/services/web/test/acceptance/src/helpers/User.js +++ b/services/web/test/acceptance/src/helpers/User.js @@ -22,6 +22,7 @@ const { db, ObjectId } = require('../../../../app/src/infrastructure/mongojs') const UserModel = require('../../../../app/src/models/User').User const UserUpdater = require('../../../../app/src/Features/User/UserUpdater') const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager') +const { promisify } = require('util') let count = 0 @@ -764,6 +765,30 @@ class User { } } +User.promises = class extends User { + doRequest(method, params) { + return new Promise((resolve, reject) => { + this.request[method.toLowerCase()](params, (err, response, body) => { + if (err) { + reject(err) + } else { + resolve({ response, body }) + } + }) + }) + } +} + +// promisify User class methods - works for methods with 0-1 output parameters, +// otherwise we will need to implement the method manually instead +const nonPromiseMethods = ['constructor', 'setExtraAttributes'] +Object.getOwnPropertyNames(User.prototype).forEach(methodName => { + const method = User.prototype[methodName] + if (typeof method === 'function' && !nonPromiseMethods.includes(methodName)) { + User.promises.prototype[methodName] = promisify(method) + } +}) + module.exports = User function __guard__(value, transform) {