Merge pull request #2057 from overleaf/spd-promisify-userhelper

Add promisified version of User helper in acceptance tests

GitOrigin-RevId: ce2c28dd0d8ec66edaef54a260a3944f4cdd55d6
This commit is contained in:
Alasdair Smith 2019-08-09 10:38:58 +01:00 committed by sharelatex
parent 1d13f41163
commit 5b458e32ac

View file

@ -22,6 +22,7 @@ const { db, ObjectId } = require('../../../../app/src/infrastructure/mongojs')
const UserModel = require('../../../../app/src/models/User').User const UserModel = require('../../../../app/src/models/User').User
const UserUpdater = require('../../../../app/src/Features/User/UserUpdater') const UserUpdater = require('../../../../app/src/Features/User/UserUpdater')
const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager') const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager')
const { promisify } = require('util')
let count = 0 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 module.exports = User
function __guard__(value, transform) { function __guard__(value, transform) {