70 lines
5.8 KiB
TypeScript
70 lines
5.8 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { approvalBlockers, page, PlanInput, request, session, validateInput } from './api';
|
|
afterEach(() => vi.unstubAllGlobals());
|
|
describe('JSON contract boundary', () => {
|
|
it('refuses missing simulation identity metadata', () => {
|
|
expect(() => session({ principal: { id: 'a', display_name: 'name' }, tenants: [], csrf_token: 'csrf' })).toThrow('Sitzungsprofil');
|
|
expect(session({ principal: { id: 'sim', display_name: 'SIMULIERT', simulated: true }, tenants: [], csrf_token: 'csrf' }).principal.simulated).toBe(true);
|
|
});
|
|
it('does not present absent completeness as a complete result', () => {
|
|
expect(page({ items: [], next_cursor: null }).completeness).toBe('unknown');
|
|
expect(page({ items: [], next_cursor: null }).truncated).toBe(true);
|
|
expect(() => page({ devices: [] })).toThrow('items');
|
|
});
|
|
const preview = { asset_id: 'asset', job_payload_hash: 'hash', payload: { action: 'refresh_inventory', asset_id: 'asset' } };
|
|
const plan = { plan_id: 'p', plan_hash: 'hash', expires_at: new Date(Date.now() + 60000).toISOString(), resolved_targets: [preview], excluded_targets: [], plan_payload: { targets: [preview] } };
|
|
it('accepts immutable target previews without execution job or attempt IDs', () => {
|
|
expect(approvalBlockers(plan)).toEqual([]);
|
|
expect(approvalBlockers({ ...plan, resolved_targets: [{ ...preview, plan_target_id: 'target' }] })).toEqual([]);
|
|
expect(approvalBlockers({ ...plan, resolved_targets: ['asset'], payloads: [preview] })).toEqual([]);
|
|
expect(approvalBlockers({ ...plan, resolved_targets: ['asset'], jobs: [preview] })).toEqual([]);
|
|
expect(approvalBlockers({ ...plan, resolved_targets: ['asset'], jobs: [{ ...preview, job_id: 'reserved', attempt_id: 'reserved-attempt' }] })).toEqual([]);
|
|
expect(approvalBlockers({ ...plan, resolved_targets: ['asset'], jobs: [{ job_payload_hash: 'hash', payload: preview.payload }] })).toEqual([]);
|
|
});
|
|
it('fails closed for missing, partial, malformed or contradictory previews', () => {
|
|
expect(approvalBlockers({ plan_id: 'p', plan_hash: 'hash' })).toContain('Unveränderliche Zielpreviews fehlen');
|
|
for (const value of [
|
|
{ ...plan, resolved_targets: [{ ...preview, job_payload_hash: '' }] },
|
|
{ ...plan, resolved_targets: [{ ...preview, payload: {} }] },
|
|
{ ...plan, resolved_targets: [{ ...preview, payload: null }] },
|
|
{ ...plan, resolved_targets: [{ ...preview, asset_id: '' }] },
|
|
{ ...plan, resolved_targets: [{ ...preview, payload: { asset_id: 'foreign' } }] },
|
|
{ ...plan, resolved_targets: ['asset', 'missing'], payloads: [preview] },
|
|
{ ...plan, resolved_targets: ['foreign'], payloads: [preview] },
|
|
{ ...plan, resolved_targets: ['asset'], payloads: [null] },
|
|
{ ...plan, resolved_targets: [null], payloads: [preview] },
|
|
{ ...plan, excluded_targets: ['missing'] },
|
|
{ ...plan, resolved_targets: ['asset'], payloads: 'invalid', jobs: [preview] },
|
|
{ ...plan, resolved_targets: [{ ...preview, payload: null }], jobs: [preview] },
|
|
]) expect(approvalBlockers(value).length).toBeGreaterThan(0);
|
|
});
|
|
it('rejects duplicate assets in both fixed targets and preview lists', () => {
|
|
expect(approvalBlockers({ ...plan, resolved_targets: [preview, preview] })).toContain('Doppelte asset_id in fester Zielmenge');
|
|
expect(approvalBlockers({ ...plan, resolved_targets: ['asset'], payloads: [preview, preview] })).toContain('Doppelte asset_id in Zielpreviews');
|
|
expect(approvalBlockers({ ...plan, resolved_targets: ['asset', 'asset'], payloads: [preview] })).toContain('Doppelte asset_id in fester Zielmenge');
|
|
});
|
|
it('preserves script UTF-8 bytes and refuses duplicate/dynamic targets', () => {
|
|
const variant = { script_utf8: 'printf hi\r\n', os_family: 'linux', encoding: 'utf-8', interpreter: 'posix_sh' };
|
|
const input: PlanInput = { tenant_id: 't', action: 'run_script', targets: { asset_ids: ['asset'] }, parameters: { variants: [variant], execution_identity: 'agent_service', timeout_seconds: 60, max_output_bytes: 4096 }, schedule: { not_before: new Date().toISOString(), start_before: new Date(Date.now() + 60000).toISOString(), offline_policy: 'wait_until_deadline' }, rollout: { max_parallel: 1, halt_after_failures: 1 }, reason: 'test' };
|
|
validateInput(input); expect(input.parameters.variants).toEqual([variant]);
|
|
expect(() => validateInput({ ...input, targets: { asset_ids: ['a', 'a'] } })).toThrow('eindeutige');
|
|
});
|
|
it('accepts explicit existing dev simulation evidence but not a principal-name guess', () => {
|
|
expect(session({ principal: 'sim-test', tenant_id: 't', testsimulation: true, csrf_token: 'csrf' }).principal.simulated).toBe(true);
|
|
expect(() => session({ principal: 'sim-test', tenant_id: 't', csrf_token: 'csrf' })).toThrow();
|
|
});
|
|
});
|
|
describe('same-origin cookie transport', () => {
|
|
it('sends CSRF and same-origin credentials, never bearer', async () => {
|
|
const fetch = vi.fn().mockResolvedValue(new Response('{}', { status: 200 })); vi.stubGlobal('fetch', fetch);
|
|
await request('/api/v1/test', { csrf: 'csrf', body: { plan_hash: 'exact' } });
|
|
expect(fetch).toHaveBeenCalledWith('/api/v1/test', expect.objectContaining({ method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': 'csrf' }, body: '{"plan_hash":"exact"}' }));
|
|
await expect(request('//external.invalid')).rejects.toThrow('gleiche Origin');
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
});
|
|
it('retains backend errors and correlation rather than returning fake success', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ code: 'POLICY_DENIED', message: 'denied', correlation_id: 'correlation' }), { status: 403 })));
|
|
await expect(request('/api/v1/test')).rejects.toThrow('POLICY_DENIED: denied (Korrelation correlation)');
|
|
});
|
|
});
|