| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- import { expect, vi } from 'vitest'
- import * as path from 'node:path'
- import sinon from 'sinon'
- import MockResponse from '../../../../../test/unit/src/helpers/MockResponse.js'
- const modulePath = path.join(
- import.meta.dirname,
- '../../../app/src/LaunchpadController.mjs'
- )
- describe('LaunchpadController', function () {
- // esmock doesn't work well with CommonJS dependencies, global imports for
- // @overleaf/settings aren't working until that module is migrated to ESM. In the
- // meantime, the workaround is to set and restore settings values
- 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('@overleaf/metrics', () => ({
- default: (ctx.Metrics = {}),
- }))
- vi.doMock(
- '../../../../../app/src/Features/User/UserRegistrationHandler.mjs',
- () => ({
- default: (ctx.UserRegistrationHandler = {
- promises: {},
- }),
- })
- )
- vi.doMock('../../../../../app/src/Features/Email/EmailHandler.js', () => ({
- default: (ctx.EmailHandler = { promises: {} }),
- }))
- vi.doMock('../../../../../app/src/Features/User/UserGetter.js', () => ({
- default: (ctx.UserGetter = {
- promises: {},
- }),
- }))
- vi.doMock('../../../../../app/src/models/User.js', () => ({
- User: ctx.User,
- }))
- vi.doMock(
- '../../../../../app/src/Features/Authentication/AuthenticationController.js',
- () => ({
- default: (ctx.AuthenticationController = {}),
- })
- )
- vi.doMock(
- '../../../../../app/src/Features/Authentication/AuthenticationManager.js',
- () => ({
- default: (ctx.AuthenticationManager = {}),
- })
- )
- vi.doMock(
- '../../../../../app/src/Features/Authentication/SessionManager.js',
- () => ({
- 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()
- 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'
- )
- ctx.res.render.callCount.should.equal(1)
- expect(ctx.res.render).to.have.been.calledWith(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
- )
- ctx.res.redirect.calledWith('/login').should.equal(true)
- })
- it('should not render the launchpad page', function (ctx) {
- ctx.res.render.callCount.should.equal(0)
- })
- })
- })
- 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'
- )
- ctx.res.render.callCount.should.equal(1)
- expect(ctx.res.render).to.have.been.calledWith(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) {
- ctx.res.redirect.callCount.should.equal(1)
- ctx.res.redirect.calledWith('/restricted').should.equal(true)
- })
- })
- })
- })
- 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)
- ctx.res.json.calledWith({ message: 'email_sent' }).should.equal(true)
- })
- 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)
- ctx.res.status.calledWith(400).should.equal(true)
- ctx.res.json
- .calledWith({
- message: 'no email address supplied',
- })
- .should.equal(true)
- })
- })
- })
- 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) {
- ctx.res.json.callCount.should.equal(1)
- expect(ctx.res.json).to.have.been.calledWith({ 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) {
- ctx.res.sendStatus.callCount.should.equal(1)
- ctx.res.sendStatus.calledWith(400).should.equal(true)
- })
- 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) {
- ctx.res.sendStatus.callCount.should.equal(1)
- ctx.res.sendStatus.calledWith(400).should.equal(true)
- })
- 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) {
- ctx.res.status.callCount.should.equal(1)
- ctx.res.status.calledWith(400).should.equal(true)
- ctx.res.json.calledWith({
- 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) {
- ctx.res.status.callCount.should.equal(1)
- ctx.res.status.calledWith(400).should.equal(true)
- ctx.res.json.calledWith({
- 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) {
- ctx.res.status.callCount.should.equal(1)
- ctx.res.status.calledWith(403).should.equal(true)
- })
- 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) {
- ctx.res.json.callCount.should.equal(1)
- expect(ctx.res.json).to.have.been.calledWith({ 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) {
- ctx.res.json.callCount.should.equal(1)
- expect(ctx.res.json.lastCall.args[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
- )
- ctx.UserRegistrationHandler.promises.registerNewUser
- .calledWith({
- email: ctx.email,
- password: 'password_here',
- first_name: ctx.email,
- last_name: '',
- })
- .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
- .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) {
- ctx.res.sendStatus.callCount.should.equal(1)
- ctx.res.sendStatus.calledWith(403).should.equal(true)
- })
- 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) {
- ctx.res.sendStatus.callCount.should.equal(1)
- ctx.res.sendStatus.calledWith(400).should.equal(true)
- })
- 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) {
- ctx.res.sendStatus.callCount.should.equal(1)
- ctx.res.sendStatus.calledWith(403).should.equal(true)
- })
- 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
- )
- ctx.UserRegistrationHandler.promises.registerNewUser
- .calledWith({
- email: ctx.email,
- password: 'password_here',
- first_name: ctx.email,
- last_name: '',
- })
- .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.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
- )
- ctx.UserRegistrationHandler.promises.registerNewUser
- .calledWith({
- email: ctx.email,
- password: 'password_here',
- first_name: ctx.email,
- last_name: '',
- })
- .should.equal(true)
- })
- })
- })
- })
|