AdminOnlyLoginTests.js 1.8 KB

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