LaunchpadControllerTests.js 33 KB

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