AdminPrivilegeAvailableTests.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Settings from '@overleaf/settings'
  2. import { expect } from 'chai'
  3. import UserHelper from './helpers/User.js'
  4. import { getSafeAdminDomainRedirect } from '../../../app/src/Features/Helpers/UrlHelper.js'
  5. const User = UserHelper.promises
  6. describe('AdminPrivilegeAvailable', function () {
  7. let adminUser, otherUser
  8. const flagBefore = Settings.adminPrivilegeAvailable
  9. after(function () {
  10. Settings.adminPrivilegeAvailable = flagBefore
  11. })
  12. beforeEach('create admin user', async function () {
  13. adminUser = new User()
  14. await adminUser.ensureUserExists()
  15. await adminUser.ensureAdmin()
  16. await adminUser.login()
  17. })
  18. let projectIdOwned, otherUsersProjectId, otherUsersProjectTokenAccessURL
  19. beforeEach('create owned project', async function () {
  20. projectIdOwned = await adminUser.createProject('owned project')
  21. })
  22. beforeEach('create other user and project', async function () {
  23. otherUser = new User({
  24. email: 'test@non-staff.com',
  25. confirmedAt: new Date(),
  26. })
  27. await otherUser.login()
  28. otherUsersProjectId = await otherUser.createProject('other users project')
  29. await otherUser.makeTokenBased(otherUsersProjectId)
  30. const {
  31. tokens: { readOnly: readOnlyToken },
  32. } = await otherUser.getProject(otherUsersProjectId)
  33. otherUsersProjectTokenAccessURL = `/read/${readOnlyToken}`
  34. })
  35. async function hasAccess(projectId) {
  36. const { response } = await adminUser.doRequest(
  37. 'GET',
  38. `/project/${projectId}`
  39. )
  40. return response.statusCode === 200
  41. }
  42. async function displayTokenAccessPage(user) {
  43. const { response } = await user.doRequest(
  44. 'GET',
  45. otherUsersProjectTokenAccessURL
  46. )
  47. expect(response.statusCode).to.equal(200)
  48. expect(response.body).to.include(otherUsersProjectTokenAccessURL)
  49. }
  50. describe('adminPrivilegeAvailable=true', function () {
  51. beforeEach(function () {
  52. Settings.adminPrivilegeAvailable = true
  53. })
  54. it('should grant the admin access to owned project', async function () {
  55. expect(await hasAccess(projectIdOwned)).to.equal(true)
  56. })
  57. it('should grant the admin access to non-owned project', async function () {
  58. expect(await hasAccess(otherUsersProjectId)).to.equal(true)
  59. })
  60. it('should display token access page for admin', async function () {
  61. await displayTokenAccessPage(adminUser)
  62. })
  63. it('should display token access page for regular user', async function () {
  64. await displayTokenAccessPage(otherUser)
  65. })
  66. it('should redirect a token grant request to project page', async function () {
  67. const { response } = await adminUser.doRequest('POST', {
  68. url: `${otherUsersProjectTokenAccessURL}/grant`,
  69. json: {
  70. confirmedByUser: true,
  71. },
  72. })
  73. expect(response.statusCode).to.equal(200)
  74. expect(response.body.redirect).to.equal(`/project/${otherUsersProjectId}`)
  75. })
  76. })
  77. describe('adminPrivilegeAvailable=false', function () {
  78. beforeEach(function () {
  79. Settings.adminPrivilegeAvailable = false
  80. })
  81. it('should grant the admin access to owned project', async function () {
  82. expect(await hasAccess(projectIdOwned)).to.equal(true)
  83. })
  84. it('should block the admin from non-owned project', async function () {
  85. expect(await hasAccess(otherUsersProjectId)).to.equal(false)
  86. })
  87. it('should display token access page for admin', async function () {
  88. displayTokenAccessPage(adminUser)
  89. })
  90. it('should display token access page for regular user', async function () {
  91. await displayTokenAccessPage(otherUser)
  92. })
  93. it('should redirect a token grant request to admin panel if belongs to non-staff', async function () {
  94. const { response } = await adminUser.doRequest('POST', {
  95. url: `${otherUsersProjectTokenAccessURL}/grant`,
  96. json: {
  97. confirmedByUser: true,
  98. },
  99. })
  100. expect(response.statusCode).to.equal(200)
  101. expect(response.body.redirect).to.equal(
  102. getSafeAdminDomainRedirect(otherUsersProjectTokenAccessURL)
  103. )
  104. })
  105. it('should redirect a token grant request to project page if belongs to staff', async function () {
  106. const staff = new User({
  107. email: `test@${Settings.adminDomains[0]}`,
  108. confirmedAt: new Date(),
  109. })
  110. await staff.ensureUserExists()
  111. await staff.ensureAdmin()
  112. await staff.login()
  113. const staffProjectId = await staff.createProject('staff user project')
  114. await staff.makeTokenBased(staffProjectId)
  115. const {
  116. tokens: { readOnly: readOnlyTokenAdmin },
  117. } = await staff.getProject(staffProjectId)
  118. const staffProjectTokenAccessURL = `/read/${readOnlyTokenAdmin}`
  119. const { response } = await adminUser.doRequest('POST', {
  120. url: `${staffProjectTokenAccessURL}/grant`,
  121. json: {
  122. confirmedByUser: true,
  123. },
  124. })
  125. expect(response.statusCode).to.equal(200)
  126. expect(response.body.redirect).to.equal(`/project/${staffProjectId}`)
  127. })
  128. })
  129. })