LaunchpadTests.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const { expect } = require('chai')
  2. const cheerio = require('cheerio')
  3. const WEB_PATH = '../../../../..'
  4. const UserHelper = require(`${WEB_PATH}/test/acceptance/src/helpers/UserHelper`)
  5. describe('Launchpad', function () {
  6. const adminEmail = 'admin@example.com'
  7. const adminPassword = 'adreadfulsecret'
  8. const user = new UserHelper()
  9. it('should show the launchpad page', async function () {
  10. const response = await user.request.get('/launchpad')
  11. expect(response.statusCode).to.equal(200)
  12. const $ = cheerio.load(response.body)
  13. expect($('h2').first().text()).to.equal('Create the first Admin account')
  14. expect($('form[name="email"]').first()).to.exist
  15. expect($('form[name="password"]').first()).to.exist
  16. })
  17. it('should allow for creation of the first admin user', async function () {
  18. // Load the launchpad page
  19. const initialPageResponse = await user.request.get('/launchpad')
  20. expect(initialPageResponse.statusCode).to.equal(200)
  21. const $ = cheerio.load(initialPageResponse.body)
  22. expect($('h2').first().text()).to.equal('Create the first Admin account')
  23. expect($('form[name="email"]').first()).to.exist
  24. expect($('form[name="password"]').first()).to.exist
  25. // Submit the form
  26. let csrfToken = await user.getCsrfToken()
  27. const postResponse = await user.request.post({
  28. url: '/launchpad/register_admin',
  29. json: {
  30. _csrf: csrfToken,
  31. email: adminEmail,
  32. password: adminPassword,
  33. },
  34. })
  35. expect(postResponse.statusCode).to.equal(200)
  36. expect(postResponse.body.redir).to.equal('')
  37. expect(postResponse.body.email).to.equal(adminEmail)
  38. expect(postResponse.body.id).to.exist
  39. // Try to load the page again
  40. const secondPageResponse = await user.request.get('/launchpad', {
  41. simple: false,
  42. })
  43. expect(secondPageResponse.statusCode).to.equal(302)
  44. expect(secondPageResponse.headers.location).to.equal('/login')
  45. // Forbid submitting the form again
  46. csrfToken = await user.getCsrfToken()
  47. const badPostResponse = await user.request.post({
  48. url: '/launchpad/register_admin',
  49. json: {
  50. _csrf: csrfToken,
  51. email: adminEmail + '1',
  52. password: adminPassword + '1',
  53. },
  54. simple: false,
  55. })
  56. expect(badPostResponse.statusCode).to.equal(403)
  57. // Log in as this new admin user
  58. const adminUser = await UserHelper.loginUser({
  59. email: adminEmail,
  60. password: adminPassword,
  61. })
  62. // Check we are actually admin
  63. expect(await adminUser.isLoggedIn()).to.equal(true)
  64. expect(adminUser.user.isAdmin).to.equal(true)
  65. })
  66. })