AdminOnlyLoginTests.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const response = await user.login()
  26. expect(response.statusCode).to.equal(403)
  27. expect(response.body).to.deep.equal({
  28. message: { type: 'error', text: 'Admin only panel' },
  29. })
  30. }
  31. describe('adminOnlyLogin=true', function () {
  32. beforeEach(function () {
  33. Settings.adminOnlyLogin = true
  34. })
  35. it('should allow the admin user to login', async function () {
  36. await expectCanLogin(adminUser)
  37. })
  38. it('should block a regular user from login', async function () {
  39. await expectRejectedLogin(regularUser)
  40. })
  41. })
  42. describe('adminOnlyLogin=false', function () {
  43. beforeEach(function () {
  44. Settings.adminOnlyLogin = false
  45. })
  46. it('should allow the admin user to login', async function () {
  47. await expectCanLogin(adminUser)
  48. })
  49. it('should allow a regular user to login', async function () {
  50. await expectCanLogin(regularUser)
  51. })
  52. })
  53. })