| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035 |
- import { beforeEach, describe, expect, it, vi } from 'vitest'
- import * as path from 'node:path'
- import sinon from 'sinon'
- import MockResponse from '../../../../../test/unit/src/helpers/MockResponse.mjs'
- const modulePath = path.join(
- import.meta.dirname,
- '../../../app/src/LaunchpadController.mjs'
- )
- describe('LaunchpadController', function () {
- beforeEach(async function (ctx) {
- ctx.user = {
- _id: '323123',
- first_name: 'fn',
- last_name: 'ln',
- save: sinon.stub().callsArgWith(0),
- }
- ctx.User = {}
- ctx.Settings = {
- adminPrivilegeAvailable: true,
- }
- vi.doMock('@overleaf/settings', () => ({ default: ctx.Settings }))
- vi.doMock(
- '../../../../../app/src/Features/User/UserRegistrationHandler.mjs',
- () => ({
- default: (ctx.UserRegistrationHandler = {
- promises: {},
- }),
- })
- )
- vi.doMock('../../../../../app/src/Features/Email/EmailHandler.mjs', () => ({
- default: (ctx.EmailHandler = { promises: {} }),
- }))
- vi.doMock('../../../../../app/src/Features/User/UserGetter.mjs', () => ({
- default: (ctx.UserGetter = {
- promises: {},
- }),
- }))
- vi.doMock('../../../../../app/src/models/User.mjs', () => ({
- User: ctx.User,
- }))
- vi.doMock(
- '../../../../../app/src/Features/Authentication/AuthenticationController.mjs',
- () => ({
- default: (ctx.AuthenticationController = {}),
- })
- )
- vi.doMock(
- '../../../../../app/src/Features/Authentication/AuthenticationManager.mjs',
- () => ({
- default: (ctx.AuthenticationManager = {}),
- })
- )
- vi.doMock(
- '../../../../../app/src/Features/Authentication/SessionManager.mjs',
- () => ({
- default: (ctx.SessionManager = {
- getSessionUser: sinon.stub(),
- }),
- })
- )
- ctx.LaunchpadController = (await import(modulePath)).default
- ctx.email = 'bob@smith.com'
- ctx.req = {
- query: {},
- body: {},
- session: {},
- }
- ctx.res = new MockResponse(vi)
- ctx.res.locals = {
- translate(key) {
- return key
- },
- }
- ctx.next = sinon.stub()
- })
- describe('launchpadPage', function () {
- beforeEach(function (ctx) {
- ctx.LaunchpadController._mocks._atLeastOneAdminExists = sinon.stub()
- ctx._atLeastOneAdminExists =
- ctx.LaunchpadController._mocks._atLeastOneAdminExists
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- })
- describe('when the user is not logged in', function () {
- beforeEach(function (ctx) {
- ctx.SessionManager.getSessionUser = sinon.stub().returns(null)
- })
- describe('when there are no admins', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- await ctx.LaunchpadController.launchpadPage(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should render the launchpad page', function (ctx) {
- const viewPath = path.join(
- import.meta.dirname,
- '../../../app/views/launchpad'
- )
- expect(ctx.res.render).toHaveBeenCalledTimes(1)
- expect(ctx.res.render).toHaveBeenCalledWith(viewPath, {
- adminUserExists: false,
- authMethod: 'local',
- })
- })
- })
- describe('when there is at least one admin', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(true)
- await ctx.LaunchpadController.launchpadPage(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should redirect to login page', function (ctx) {
- ctx.AuthenticationController.setRedirectInSession.callCount.should.equal(
- 1
- )
- expect(ctx.res.redirect).toHaveBeenCalledWith('/login')
- })
- it('should not render the launchpad page', function (ctx) {
- expect(ctx.res.render).not.toHaveBeenCalled()
- })
- })
- })
- describe('when the user is logged in', function () {
- beforeEach(function (ctx) {
- ctx.user = {
- _id: 'abcd',
- email: 'abcd@example.com',
- }
- ctx.SessionManager.getSessionUser.returns(ctx.user)
- ctx._atLeastOneAdminExists.resolves(true)
- })
- describe('when the user is an admin', function () {
- beforeEach(async function (ctx) {
- ctx.UserGetter.promises.getUser = sinon
- .stub()
- .resolves({ isAdmin: true })
- await ctx.LaunchpadController.launchpadPage(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should render the launchpad page', function (ctx) {
- const viewPath = path.join(
- import.meta.dirname,
- '../../../app/views/launchpad'
- )
- expect(ctx.res.render).toHaveBeenCalledTimes(1)
- expect(ctx.res.render).toHaveBeenCalledWith(viewPath, {
- wsUrl: undefined,
- adminUserExists: true,
- authMethod: 'local',
- })
- })
- })
- describe('when the user is not an admin', function () {
- beforeEach(async function (ctx) {
- ctx.UserGetter.promises.getUser = sinon
- .stub()
- .resolves({ isAdmin: false })
- await ctx.LaunchpadController.launchpadPage(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should redirect to restricted page', function (ctx) {
- expect(ctx.res.redirect).toHaveBeenCalledTimes(1)
- expect(ctx.res.redirect).toHaveBeenCalledWith('/restricted')
- })
- })
- })
- })
- describe('_atLeastOneAdminExists', function () {
- describe('when there are no admins', function () {
- beforeEach(function (ctx) {
- ctx.UserGetter.promises.getUser = sinon.stub().resolves(null)
- })
- it('should callback with false', async function (ctx) {
- const exists = await ctx.LaunchpadController._atLeastOneAdminExists()
- expect(exists).to.equal(false)
- })
- })
- describe('when there are some admins', function () {
- beforeEach(function (ctx) {
- ctx.UserGetter.promises.getUser = sinon.stub().resolves({ _id: 'abcd' })
- })
- it('should callback with true', async function (ctx) {
- const exists = await ctx.LaunchpadController._atLeastOneAdminExists()
- expect(exists).to.equal(true)
- })
- })
- describe('when getUser produces an error', function () {
- beforeEach(function (ctx) {
- ctx.UserGetter.promises.getUser = sinon
- .stub()
- .rejects(new Error('woops'))
- })
- it('should produce an error', async function (ctx) {
- await expect(ctx.LaunchpadController._atLeastOneAdminExists()).rejected
- })
- })
- })
- describe('sendTestEmail', function () {
- beforeEach(function (ctx) {
- ctx.EmailHandler.promises.sendEmail = sinon.stub().resolves()
- ctx.req.body.email = 'someone@example.com'
- })
- it('should produce a 200 response', async function (ctx) {
- await ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
- expect(ctx.res.json).toHaveBeenCalledWith({ message: 'email_sent' })
- })
- it('should not call next with an error', function (ctx) {
- ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
- ctx.next.callCount.should.equal(0)
- })
- it('should have called sendEmail', async function (ctx) {
- await ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
- ctx.EmailHandler.promises.sendEmail.callCount.should.equal(1)
- ctx.EmailHandler.promises.sendEmail
- .calledWith('testEmail')
- .should.equal(true)
- })
- describe('when sendEmail produces an error', function () {
- beforeEach(function (ctx) {
- ctx.EmailHandler.promises.sendEmail = sinon
- .stub()
- .rejects(new Error('woops'))
- })
- it('should call next with an error', async function (ctx) {
- await new Promise(resolve => {
- ctx.next = sinon.stub().callsFake(err => {
- expect(err).to.be.instanceof(Error)
- ctx.next.callCount.should.equal(1)
- resolve()
- })
- ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
- })
- })
- })
- describe('when no email address is supplied', function () {
- beforeEach(function (ctx) {
- ctx.req.body.email = undefined
- })
- it('should produce a 400 response', function (ctx) {
- ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
- expect(ctx.res.status).toHaveBeenCalledWith(400)
- expect(ctx.res.json).toHaveBeenCalledWith({
- message: 'no email address supplied',
- })
- })
- })
- })
- describe('registerAdmin', function () {
- beforeEach(function (ctx) {
- ctx.LaunchpadController._mocks._atLeastOneAdminExists = sinon.stub()
- ctx._atLeastOneAdminExists =
- ctx.LaunchpadController._mocks._atLeastOneAdminExists
- })
- describe('when all goes well', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.password = 'a_really_bad_password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon
- .stub()
- .resolves(ctx.user)
- ctx.User.updateOne = sinon
- .stub()
- .returns({ exec: sinon.stub().resolves() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
- ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should send back a json response', function (ctx) {
- expect(ctx.res.json).toHaveBeenCalledTimes(1)
- expect(ctx.res.json).toHaveBeenCalledWith({ redir: '/launchpad' })
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should have called registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 1
- )
- ctx.UserRegistrationHandler.promises.registerNewUser
- .calledWith({ email: ctx.email, password: ctx.password })
- .should.equal(true)
- })
- it('should have updated the user to make them an admin', function (ctx) {
- ctx.User.updateOne.callCount.should.equal(1)
- ctx.User.updateOne
- .calledWithMatch(
- { _id: ctx.user._id },
- {
- $set: {
- isAdmin: true,
- emails: [
- { email: ctx.user.email, reversedHostname: 'moc.elpmaxe' },
- ],
- },
- }
- )
- .should.equal(true)
- })
- })
- describe('when no email is supplied', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = undefined
- ctx.password = 'a_really_bad_password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should send a 400 response', function (ctx) {
- expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
- expect(ctx.res.sendStatus).toHaveBeenCalledWith(400)
- })
- it('should not check for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(0)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when no password is supplied', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.password = undefined
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should send a 400 response', function (ctx) {
- expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
- expect(ctx.res.sendStatus).toHaveBeenCalledWith(400)
- })
- it('should not check for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(0)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when an invalid email is supplied', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.password = 'invalid password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- ctx.AuthenticationManager.validateEmail = sinon
- .stub()
- .returns(new Error('bad email'))
- ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should send a 400 response', function (ctx) {
- expect(ctx.res.status).toHaveBeenCalledTimes(1)
- expect(ctx.res.status).toHaveBeenCalledWith(400)
- expect(ctx.res.json).toHaveBeenCalledWith({
- message: { type: 'error', text: 'bad email' },
- })
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when an invalid password is supplied', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.password = 'invalid password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
- ctx.AuthenticationManager.validatePassword = sinon
- .stub()
- .returns(new Error('bad password'))
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should send a 400 response', function (ctx) {
- expect(ctx.res.status).toHaveBeenCalledTimes(1)
- expect(ctx.res.status).toHaveBeenCalledWith(400)
- expect(ctx.res.json).toHaveBeenCalledWith({
- message: { type: 'error', text: 'bad password' },
- })
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when there are already existing admins', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(true)
- ctx.email = 'someone@example.com'
- ctx.password = 'a_really_bad_password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
- ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should send a 403 response', function (ctx) {
- expect(ctx.res.status).toHaveBeenCalledTimes(1)
- expect(ctx.res.status).toHaveBeenCalledWith(403)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when checking admins produces an error', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.rejects(new Error('woops'))
- ctx.email = 'someone@example.com'
- ctx.password = 'a_really_bad_password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should call next with an error', function (ctx) {
- ctx.next.callCount.should.equal(1)
- expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when registerNewUser produces an error', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.password = 'a_really_bad_password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon
- .stub()
- .rejects(new Error('woops'))
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
- ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should call next with an error', function (ctx) {
- ctx.next.callCount.should.equal(1)
- expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should have called registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 1
- )
- ctx.UserRegistrationHandler.promises.registerNewUser
- .calledWith({ email: ctx.email, password: ctx.password })
- .should.equal(true)
- })
- it('should not call update', function (ctx) {
- ctx.User.updateOne.callCount.should.equal(0)
- })
- })
- describe('when user update produces an error', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.password = 'a_really_bad_password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon
- .stub()
- .resolves(ctx.user)
- ctx.User.updateOne = sinon.stub().returns({
- exec: sinon.stub().rejects(new Error('woops')),
- })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
- ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should call next with an error', function (ctx) {
- ctx.next.callCount.should.equal(1)
- expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should have called registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 1
- )
- ctx.UserRegistrationHandler.promises.registerNewUser
- .calledWith({ email: ctx.email, password: ctx.password })
- .should.equal(true)
- })
- })
- describe('when overleaf', function () {
- beforeEach(async function (ctx) {
- ctx.Settings.overleaf = { one: 1 }
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.password = 'a_really_bad_password'
- ctx.req.body.email = ctx.email
- ctx.req.body.password = ctx.password
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon
- .stub()
- .resolves(ctx.user)
- ctx.User.updateOne = sinon
- .stub()
- .returns({ exec: sinon.stub().resolves() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
- ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
- ctx.UserGetter.promises.getUser = sinon.stub().resolves({ _id: '1234' })
- await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
- })
- it('should send back a json response', function (ctx) {
- expect(ctx.res.json).toHaveBeenCalledTimes(1)
- expect(ctx.res.json).toHaveBeenCalledWith({ redir: '/launchpad' })
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should have called registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 1
- )
- ctx.UserRegistrationHandler.promises.registerNewUser
- .calledWith({ email: ctx.email, password: ctx.password })
- .should.equal(true)
- })
- it('should have updated the user to make them an admin', function (ctx) {
- ctx.User.updateOne
- .calledWith(
- { _id: ctx.user._id },
- {
- $set: {
- isAdmin: true,
- emails: [
- { email: ctx.user.email, reversedHostname: 'moc.elpmaxe' },
- ],
- },
- }
- )
- .should.equal(true)
- })
- })
- })
- describe('registerExternalAuthAdmin', function () {
- beforeEach(function (ctx) {
- ctx.Settings.ldap = { one: 1 }
- ctx.LaunchpadController._mocks._atLeastOneAdminExists = sinon.stub()
- ctx._atLeastOneAdminExists =
- ctx.LaunchpadController._mocks._atLeastOneAdminExists
- })
- describe('when all goes well', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.req.body.email = ctx.email
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon
- .stub()
- .resolves(ctx.user)
- ctx.User.updateOne = sinon
- .stub()
- .returns({ exec: sinon.stub().resolves() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should send back a json response', function (ctx) {
- expect(ctx.res.json).toHaveBeenCalledTimes(1)
- expect(ctx.res.json.mock.lastCall[0].email).to.equal(ctx.email)
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should have called registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 1
- )
- const call =
- ctx.UserRegistrationHandler.promises.registerNewUser.firstCall
- expect(call.args[0]).to.include({
- email: ctx.email,
- first_name: ctx.email,
- last_name: '',
- })
- expect(call.args[0].password).to.be.a('string')
- expect(call.args[0].password).to.not.equal('password_here')
- expect(call.args[0].password.length).to.be.at.least(16)
- })
- it('should have updated the user to make them an admin', function (ctx) {
- ctx.User.updateOne.callCount.should.equal(1)
- ctx.User.updateOne
- .calledWith(
- { _id: ctx.user._id },
- {
- $set: {
- isAdmin: true,
- emails: [
- { email: ctx.user.email, reversedHostname: 'moc.elpmaxe' },
- ],
- },
- }
- )
- .should.equal(true)
- })
- it('should have set a redirect in session', function (ctx) {
- ctx.AuthenticationController.setRedirectInSession.callCount.should.equal(
- 1
- )
- ctx.AuthenticationController.setRedirectInSession
- .calledWith(ctx.req, '/launchpad')
- .should.equal(true)
- })
- })
- describe('when the authMethod is invalid', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = undefined
- ctx.req.body.email = ctx.email
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerExternalAuthAdmin(
- 'NOTAVALIDAUTHMETHOD'
- )(ctx.req, ctx.res, ctx.next)
- })
- it('should send a 403 response', function (ctx) {
- expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
- expect(ctx.res.sendStatus).toHaveBeenCalledWith(403)
- })
- it('should not check for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(0)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when no email is supplied', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = undefined
- ctx.req.body.email = ctx.email
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should send a 400 response', function (ctx) {
- expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
- expect(ctx.res.sendStatus).toHaveBeenCalledWith(400)
- })
- it('should not check for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(0)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when there are already existing admins', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(true)
- ctx.email = 'someone@example.com'
- ctx.req.body.email = ctx.email
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should send a 403 response', function (ctx) {
- expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
- expect(ctx.res.sendStatus).toHaveBeenCalledWith(403)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when checking admins produces an error', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.rejects(new Error('woops'))
- ctx.email = 'someone@example.com'
- ctx.req.body.email = ctx.email
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should call next with an error', function (ctx) {
- ctx.next.callCount.should.equal(1)
- expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should not call registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 0
- )
- })
- })
- describe('when registerNewUser produces an error', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.req.body.email = ctx.email
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon
- .stub()
- .rejects(new Error('woops'))
- ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should call next with an error', function (ctx) {
- ctx.next.callCount.should.equal(1)
- expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should have called registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 1
- )
- const call =
- ctx.UserRegistrationHandler.promises.registerNewUser.firstCall
- expect(call.args[0]).to.include({
- email: ctx.email,
- first_name: ctx.email,
- last_name: '',
- })
- expect(call.args[0].password).to.be.a('string')
- expect(call.args[0].password).to.not.equal('password_here')
- expect(call.args[0].password.length).to.be.at.least(16)
- })
- it('should not call update', function (ctx) {
- ctx.User.updateOne.callCount.should.equal(0)
- })
- })
- describe('when user update produces an error', function () {
- beforeEach(async function (ctx) {
- ctx._atLeastOneAdminExists.resolves(false)
- ctx.email = 'someone@example.com'
- ctx.req.body.email = ctx.email
- ctx.user = {
- _id: 'abcdef',
- email: ctx.email,
- }
- ctx.UserRegistrationHandler.promises.registerNewUser = sinon
- .stub()
- .resolves(ctx.user)
- ctx.User.updateOne = sinon.stub().returns({
- exec: sinon.stub().rejects(new Error('woops')),
- })
- ctx.AuthenticationController.setRedirectInSession = sinon.stub()
- await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
- ctx.req,
- ctx.res,
- ctx.next
- )
- })
- it('should call next with an error', function (ctx) {
- ctx.next.callCount.should.equal(1)
- expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
- })
- it('should have checked for existing admins', function (ctx) {
- ctx._atLeastOneAdminExists.callCount.should.equal(1)
- })
- it('should have called registerNewUser', function (ctx) {
- ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
- 1
- )
- const call =
- ctx.UserRegistrationHandler.promises.registerNewUser.firstCall
- expect(call.args[0]).to.include({
- email: ctx.email,
- first_name: ctx.email,
- last_name: '',
- })
- expect(call.args[0].password).to.be.a('string')
- expect(call.args[0].password).to.not.equal('password_here')
- expect(call.args[0].password.length).to.be.at.least(16)
- })
- })
- })
- })
|