chore: import upstream snapshot with attribution
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
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
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { mockCheckWorkspaceAccess, dbState } = vi.hoisted(() => ({
|
||||
mockCheckWorkspaceAccess: vi.fn(),
|
||||
dbState: { results: [] as any[][] },
|
||||
}))
|
||||
|
||||
function makeChain() {
|
||||
const chain: any = {}
|
||||
chain.from = vi.fn(() => chain)
|
||||
chain.where = vi.fn(() => chain)
|
||||
chain.limit = vi.fn(() => Promise.resolve(dbState.results.shift() ?? []))
|
||||
return chain
|
||||
}
|
||||
|
||||
vi.mock('@sim/db', () => ({
|
||||
db: { select: vi.fn(() => makeChain()) },
|
||||
}))
|
||||
|
||||
vi.mock('@sim/db/schema', () => ({
|
||||
credentialTypeEnum: {
|
||||
enumValues: ['oauth', 'env_workspace', 'env_personal', 'service_account'],
|
||||
},
|
||||
credential: {
|
||||
id: 'credential.id',
|
||||
workspaceId: 'credential.workspaceId',
|
||||
type: 'credential.type',
|
||||
},
|
||||
credentialMember: {
|
||||
credentialId: 'credentialMember.credentialId',
|
||||
userId: 'credentialMember.userId',
|
||||
status: 'credentialMember.status',
|
||||
role: 'credentialMember.role',
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('drizzle-orm', () => ({
|
||||
and: vi.fn((...args: unknown[]) => ({ and: args })),
|
||||
eq: vi.fn((a: unknown, b: unknown) => ({ eq: [a, b] })),
|
||||
inArray: vi.fn((a: unknown, b: unknown) => ({ inArray: [a, b] })),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/workspaces/permissions/utils', () => ({
|
||||
checkWorkspaceAccess: mockCheckWorkspaceAccess,
|
||||
}))
|
||||
|
||||
import { getCredentialActorContext } from '@/lib/credentials/access'
|
||||
|
||||
const workspaceAdminAccess = { hasAccess: true, canWrite: true, canAdmin: true }
|
||||
const noWorkspaceAccess = { hasAccess: false, canWrite: false, canAdmin: false }
|
||||
|
||||
describe('getCredentialActorContext', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
dbState.results = []
|
||||
})
|
||||
|
||||
it('treats an explicit credential admin membership as admin', async () => {
|
||||
dbState.results = [[{ id: 'c1', workspaceId: 'ws', type: 'oauth' }], [{ role: 'admin' }]]
|
||||
mockCheckWorkspaceAccess.mockResolvedValue({ hasAccess: true, canWrite: true, canAdmin: false })
|
||||
|
||||
const ctx = await getCredentialActorContext('c1', 'user1')
|
||||
|
||||
expect(ctx.isAdmin).toBe(true)
|
||||
})
|
||||
|
||||
it('derives credential admin from workspace admin for shared credentials', async () => {
|
||||
dbState.results = [[{ id: 'c1', workspaceId: 'ws', type: 'oauth' }], []]
|
||||
mockCheckWorkspaceAccess.mockResolvedValue(workspaceAdminAccess)
|
||||
|
||||
const ctx = await getCredentialActorContext('c1', 'admin-user')
|
||||
|
||||
expect(ctx.isAdmin).toBe(true)
|
||||
})
|
||||
|
||||
it('does not derive credential admin on personal env credentials', async () => {
|
||||
dbState.results = [[{ id: 'c1', workspaceId: 'ws', type: 'env_personal' }], []]
|
||||
mockCheckWorkspaceAccess.mockResolvedValue(workspaceAdminAccess)
|
||||
|
||||
const ctx = await getCredentialActorContext('c1', 'admin-user')
|
||||
|
||||
expect(ctx.isAdmin).toBe(false)
|
||||
})
|
||||
|
||||
it('is not admin for a non-admin without membership', async () => {
|
||||
dbState.results = [[{ id: 'c1', workspaceId: 'ws', type: 'oauth' }], []]
|
||||
mockCheckWorkspaceAccess.mockResolvedValue({
|
||||
hasAccess: true,
|
||||
canWrite: false,
|
||||
canAdmin: false,
|
||||
})
|
||||
|
||||
const ctx = await getCredentialActorContext('c1', 'reader-user')
|
||||
|
||||
expect(ctx.isAdmin).toBe(false)
|
||||
})
|
||||
|
||||
it('returns empty context when the credential does not exist', async () => {
|
||||
dbState.results = [[]]
|
||||
|
||||
const ctx = await getCredentialActorContext('missing', 'user1')
|
||||
|
||||
expect(ctx.credential).toBeNull()
|
||||
expect(ctx.isAdmin).toBe(false)
|
||||
expect(mockCheckWorkspaceAccess).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('exposes workspace access flags from checkWorkspaceAccess', async () => {
|
||||
dbState.results = [[{ id: 'c1', workspaceId: 'ws', type: 'oauth' }], []]
|
||||
mockCheckWorkspaceAccess.mockResolvedValue(noWorkspaceAccess)
|
||||
|
||||
const ctx = await getCredentialActorContext('c1', 'outsider')
|
||||
|
||||
expect(ctx.hasWorkspaceAccess).toBe(false)
|
||||
expect(ctx.canWriteWorkspace).toBe(false)
|
||||
expect(ctx.isAdmin).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user