Skip to content

Commit

Permalink
Add unit tests for space permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Jan 30, 2023
1 parent d59cf45 commit 73220af
Showing 1 changed file with 213 additions and 0 deletions.
213 changes: 213 additions & 0 deletions packages/web-client/tests/unit/helpers/space/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,217 @@ describe('buildSpace', () => {
expect(space.isManager(mock<User>({ uuid }))).toBe(data.expectedResult)
})
})

it.each([
{ role: spaceRoleViewer.name, expectedResult: false },
{ role: spaceRoleEditor.name, expectedResult: true },
{ role: spaceRoleManager.name, expectedResult: true }
])('canUpload', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.role, grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.canUpload({ user: mock<User>({ uuid }) })).toBe(data.expectedResult)
})

it.each([
{
userRole: 'user',
spaceRole: spaceRoleManager.name,
spaceDisabled: true,
expectedResult: true
},
{
userRole: 'user',
spaceRole: spaceRoleEditor.name,
spaceDisabled: true,
expectedResult: false
},
{
userRole: 'user',
spaceRole: spaceRoleViewer.name,
spaceDisabled: true,
expectedResult: false
},
{
userRole: 'spaceadmin',
spaceRole: spaceRoleViewer.name,
spaceDisabled: true,
expectedResult: true
},
{
userRole: 'spaceadmin',
spaceRole: spaceRoleViewer.name,
spaceDisabled: false,
expectedResult: false
}
])('canBeDeleted', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.spaceRole, grantedToIdentities: [{ user: { id: uuid } }] }],
...(data.spaceDisabled && { deleted: { state: 'trashed' } })
}
}) as ProjectSpaceResource
expect(space.canBeDeleted({ user: mock<User>({ uuid, role: { name: data.userRole } }) })).toBe(
data.expectedResult
)
})

it.each([
{ userRole: 'user', spaceRole: spaceRoleManager.name, expectedResult: true },
{ userRole: 'user', spaceRole: spaceRoleEditor.name, expectedResult: false },
{ userRole: 'user', spaceRole: spaceRoleViewer.name, expectedResult: false },
{ userRole: 'spaceadmin', spaceRole: spaceRoleViewer.name, expectedResult: true }
])('canRename', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.spaceRole, grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.canRename({ user: mock<User>({ uuid, role: { name: data.userRole } }) })).toBe(
data.expectedResult
)
})

it.each([
{ userRole: 'user', spaceRole: spaceRoleManager.name, expectedResult: true },
{ userRole: 'user', spaceRole: spaceRoleEditor.name, expectedResult: false },
{ userRole: 'user', spaceRole: spaceRoleViewer.name, expectedResult: false },
{ userRole: 'spaceadmin', spaceRole: spaceRoleViewer.name, expectedResult: true }
])('canEditDescription', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.spaceRole, grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(
space.canEditDescription({ user: mock<User>({ uuid, role: { name: data.userRole } }) })
).toBe(data.expectedResult)
})

it.each([
{
userRole: 'user',
spaceRole: spaceRoleManager.name,
spaceDisabled: true,
expectedResult: true
},
{
userRole: 'user',
spaceRole: spaceRoleEditor.name,
spaceDisabled: true,
expectedResult: false
},
{
userRole: 'user',
spaceRole: spaceRoleViewer.name,
spaceDisabled: true,
expectedResult: false
},
{
userRole: 'spaceadmin',
spaceRole: spaceRoleViewer.name,
spaceDisabled: true,
expectedResult: true
},
{
userRole: 'spaceadmin',
spaceRole: spaceRoleViewer.name,
spaceDisabled: false,
expectedResult: false
}
])('canRestore', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.spaceRole, grantedToIdentities: [{ user: { id: uuid } }] }],
...(data.spaceDisabled && { deleted: { state: 'trashed' } })
}
}) as ProjectSpaceResource
expect(space.canRestore({ user: mock<User>({ uuid, role: { name: data.userRole } }) })).toBe(
data.expectedResult
)
})

it.each([
{
userRole: 'user',
spaceRole: spaceRoleManager.name,
spaceDisabled: false,
expectedResult: true
},
{
userRole: 'user',
spaceRole: spaceRoleEditor.name,
spaceDisabled: false,
expectedResult: false
},
{
userRole: 'user',
spaceRole: spaceRoleViewer.name,
spaceDisabled: false,
expectedResult: false
},
{
userRole: 'spaceadmin',
spaceRole: spaceRoleViewer.name,
spaceDisabled: false,
expectedResult: true
},
{
userRole: 'spaceadmin',
spaceRole: spaceRoleViewer.name,
spaceDisabled: true,
expectedResult: false
}
])('canDisable', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.spaceRole, grantedToIdentities: [{ user: { id: uuid } }] }],
...(data.spaceDisabled && { deleted: { state: 'trashed' } })
}
}) as ProjectSpaceResource
expect(space.canDisable({ user: mock<User>({ uuid, role: { name: data.userRole } }) })).toBe(
data.expectedResult
)
})

it.each([
{ role: spaceRoleManager.name, expectedResult: true },
{ role: spaceRoleEditor.name, expectedResult: false },
{ role: spaceRoleViewer.name, expectedResult: false }
])('canShare', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.role, grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.canShare({ user: mock<User>({ uuid }) })).toBe(data.expectedResult)
})

it.each([
{ role: spaceRoleManager.name, expectedResult: true },
{ role: spaceRoleEditor.name, expectedResult: true },
{ role: spaceRoleViewer.name, expectedResult: false }
])('canEditImage', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.role, grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.canEditImage({ user: mock<User>({ uuid }) })).toBe(data.expectedResult)
})

it.each([
{ role: spaceRoleManager.name, expectedResult: true },
{ role: spaceRoleEditor.name, expectedResult: true },
{ role: spaceRoleViewer.name, expectedResult: false }
])('canEditReadme', (data) => {
const space = buildSpace({
root: {
permissions: [{ roles: data.role, grantedToIdentities: [{ user: { id: uuid } }] }]
}
}) as ProjectSpaceResource
expect(space.canEditReadme({ user: mock<User>({ uuid }) })).toBe(data.expectedResult)
})
})

0 comments on commit 73220af

Please sign in to comment.