feat(workspaces): leave workspace (#2585)
This commit is contained in:
@@ -15,7 +15,9 @@ import {
|
||||
CreateWorkspaceDocument,
|
||||
GetActiveUserWorkspacesDocument,
|
||||
GetWorkspaceDocument,
|
||||
UpdateWorkspaceDocument
|
||||
UpdateWorkspaceDocument,
|
||||
ActiveUserLeaveWorkspaceDocument,
|
||||
UpdateWorkspaceRoleDocument
|
||||
} from '@/test/graphql/generated/graphql'
|
||||
import { Workspace } from '@/modules/workspacesCore/domain/types'
|
||||
import { beforeEachContext } from '@/test/hooks'
|
||||
@@ -35,9 +37,17 @@ describe('Workspaces GQL CRUD', () => {
|
||||
role: Roles.Server.Admin
|
||||
}
|
||||
|
||||
const userB: BasicTestUser = {
|
||||
id: '',
|
||||
name: 'Alice speckle',
|
||||
email: 'alice-speckle@example.org',
|
||||
role: Roles.Server.User
|
||||
}
|
||||
|
||||
before(async () => {
|
||||
await beforeEachContext()
|
||||
await createTestUser(testUser)
|
||||
await Promise.all([createTestUser(testUser), createTestUser(userB)])
|
||||
|
||||
const token = await createAuthTokenForUser(testUser.id, AllScopes)
|
||||
apollo = await testApolloServer({
|
||||
context: createTestContext({
|
||||
@@ -179,5 +189,72 @@ describe('Workspaces GQL CRUD', () => {
|
||||
expect(data?.workspace.description).to.equal('')
|
||||
})
|
||||
})
|
||||
describe('mutation activeUserMutations.userWorkspaceMutations', () => {
|
||||
describe('leave', () => {
|
||||
it('allows the active user to leave a workspace', async () => {
|
||||
const name = cryptoRandomString({ length: 6 })
|
||||
const workspaceCreateResult = await apollo.execute(CreateWorkspaceDocument, {
|
||||
input: { name }
|
||||
})
|
||||
|
||||
const id = workspaceCreateResult.data?.workspaceMutations.create.id
|
||||
if (!id) throw new Error('This should have succeeded')
|
||||
|
||||
await apollo.execute(UpdateWorkspaceRoleDocument, {
|
||||
input: {
|
||||
userId: userB.id,
|
||||
workspaceId: id,
|
||||
role: Roles.Workspace.Admin
|
||||
}
|
||||
})
|
||||
|
||||
let userWorkspaces = await apollo.execute(GetActiveUserWorkspacesDocument, {})
|
||||
|
||||
expect(
|
||||
userWorkspaces.data?.activeUser?.workspaces.items
|
||||
.map((i) => i.name)
|
||||
.includes(name)
|
||||
).to.be.true
|
||||
|
||||
const leaveResult = await apollo.execute(ActiveUserLeaveWorkspaceDocument, {
|
||||
id
|
||||
})
|
||||
|
||||
expect(leaveResult.errors).to.be.undefined
|
||||
|
||||
userWorkspaces = await apollo.execute(GetActiveUserWorkspacesDocument, {})
|
||||
expect(
|
||||
userWorkspaces.data?.activeUser?.workspaces.items
|
||||
.map((i) => i.name)
|
||||
.includes(name)
|
||||
).to.be.false
|
||||
})
|
||||
it('stops the last workspace admin from leaving the workspace', async () => {
|
||||
const name = cryptoRandomString({ length: 6 })
|
||||
const workspaceCreateResult = await apollo.execute(CreateWorkspaceDocument, {
|
||||
input: { name }
|
||||
})
|
||||
|
||||
const id = workspaceCreateResult.data?.workspaceMutations.create.id
|
||||
if (!id) throw new Error('This should have succeeded')
|
||||
|
||||
const leaveResult = await apollo.execute(ActiveUserLeaveWorkspaceDocument, {
|
||||
id
|
||||
})
|
||||
|
||||
expect(leaveResult.errors?.length).to.be.greaterThanOrEqual(1)
|
||||
|
||||
const userWorkspaces = await apollo.execute(
|
||||
GetActiveUserWorkspacesDocument,
|
||||
{}
|
||||
)
|
||||
expect(
|
||||
userWorkspaces.data?.activeUser?.workspaces.items
|
||||
.map((i) => i.name)
|
||||
.includes(name)
|
||||
).to.be.true
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user