Files
speckle-server/packages/server/modules/shared/test/unit/eventBus.spec.ts
T
Gergő Jedlicska 08e941f8af Poor man's SSO (#2641)
* Implemented workspace general page

* Added notifications to user input

* Allowed non-admins to view but not edit

* Added redirect to homeroute

* Fixed validation

* Squashed commit of the following:

commit 7bf14ab8af0f76b4c9d0aa87fc08085af7c34959
Author: Chuck Driesler <chuck@speckle.systems>
Date:   Tue Aug 6 19:40:50 2024 +0200

    mob next [ci-skip] [ci skip] [skip ci]

    lastFile:packages/server/modules/workspacesCore/migrations/20240806160740_workspace_domains.ts

commit 8aa3fb0cb052c10eeeb83bf9874ae0d1c065e480
Author: Alessandro Magionami <alessandro.magionami@gmail.com>
Date:   Tue Aug 6 18:54:15 2024 +0200

    mob next [ci-skip] [ci skip] [skip ci]

    lastFile:packages/server/modules/core/domain/userEmails/operations.ts

commit 66dfd0cf6c15a789c8f96a65a3168323e83a7b9e
Author: Chuck Driesler <chuck@speckle.systems>
Date:   Tue Aug 6 18:30:22 2024 +0200

    mob next [ci-skip] [ci skip] [skip ci]

    lastFile:packages/server/modules/workspacesCore/domain/types.ts

Co-authored-by: Alessandro Magionami <alessandro.magionami@gmail.com>

* Move General to workspaces folder

* feat(workspaces): inputs on security section

* feat(workspaces): add domain to workspace mutation

* chore(workspaces): add blocked domains list

* fix(workspaces): modals with buttons

* feat(workspaceDomains): delete domain

* fix(workspaces): use  mutation

* fix(workspaces): present user verified domains as options

* Moved sidebar menu to a composable

* Added coming soon tag back

* feat(workspaces): create domains resolver for workspace

* chore(workspaces): fix tests

* chore(workspaces): fix types

* chore(workspaces): fix linter

* fix(workspaces): do some delete I think

* chore(workspaces): add domainBasedMembershipProtectionEnabled field to workspace

* chore(workspaces): improve validation for email domain

* fix(workspace): query and do the thing

* chore(workspaces): add graphql schema for domainBasedMembershipProtection

* chore(workspaces): lint and test failures

* fix(workspaces): test issues w new field

* feat(workspaces): add discoverability flag

* chore(workspaces): they made me do it

* feat(workspaces): enable toggling domain protection

* feat(workspaces): add discoverability toggle to workspace settings

* feat(workspace): auto enable discoverability on first domain registration

* feat(workspace): discoverability toggle fixes

* fix(eventBus): fix tests

