import { Test } from '@nestjs/testing'; import { RuntimeReadController, RuntimeReadAccess, } from './runtime-read.controller'; import { HermesIntegrationService } from './hermes-integration.service'; import { HermesError } from './hermes.adapter'; import { ForbiddenException, ServiceUnavailableException, } from '@nestjs/common'; describe('runtime read boundary', () => { const health = jest.fn(); const profiles = jest.fn(); const status = jest.fn(); const service = { health, profiles, status, } as unknown as HermesIntegrationService; const access = new RuntimeReadAccess({ 'admin-1': ['default'] }); const controller = new RuntimeReadController(service, access); beforeEach(() => jest.clearAllMocks()); it('denies unconfigured identity without IO', async () => { await expect( controller.health({ user: { id: 'other', role: 'Admin' } }), ).rejects.toBeInstanceOf(ForbiddenException); expect(health).not.toHaveBeenCalled(); }); it('uses server scope not body or user supplied profiles', async () => { status.mockResolvedValue({ overall: 'ok' }); await expect( controller.status( { user: { id: 'admin-1', role: 'Admin', profiles: ['other'] } }, { profile: 'other' }, ), ).rejects.toBeInstanceOf(ForbiddenException); expect(status).not.toHaveBeenCalled(); await controller.status( { user: { id: 'admin-1', role: 'Admin' } }, { profile: 'default' }, ); expect(status).toHaveBeenCalledWith( { subject: 'admin-1', profiles: ['default'] }, 'default', ); }); it('rejects duplicate/missing/unknown query parameters before IO', async () => { for (const q of [ {}, { profile: ['default', 'other'] }, { profile: 'default', token: 'leak' }, ]) await expect( controller.status({ user: { id: 'admin-1', role: 'Admin' } }, q), ).rejects.toBeInstanceOf(ForbiddenException); expect(status).not.toHaveBeenCalled(); }); it('maps credential failure without leaking upstream errors', async () => { health.mockRejectedValue(new HermesError('AUTH_REQUIRED')); await expect( controller.health({ user: { id: 'admin-1', role: 'Admin' } }), ).rejects.toThrow('Runtime unavailable'); }); it('rejects API keys and non-admins even when subject is configured', async () => { for (const user of [ { id: 'admin-1', role: 'User' }, { id: 'admin-1', role: 'Admin', isApiKey: true }, ]) await expect(controller.health({ user })).rejects.toBeInstanceOf( ForbiddenException, ); expect(health).not.toHaveBeenCalled(); }); });