import { describe, expect, it } from 'vitest'; import { OpenFgaRelationships, isProjectAccessAllowed, openFgaFromEnv, type ProjectRelationshipScope, } from '../../packages/siax-access/src/index'; const STORE = '01J8ZQ6K3M9V4T7R2X5C8B1N0A'; const MODEL = '01J8ZQ6K3M9V4T7R2X5C8B1N0B'; const TOKEN = 'test-service-token'; const scope: ProjectRelationshipScope = { tenantId: 't_0pt', projectId: 'prj_alpha', actorId: 'user_123', }; function client(handler: (url: string, init?: RequestInit) => Response) { return new OpenFgaRelationships({ baseUrl: 'https://fga.siax.io', storeId: STORE, modelId: MODEL, serviceToken: TOKEN, fetchImpl: (async (url: string | URL | Request, init?: RequestInit) => handler(String(url), init)) as typeof fetch, }); } function tupleResponse(allowed: boolean): Response { return new Response(JSON.stringify({ allowed }), { status: 200 }); } describe('@siax/access OpenFgaRelationships', () => { it('refuses non-https configuration and non-ULID ids', () => { expect( () => new OpenFgaRelationships({ baseUrl: 'http://fga.siax.io', storeId: STORE, modelId: MODEL, serviceToken: TOKEN, }), ).toThrow(); expect( () => new OpenFgaRelationships({ baseUrl: 'https://fga.siax.io', storeId: 'not-a-ulid', modelId: MODEL, serviceToken: TOKEN, }), ).toThrow(); }); it('requires both the tenant membership and the project parent tuple', async () => { const seen: string[] = []; const c = client((_url, init) => { const body = JSON.parse(String(init?.body)) as { tuple_key: { relation: string }; }; seen.push(body.tuple_key.relation); return tupleResponse(true); }); await expect(c.checkProject(scope)).resolves.toBe('relationships_match'); expect(seen).toEqual(['member', 'tenant']); }); it('denies the project when membership is denied even if the parent tuple allows', async () => { let call = 0; const c = client(() => tupleResponse(call++ === 1)); await expect(c.checkProject(scope)).resolves.toBe('denied'); }); it('reports unavailable (never allowed) when OpenFGA fails', async () => { const c = client(() => new Response('boom', { status: 503 })); await expect(c.checkProject(scope)).resolves.toBe('unavailable'); await expect(isProjectAccessAllowed(scope, { relationships: c })).resolves.toBe(false); }); it('rejects malformed scope values before any request', async () => { let called = false; const c = client(() => { called = true; return tupleResponse(true); }); await expect(c.checkProject({ tenantId: 't/../x', projectId: 'p', actorId: 'a' })).resolves.toBe( 'denied', ); expect(called).toBe(false); }); it('fails closed when OpenFGA is not configured', async () => { await expect(isProjectAccessAllowed(scope, { relationships: undefined })).resolves.toBe(false); expect(openFgaFromEnv({} as NodeJS.ProcessEnv)).toBeUndefined(); }); it('applies the additional policy only after a relationship match', async () => { const c = client(() => tupleResponse(true)); await expect( isProjectAccessAllowed(scope, { relationships: c, additionalPolicy: () => false, }), ).resolves.toBe(false); await expect( isProjectAccessAllowed(scope, { relationships: c, additionalPolicy: () => { throw new Error('policy store down'); }, }), ).resolves.toBe(false); }); });