AdminPrivilegeAvailableTests.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const Settings = require('@overleaf/settings')
  2. const { expect } = require('chai')
  3. const User = require('./helpers/User').promises
  4. describe('AdminPrivilegeAvailable', function () {
  5. let adminUser, otherUser
  6. const flagBefore = Settings.adminPrivilegeAvailable
  7. after(function () {
  8. Settings.adminPrivilegeAvailable = flagBefore
  9. })
  10. beforeEach('create admin user', async function () {
  11. adminUser = new User()
  12. await adminUser.ensureUserExists()
  13. await adminUser.ensureAdmin()
  14. await adminUser.login()
  15. })
  16. let projectIdOwned, otherUsersProjectId, otherUsersProjectTokenAccessURL
  17. beforeEach('create owned project', async function () {
  18. projectIdOwned = await adminUser.createProject('owned project')
  19. })
  20. beforeEach('create other user and project', async function () {
  21. otherUser = new User()
  22. await otherUser.login()
  23. otherUsersProjectId = await otherUser.createProject('other users project')
  24. await otherUser.makeTokenBased(otherUsersProjectId)
  25. const {
  26. tokens: { readOnly: readOnlyToken },
  27. } = await otherUser.getProject(otherUsersProjectId)
  28. otherUsersProjectTokenAccessURL = `/read/${readOnlyToken}`
  29. })
  30. async function hasAccess(projectId) {
  31. const { response } = await adminUser.doRequest(
  32. 'GET',
  33. `/project/${projectId}`
  34. )
  35. return response.statusCode === 200
  36. }
  37. async function displayTokenAccessPage(user) {
  38. const { response } = await user.doRequest(
  39. 'GET',
  40. otherUsersProjectTokenAccessURL
  41. )
  42. expect(response.statusCode).to.equal(200)
  43. expect(response.body).to.include(otherUsersProjectTokenAccessURL)
  44. }
  45. describe('adminPrivilegeAvailable=true', function () {
  46. beforeEach(function () {
  47. Settings.adminPrivilegeAvailable = true
  48. })
  49. it('should grant the admin access to owned project', async function () {
  50. expect(await hasAccess(projectIdOwned)).to.equal(true)
  51. })
  52. it('should grant the admin access to non-owned project', async function () {
  53. expect(await hasAccess(otherUsersProjectId)).to.equal(true)
  54. })
  55. it('should display token access page for admin', async function () {
  56. await displayTokenAccessPage(adminUser)
  57. })
  58. it('should display token access page for regular user', async function () {
  59. await displayTokenAccessPage(otherUser)
  60. })
  61. })
  62. describe('adminPrivilegeAvailable=false', function () {
  63. beforeEach(function () {
  64. Settings.adminPrivilegeAvailable = false
  65. })
  66. it('should grant the admin access to owned project', async function () {
  67. expect(await hasAccess(projectIdOwned)).to.equal(true)
  68. })
  69. it('should block the admin from non-owned project', async function () {
  70. expect(await hasAccess(otherUsersProjectId)).to.equal(false)
  71. })
  72. it('should redirect a token access request to admin panel', async function () {
  73. const { response } = await adminUser.doRequest(
  74. 'GET',
  75. otherUsersProjectTokenAccessURL
  76. )
  77. expect(response.statusCode).to.equal(302)
  78. expect(response.headers.location).to.equal(
  79. Settings.adminUrl + otherUsersProjectTokenAccessURL
  80. )
  81. })
  82. it('should display token access page for regular user', async function () {
  83. await displayTokenAccessPage(otherUser)
  84. })
  85. })
  86. })