AdminPrivilegeAvailableTests.js 4.9 KB

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