AdminOnlyLoginTests.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(
  30. /login failed: status=403 body={"message":{"type":"error","text":"Admin only panel"}}/
  31. )
  32. }
  33. }
  34. describe('adminOnlyLogin=true', function () {
  35. beforeEach(function () {
  36. Settings.adminOnlyLogin = true
  37. })
  38. it('should allow the admin user to login', async function () {
  39. await expectCanLogin(adminUser)
  40. })
  41. it('should block a regular user from login', async function () {
  42. await expectRejectedLogin(regularUser)
  43. })
  44. })
  45. describe('adminOnlyLogin=false', function () {
  46. beforeEach(function () {
  47. Settings.adminOnlyLogin = false
  48. })
  49. it('should allow the admin user to login', async function () {
  50. await expectCanLogin(adminUser)
  51. })
  52. it('should allow a regular user to login', async function () {
  53. await expectCanLogin(regularUser)
  54. })
  55. })
  56. })