fix(workspaces): assign role on workspace join (#2864)

This commit is contained in:
Gergő Jedlicska
2024-09-03 18:54:06 +02:00
committed by GitHub
parent beabb5552b
commit c3b05abd6f
5 changed files with 127 additions and 9 deletions
@@ -1,10 +1,14 @@
import cryptoRandomString from 'crypto-random-string'
import { WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
import { Roles } from '@speckle/shared'
import { Roles, StreamRoles } from '@speckle/shared'
import { StreamAclRecord, StreamRecord } from '@/modules/core/helpers/types'
import { onProjectCreatedFactory } from '@/modules/workspaces/events/eventListener'
import {
onProjectCreatedFactory,
onWorkspaceJoinedFactory
} from '@/modules/workspaces/events/eventListener'
import { expect } from 'chai'
import { mapWorkspaceRoleToInitialProjectRole } from '@/modules/workspaces/domain/logic'
import { chunk } from 'lodash'
describe('Event handlers', () => {
describe('onProjectCreatedFactory creates a function, that', () => {
@@ -57,4 +61,60 @@ describe('Event handlers', () => {
expect(projectRoles.length).to.equal(2)
})
})
describe('onWorkspaceJoinedFactory creates a function, that', () => {
it('assigns no project roles if the role mapping returns null', async () => {
await onWorkspaceJoinedFactory({
getDefaultWorkspaceProjectRoleMapping: async () => ({
[Roles.Workspace.Admin]: Roles.Stream.Owner,
[Roles.Workspace.Member]: Roles.Stream.Contributor,
[Roles.Workspace.Guest]: null
}),
async *queryAllWorkspaceProjects() {
expect.fail()
},
upsertProjectRole: async () => {
expect.fail()
}
})({
role: Roles.Workspace.Guest,
userId: cryptoRandomString({ length: 10 }),
workspaceId: cryptoRandomString({ length: 10 })
})
})
it('assigns the mapped projects roles to all queried project', async () => {
const projectIds = [
cryptoRandomString({ length: 10 }),
cryptoRandomString({ length: 10 }),
cryptoRandomString({ length: 10 }),
cryptoRandomString({ length: 10 })
]
const userId = cryptoRandomString({ length: 10 })
const projectRole = Roles.Stream.Reviewer
const storedRoles: { userId: string; role: StreamRoles; projectId: string }[] = []
await onWorkspaceJoinedFactory({
getDefaultWorkspaceProjectRoleMapping: async () => ({
[Roles.Workspace.Admin]: Roles.Stream.Owner,
[Roles.Workspace.Member]: projectRole,
[Roles.Workspace.Guest]: null
}),
async *queryAllWorkspaceProjects() {
for (const projIds of chunk(projectIds, 3)) {
yield projIds.map((projId) => ({ id: projId } as unknown as StreamRecord))
}
},
upsertProjectRole: async (args) => {
storedRoles.push(args)
return {} as StreamRecord
}
})({
role: Roles.Workspace.Member,
userId,
workspaceId: cryptoRandomString({ length: 10 })
})
expect(storedRoles).deep.equals(
projectIds.map((projectId) => ({ projectId, role: projectRole, userId }))
)
})
})
})