LaunchpadController.test.mjs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. import * as path from 'node:path'
  3. import sinon from 'sinon'
  4. import MockResponse from '../../../../../test/unit/src/helpers/MockResponse.mjs'
  5. const modulePath = path.join(
  6. import.meta.dirname,
  7. '../../../app/src/LaunchpadController.mjs'
  8. )
  9. describe('LaunchpadController', function () {
  10. beforeEach(async function (ctx) {
  11. ctx.user = {
  12. _id: '323123',
  13. first_name: 'fn',
  14. last_name: 'ln',
  15. save: sinon.stub().callsArgWith(0),
  16. }
  17. ctx.User = {}
  18. ctx.Settings = {
  19. adminPrivilegeAvailable: true,
  20. }
  21. vi.doMock('@overleaf/settings', () => ({ default: ctx.Settings }))
  22. vi.doMock('@overleaf/metrics', () => ({
  23. default: (ctx.Metrics = {}),
  24. }))
  25. vi.doMock(
  26. '../../../../../app/src/Features/User/UserRegistrationHandler.mjs',
  27. () => ({
  28. default: (ctx.UserRegistrationHandler = {
  29. promises: {},
  30. }),
  31. })
  32. )
  33. vi.doMock('../../../../../app/src/Features/Email/EmailHandler.mjs', () => ({
  34. default: (ctx.EmailHandler = { promises: {} }),
  35. }))
  36. vi.doMock('../../../../../app/src/Features/User/UserGetter.mjs', () => ({
  37. default: (ctx.UserGetter = {
  38. promises: {},
  39. }),
  40. }))
  41. vi.doMock('../../../../../app/src/models/User.mjs', () => ({
  42. User: ctx.User,
  43. }))
  44. vi.doMock(
  45. '../../../../../app/src/Features/Authentication/AuthenticationController.mjs',
  46. () => ({
  47. default: (ctx.AuthenticationController = {}),
  48. })
  49. )
  50. vi.doMock(
  51. '../../../../../app/src/Features/Authentication/AuthenticationManager.mjs',
  52. () => ({
  53. default: (ctx.AuthenticationManager = {}),
  54. })
  55. )
  56. vi.doMock(
  57. '../../../../../app/src/Features/Authentication/SessionManager.mjs',
  58. () => ({
  59. default: (ctx.SessionManager = {
  60. getSessionUser: sinon.stub(),
  61. }),
  62. })
  63. )
  64. ctx.LaunchpadController = (await import(modulePath)).default
  65. ctx.email = 'bob@smith.com'
  66. ctx.req = {
  67. query: {},
  68. body: {},
  69. session: {},
  70. }
  71. ctx.res = new MockResponse(vi)
  72. ctx.res.locals = {
  73. translate(key) {
  74. return key
  75. },
  76. }
  77. ctx.next = sinon.stub()
  78. })
  79. describe('launchpadPage', function () {
  80. beforeEach(function (ctx) {
  81. ctx.LaunchpadController._mocks._atLeastOneAdminExists = sinon.stub()
  82. ctx._atLeastOneAdminExists =
  83. ctx.LaunchpadController._mocks._atLeastOneAdminExists
  84. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  85. })
  86. describe('when the user is not logged in', function () {
  87. beforeEach(function (ctx) {
  88. ctx.SessionManager.getSessionUser = sinon.stub().returns(null)
  89. })
  90. describe('when there are no admins', function () {
  91. beforeEach(async function (ctx) {
  92. ctx._atLeastOneAdminExists.resolves(false)
  93. await ctx.LaunchpadController.launchpadPage(
  94. ctx.req,
  95. ctx.res,
  96. ctx.next
  97. )
  98. })
  99. it('should render the launchpad page', function (ctx) {
  100. const viewPath = path.join(
  101. import.meta.dirname,
  102. '../../../app/views/launchpad'
  103. )
  104. expect(ctx.res.render).toHaveBeenCalledTimes(1)
  105. expect(ctx.res.render).toHaveBeenCalledWith(viewPath, {
  106. adminUserExists: false,
  107. authMethod: 'local',
  108. })
  109. })
  110. })
  111. describe('when there is at least one admin', function () {
  112. beforeEach(async function (ctx) {
  113. ctx._atLeastOneAdminExists.resolves(true)
  114. await ctx.LaunchpadController.launchpadPage(
  115. ctx.req,
  116. ctx.res,
  117. ctx.next
  118. )
  119. })
  120. it('should redirect to login page', function (ctx) {
  121. ctx.AuthenticationController.setRedirectInSession.callCount.should.equal(
  122. 1
  123. )
  124. expect(ctx.res.redirect).toHaveBeenCalledWith('/login')
  125. })
  126. it('should not render the launchpad page', function (ctx) {
  127. expect(ctx.res.render).not.toHaveBeenCalled()
  128. })
  129. })
  130. })
  131. describe('when the user is logged in', function () {
  132. beforeEach(function (ctx) {
  133. ctx.user = {
  134. _id: 'abcd',
  135. email: 'abcd@example.com',
  136. }
  137. ctx.SessionManager.getSessionUser.returns(ctx.user)
  138. ctx._atLeastOneAdminExists.resolves(true)
  139. })
  140. describe('when the user is an admin', function () {
  141. beforeEach(async function (ctx) {
  142. ctx.UserGetter.promises.getUser = sinon
  143. .stub()
  144. .resolves({ isAdmin: true })
  145. await ctx.LaunchpadController.launchpadPage(
  146. ctx.req,
  147. ctx.res,
  148. ctx.next
  149. )
  150. })
  151. it('should render the launchpad page', function (ctx) {
  152. const viewPath = path.join(
  153. import.meta.dirname,
  154. '../../../app/views/launchpad'
  155. )
  156. expect(ctx.res.render).toHaveBeenCalledTimes(1)
  157. expect(ctx.res.render).toHaveBeenCalledWith(viewPath, {
  158. wsUrl: undefined,
  159. adminUserExists: true,
  160. authMethod: 'local',
  161. })
  162. })
  163. })
  164. describe('when the user is not an admin', function () {
  165. beforeEach(async function (ctx) {
  166. ctx.UserGetter.promises.getUser = sinon
  167. .stub()
  168. .resolves({ isAdmin: false })
  169. await ctx.LaunchpadController.launchpadPage(
  170. ctx.req,
  171. ctx.res,
  172. ctx.next
  173. )
  174. })
  175. it('should redirect to restricted page', function (ctx) {
  176. expect(ctx.res.redirect).toHaveBeenCalledTimes(1)
  177. expect(ctx.res.redirect).toHaveBeenCalledWith('/restricted')
  178. })
  179. })
  180. })
  181. })
  182. describe('_atLeastOneAdminExists', function () {
  183. describe('when there are no admins', function () {
  184. beforeEach(function (ctx) {
  185. ctx.UserGetter.promises.getUser = sinon.stub().resolves(null)
  186. })
  187. it('should callback with false', async function (ctx) {
  188. const exists = await ctx.LaunchpadController._atLeastOneAdminExists()
  189. expect(exists).to.equal(false)
  190. })
  191. })
  192. describe('when there are some admins', function () {
  193. beforeEach(function (ctx) {
  194. ctx.UserGetter.promises.getUser = sinon.stub().resolves({ _id: 'abcd' })
  195. })
  196. it('should callback with true', async function (ctx) {
  197. const exists = await ctx.LaunchpadController._atLeastOneAdminExists()
  198. expect(exists).to.equal(true)
  199. })
  200. })
  201. describe('when getUser produces an error', function () {
  202. beforeEach(function (ctx) {
  203. ctx.UserGetter.promises.getUser = sinon
  204. .stub()
  205. .rejects(new Error('woops'))
  206. })
  207. it('should produce an error', async function (ctx) {
  208. await expect(ctx.LaunchpadController._atLeastOneAdminExists()).rejected
  209. })
  210. })
  211. })
  212. describe('sendTestEmail', function () {
  213. beforeEach(function (ctx) {
  214. ctx.EmailHandler.promises.sendEmail = sinon.stub().resolves()
  215. ctx.req.body.email = 'someone@example.com'
  216. })
  217. it('should produce a 200 response', async function (ctx) {
  218. await ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
  219. expect(ctx.res.json).toHaveBeenCalledWith({ message: 'email_sent' })
  220. })
  221. it('should not call next with an error', function (ctx) {
  222. ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
  223. ctx.next.callCount.should.equal(0)
  224. })
  225. it('should have called sendEmail', async function (ctx) {
  226. await ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
  227. ctx.EmailHandler.promises.sendEmail.callCount.should.equal(1)
  228. ctx.EmailHandler.promises.sendEmail
  229. .calledWith('testEmail')
  230. .should.equal(true)
  231. })
  232. describe('when sendEmail produces an error', function () {
  233. beforeEach(function (ctx) {
  234. ctx.EmailHandler.promises.sendEmail = sinon
  235. .stub()
  236. .rejects(new Error('woops'))
  237. })
  238. it('should call next with an error', async function (ctx) {
  239. await new Promise(resolve => {
  240. ctx.next = sinon.stub().callsFake(err => {
  241. expect(err).to.be.instanceof(Error)
  242. ctx.next.callCount.should.equal(1)
  243. resolve()
  244. })
  245. ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
  246. })
  247. })
  248. })
  249. describe('when no email address is supplied', function () {
  250. beforeEach(function (ctx) {
  251. ctx.req.body.email = undefined
  252. })
  253. it('should produce a 400 response', function (ctx) {
  254. ctx.LaunchpadController.sendTestEmail(ctx.req, ctx.res, ctx.next)
  255. expect(ctx.res.status).toHaveBeenCalledWith(400)
  256. expect(ctx.res.json).toHaveBeenCalledWith({
  257. message: 'no email address supplied',
  258. })
  259. })
  260. })
  261. })
  262. describe('registerAdmin', function () {
  263. beforeEach(function (ctx) {
  264. ctx.LaunchpadController._mocks._atLeastOneAdminExists = sinon.stub()
  265. ctx._atLeastOneAdminExists =
  266. ctx.LaunchpadController._mocks._atLeastOneAdminExists
  267. })
  268. describe('when all goes well', function () {
  269. beforeEach(async function (ctx) {
  270. ctx._atLeastOneAdminExists.resolves(false)
  271. ctx.email = 'someone@example.com'
  272. ctx.password = 'a_really_bad_password'
  273. ctx.req.body.email = ctx.email
  274. ctx.req.body.password = ctx.password
  275. ctx.user = {
  276. _id: 'abcdef',
  277. email: ctx.email,
  278. }
  279. ctx.UserRegistrationHandler.promises.registerNewUser = sinon
  280. .stub()
  281. .resolves(ctx.user)
  282. ctx.User.updateOne = sinon
  283. .stub()
  284. .returns({ exec: sinon.stub().resolves() })
  285. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  286. ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
  287. ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
  288. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  289. })
  290. it('should send back a json response', function (ctx) {
  291. expect(ctx.res.json).toHaveBeenCalledTimes(1)
  292. expect(ctx.res.json).toHaveBeenCalledWith({ redir: '/launchpad' })
  293. })
  294. it('should have checked for existing admins', function (ctx) {
  295. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  296. })
  297. it('should have called registerNewUser', function (ctx) {
  298. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  299. 1
  300. )
  301. ctx.UserRegistrationHandler.promises.registerNewUser
  302. .calledWith({ email: ctx.email, password: ctx.password })
  303. .should.equal(true)
  304. })
  305. it('should have updated the user to make them an admin', function (ctx) {
  306. ctx.User.updateOne.callCount.should.equal(1)
  307. ctx.User.updateOne
  308. .calledWithMatch(
  309. { _id: ctx.user._id },
  310. {
  311. $set: {
  312. isAdmin: true,
  313. emails: [
  314. { email: ctx.user.email, reversedHostname: 'moc.elpmaxe' },
  315. ],
  316. },
  317. }
  318. )
  319. .should.equal(true)
  320. })
  321. })
  322. describe('when no email is supplied', function () {
  323. beforeEach(async function (ctx) {
  324. ctx._atLeastOneAdminExists.resolves(false)
  325. ctx.email = undefined
  326. ctx.password = 'a_really_bad_password'
  327. ctx.req.body.email = ctx.email
  328. ctx.req.body.password = ctx.password
  329. ctx.user = {
  330. _id: 'abcdef',
  331. email: ctx.email,
  332. }
  333. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  334. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  335. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  336. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  337. })
  338. it('should send a 400 response', function (ctx) {
  339. expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
  340. expect(ctx.res.sendStatus).toHaveBeenCalledWith(400)
  341. })
  342. it('should not check for existing admins', function (ctx) {
  343. ctx._atLeastOneAdminExists.callCount.should.equal(0)
  344. })
  345. it('should not call registerNewUser', function (ctx) {
  346. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  347. 0
  348. )
  349. })
  350. })
  351. describe('when no password is supplied', function () {
  352. beforeEach(async function (ctx) {
  353. ctx._atLeastOneAdminExists.resolves(false)
  354. ctx.email = 'someone@example.com'
  355. ctx.password = undefined
  356. ctx.req.body.email = ctx.email
  357. ctx.req.body.password = ctx.password
  358. ctx.user = {
  359. _id: 'abcdef',
  360. email: ctx.email,
  361. }
  362. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  363. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  364. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  365. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  366. })
  367. it('should send a 400 response', function (ctx) {
  368. expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
  369. expect(ctx.res.sendStatus).toHaveBeenCalledWith(400)
  370. })
  371. it('should not check for existing admins', function (ctx) {
  372. ctx._atLeastOneAdminExists.callCount.should.equal(0)
  373. })
  374. it('should not call registerNewUser', function (ctx) {
  375. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  376. 0
  377. )
  378. })
  379. })
  380. describe('when an invalid email is supplied', function () {
  381. beforeEach(async function (ctx) {
  382. ctx._atLeastOneAdminExists.resolves(false)
  383. ctx.email = 'someone@example.com'
  384. ctx.password = 'invalid password'
  385. ctx.req.body.email = ctx.email
  386. ctx.req.body.password = ctx.password
  387. ctx.user = {
  388. _id: 'abcdef',
  389. email: ctx.email,
  390. }
  391. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  392. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  393. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  394. ctx.AuthenticationManager.validateEmail = sinon
  395. .stub()
  396. .returns(new Error('bad email'))
  397. ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
  398. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  399. })
  400. it('should send a 400 response', function (ctx) {
  401. expect(ctx.res.status).toHaveBeenCalledTimes(1)
  402. expect(ctx.res.status).toHaveBeenCalledWith(400)
  403. expect(ctx.res.json).toHaveBeenCalledWith({
  404. message: { type: 'error', text: 'bad email' },
  405. })
  406. })
  407. it('should not call registerNewUser', function (ctx) {
  408. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  409. 0
  410. )
  411. })
  412. })
  413. describe('when an invalid password is supplied', function () {
  414. beforeEach(async function (ctx) {
  415. ctx._atLeastOneAdminExists.resolves(false)
  416. ctx.email = 'someone@example.com'
  417. ctx.password = 'invalid password'
  418. ctx.req.body.email = ctx.email
  419. ctx.req.body.password = ctx.password
  420. ctx.user = {
  421. _id: 'abcdef',
  422. email: ctx.email,
  423. }
  424. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  425. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  426. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  427. ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
  428. ctx.AuthenticationManager.validatePassword = sinon
  429. .stub()
  430. .returns(new Error('bad password'))
  431. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  432. })
  433. it('should send a 400 response', function (ctx) {
  434. expect(ctx.res.status).toHaveBeenCalledTimes(1)
  435. expect(ctx.res.status).toHaveBeenCalledWith(400)
  436. expect(ctx.res.json).toHaveBeenCalledWith({
  437. message: { type: 'error', text: 'bad password' },
  438. })
  439. })
  440. it('should not call registerNewUser', function (ctx) {
  441. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  442. 0
  443. )
  444. })
  445. })
  446. describe('when there are already existing admins', function () {
  447. beforeEach(async function (ctx) {
  448. ctx._atLeastOneAdminExists.resolves(true)
  449. ctx.email = 'someone@example.com'
  450. ctx.password = 'a_really_bad_password'
  451. ctx.req.body.email = ctx.email
  452. ctx.req.body.password = ctx.password
  453. ctx.user = {
  454. _id: 'abcdef',
  455. email: ctx.email,
  456. }
  457. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  458. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  459. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  460. ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
  461. ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
  462. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  463. })
  464. it('should send a 403 response', function (ctx) {
  465. expect(ctx.res.status).toHaveBeenCalledTimes(1)
  466. expect(ctx.res.status).toHaveBeenCalledWith(403)
  467. })
  468. it('should not call registerNewUser', function (ctx) {
  469. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  470. 0
  471. )
  472. })
  473. })
  474. describe('when checking admins produces an error', function () {
  475. beforeEach(async function (ctx) {
  476. ctx._atLeastOneAdminExists.rejects(new Error('woops'))
  477. ctx.email = 'someone@example.com'
  478. ctx.password = 'a_really_bad_password'
  479. ctx.req.body.email = ctx.email
  480. ctx.req.body.password = ctx.password
  481. ctx.user = {
  482. _id: 'abcdef',
  483. email: ctx.email,
  484. }
  485. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  486. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  487. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  488. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  489. })
  490. it('should call next with an error', function (ctx) {
  491. ctx.next.callCount.should.equal(1)
  492. expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  493. })
  494. it('should have checked for existing admins', function (ctx) {
  495. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  496. })
  497. it('should not call registerNewUser', function (ctx) {
  498. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  499. 0
  500. )
  501. })
  502. })
  503. describe('when registerNewUser produces an error', function () {
  504. beforeEach(async function (ctx) {
  505. ctx._atLeastOneAdminExists.resolves(false)
  506. ctx.email = 'someone@example.com'
  507. ctx.password = 'a_really_bad_password'
  508. ctx.req.body.email = ctx.email
  509. ctx.req.body.password = ctx.password
  510. ctx.user = {
  511. _id: 'abcdef',
  512. email: ctx.email,
  513. }
  514. ctx.UserRegistrationHandler.promises.registerNewUser = sinon
  515. .stub()
  516. .rejects(new Error('woops'))
  517. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  518. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  519. ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
  520. ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
  521. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  522. })
  523. it('should call next with an error', function (ctx) {
  524. ctx.next.callCount.should.equal(1)
  525. expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  526. })
  527. it('should have checked for existing admins', function (ctx) {
  528. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  529. })
  530. it('should have called registerNewUser', function (ctx) {
  531. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  532. 1
  533. )
  534. ctx.UserRegistrationHandler.promises.registerNewUser
  535. .calledWith({ email: ctx.email, password: ctx.password })
  536. .should.equal(true)
  537. })
  538. it('should not call update', function (ctx) {
  539. ctx.User.updateOne.callCount.should.equal(0)
  540. })
  541. })
  542. describe('when user update produces an error', function () {
  543. beforeEach(async function (ctx) {
  544. ctx._atLeastOneAdminExists.resolves(false)
  545. ctx.email = 'someone@example.com'
  546. ctx.password = 'a_really_bad_password'
  547. ctx.req.body.email = ctx.email
  548. ctx.req.body.password = ctx.password
  549. ctx.user = {
  550. _id: 'abcdef',
  551. email: ctx.email,
  552. }
  553. ctx.UserRegistrationHandler.promises.registerNewUser = sinon
  554. .stub()
  555. .resolves(ctx.user)
  556. ctx.User.updateOne = sinon.stub().returns({
  557. exec: sinon.stub().rejects(new Error('woops')),
  558. })
  559. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  560. ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
  561. ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
  562. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  563. })
  564. it('should call next with an error', function (ctx) {
  565. ctx.next.callCount.should.equal(1)
  566. expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  567. })
  568. it('should have checked for existing admins', function (ctx) {
  569. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  570. })
  571. it('should have called registerNewUser', function (ctx) {
  572. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  573. 1
  574. )
  575. ctx.UserRegistrationHandler.promises.registerNewUser
  576. .calledWith({ email: ctx.email, password: ctx.password })
  577. .should.equal(true)
  578. })
  579. })
  580. describe('when overleaf', function () {
  581. beforeEach(async function (ctx) {
  582. ctx.Settings.overleaf = { one: 1 }
  583. ctx._atLeastOneAdminExists.resolves(false)
  584. ctx.email = 'someone@example.com'
  585. ctx.password = 'a_really_bad_password'
  586. ctx.req.body.email = ctx.email
  587. ctx.req.body.password = ctx.password
  588. ctx.user = {
  589. _id: 'abcdef',
  590. email: ctx.email,
  591. }
  592. ctx.UserRegistrationHandler.promises.registerNewUser = sinon
  593. .stub()
  594. .resolves(ctx.user)
  595. ctx.User.updateOne = sinon
  596. .stub()
  597. .returns({ exec: sinon.stub().resolves() })
  598. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  599. ctx.AuthenticationManager.validateEmail = sinon.stub().returns(null)
  600. ctx.AuthenticationManager.validatePassword = sinon.stub().returns(null)
  601. ctx.UserGetter.promises.getUser = sinon.stub().resolves({ _id: '1234' })
  602. await ctx.LaunchpadController.registerAdmin(ctx.req, ctx.res, ctx.next)
  603. })
  604. it('should send back a json response', function (ctx) {
  605. expect(ctx.res.json).toHaveBeenCalledTimes(1)
  606. expect(ctx.res.json).toHaveBeenCalledWith({ redir: '/launchpad' })
  607. })
  608. it('should have checked for existing admins', function (ctx) {
  609. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  610. })
  611. it('should have called registerNewUser', function (ctx) {
  612. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  613. 1
  614. )
  615. ctx.UserRegistrationHandler.promises.registerNewUser
  616. .calledWith({ email: ctx.email, password: ctx.password })
  617. .should.equal(true)
  618. })
  619. it('should have updated the user to make them an admin', function (ctx) {
  620. ctx.User.updateOne
  621. .calledWith(
  622. { _id: ctx.user._id },
  623. {
  624. $set: {
  625. isAdmin: true,
  626. emails: [
  627. { email: ctx.user.email, reversedHostname: 'moc.elpmaxe' },
  628. ],
  629. },
  630. }
  631. )
  632. .should.equal(true)
  633. })
  634. })
  635. })
  636. describe('registerExternalAuthAdmin', function () {
  637. beforeEach(function (ctx) {
  638. ctx.Settings.ldap = { one: 1 }
  639. ctx.LaunchpadController._mocks._atLeastOneAdminExists = sinon.stub()
  640. ctx._atLeastOneAdminExists =
  641. ctx.LaunchpadController._mocks._atLeastOneAdminExists
  642. })
  643. describe('when all goes well', function () {
  644. beforeEach(async function (ctx) {
  645. ctx._atLeastOneAdminExists.resolves(false)
  646. ctx.email = 'someone@example.com'
  647. ctx.req.body.email = ctx.email
  648. ctx.user = {
  649. _id: 'abcdef',
  650. email: ctx.email,
  651. }
  652. ctx.UserRegistrationHandler.promises.registerNewUser = sinon
  653. .stub()
  654. .resolves(ctx.user)
  655. ctx.User.updateOne = sinon
  656. .stub()
  657. .returns({ exec: sinon.stub().resolves() })
  658. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  659. await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
  660. ctx.req,
  661. ctx.res,
  662. ctx.next
  663. )
  664. })
  665. it('should send back a json response', function (ctx) {
  666. expect(ctx.res.json).toHaveBeenCalledTimes(1)
  667. expect(ctx.res.json.mock.lastCall[0].email).to.equal(ctx.email)
  668. })
  669. it('should have checked for existing admins', function (ctx) {
  670. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  671. })
  672. it('should have called registerNewUser', function (ctx) {
  673. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  674. 1
  675. )
  676. ctx.UserRegistrationHandler.promises.registerNewUser
  677. .calledWith({
  678. email: ctx.email,
  679. password: 'password_here',
  680. first_name: ctx.email,
  681. last_name: '',
  682. })
  683. .should.equal(true)
  684. })
  685. it('should have updated the user to make them an admin', function (ctx) {
  686. ctx.User.updateOne.callCount.should.equal(1)
  687. ctx.User.updateOne
  688. .calledWith(
  689. { _id: ctx.user._id },
  690. {
  691. $set: {
  692. isAdmin: true,
  693. emails: [
  694. { email: ctx.user.email, reversedHostname: 'moc.elpmaxe' },
  695. ],
  696. },
  697. }
  698. )
  699. .should.equal(true)
  700. })
  701. it('should have set a redirect in session', function (ctx) {
  702. ctx.AuthenticationController.setRedirectInSession.callCount.should.equal(
  703. 1
  704. )
  705. ctx.AuthenticationController.setRedirectInSession
  706. .calledWith(ctx.req, '/launchpad')
  707. .should.equal(true)
  708. })
  709. })
  710. describe('when the authMethod is invalid', function () {
  711. beforeEach(async function (ctx) {
  712. ctx._atLeastOneAdminExists.resolves(false)
  713. ctx.email = undefined
  714. ctx.req.body.email = ctx.email
  715. ctx.user = {
  716. _id: 'abcdef',
  717. email: ctx.email,
  718. }
  719. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  720. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  721. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  722. await ctx.LaunchpadController.registerExternalAuthAdmin(
  723. 'NOTAVALIDAUTHMETHOD'
  724. )(ctx.req, ctx.res, ctx.next)
  725. })
  726. it('should send a 403 response', function (ctx) {
  727. expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
  728. expect(ctx.res.sendStatus).toHaveBeenCalledWith(403)
  729. })
  730. it('should not check for existing admins', function (ctx) {
  731. ctx._atLeastOneAdminExists.callCount.should.equal(0)
  732. })
  733. it('should not call registerNewUser', function (ctx) {
  734. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  735. 0
  736. )
  737. })
  738. })
  739. describe('when no email is supplied', function () {
  740. beforeEach(async function (ctx) {
  741. ctx._atLeastOneAdminExists.resolves(false)
  742. ctx.email = undefined
  743. ctx.req.body.email = ctx.email
  744. ctx.user = {
  745. _id: 'abcdef',
  746. email: ctx.email,
  747. }
  748. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  749. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  750. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  751. await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
  752. ctx.req,
  753. ctx.res,
  754. ctx.next
  755. )
  756. })
  757. it('should send a 400 response', function (ctx) {
  758. expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
  759. expect(ctx.res.sendStatus).toHaveBeenCalledWith(400)
  760. })
  761. it('should not check for existing admins', function (ctx) {
  762. ctx._atLeastOneAdminExists.callCount.should.equal(0)
  763. })
  764. it('should not call registerNewUser', function (ctx) {
  765. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  766. 0
  767. )
  768. })
  769. })
  770. describe('when there are already existing admins', function () {
  771. beforeEach(async function (ctx) {
  772. ctx._atLeastOneAdminExists.resolves(true)
  773. ctx.email = 'someone@example.com'
  774. ctx.req.body.email = ctx.email
  775. ctx.user = {
  776. _id: 'abcdef',
  777. email: ctx.email,
  778. }
  779. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  780. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  781. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  782. await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
  783. ctx.req,
  784. ctx.res,
  785. ctx.next
  786. )
  787. })
  788. it('should send a 403 response', function (ctx) {
  789. expect(ctx.res.sendStatus).toHaveBeenCalledTimes(1)
  790. expect(ctx.res.sendStatus).toHaveBeenCalledWith(403)
  791. })
  792. it('should not call registerNewUser', function (ctx) {
  793. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  794. 0
  795. )
  796. })
  797. })
  798. describe('when checking admins produces an error', function () {
  799. beforeEach(async function (ctx) {
  800. ctx._atLeastOneAdminExists.rejects(new Error('woops'))
  801. ctx.email = 'someone@example.com'
  802. ctx.req.body.email = ctx.email
  803. ctx.user = {
  804. _id: 'abcdef',
  805. email: ctx.email,
  806. }
  807. ctx.UserRegistrationHandler.promises.registerNewUser = sinon.stub()
  808. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  809. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  810. await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
  811. ctx.req,
  812. ctx.res,
  813. ctx.next
  814. )
  815. })
  816. it('should call next with an error', function (ctx) {
  817. ctx.next.callCount.should.equal(1)
  818. expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  819. })
  820. it('should have checked for existing admins', function (ctx) {
  821. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  822. })
  823. it('should not call registerNewUser', function (ctx) {
  824. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  825. 0
  826. )
  827. })
  828. })
  829. describe('when registerNewUser produces an error', function () {
  830. beforeEach(async function (ctx) {
  831. ctx._atLeastOneAdminExists.resolves(false)
  832. ctx.email = 'someone@example.com'
  833. ctx.req.body.email = ctx.email
  834. ctx.user = {
  835. _id: 'abcdef',
  836. email: ctx.email,
  837. }
  838. ctx.UserRegistrationHandler.promises.registerNewUser = sinon
  839. .stub()
  840. .rejects(new Error('woops'))
  841. ctx.User.updateOne = sinon.stub().returns({ exec: sinon.stub() })
  842. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  843. await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
  844. ctx.req,
  845. ctx.res,
  846. ctx.next
  847. )
  848. })
  849. it('should call next with an error', function (ctx) {
  850. ctx.next.callCount.should.equal(1)
  851. expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  852. })
  853. it('should have checked for existing admins', function (ctx) {
  854. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  855. })
  856. it('should have called registerNewUser', function (ctx) {
  857. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  858. 1
  859. )
  860. ctx.UserRegistrationHandler.promises.registerNewUser
  861. .calledWith({
  862. email: ctx.email,
  863. password: 'password_here',
  864. first_name: ctx.email,
  865. last_name: '',
  866. })
  867. .should.equal(true)
  868. })
  869. it('should not call update', function (ctx) {
  870. ctx.User.updateOne.callCount.should.equal(0)
  871. })
  872. })
  873. describe('when user update produces an error', function () {
  874. beforeEach(async function (ctx) {
  875. ctx._atLeastOneAdminExists.resolves(false)
  876. ctx.email = 'someone@example.com'
  877. ctx.req.body.email = ctx.email
  878. ctx.user = {
  879. _id: 'abcdef',
  880. email: ctx.email,
  881. }
  882. ctx.UserRegistrationHandler.promises.registerNewUser = sinon
  883. .stub()
  884. .resolves(ctx.user)
  885. ctx.User.updateOne = sinon.stub().returns({
  886. exec: sinon.stub().rejects(new Error('woops')),
  887. })
  888. ctx.AuthenticationController.setRedirectInSession = sinon.stub()
  889. await ctx.LaunchpadController.registerExternalAuthAdmin('ldap')(
  890. ctx.req,
  891. ctx.res,
  892. ctx.next
  893. )
  894. })
  895. it('should call next with an error', function (ctx) {
  896. ctx.next.callCount.should.equal(1)
  897. expect(ctx.next.lastCall.args[0]).to.be.instanceof(Error)
  898. })
  899. it('should have checked for existing admins', function (ctx) {
  900. ctx._atLeastOneAdminExists.callCount.should.equal(1)
  901. })
  902. it('should have called registerNewUser', function (ctx) {
  903. ctx.UserRegistrationHandler.promises.registerNewUser.callCount.should.equal(
  904. 1
  905. )
  906. ctx.UserRegistrationHandler.promises.registerNewUser
  907. .calledWith({
  908. email: ctx.email,
  909. password: 'password_here',
  910. first_name: ctx.email,
  911. last_name: '',
  912. })
  913. .should.equal(true)
  914. })
  915. })
  916. })
  917. })