AdminOnlyLoginTests.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Settings from '@overleaf/settings'
  2. import { expect } from 'chai'
  3. import UserHelper from './helpers/User.mjs'
  4. const User = UserHelper.promises
  5. describe('AdminOnlyLogin', function () {
  6. let adminUser, regularUser
  7. const flagBefore = Settings.adminOnlyLogin
  8. after(function () {
  9. Settings.adminOnlyLogin = flagBefore
  10. })
  11. beforeEach('create admin user', async function () {
  12. adminUser = new User()
  13. await adminUser.ensureUserExists()
  14. await adminUser.ensureAdmin()
  15. })
  16. beforeEach('create regular user', async function () {
  17. regularUser = new User()
  18. await regularUser.ensureUserExists()
  19. })
  20. async function expectCanLogin(user) {
  21. const response = await user.login()
  22. expect(response.statusCode).to.equal(200)
  23. expect(response.body).to.deep.equal({ redir: '/project' })
  24. }
  25. async function expectRejectedLogin(user) {
  26. try {
  27. await user.login()
  28. expect.fail('expected the login request to fail')
  29. } catch (err) {
  30. expect(err).to.match(/login failed: status=403/)
  31. expect(err.info.body).to.deep.equal({
  32. message: { type: 'error', text: 'Admin only panel' },
  33. })
  34. }
  35. }
  36. describe('adminOnlyLogin=true', function () {
  37. beforeEach(function () {
  38. Settings.adminOnlyLogin = true
  39. })
  40. it('should allow the admin user to login', async function () {
  41. await expectCanLogin(adminUser)
  42. })
  43. it('should block a regular user from login', async function () {
  44. await expectRejectedLogin(regularUser)
  45. })
  46. })
  47. describe('adminOnlyLogin=false', function () {
  48. beforeEach(function () {
  49. Settings.adminOnlyLogin = false
  50. })
  51. it('should allow the admin user to login', async function () {
  52. await expectCanLogin(adminUser)
  53. })
  54. it('should allow a regular user to login', async function () {
  55. await expectCanLogin(regularUser)
  56. })
  57. })
  58. })