LaunchpadTests.mjs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { expect } from 'chai'
  2. import cheerio from 'cheerio'
  3. import UserHelper from '../../../../../test/acceptance/src/helpers/UserHelper.mjs'
  4. describe('Launchpad', function () {
  5. const adminEmail = 'admin@example.com'
  6. const adminPassword = 'adreadfulsecret'
  7. const user = new UserHelper()
  8. it('should show the launchpad page', async function () {
  9. const response = await user.fetch('/launchpad')
  10. expect(response.status).to.equal(200)
  11. const body = await response.text()
  12. const $ = cheerio.load(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.fetch('/launchpad')
  20. expect(initialPageResponse.status).to.equal(200)
  21. const initialPageBody = await initialPageResponse.text()
  22. const $ = cheerio.load(initialPageBody)
  23. expect($('h2').first().text()).to.equal('Create the first Admin account')
  24. expect($('form[name="email"]').first()).to.exist
  25. expect($('form[name="password"]').first()).to.exist
  26. // Submit the form
  27. let csrfToken = await user.getCsrfToken()
  28. const postResponse = await user.fetch('/launchpad/register_admin', {
  29. method: 'POST',
  30. headers: {
  31. 'Content-Type': 'application/json',
  32. Accept: 'application/json',
  33. },
  34. body: JSON.stringify({
  35. _csrf: csrfToken,
  36. email: adminEmail,
  37. password: adminPassword,
  38. }),
  39. })
  40. expect(postResponse.status).to.equal(200)
  41. const postBody = await postResponse.json()
  42. expect(postBody).to.deep.equal({ redir: '/launchpad' })
  43. // Try to load the page again
  44. const secondPageResponse = await user.fetch('/launchpad')
  45. expect(secondPageResponse.status).to.equal(302)
  46. expect(secondPageResponse.headers.get('location')).to.equal(
  47. UserHelper.url('/login').toString()
  48. )
  49. // Forbid submitting the form again
  50. csrfToken = await user.getCsrfToken()
  51. const badPostResponse = await user.fetch('/launchpad/register_admin', {
  52. method: 'POST',
  53. headers: {
  54. 'Content-Type': 'application/json',
  55. Accept: 'application/json',
  56. },
  57. body: JSON.stringify({
  58. _csrf: csrfToken,
  59. email: adminEmail + '1',
  60. password: adminPassword + '1',
  61. }),
  62. })
  63. expect(badPostResponse.status).to.equal(403)
  64. expect(await badPostResponse.json()).to.deep.equal({
  65. message: { type: 'error', text: 'admin user already exists' },
  66. })
  67. // Log in as this new admin user
  68. const adminUser = await UserHelper.loginUser({
  69. email: adminEmail,
  70. password: adminPassword,
  71. })
  72. // Check we are actually admin
  73. expect(await adminUser.isLoggedIn()).to.equal(true)
  74. expect(adminUser.user.isAdmin).to.equal(true)
  75. // Check reversedHostName is stored
  76. expect(adminUser.user.emails[0].reversedHostname).to.equal('moc.elpmaxe')
  77. })
  78. })