LaunchpadTests.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.fetch('/launchpad')
  11. expect(response.status).to.equal(200)
  12. const body = await response.text()
  13. const $ = cheerio.load(body)
  14. expect($('h2').first().text()).to.equal('Create the first Admin account')
  15. expect($('form[name="email"]').first()).to.exist
  16. expect($('form[name="password"]').first()).to.exist
  17. })
  18. it('should allow for creation of the first admin user', async function () {
  19. // Load the launchpad page
  20. const initialPageResponse = await user.fetch('/launchpad')
  21. expect(initialPageResponse.status).to.equal(200)
  22. const initialPageBody = await initialPageResponse.text()
  23. const $ = cheerio.load(initialPageBody)
  24. expect($('h2').first().text()).to.equal('Create the first Admin account')
  25. expect($('form[name="email"]').first()).to.exist
  26. expect($('form[name="password"]').first()).to.exist
  27. // Submit the form
  28. let csrfToken = await user.getCsrfToken()
  29. const postResponse = await user.fetch('/launchpad/register_admin', {
  30. method: 'POST',
  31. headers: {
  32. 'Content-Type': 'application/json',
  33. Accept: 'application/json',
  34. },
  35. body: JSON.stringify({
  36. _csrf: csrfToken,
  37. email: adminEmail,
  38. password: adminPassword,
  39. }),
  40. })
  41. expect(postResponse.status).to.equal(200)
  42. const postBody = await postResponse.json()
  43. expect(postBody).to.deep.equal({ redir: '/launchpad' })
  44. // Try to load the page again
  45. const secondPageResponse = await user.fetch('/launchpad')
  46. expect(secondPageResponse.status).to.equal(302)
  47. expect(secondPageResponse.headers.get('location')).to.equal(
  48. UserHelper.url('/login').toString()
  49. )
  50. // Forbid submitting the form again
  51. csrfToken = await user.getCsrfToken()
  52. const badPostResponse = await user.fetch('/launchpad/register_admin', {
  53. method: 'POST',
  54. headers: {
  55. 'Content-Type': 'application/json',
  56. Accept: 'application/json',
  57. },
  58. body: JSON.stringify({
  59. _csrf: csrfToken,
  60. email: adminEmail + '1',
  61. password: adminPassword + '1',
  62. }),
  63. })
  64. expect(badPostResponse.status).to.equal(403)
  65. // Log in as this new admin user
  66. const adminUser = await UserHelper.loginUser({
  67. email: adminEmail,
  68. password: adminPassword,
  69. })
  70. // Check we are actually admin
  71. expect(await adminUser.isLoggedIn()).to.equal(true)
  72. expect(adminUser.user.isAdmin).to.equal(true)
  73. })
  74. })