Repository navigation
Expand file tree
/
Copy pathgate.report.test.ts
More file actions
73 lines (61 loc) · 2.92 KB
/
Copy pathgate.report.test.ts
File metadata and controls
73 lines (61 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { createHmac } from 'node:crypto';
import { reportAccess, reportGateMode, unlockRefusal, unlockToken } from './gate';
const KEYS = ['AEO_GATE_MODE', 'AEO_REPORT_GATE', 'AEO_UNLOCK_COOKIE_SECRET', 'SUPABASE_SERVICE_ROLE_KEY'];
let log: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
for (const k of KEYS) delete process.env[k];
process.env.SUPABASE_SERVICE_ROLE_KEY = 'service-role-key';
log = vi.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => { log.mockRestore(); for (const k of KEYS) delete process.env[k]; });
describe('report gate: on by default, separate from the run gate', () => {
it('is enforce with nothing set, even when the run gate is off', () => {
expect(reportGateMode()).toBe('enforce');
expect(reportAccess('audit-1', undefined)).toBe('preview');
});
it('can be switched off or to observe on its own', () => {
process.env.AEO_REPORT_GATE = 'off';
expect(reportAccess('audit-1', undefined)).toBe('full');
process.env.AEO_REPORT_GATE = 'observe';
expect(reportAccess('audit-1', undefined)).toBe('full');
expect(log.mock.calls.flat().join(' ')).toContain('[gate] observe would-refuse REPORT_LOCKED');
});
it('an unknown value stays enforce (fails closed)', () => {
process.env.AEO_REPORT_GATE = 'disabled';
expect(reportGateMode()).toBe('enforce');
});
it('a valid proof opens the report', () => {
expect(reportAccess('audit-1', unlockToken('audit-1')!)).toBe('full');
});
it('a proof for another audit does not', () => {
expect(reportAccess('audit-2', unlockToken('audit-1')!)).toBe('preview');
});
});
describe('unlock proof key', () => {
it('works without a dedicated key, from the server key', () => {
expect(unlockToken('audit-1')).toMatch(/^[A-Za-z0-9_-]{43}$/);
});
it('a dedicated key, when set, gives a different proof', () => {
const derived = unlockToken('audit-1');
process.env.AEO_UNLOCK_COOKIE_SECRET = 'dedicated';
expect(unlockToken('audit-1')).not.toBe(derived);
});
it('the proof is not signed with the server key itself (a separate derived key)', () => {
const t = unlockToken('audit-1')!;
const withServerKey = createHmac('sha256', 'service-role-key').update('unlock:audit-1').digest('base64url');
expect(t).not.toBe(withServerKey);
});
it('no key at all gives no proof, so the report stays locked', () => {
delete process.env.SUPABASE_SERVICE_ROLE_KEY;
expect(unlockToken('audit-1')).toBeNull();
expect(reportAccess('audit-1', 'anything')).toBe('preview');
});
});
describe('unlock readiness follows the report gate', () => {
it('refuses an unfinished audit with nothing set', () => {
expect(unlockRefusal('audit-1', 'pending', false)?.status).toBe(409);
expect(unlockRefusal('audit-1', 'complete', false)?.status).toBe(409);
expect(unlockRefusal('audit-1', 'complete', true)).toBeNull();
});
});