authRequireGuestsToLogin.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, it, before, after } from 'mocha'
  2. import { expect } from 'chai'
  3. import { By } from 'selenium-webdriver'
  4. import {
  5. takeScreenshotOnFailure,
  6. } from '../../lib/elements.js'
  7. describe('config: authRequireGuestsToLogin', function () {
  8. this.timeout(30000)
  9. before(async function () {
  10. await runner.start('authRequireGuestsToLogin')
  11. })
  12. after(async () => {
  13. await runner.stop()
  14. })
  15. afterEach(function () {
  16. takeScreenshotOnFailure(this.currentTest, webdriver);
  17. });
  18. it('Guest is redirected to login', async function () {
  19. // Don't use getRootAndWait here because we want to test the redirect, and getRootAndWait waits for the dashboard to load
  20. await webdriver.get(runner.baseUrl())
  21. await webdriver.wait(async () => {
  22. const loginRequired = await webdriver.executeScript(
  23. 'return !!(window.initResponse && window.initResponse.loginRequired)'
  24. )
  25. const url = await webdriver.getCurrentUrl()
  26. return loginRequired && url.includes('/login')
  27. }, 15000, 'Guest should be redirected to login after Init')
  28. // Verify login UI elements are present
  29. const loginElements = await webdriver.findElements(By.css('form.local-login-form, .login-oauth2, .login-disabled'))
  30. expect(loginElements.length).to.be.greaterThan(0)
  31. console.log('✓ Login page loaded correctly')
  32. })
  33. it('Guest does not receive navigation or dashboards before login', async function () {
  34. await webdriver.get(runner.baseUrl())
  35. await webdriver.wait(async () => {
  36. return await webdriver.executeScript(
  37. 'return !!(window.initResponse && window.initResponse.loginRequired)'
  38. )
  39. }, 15000, 'Init should report loginRequired')
  40. const initState = await webdriver.executeScript(`
  41. const init = window.initResponse || {}
  42. return {
  43. showNavigation: !!init.showNavigation,
  44. rootDashboards: init.rootDashboards || [],
  45. rootDashboardEntries: init.rootDashboardEntries || []
  46. }
  47. `)
  48. expect(initState.showNavigation).to.equal(false)
  49. expect(initState.rootDashboards).to.deep.equal([])
  50. expect(initState.rootDashboardEntries).to.deep.equal([])
  51. const sidebars = await webdriver.findElements(By.css('#mainnav, #picocrank-sidebar'))
  52. expect(sidebars.length).to.equal(0)
  53. console.log('✓ Navigation and dashboards hidden while login is required')
  54. })
  55. })