* feat(workspaces): user discoverable workspaces (#2620)

* feat(workspaces): it works just trust me

* fix(workspaces): don't worry about it

* fix(workspaces); happy path success

* fix(workspaces): almost there

* fix(workspaces): successful tests!

* fix(workspaces): we have DISCOVERED (#2621)

* Fixed linting issue

* Updated query

* Updated validation rules

* Updated validation rules

* Fix unsaved file with type export

* Addressed PR comments

* Updated cache

* Updated item classes, add fragment back

* Gergo/web 1574 join workspaces via discovery (#2623)

* chore(useremails): add find verified emails by user function

* chore(workspace): table helper for workspace domains

* chore(workspace): get workspace with domains function

* chore(workspace): test get workspace with domains function

* feat(workspace): restrict workspace membership when updating workspace role

* chore(workspaces): fix types

* feat(workspaces): WIP join

* feat(workspaces): join button makes u join

* chore(useremails): fix type for find verified emails function

* feat(workspaces): join

* feat(workspace): prevent inviting user without email matching domain

* chore(workspaces): fix linter

* fix(workspaces): invoke join (gergo wrote this)

* fuck

* fix(workspaces): properly get discoverable workspaces

* fix(workspaces): test

---------

Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
Co-authored-by: Chuck Driesler <chuck@speckle.systems>

* fix(workspaces): some query stuff

* fix(workspaces): mutate cache instead of refetch

* fix(workspaces): more adjustments to gql query and fragment structure

* fix(workspaces): queries, style, structure

* fix(workspaces): match discoverability with current styles

* chore(workspaces): lint lint lint

* fix(workspaces): got it twisted

* chore(workspaces): fix test

* fix(workspaces): route to joined workspace on join

---------

Co-authored-by: Mike Tasset <mike.tasset@gmail.com>
Co-authored-by: Chuck Driesler <chuck@speckle.systems>
Co-authored-by: Alessandro Magionami <alessandro.magionami@gmail.com>
2024-08-26 13:33:16 +02:00

196 lines
6.0 KiB
TypeScript

import { getEventBus, initializeEventBus } from '@/modules/shared/services/eventBus'
import { WorkspaceEvents } from '@/modules/workspacesCore/domain/events'
import { Workspace } from '@/modules/workspacesCore/domain/types'
import { Roles } from '@speckle/shared'
import { expect } from 'chai'
import cryptoRandomString from 'crypto-random-string'
const createFakeWorkspace = (): Omit<Workspace, 'domains'> => {
return {
id: cryptoRandomString({ length: 10 }),
description: cryptoRandomString({ length: 10 }),
logo: null,
defaultLogoIndex: 0,
name: cryptoRandomString({ length: 10 }),
updatedAt: new Date(),
createdAt: new Date(),
domainBasedMembershipProtectionEnabled: false,
discoverabilityEnabled: false
}
}
describe('Event Bus', () => {
describe('initializeEventBus creates an event bus instance, that', () => {
it('calls back all the listeners', async () => {
const testEventBus = initializeEventBus()
const eventNames: string[] = []
testEventBus.listen('test.string', ({ eventName }) => {
eventNames.push(eventName)
})
testEventBus.listen('test.string', ({ eventName }) => {
eventNames.push(eventName)
})
await testEventBus.emit({ eventName: 'test.number', payload: 1 })
expect(eventNames.length).to.equal(0)
const eventName = 'test.string' as const
await testEventBus.emit({ eventName, payload: 'fake event' })
expect(eventNames.length).to.equal(2)
expect(eventNames).to.deep.equal([eventName, eventName])
})
it('can removes listeners from itself', async () => {
const testEventBus = initializeEventBus()
const eventNumbers: number[] = []
testEventBus.listen('test.string', () => {
eventNumbers.push(1)
})
const listenerOff = testEventBus.listen('test.string', () => {
eventNumbers.push(2)
})
await testEventBus.emit({ eventName: 'test.string', payload: 'fake event' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 2])
listenerOff()
await testEventBus.emit({ eventName: 'test.string', payload: 'fake event' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 1, 2])
})
it('returns results from listeners to the emitter', async () => {
const testEventBus = initializeEventBus()
testEventBus.listen('test.string', ({ payload }) => ({
outcome: payload
}))
const lookWhatHappened = 'echo this back to me'
const results = await testEventBus.emit({
eventName: 'test.string',
payload: lookWhatHappened
})
expect(results.length).to.equal(1)
expect(results[0]).to.deep.equal({ outcome: lookWhatHappened })
})
it('bubbles up listener exceptions to emitter', async () => {
const testEventBus = initializeEventBus()
testEventBus.listen('test.string', ({ payload }) => {
throw new Error(payload)
})
const lookWhatHappened = 'kabumm'
try {
await testEventBus.emit({ eventName: 'test.string', payload: lookWhatHappened })
throw new Error('this should have thrown by now')
} catch (error) {
if (error instanceof Error) {
expect(error.message).to.equal(lookWhatHappened)
} else {
throw error
}
}
})
it('can be destroyed, removing all listeners', async () => {
const testEventBus = initializeEventBus()
const eventNumbers: number[] = []
testEventBus.listen('test.string', () => {
eventNumbers.push(1)
})
testEventBus.listen('test.string', () => {
eventNumbers.push(2)
})
await testEventBus.emit({ eventName: 'test.string', payload: 'test' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 2])
testEventBus.destroy()
await testEventBus.emit({ eventName: 'test.string', payload: 'test' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 2])
})
})
describe('getEventBus', () => {
it('returns a unified event bus instance', async () => {
const bus1 = getEventBus()
const bus2 = getEventBus()
const workspaces: Workspace[] = []
bus1.listen(WorkspaceEvents.Created, ({ payload }) => {
workspaces.push(payload)
})
bus2.listen(WorkspaceEvents.Created, ({ payload }) => {
workspaces.push(payload)
})
const workspacePayload = {
...createFakeWorkspace(),
createdByUserId: cryptoRandomString({ length: 10 }),
eventName: WorkspaceEvents.Created,
domains: []
}
await bus1.emit({
eventName: WorkspaceEvents.Created,
payload: { ...workspacePayload }
})
expect(workspaces.length).to.equal(2)
expect(workspaces).to.deep.equal([workspacePayload, workspacePayload])
})
it('allows to subscribe to wildcard events', async () => {
const eventBus = getEventBus()
const events: string[] = []
eventBus.listen('workspace.*', ({ payload, eventName }) => {
switch (eventName) {
case 'workspace.created':
events.push(payload.id)
break
case 'workspace.role-deleted':
events.push(payload.userId)
break
default:
events.push('default')
}
})
const workspace = createFakeWorkspace()
await eventBus.emit({
eventName: WorkspaceEvents.Created,
payload: {
...workspace,
createdByUserId: cryptoRandomString({ length: 10 })
}
})
const workspaceAcl = {
userId: cryptoRandomString({ length: 10 }),
workspaceId: cryptoRandomString({ length: 10 }),
role: Roles.Workspace.Member
}
await eventBus.emit({
eventName: WorkspaceEvents.RoleDeleted,
payload: workspaceAcl
})
await eventBus.emit({
eventName: WorkspaceEvents.RoleUpdated,
payload: workspaceAcl
})
expect([workspace.id, workspaceAcl.userId, 'default']).to.deep.equal(events)
})
})
})