LaunchpadController.test.mjs 35 KB

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