LaunchpadTests.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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).to.deep.equal({ redir: '/launchpad' })
  37. // Try to load the page again
  38. const secondPageResponse = await user.request.get('/launchpad', {
  39. simple: false,
  40. })
  41. expect(secondPageResponse.statusCode).to.equal(302)
  42. expect(secondPageResponse.headers.location).to.equal('/login')
  43. // Forbid submitting the form again
  44. csrfToken = await user.getCsrfToken()
  45. const badPostResponse = await user.request.post({
  46. url: '/launchpad/register_admin',
  47. json: {
  48. _csrf: csrfToken,
  49. email: adminEmail + '1',
  50. password: adminPassword + '1',
  51. },
  52. simple: false,
  53. })
  54. expect(badPostResponse.statusCode).to.equal(403)
  55. // Log in as this new admin user
  56. const adminUser = await UserHelper.loginUser({
  57. email: adminEmail,
  58. password: adminPassword,
  59. })
  60. // Check we are actually admin
  61. expect(await adminUser.isLoggedIn()).to.equal(true)
  62. expect(adminUser.user.isAdmin).to.equal(true)
  63. })
  64. })