feat(workspaces): add repository function implementations

Co-authored-by: Charles Driesler <chuck@speckle.systems>
This commit is contained in:
Gergő Jedlicska
2024-07-04 14:46:48 +02:00
committed by GitHub
parent 8ba0b09f45
commit 28ce7c757c
10 changed files with 201 additions and 27 deletions
@@ -0,0 +1,94 @@
import {
getWorkspaceFactory,
upsertWorkspaceFactory,
upsertWorkspaceRoleFactory
} from '@/modules/workspaces/repositories/workspaces'
import db from '@/db/knex'
import cryptoRandomString from 'crypto-random-string'
import { expect } from 'chai'
import { Workspace, WorkspaceAcl } from '@/modules/workspaces/domain/types'
import { expectToThrow } from '@/test/assertionHelper'
const getWorkspace = getWorkspaceFactory({ db })
const upsertWorkspace = upsertWorkspaceFactory({ db })
const upsertWorkspaceRole = upsertWorkspaceRoleFactory({ db })
const createAndStoreTestWorkspace = async (): Promise<Workspace> => {
const workspace: Workspace = {
id: cryptoRandomString({ length: 10 }),
name: cryptoRandomString({ length: 10 }),
createdAt: new Date(),
updatedAt: new Date(),
description: null,
logoUrl: null
}
await upsertWorkspace({ workspace })
return workspace
}
describe('Workspace repositories', () => {
describe('getWorkspaceFactory creates a function, that', () => {
it('returns null if the workspace is not found', async () => {
const workspace = await getWorkspace({
workspaceId: cryptoRandomString({ length: 10 })
})
expect(workspace).to.be.null
})
// not testing get here, we're going to use that for testing upsert
})
describe('upsertWorkspaceFactory creates a function, that', () => {
it('upserts the workspace', async () => {
const testWorkspace = await createAndStoreTestWorkspace()
const storedWorkspace = await getWorkspace({ workspaceId: testWorkspace.id })
expect(storedWorkspace).to.deep.equal(testWorkspace)
const modifiedTestWorkspace: Workspace = {
...testWorkspace,
description: 'now im adding a description to the workspace'
}
await upsertWorkspace({ workspace: modifiedTestWorkspace })
const modifiedStoredWorkspace = await getWorkspace({
workspaceId: testWorkspace.id
})
expect(modifiedStoredWorkspace).to.deep.equal(modifiedTestWorkspace)
})
it('updates only relevant workspace fields', async () => {
const testWorkspace = await createAndStoreTestWorkspace()
const storedWorkspace = await getWorkspace({ workspaceId: testWorkspace.id })
expect(storedWorkspace).to.deep.equal(testWorkspace)
await upsertWorkspace({
workspace: {
...testWorkspace,
id: cryptoRandomString({ length: 13 }),
createdAt: new Date()
}
})
const modifiedStoredWorkspace = await getWorkspace({
workspaceId: testWorkspace.id
})
expect(modifiedStoredWorkspace).to.deep.equal(testWorkspace)
})
})
describe('upsertWorkspaceRoleFactory creates a function, that', () => {
it('throws if an unknown role is provided', async () => {
const role: WorkspaceAcl = {
// @ts-expect-error type asserts valid values for `role`
role: 'fake-role',
userId: '',
workspaceId: ''
}
await expectToThrow(() => upsertWorkspaceRole(role))
})
})
})
@@ -4,12 +4,12 @@ import { Roles } from '@speckle/shared'
import { expect } from 'chai'
import cryptoRandomString from 'crypto-random-string'
describe('Workspace creation', () => {
describe('Workspace services', () => {
describe('createWorkspaceFactory creates a function, that', () => {
it('stores the workspace', async () => {
const storedWorkspaces: Workspace[] = []
const createWorkspace = createWorkspaceFactory({
storeWorkspace: async ({ workspace }: { workspace: Workspace }) => {
upsertWorkspace: async ({ workspace }: { workspace: Workspace }) => {
storedWorkspaces.push(workspace)
},
upsertWorkspaceRole: async () => {},
@@ -32,7 +32,7 @@ describe('Workspace creation', () => {
it('makes the workspace creator becomes a workspace:admin', async () => {
const storedRole: WorkspaceAcl[] = []
const createWorkspace = createWorkspaceFactory({
storeWorkspace: async () => {},
upsertWorkspace: async () => {},
upsertWorkspaceRole: async (workspaceAcl: WorkspaceAcl) => {
storedRole.push(workspaceAcl)
},
@@ -64,7 +64,7 @@ describe('Workspace creation', () => {
payload: {}
}
const createWorkspace = createWorkspaceFactory({
storeWorkspace: async () => {},
upsertWorkspace: async () => {},
upsertWorkspaceRole: async () => {},
emitWorkspaceEvent: async ({ event, payload }) => {
eventData.isCalled = true