2024-10-14 06:41:11 -04:00
|
|
|
import Settings from '@overleaf/settings'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import UserHelper from './helpers/User.js'
|
|
|
|
import { getSafeAdminDomainRedirect } from '../../../app/src/Features/Helpers/UrlHelper.js'
|
|
|
|
|
|
|
|
const User = UserHelper.promises
|
2022-03-31 06:34:49 -04:00
|
|
|
|
|
|
|
describe('AdminPrivilegeAvailable', function () {
|
2022-03-31 06:35:16 -04:00
|
|
|
let adminUser, otherUser
|
2022-03-31 06:34:49 -04:00
|
|
|
const flagBefore = Settings.adminPrivilegeAvailable
|
|
|
|
after(function () {
|
|
|
|
Settings.adminPrivilegeAvailable = flagBefore
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach('create admin user', async function () {
|
|
|
|
adminUser = new User()
|
|
|
|
await adminUser.ensureUserExists()
|
|
|
|
await adminUser.ensureAdmin()
|
|
|
|
await adminUser.login()
|
|
|
|
})
|
|
|
|
|
2022-03-31 06:35:16 -04:00
|
|
|
let projectIdOwned, otherUsersProjectId, otherUsersProjectTokenAccessURL
|
2022-03-31 06:34:49 -04:00
|
|
|
beforeEach('create owned project', async function () {
|
|
|
|
projectIdOwned = await adminUser.createProject('owned project')
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach('create other user and project', async function () {
|
2024-07-15 06:23:03 -04:00
|
|
|
otherUser = new User({
|
|
|
|
email: 'test@non-staff.com',
|
|
|
|
confirmedAt: new Date(),
|
|
|
|
})
|
2022-03-31 06:34:49 -04:00
|
|
|
await otherUser.login()
|
|
|
|
|
|
|
|
otherUsersProjectId = await otherUser.createProject('other users project')
|
2022-03-31 06:35:16 -04:00
|
|
|
await otherUser.makeTokenBased(otherUsersProjectId)
|
|
|
|
const {
|
|
|
|
tokens: { readOnly: readOnlyToken },
|
|
|
|
} = await otherUser.getProject(otherUsersProjectId)
|
|
|
|
otherUsersProjectTokenAccessURL = `/read/${readOnlyToken}`
|
2022-03-31 06:34:49 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
async function hasAccess(projectId) {
|
|
|
|
const { response } = await adminUser.doRequest(
|
|
|
|
'GET',
|
|
|
|
`/project/${projectId}`
|
|
|
|
)
|
|
|
|
return response.statusCode === 200
|
|
|
|
}
|
|
|
|
|
2022-03-31 06:35:16 -04:00
|
|
|
async function displayTokenAccessPage(user) {
|
|
|
|
const { response } = await user.doRequest(
|
|
|
|
'GET',
|
|
|
|
otherUsersProjectTokenAccessURL
|
|
|
|
)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.body).to.include(otherUsersProjectTokenAccessURL)
|
|
|
|
}
|
|
|
|
|
2022-03-31 06:34:49 -04:00
|
|
|
describe('adminPrivilegeAvailable=true', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
Settings.adminPrivilegeAvailable = true
|
|
|
|
})
|
|
|
|
it('should grant the admin access to owned project', async function () {
|
|
|
|
expect(await hasAccess(projectIdOwned)).to.equal(true)
|
|
|
|
})
|
|
|
|
it('should grant the admin access to non-owned project', async function () {
|
|
|
|
expect(await hasAccess(otherUsersProjectId)).to.equal(true)
|
|
|
|
})
|
2022-03-31 06:35:16 -04:00
|
|
|
it('should display token access page for admin', async function () {
|
|
|
|
await displayTokenAccessPage(adminUser)
|
|
|
|
})
|
|
|
|
it('should display token access page for regular user', async function () {
|
|
|
|
await displayTokenAccessPage(otherUser)
|
|
|
|
})
|
2024-07-15 06:23:03 -04:00
|
|
|
it('should redirect a token grant request to project page', async function () {
|
|
|
|
const { response } = await adminUser.doRequest('POST', {
|
|
|
|
url: `${otherUsersProjectTokenAccessURL}/grant`,
|
|
|
|
json: {
|
|
|
|
confirmedByUser: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.body.redirect).to.equal(`/project/${otherUsersProjectId}`)
|
|
|
|
})
|
2022-03-31 06:34:49 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('adminPrivilegeAvailable=false', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
Settings.adminPrivilegeAvailable = false
|
|
|
|
})
|
|
|
|
it('should grant the admin access to owned project', async function () {
|
|
|
|
expect(await hasAccess(projectIdOwned)).to.equal(true)
|
|
|
|
})
|
|
|
|
it('should block the admin from non-owned project', async function () {
|
|
|
|
expect(await hasAccess(otherUsersProjectId)).to.equal(false)
|
|
|
|
})
|
2024-07-15 06:23:03 -04:00
|
|
|
it('should display token access page for admin', async function () {
|
|
|
|
displayTokenAccessPage(adminUser)
|
2022-03-31 06:35:16 -04:00
|
|
|
})
|
|
|
|
it('should display token access page for regular user', async function () {
|
|
|
|
await displayTokenAccessPage(otherUser)
|
|
|
|
})
|
2024-07-15 06:23:03 -04:00
|
|
|
it('should redirect a token grant request to admin panel if belongs to non-staff', async function () {
|
|
|
|
const { response } = await adminUser.doRequest('POST', {
|
|
|
|
url: `${otherUsersProjectTokenAccessURL}/grant`,
|
|
|
|
json: {
|
|
|
|
confirmedByUser: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.body.redirect).to.equal(
|
|
|
|
getSafeAdminDomainRedirect(otherUsersProjectTokenAccessURL)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should redirect a token grant request to project page if belongs to staff', async function () {
|
|
|
|
const staff = new User({
|
|
|
|
email: `test@${Settings.adminDomains[0]}`,
|
|
|
|
confirmedAt: new Date(),
|
|
|
|
})
|
|
|
|
await staff.ensureUserExists()
|
|
|
|
await staff.ensureAdmin()
|
|
|
|
await staff.login()
|
|
|
|
|
|
|
|
const staffProjectId = await staff.createProject('staff user project')
|
|
|
|
await staff.makeTokenBased(staffProjectId)
|
|
|
|
const {
|
|
|
|
tokens: { readOnly: readOnlyTokenAdmin },
|
|
|
|
} = await staff.getProject(staffProjectId)
|
|
|
|
const staffProjectTokenAccessURL = `/read/${readOnlyTokenAdmin}`
|
|
|
|
|
|
|
|
const { response } = await adminUser.doRequest('POST', {
|
|
|
|
url: `${staffProjectTokenAccessURL}/grant`,
|
|
|
|
json: {
|
|
|
|
confirmedByUser: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.body.redirect).to.equal(`/project/${staffProjectId}`)
|
|
|
|
})
|
2022-03-31 06:34:49 -04:00
|
|
|
})
|
|
|
|
})
|