[web] Promisify LaunchpadController (#18497)

* Remove unused exports and decaffeinate comment

* Update export format

* Make `launchpadPage` async

* Make `_atLeastOneAdminExists` async

* Make `sendTestEmail` async

* Make `registerExternalAuthAdmin` async

* Make `registerAdmin` async

* Update tests LaunchpadControllerTests.js

* Use `expressify(...)`

* Fix unit tests: export `LaunchpadController`

* Fix unit tests by awaiting expressified functions (could include breaking changes!)

* Call `.exec()` after `User.updateOne(...)`

* Revert promisification of `expressify`

* Revert some `return` removal in tests

GitOrigin-RevId: c73a38bf1cf798830df046fe96d3516178655ea6
This commit is contained in:
Antoine Clausse 2024-06-05 12:24:20 +02:00 committed by Copybot
parent c24ace801b
commit 258289e65a
2 changed files with 362 additions and 399 deletions

View file

@ -1,25 +1,10 @@
/* eslint-disable
n/handle-callback-err,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let LaunchpadController
const OError = require('@overleaf/o-error') const OError = require('@overleaf/o-error')
const { expressify } = require('@overleaf/promise-utils')
const Settings = require('@overleaf/settings') const Settings = require('@overleaf/settings')
const Path = require('path') const Path = require('path')
const Url = require('url')
const logger = require('@overleaf/logger') const logger = require('@overleaf/logger')
const metrics = require('@overleaf/metrics')
const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler') const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler')
const EmailHandler = require('../../../../app/src/Features/Email/EmailHandler') const EmailHandler = require('../../../../app/src/Features/Email/EmailHandler')
const _ = require('lodash')
const UserGetter = require('../../../../app/src/Features/User/UserGetter') const UserGetter = require('../../../../app/src/Features/User/UserGetter')
const { User } = require('../../../../app/src/models/User') const { User } = require('../../../../app/src/models/User')
const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager') const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager')
@ -29,7 +14,7 @@ const {
hasAdminAccess, hasAdminAccess,
} = require('../../../../app/src/Features/Helpers/AdminAuthorizationHelper') } = require('../../../../app/src/Features/Helpers/AdminAuthorizationHelper')
module.exports = LaunchpadController = { const _LaunchpadController = {
_getAuthMethod() { _getAuthMethod() {
if (Settings.ldap) { if (Settings.ldap) {
return 'ldap' return 'ldap'
@ -40,15 +25,12 @@ module.exports = LaunchpadController = {
} }
}, },
launchpadPage(req, res, next) { async launchpadPage(req, res) {
// TODO: check if we're using external auth? // TODO: check if we're using external auth?
// * how does all this work with ldap and saml? // * how does all this work with ldap and saml?
const sessionUser = SessionManager.getSessionUser(req.session) const sessionUser = SessionManager.getSessionUser(req.session)
const authMethod = LaunchpadController._getAuthMethod() const authMethod = LaunchpadController._getAuthMethod()
LaunchpadController._atLeastOneAdminExists(function (err, adminUserExists) { const adminUserExists = await LaunchpadController._atLeastOneAdminExists()
if (err != null) {
return next(err)
}
if (!sessionUser) { if (!sessionUser) {
if (!adminUserExists) { if (!adminUserExists) {
res.render(Path.resolve(__dirname, '../views/launchpad'), { res.render(Path.resolve(__dirname, '../views/launchpad'), {
@ -60,13 +42,9 @@ module.exports = LaunchpadController = {
res.redirect('/login') res.redirect('/login')
} }
} else { } else {
UserGetter.getUser( const user = await UserGetter.promises.getUser(sessionUser._id, {
sessionUser._id, isAdmin: 1,
{ isAdmin: 1 }, })
function (err, user) {
if (err != null) {
return next(err)
}
if (hasAdminAccess(user)) { if (hasAdminAccess(user)) {
res.render(Path.resolve(__dirname, '../views/launchpad'), { res.render(Path.resolve(__dirname, '../views/launchpad'), {
wsUrl: Settings.wsUrl, wsUrl: Settings.wsUrl,
@ -77,28 +55,17 @@ module.exports = LaunchpadController = {
res.redirect('/restricted') res.redirect('/restricted')
} }
} }
)
}
})
}, },
_atLeastOneAdminExists(callback) { async _atLeastOneAdminExists() {
if (callback == null) { const user = await UserGetter.promises.getUser(
callback = function () {}
}
UserGetter.getUser(
{ isAdmin: true }, { isAdmin: true },
{ _id: 1, isAdmin: 1 }, { _id: 1, isAdmin: 1 }
function (err, user) {
if (err != null) {
return callback(err)
}
callback(null, user != null)
}
) )
return Boolean(user)
}, },
sendTestEmail(req, res, next) { async sendTestEmail(req, res) {
const { email } = req.body const { email } = req.body
if (!email) { if (!email) {
logger.debug({}, 'no email address supplied') logger.debug({}, 'no email address supplied')
@ -108,20 +75,20 @@ module.exports = LaunchpadController = {
} }
logger.debug({ email }, 'sending test email') logger.debug({ email }, 'sending test email')
const emailOptions = { to: email } const emailOptions = { to: email }
EmailHandler.sendEmail('testEmail', emailOptions, function (err) { try {
if (err != null) { await EmailHandler.promises.sendEmail('testEmail', emailOptions)
logger.debug({ email }, 'sent test email')
res.json({ message: res.locals.translate('email_sent') })
} catch (err) {
OError.tag(err, 'error sending test email', { OError.tag(err, 'error sending test email', {
email, email,
}) })
return next(err) throw err
} }
logger.debug({ email }, 'sent test email')
res.json({ message: res.locals.translate('email_sent') })
})
}, },
registerExternalAuthAdmin(authMethod) { registerExternalAuthAdmin(authMethod) {
return function (req, res, next) { return expressify(async function (req, res) {
if (LaunchpadController._getAuthMethod() !== authMethod) { if (LaunchpadController._getAuthMethod() !== authMethod) {
logger.debug( logger.debug(
{ authMethod }, { authMethod },
@ -136,10 +103,8 @@ module.exports = LaunchpadController = {
} }
logger.debug({ email }, 'attempted register first admin user') logger.debug({ email }, 'attempted register first admin user')
LaunchpadController._atLeastOneAdminExists(function (err, exists) {
if (err != null) { const exists = await LaunchpadController._atLeastOneAdminExists()
return next(err)
}
if (exists) { if (exists) {
logger.debug( logger.debug(
@ -160,29 +125,30 @@ module.exports = LaunchpadController = {
'creating admin account for specified external-auth user' 'creating admin account for specified external-auth user'
) )
UserRegistrationHandler.registerNewUser(body, function (err, user) { let user
if (err != null) { try {
user = await UserRegistrationHandler.promises.registerNewUser(body)
} catch (err) {
OError.tag(err, 'error with registerNewUser', { OError.tag(err, 'error with registerNewUser', {
email, email,
authMethod, authMethod,
}) })
return next(err) throw err
} }
// Ignore spurious floating promises warning until we promisify try {
// eslint-disable-next-line @typescript-eslint/no-floating-promises await User.updateOne(
User.updateOne(
{ _id: user._id }, { _id: user._id },
{ {
$set: { isAdmin: true }, $set: { isAdmin: true },
emails: [{ email }], emails: [{ email }],
}, }
function (err) { ).exec()
if (err != null) { } catch (err) {
OError.tag(err, 'error setting user to admin', { OError.tag(err, 'error setting user to admin', {
user_id: user._id, user_id: user._id,
}) })
return next(err) throw err
} }
AuthenticationController.setRedirectInSession(req, '/launchpad') AuthenticationController.setRedirectInSession(req, '/launchpad')
@ -192,14 +158,10 @@ module.exports = LaunchpadController = {
) )
res.json({ redir: '/launchpad', email }) res.json({ redir: '/launchpad', email })
}
)
}) })
})
}
}, },
registerAdmin(req, res, next) { async registerAdmin(req, res) {
const { email } = req.body const { email } = req.body
const { password } = req.body const { password } = req.body
if (!email || !password) { if (!email || !password) {
@ -208,10 +170,7 @@ module.exports = LaunchpadController = {
} }
logger.debug({ email }, 'attempted register first admin user') logger.debug({ email }, 'attempted register first admin user')
LaunchpadController._atLeastOneAdminExists(function (err, exists) { const exists = await LaunchpadController._atLeastOneAdminExists()
if (err != null) {
return next(err)
}
if (exists) { if (exists) {
logger.debug( logger.debug(
@ -241,38 +200,40 @@ module.exports = LaunchpadController = {
} }
const body = { email, password } const body = { email, password }
UserRegistrationHandler.registerNewUser(body, function (err, user) {
if (err != null) { const user = await UserRegistrationHandler.promises.registerNewUser(body)
return next(err)
}
logger.debug({ userId: user._id }, 'making user an admin') logger.debug({ userId: user._id }, 'making user an admin')
// Ignore spurious floating promises warning until we promisify
// eslint-disable-next-line @typescript-eslint/no-floating-promises try {
User.updateOne( await User.updateOne(
{ _id: user._id }, { _id: user._id },
{ {
$set: { $set: {
isAdmin: true, isAdmin: true,
emails: [{ email }], emails: [{ email }],
}, },
}, }
function (err) { ).exec()
if (err != null) { } catch (err) {
OError.tag(err, 'error setting user to admin', { OError.tag(err, 'error setting user to admin', {
user_id: user._id, user_id: user._id,
}) })
return next(err) throw err
} }
logger.debug( logger.debug({ email, userId: user._id }, 'created first admin account')
{ email, userId: user._id },
'created first admin account'
)
res.json({ redir: '/launchpad' }) res.json({ redir: '/launchpad' })
}
)
})
})
}, },
} }
const LaunchpadController = {
launchpadPage: expressify(_LaunchpadController.launchpadPage),
registerAdmin: expressify(_LaunchpadController.registerAdmin),
registerExternalAuthAdmin: _LaunchpadController.registerExternalAuthAdmin,
sendTestEmail: expressify(_LaunchpadController.sendTestEmail),
_atLeastOneAdminExists: _LaunchpadController._atLeastOneAdminExists,
_getAuthMethod: _LaunchpadController._getAuthMethod,
}
module.exports = LaunchpadController

View file

@ -11,9 +11,9 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const { expect } = require('chai') const { expect } = require('chai')
const sinon = require('sinon') const sinon = require('sinon')
const MockResponse = require('../../../../../test/unit/src/helpers/MockResponse')
const modulePath = require('path').join( const modulePath = require('path').join(
__dirname, __dirname,
'../../../app/src/LaunchpadController.js' '../../../app/src/LaunchpadController.js'
@ -36,10 +36,14 @@ describe('LaunchpadController', function () {
}), }),
'@overleaf/metrics': (this.Metrics = {}), '@overleaf/metrics': (this.Metrics = {}),
'../../../../app/src/Features/User/UserRegistrationHandler': '../../../../app/src/Features/User/UserRegistrationHandler':
(this.UserRegistrationHandler = {}), (this.UserRegistrationHandler = {
promises: {},
}),
'../../../../app/src/Features/Email/EmailHandler': (this.EmailHandler = '../../../../app/src/Features/Email/EmailHandler': (this.EmailHandler =
{}), { promises: {} }),
'../../../../app/src/Features/User/UserGetter': (this.UserGetter = {}), '../../../../app/src/Features/User/UserGetter': (this.UserGetter = {
promises: {},
}),
'../../../../app/src/models/User': { User: this.User }, '../../../../app/src/models/User': { User: this.User },
'../../../../app/src/Features/Authentication/AuthenticationController': '../../../../app/src/Features/Authentication/AuthenticationController':
(this.AuthenticationController = {}), (this.AuthenticationController = {}),
@ -58,21 +62,12 @@ describe('LaunchpadController', function () {
session: {}, session: {},
} }
this.res = { this.res = new MockResponse()
render: sinon.stub(), this.res.locals = {
redirect: sinon.stub(),
json: sinon.stub(),
send: sinon.stub(),
sendStatus: sinon.stub(),
locals: {
translate(key) { translate(key) {
return key return key
}, },
},
} }
this.res.status = sinon.stub().returns(this.res)
return (this.next = sinon.stub())
}) })
describe('launchpadPage', function () { describe('launchpadPage', function () {
@ -91,12 +86,13 @@ describe('LaunchpadController', function () {
describe('when the user is not logged in', function () { describe('when the user is not logged in', function () {
beforeEach(function () { beforeEach(function () {
this.SessionManager.getSessionUser = sinon.stub().returns(null) this.SessionManager.getSessionUser = sinon.stub().returns(null)
return (this.res.render = sinon.stub())
}) })
describe('when there are no admins', function () { describe('when there are no admins', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.res.callback = done
this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.launchpadPage( return this.LaunchpadController.launchpadPage(
this.req, this.req,
this.res, this.res,
@ -120,8 +116,10 @@ describe('LaunchpadController', function () {
}) })
describe('when there is at least one admin', function () { describe('when there is at least one admin', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, true) this._atLeastOneAdminExists.resolves(true)
this.res.callback = done
this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.launchpadPage( return this.LaunchpadController.launchpadPage(
this.req, this.req,
this.res, this.res,
@ -149,16 +147,16 @@ describe('LaunchpadController', function () {
email: 'abcd@example.com', email: 'abcd@example.com',
} }
this.SessionManager.getSessionUser = sinon.stub().returns(this.user) this.SessionManager.getSessionUser = sinon.stub().returns(this.user)
this._atLeastOneAdminExists.callsArgWith(0, null, true) this._atLeastOneAdminExists.resolves(true)
this.res.render = sinon.stub()
return (this.res.redirect = sinon.stub())
}) })
describe('when the user is an admin', function () { describe('when the user is an admin', function () {
beforeEach(function () { beforeEach(function (done) {
this.UserGetter.getUser = sinon this.UserGetter.promises.getUser = sinon
.stub() .stub()
.callsArgWith(2, null, { isAdmin: true }) .resolves({ isAdmin: true })
this.res.callback = done
this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.launchpadPage( return this.LaunchpadController.launchpadPage(
this.req, this.req,
this.res, this.res,
@ -183,10 +181,12 @@ describe('LaunchpadController', function () {
}) })
describe('when the user is not an admin', function () { describe('when the user is not an admin', function () {
beforeEach(function () { beforeEach(function (done) {
this.UserGetter.getUser = sinon this.UserGetter.promises.getUser = sinon
.stub() .stub()
.callsArgWith(2, null, { isAdmin: false }) .resolves({ isAdmin: false })
this.res.callback = done
this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.launchpadPage( return this.LaunchpadController.launchpadPage(
this.req, this.req,
this.res, this.res,
@ -205,75 +205,54 @@ describe('LaunchpadController', function () {
describe('_atLeastOneAdminExists', function () { describe('_atLeastOneAdminExists', function () {
describe('when there are no admins', function () { describe('when there are no admins', function () {
beforeEach(function () { beforeEach(function () {
return (this.UserGetter.getUser = sinon return (this.UserGetter.promises.getUser = sinon.stub().resolves(null))
.stub()
.callsArgWith(2, null, null))
}) })
it('should callback with false', function (done) { it('should callback with false', async function () {
return this.LaunchpadController._atLeastOneAdminExists( const exists = await this.LaunchpadController._atLeastOneAdminExists()
(err, exists) => {
expect(err).to.equal(null)
expect(exists).to.equal(false) expect(exists).to.equal(false)
return done()
}
)
}) })
}) })
describe('when there are some admins', function () { describe('when there are some admins', function () {
beforeEach(function () { beforeEach(function () {
return (this.UserGetter.getUser = sinon return (this.UserGetter.promises.getUser = sinon
.stub() .stub()
.callsArgWith(2, null, { _id: 'abcd' })) .resolves({ _id: 'abcd' }))
}) })
it('should callback with true', function (done) { it('should callback with true', async function () {
return this.LaunchpadController._atLeastOneAdminExists( const exists = await this.LaunchpadController._atLeastOneAdminExists()
(err, exists) => {
expect(err).to.equal(null)
expect(exists).to.equal(true) expect(exists).to.equal(true)
return done()
}
)
}) })
}) })
describe('when getUser produces an error', function () { describe('when getUser produces an error', function () {
beforeEach(function () { beforeEach(function () {
return (this.UserGetter.getUser = sinon return (this.UserGetter.promises.getUser = sinon
.stub() .stub()
.callsArgWith(2, new Error('woops'))) .rejects(new Error('woops')))
}) })
it('should produce an error', function (done) { it('should produce an error', async function () {
return this.LaunchpadController._atLeastOneAdminExists( await expect(this.LaunchpadController._atLeastOneAdminExists()).rejected
(err, exists) => {
expect(err).to.not.equal(null)
expect(err).to.be.instanceof(Error)
expect(exists).to.equal(undefined)
return done()
}
)
}) })
}) })
}) })
describe('sendTestEmail', function () { describe('sendTestEmail', function () {
beforeEach(function () { beforeEach(function () {
this.EmailHandler.sendEmail = sinon.stub().callsArgWith(2, null) this.EmailHandler.promises.sendEmail = sinon.stub().resolves()
this.req.body.email = 'someone@example.com' this.req.body.email = 'someone@example.com'
this.res.sendStatus = sinon.stub()
return (this.next = sinon.stub()) return (this.next = sinon.stub())
}) })
it('should produce a 200 response', function () { it('should produce a 200 response', function (done) {
this.res.callback = () => {
this.res.json.calledWith({ message: 'email_sent' }).should.equal(true)
done()
}
this.LaunchpadController.sendTestEmail(this.req, this.res, this.next) this.LaunchpadController.sendTestEmail(this.req, this.res, this.next)
this.res.json
.calledWith({
message: 'email_sent',
})
.should.equal(true)
}) })
it('should not call next with an error', function () { it('should not call next with an error', function () {
@ -283,23 +262,26 @@ describe('LaunchpadController', function () {
it('should have called sendEmail', function () { it('should have called sendEmail', function () {
this.LaunchpadController.sendTestEmail(this.req, this.res, this.next) this.LaunchpadController.sendTestEmail(this.req, this.res, this.next)
this.EmailHandler.sendEmail.callCount.should.equal(1) this.EmailHandler.promises.sendEmail.callCount.should.equal(1)
return this.EmailHandler.sendEmail return this.EmailHandler.promises.sendEmail
.calledWith('testEmail') .calledWith('testEmail')
.should.equal(true) .should.equal(true)
}) })
describe('when sendEmail produces an error', function () { describe('when sendEmail produces an error', function () {
beforeEach(function () { beforeEach(function () {
return (this.EmailHandler.sendEmail = sinon return (this.EmailHandler.promises.sendEmail = sinon
.stub() .stub()
.callsArgWith(2, new Error('woops'))) .rejects(new Error('woops')))
}) })
it('should call next with an error', function () { it('should call next with an error', function (done) {
this.LaunchpadController.sendTestEmail(this.req, this.res, this.next) this.next = sinon.stub().callsFake(err => {
expect(err).to.be.instanceof(Error)
this.next.callCount.should.equal(1) this.next.callCount.should.equal(1)
return expect(this.next.lastCall.args[0]).to.be.instanceof(Error) done()
})
this.LaunchpadController.sendTestEmail(this.req, this.res, this.next)
}) })
}) })
@ -333,8 +315,8 @@ describe('LaunchpadController', function () {
}) })
describe('when all goes well', function () { describe('when all goes well', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'a_really_bad_password' this.password = 'a_really_bad_password'
this.req.body.email = this.email this.req.body.email = this.email
@ -343,15 +325,17 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon this.UserRegistrationHandler.promises.registerNewUser = sinon
.stub() .stub()
.callsArgWith(1, null, this.user) .resolves(this.user)
this.User.updateOne = sinon.stub().callsArgWith(2, null) this.User.updateOne = sinon
.stub()
.returns({ exec: sinon.stub().resolves() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.AuthenticationManager.validateEmail = sinon.stub().returns(null) this.AuthenticationManager.validateEmail = sinon.stub().returns(null)
this.AuthenticationManager.validatePassword = sinon.stub().returns(null) this.AuthenticationManager.validatePassword = sinon.stub().returns(null)
this.res.json = sinon.stub() this.next = sinon.stub().callsFake(() => done())
this.next = sinon.stub() this.res.callback = done
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -369,8 +353,10 @@ describe('LaunchpadController', function () {
}) })
it('should have called registerNewUser', function () { it('should have called registerNewUser', function () {
this.UserRegistrationHandler.registerNewUser.callCount.should.equal(1) this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
return this.UserRegistrationHandler.registerNewUser 1
)
return this.UserRegistrationHandler.promises.registerNewUser
.calledWith({ email: this.email, password: this.password }) .calledWith({ email: this.email, password: this.password })
.should.equal(true) .should.equal(true)
}) })
@ -393,7 +379,7 @@ describe('LaunchpadController', function () {
describe('when no email is supplied', function () { describe('when no email is supplied', function () {
beforeEach(function () { beforeEach(function () {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = undefined this.email = undefined
this.password = 'a_really_bad_password' this.password = 'a_really_bad_password'
this.req.body.email = this.email this.req.body.email = this.email
@ -402,10 +388,9 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.sendStatus = sinon.stub()
this.next = sinon.stub() this.next = sinon.stub()
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
@ -424,7 +409,7 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
@ -432,7 +417,7 @@ describe('LaunchpadController', function () {
describe('when no password is supplied', function () { describe('when no password is supplied', function () {
beforeEach(function () { beforeEach(function () {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = undefined this.password = undefined
this.req.body.email = this.email this.req.body.email = this.email
@ -441,10 +426,9 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.sendStatus = sinon.stub()
this.next = sinon.stub() this.next = sinon.stub()
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
@ -463,15 +447,15 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when an invalid email is supplied', function () { describe('when an invalid email is supplied', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'invalid password' this.password = 'invalid password'
this.req.body.email = this.email this.req.body.email = this.email
@ -480,15 +464,15 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.AuthenticationManager.validateEmail = sinon this.AuthenticationManager.validateEmail = sinon
.stub() .stub()
.returns(new Error('bad email')) .returns(new Error('bad email'))
this.AuthenticationManager.validatePassword = sinon.stub().returns(null) this.AuthenticationManager.validatePassword = sinon.stub().returns(null)
this.res.sendStatus = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -505,15 +489,15 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when an invalid password is supplied', function () { describe('when an invalid password is supplied', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'invalid password' this.password = 'invalid password'
this.req.body.email = this.email this.req.body.email = this.email
@ -522,15 +506,15 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.AuthenticationManager.validateEmail = sinon.stub().returns(null) this.AuthenticationManager.validateEmail = sinon.stub().returns(null)
this.AuthenticationManager.validatePassword = sinon this.AuthenticationManager.validatePassword = sinon
.stub() .stub()
.returns(new Error('bad password')) .returns(new Error('bad password'))
this.res.sendStatus = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -547,15 +531,15 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when there are already existing admins', function () { describe('when there are already existing admins', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, true) this._atLeastOneAdminExists.resolves(true)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'a_really_bad_password' this.password = 'a_really_bad_password'
this.req.body.email = this.email this.req.body.email = this.email
@ -564,13 +548,13 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.AuthenticationManager.validateEmail = sinon.stub().returns(null) this.AuthenticationManager.validateEmail = sinon.stub().returns(null)
this.AuthenticationManager.validatePassword = sinon.stub().returns(null) this.AuthenticationManager.validatePassword = sinon.stub().returns(null)
this.res.sendStatus = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -584,15 +568,15 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when checking admins produces an error', function () { describe('when checking admins produces an error', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, new Error('woops')) this._atLeastOneAdminExists.rejects(new Error('woops'))
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'a_really_bad_password' this.password = 'a_really_bad_password'
this.req.body.email = this.email this.req.body.email = this.email
@ -601,11 +585,11 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.sendStatus = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -623,15 +607,15 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when registerNewUser produces an error', function () { describe('when registerNewUser produces an error', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'a_really_bad_password' this.password = 'a_really_bad_password'
this.req.body.email = this.email this.req.body.email = this.email
@ -640,15 +624,15 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon this.UserRegistrationHandler.promises.registerNewUser = sinon
.stub() .stub()
.callsArgWith(1, new Error('woops')) .rejects(new Error('woops'))
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.AuthenticationManager.validateEmail = sinon.stub().returns(null) this.AuthenticationManager.validateEmail = sinon.stub().returns(null)
this.AuthenticationManager.validatePassword = sinon.stub().returns(null) this.AuthenticationManager.validatePassword = sinon.stub().returns(null)
this.res.json = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -666,8 +650,10 @@ describe('LaunchpadController', function () {
}) })
it('should have called registerNewUser', function () { it('should have called registerNewUser', function () {
this.UserRegistrationHandler.registerNewUser.callCount.should.equal(1) this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
return this.UserRegistrationHandler.registerNewUser 1
)
return this.UserRegistrationHandler.promises.registerNewUser
.calledWith({ email: this.email, password: this.password }) .calledWith({ email: this.email, password: this.password })
.should.equal(true) .should.equal(true)
}) })
@ -678,8 +664,8 @@ describe('LaunchpadController', function () {
}) })
describe('when user update produces an error', function () { describe('when user update produces an error', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'a_really_bad_password' this.password = 'a_really_bad_password'
this.req.body.email = this.email this.req.body.email = this.email
@ -688,15 +674,17 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon this.UserRegistrationHandler.promises.registerNewUser = sinon
.stub() .stub()
.callsArgWith(1, null, this.user) .resolves(this.user)
this.User.updateOne = sinon.stub().callsArgWith(2, new Error('woops')) this.User.updateOne = sinon.stub().returns({
exec: sinon.stub().rejects(new Error('woops')),
})
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.AuthenticationManager.validateEmail = sinon.stub().returns(null) this.AuthenticationManager.validateEmail = sinon.stub().returns(null)
this.AuthenticationManager.validatePassword = sinon.stub().returns(null) this.AuthenticationManager.validatePassword = sinon.stub().returns(null)
this.res.json = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -714,18 +702,20 @@ describe('LaunchpadController', function () {
}) })
it('should have called registerNewUser', function () { it('should have called registerNewUser', function () {
this.UserRegistrationHandler.registerNewUser.callCount.should.equal(1) this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
return this.UserRegistrationHandler.registerNewUser 1
)
return this.UserRegistrationHandler.promises.registerNewUser
.calledWith({ email: this.email, password: this.password }) .calledWith({ email: this.email, password: this.password })
.should.equal(true) .should.equal(true)
}) })
}) })
describe('when overleaf', function () { describe('when overleaf', function () {
beforeEach(function () { beforeEach(function (done) {
this.Settings.overleaf = { one: 1 } this.Settings.overleaf = { one: 1 }
this.Settings.createV1AccountOnLogin = true this.Settings.createV1AccountOnLogin = true
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.password = 'a_really_bad_password' this.password = 'a_really_bad_password'
this.req.body.email = this.email this.req.body.email = this.email
@ -734,18 +724,20 @@ describe('LaunchpadController', function () {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon this.UserRegistrationHandler.promises.registerNewUser = sinon
.stub() .stub()
.callsArgWith(1, null, this.user) .resolves(this.user)
this.User.updateOne = sinon.stub().callsArgWith(2, null) this.User.updateOne = sinon
.stub()
.returns({ exec: sinon.stub().resolves() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.AuthenticationManager.validateEmail = sinon.stub().returns(null) this.AuthenticationManager.validateEmail = sinon.stub().returns(null)
this.AuthenticationManager.validatePassword = sinon.stub().returns(null) this.AuthenticationManager.validatePassword = sinon.stub().returns(null)
this.UserGetter.getUser = sinon this.UserGetter.promises.getUser = sinon
.stub() .stub()
.callsArgWith(1, null, { _id: '1234' }) .resolves({ _id: '1234' })
this.res.json = sinon.stub() this.next = sinon.stub().callsFake(() => done())
this.next = sinon.stub() this.res.callback = done
return this.LaunchpadController.registerAdmin( return this.LaunchpadController.registerAdmin(
this.req, this.req,
this.res, this.res,
@ -763,8 +755,10 @@ describe('LaunchpadController', function () {
}) })
it('should have called registerNewUser', function () { it('should have called registerNewUser', function () {
this.UserRegistrationHandler.registerNewUser.callCount.should.equal(1) this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
return this.UserRegistrationHandler.registerNewUser 1
)
return this.UserRegistrationHandler.promises.registerNewUser
.calledWith({ email: this.email, password: this.password }) .calledWith({ email: this.email, password: this.password })
.should.equal(true) .should.equal(true)
}) })
@ -799,21 +793,23 @@ describe('LaunchpadController', function () {
}) })
describe('when all goes well', function () { describe('when all goes well', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.req.body.email = this.email this.req.body.email = this.email
this.user = { this.user = {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon this.UserRegistrationHandler.promises.registerNewUser = sinon
.stub() .stub()
.callsArgWith(1, null, this.user) .resolves(this.user)
this.User.updateOne = sinon.stub().callsArgWith(2, null) this.User.updateOne = sinon
.stub()
.returns({ exec: sinon.stub().resolves() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.json = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerExternalAuthAdmin('ldap')( return this.LaunchpadController.registerExternalAuthAdmin('ldap')(
this.req, this.req,
this.res, this.res,
@ -831,8 +827,10 @@ describe('LaunchpadController', function () {
}) })
it('should have called registerNewUser', function () { it('should have called registerNewUser', function () {
this.UserRegistrationHandler.registerNewUser.callCount.should.equal(1) this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
return this.UserRegistrationHandler.registerNewUser 1
)
return this.UserRegistrationHandler.promises.registerNewUser
.calledWith({ .calledWith({
email: this.email, email: this.email,
password: 'password_here', password: 'password_here',
@ -867,17 +865,16 @@ describe('LaunchpadController', function () {
describe('when the authMethod is invalid', function () { describe('when the authMethod is invalid', function () {
beforeEach(function () { beforeEach(function () {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = undefined this.email = undefined
this.req.body.email = this.email this.req.body.email = this.email
this.user = { this.user = {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.sendStatus = sinon.stub()
this.next = sinon.stub() this.next = sinon.stub()
return this.LaunchpadController.registerExternalAuthAdmin( return this.LaunchpadController.registerExternalAuthAdmin(
'NOTAVALIDAUTHMETHOD' 'NOTAVALIDAUTHMETHOD'
@ -894,7 +891,7 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
@ -902,17 +899,16 @@ describe('LaunchpadController', function () {
describe('when no email is supplied', function () { describe('when no email is supplied', function () {
beforeEach(function () { beforeEach(function () {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = undefined this.email = undefined
this.req.body.email = this.email this.req.body.email = this.email
this.user = { this.user = {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.sendStatus = sinon.stub()
this.next = sinon.stub() this.next = sinon.stub()
return this.LaunchpadController.registerExternalAuthAdmin('ldap')( return this.LaunchpadController.registerExternalAuthAdmin('ldap')(
this.req, this.req,
@ -931,26 +927,26 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when there are already existing admins', function () { describe('when there are already existing admins', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, true) this._atLeastOneAdminExists.resolves(true)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.req.body.email = this.email this.req.body.email = this.email
this.user = { this.user = {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.sendStatus = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerExternalAuthAdmin('ldap')( return this.LaunchpadController.registerExternalAuthAdmin('ldap')(
this.req, this.req,
this.res, this.res,
@ -964,26 +960,26 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when checking admins produces an error', function () { describe('when checking admins produces an error', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, new Error('woops')) this._atLeastOneAdminExists.rejects(new Error('woops'))
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.req.body.email = this.email this.req.body.email = this.email
this.user = { this.user = {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon.stub() this.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.sendStatus = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerExternalAuthAdmin('ldap')( return this.LaunchpadController.registerExternalAuthAdmin('ldap')(
this.req, this.req,
this.res, this.res,
@ -1001,28 +997,28 @@ describe('LaunchpadController', function () {
}) })
it('should not call registerNewUser', function () { it('should not call registerNewUser', function () {
return this.UserRegistrationHandler.registerNewUser.callCount.should.equal( return this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
0 0
) )
}) })
}) })
describe('when registerNewUser produces an error', function () { describe('when registerNewUser produces an error', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.req.body.email = this.email this.req.body.email = this.email
this.user = { this.user = {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon this.UserRegistrationHandler.promises.registerNewUser = sinon
.stub() .stub()
.callsArgWith(1, new Error('woops')) .rejects(new Error('woops'))
this.User.updateOne = sinon.stub() this.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.json = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerExternalAuthAdmin('ldap')( return this.LaunchpadController.registerExternalAuthAdmin('ldap')(
this.req, this.req,
this.res, this.res,
@ -1040,8 +1036,10 @@ describe('LaunchpadController', function () {
}) })
it('should have called registerNewUser', function () { it('should have called registerNewUser', function () {
this.UserRegistrationHandler.registerNewUser.callCount.should.equal(1) this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
return this.UserRegistrationHandler.registerNewUser 1
)
return this.UserRegistrationHandler.promises.registerNewUser
.calledWith({ .calledWith({
email: this.email, email: this.email,
password: 'password_here', password: 'password_here',
@ -1057,21 +1055,23 @@ describe('LaunchpadController', function () {
}) })
describe('when user update produces an error', function () { describe('when user update produces an error', function () {
beforeEach(function () { beforeEach(function (done) {
this._atLeastOneAdminExists.callsArgWith(0, null, false) this._atLeastOneAdminExists.resolves(false)
this.email = 'someone@example.com' this.email = 'someone@example.com'
this.req.body.email = this.email this.req.body.email = this.email
this.user = { this.user = {
_id: 'abcdef', _id: 'abcdef',
email: this.email, email: this.email,
} }
this.UserRegistrationHandler.registerNewUser = sinon this.UserRegistrationHandler.promises.registerNewUser = sinon
.stub() .stub()
.callsArgWith(1, null, this.user) .resolves(this.user)
this.User.updateOne = sinon.stub().callsArgWith(2, new Error('woops')) this.User.updateOne = sinon.stub().returns({
exec: sinon.stub().rejects(new Error('woops')),
})
this.AuthenticationController.setRedirectInSession = sinon.stub() this.AuthenticationController.setRedirectInSession = sinon.stub()
this.res.json = sinon.stub() this.res.callback = done
this.next = sinon.stub() this.next = sinon.stub().callsFake(() => done())
return this.LaunchpadController.registerExternalAuthAdmin('ldap')( return this.LaunchpadController.registerExternalAuthAdmin('ldap')(
this.req, this.req,
this.res, this.res,
@ -1089,8 +1089,10 @@ describe('LaunchpadController', function () {
}) })
it('should have called registerNewUser', function () { it('should have called registerNewUser', function () {
this.UserRegistrationHandler.registerNewUser.callCount.should.equal(1) this.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
return this.UserRegistrationHandler.registerNewUser 1
)
return this.UserRegistrationHandler.promises.registerNewUser
.calledWith({ .calledWith({
email: this.email, email: this.email,
password: 'password_here', password: 'password_here',