Files
simstudioai--sim/apps/sim/app/api/v1/audit-logs/query.test.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

173 lines
5.2 KiB
TypeScript

/**
* @vitest-environment node
*
* Tests for the enterprise audit-log tenant boundary. The global drizzle-orm
* mock returns structured operator objects, so these tests assert directly on
* the predicate tree.
*/
import { dbChainMock, dbChainMockFns } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('@sim/db', () => dbChainMock)
import { buildOrgScopeCondition, getOrgWorkspaceIds } from '@/app/api/v1/audit-logs/query'
const ORG_ID = 'org-1'
const MEMBER_IDS = ['user-1', 'user-2']
const WORKSPACE_IDS = ['ws-1', 'ws-2']
interface MockCondition {
type?: string
conditions?: MockCondition[]
column?: string
values?: string[]
left?: string
right?: string
strings?: string[]
}
function asCondition(value: unknown): MockCondition {
return value as MockCondition
}
/**
* Asserts the condition matches null-workspace rows tied to the organization
* via metadata or the organization resource itself.
*/
function expectOrgLevelCondition(condition: MockCondition, organizationId: string): void {
expect(condition.type).toBe('and')
const [nullCheck, orgLink] = condition.conditions!
expect(nullCheck).toMatchObject({ type: 'isNull', column: 'workspaceId' })
expect(orgLink.type).toBe('or')
const [metadataMatch, orgResourceMatch] = orgLink.conditions!
expect(metadataMatch.strings?.join('?')).toContain("->>'organizationId' =")
expect(metadataMatch.values).toContain(organizationId)
expect(orgResourceMatch.type).toBe('and')
expect(orgResourceMatch.conditions).toEqual([
expect.objectContaining({ type: 'eq', left: 'resourceType', right: 'organization' }),
expect.objectContaining({ type: 'eq', left: 'resourceId', right: organizationId }),
])
}
describe('buildOrgScopeCondition', () => {
it('never uses actor membership as a standalone boundary (default scope)', () => {
const condition = asCondition(
buildOrgScopeCondition({
organizationId: ORG_ID,
orgWorkspaceIds: WORKSPACE_IDS,
orgMemberIds: MEMBER_IDS,
includeDeparted: false,
})
)
expect(condition.type).toBe('and')
const [orgScope, actorFilter] = condition.conditions!
expect(orgScope.type).toBe('or')
const [workspaceScope, orgLevel] = orgScope.conditions!
expect(workspaceScope).toMatchObject({
type: 'inArray',
column: 'workspaceId',
values: WORKSPACE_IDS,
})
expectOrgLevelCondition(orgLevel, ORG_ID)
expect(actorFilter).toMatchObject({
type: 'or',
conditions: [
expect.objectContaining({ type: 'inArray', column: 'actorId', values: MEMBER_IDS }),
expect.objectContaining({ type: 'isNull', column: 'actorId' }),
],
})
})
it('omits the actor filter entirely when includeDeparted is true', () => {
const condition = asCondition(
buildOrgScopeCondition({
organizationId: ORG_ID,
orgWorkspaceIds: WORKSPACE_IDS,
orgMemberIds: MEMBER_IDS,
includeDeparted: true,
})
)
expect(condition.type).toBe('or')
const [workspaceScope, orgLevel] = condition.conditions!
expect(workspaceScope).toMatchObject({
type: 'inArray',
column: 'workspaceId',
values: WORKSPACE_IDS,
})
expectOrgLevelCondition(orgLevel, ORG_ID)
expect(JSON.stringify(condition)).not.toContain('actorId')
})
it('falls back to the org-level branch alone when the org has no workspaces', () => {
const condition = asCondition(
buildOrgScopeCondition({
organizationId: ORG_ID,
orgWorkspaceIds: [],
orgMemberIds: MEMBER_IDS,
includeDeparted: true,
})
)
expectOrgLevelCondition(condition, ORG_ID)
})
it('still applies the actor filter on top of the org scope with no workspaces', () => {
const condition = asCondition(
buildOrgScopeCondition({
organizationId: ORG_ID,
orgWorkspaceIds: [],
orgMemberIds: MEMBER_IDS,
includeDeparted: false,
})
)
expect(condition.type).toBe('and')
const [orgLevel, actorFilter] = condition.conditions!
expectOrgLevelCondition(orgLevel, ORG_ID)
expect(actorFilter).toMatchObject({
type: 'or',
conditions: [
expect.objectContaining({ type: 'inArray', column: 'actorId', values: MEMBER_IDS }),
expect.objectContaining({ type: 'isNull', column: 'actorId' }),
],
})
})
it('only matches system events when the org has no current members', () => {
const condition = asCondition(
buildOrgScopeCondition({
organizationId: ORG_ID,
orgWorkspaceIds: WORKSPACE_IDS,
orgMemberIds: [],
includeDeparted: false,
})
)
expect(condition.type).toBe('and')
const [, actorFilter] = condition.conditions!
expect(actorFilter).toMatchObject({ type: 'isNull', column: 'actorId' })
})
})
describe('getOrgWorkspaceIds', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('selects workspaces by organization ownership, not member ownership', async () => {
const ids = await getOrgWorkspaceIds(ORG_ID)
expect(ids).toEqual([])
expect(dbChainMockFns.where).toHaveBeenCalledWith(
expect.objectContaining({ type: 'eq', left: 'organizationId', right: ORG_ID })
)
})
})