AdminPrivilegeAvailableTests.mjs 5.0 KB

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