Repository navigation
Expand file tree
/
Copy pathroute.test.ts
More file actions
194 lines (176 loc) · 7.53 KB
/
Copy pathroute.test.ts
File metadata and controls
194 lines (176 loc) · 7.53 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
const { rateLimitMock, supabaseInsertMock, today } = vi.hoisted(() => ({
rateLimitMock: vi.fn(),
supabaseInsertMock: vi.fn(),
today: { count: 0, since: '', publicOnly: false },
}));
const { staffMock } = vi.hoisted(() => ({ staffMock: vi.fn(async (): Promise<string | null> => null) }));
vi.mock('@/lib/staff', async (orig) => ({
...(await orig<typeof import('@/lib/staff')>()),
staffEmail: staffMock,
}));
vi.mock('@/lib/rateLimit', () => ({
checkRateLimit: rateLimitMock,
/* getClientIp moved into this module so both rate-limited routes bucket callers
identically; the route imports it from here now. */
getClientIp: () => '203.0.113.7',
}));
vi.mock('@supabase/supabase-js', () => ({
createClient: () => ({
from: () => ({
select: () => {
const result = () => ({ count: today.count, error: null });
return {
gte: (_col: string, since: string) => {
today.since = since;
const p = Promise.resolve(result());
return Object.assign(p, {
is: async (col: string, v: unknown) => {
if (col === 'requested_by' && v === null) today.publicOnly = true;
return result();
},
});
},
};
},
insert: (...args: unknown[]) => {
supabaseInsertMock(...args);
return {
select: () => ({
single: async () => supabaseInsertMock.mock.results.at(-1)!.value,
}),
};
},
}),
}),
}));
function makeRequest(body: unknown, headers: Record<string, string> = {}) {
return new Request('http://localhost:3000/api/audit/start', {
method: 'POST',
headers: { 'content-type': 'application/json', ...headers },
body: typeof body === 'string' ? body : JSON.stringify(body),
}) as unknown as import('next/server').NextRequest;
}
beforeEach(() => {
vi.resetModules();
rateLimitMock.mockReset();
supabaseInsertMock.mockReset();
rateLimitMock.mockResolvedValue({ limited: false });
supabaseInsertMock.mockReturnValue({ data: { id: 'audit-123' }, error: null });
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://example.supabase.co';
process.env.SUPABASE_SERVICE_ROLE_KEY = 'service-role-key';
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000';
process.env.INTERNAL_CRAWLER_SECRET = 'secret';
process.env.RATE_LIMIT_MAX = '5';
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(new Response(null, { status: 202 })),
);
});
describe('POST /api/audit/start', () => {
it('returns 201 with auditId for a valid URL', async () => {
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'https://example.com/some/path?q=1' }));
expect(res.status).toBe(201);
const json = await res.json();
expect(json).toEqual({ auditId: 'audit-123' });
expect(supabaseInsertMock).toHaveBeenCalledWith(
expect.objectContaining({ url: 'https://example.com', status: 'pending' }),
);
});
it('rejects private IP hostnames with 400', async () => {
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'http://10.0.0.1/admin' }));
expect(res.status).toBe(400);
const json = await res.json();
expect(json.error).toMatch(/private\/local/);
});
it('rejects localhost with 400', async () => {
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'http://localhost:3000/' }));
expect(res.status).toBe(400);
const json = await res.json();
expect(json.error).toMatch(/private\/local/);
});
it('rejects malformed URLs with 400', async () => {
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'not-a-url' }));
expect(res.status).toBe(400);
});
it('returns 429 with Retry-After when rate-limited', async () => {
rateLimitMock.mockResolvedValueOnce({ limited: true });
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'https://example.com' }));
expect(res.status).toBe(429);
expect(res.headers.get('Retry-After')).toBe('3600');
const json = await res.json();
expect(json).toEqual({ error: 'Rate limit exceeded', retryAfterSeconds: 3600 });
});
it('returns 500 when Supabase INSERT fails', async () => {
supabaseInsertMock.mockReturnValueOnce({ data: null, error: { message: 'boom' } });
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'https://example.com' }));
expect(res.status).toBe(500);
const json = await res.json();
expect(json).toEqual({ error: 'Database error' });
});
});
describe('POST /api/audit/start daily cap', () => {
const GATE_KEYS = ['AEO_GATE_MODE', 'AEO_DAILY_AUDIT_CAP', 'VERCEL_ENV', 'ANTHROPIC_API_KEY', 'AEO_UNLOCK_COOKIE_SECRET'];
beforeEach(() => {
today.count = 0;
Object.assign(process.env, {
VERCEL_ENV: 'production', ANTHROPIC_API_KEY: 'a', AEO_DAILY_AUDIT_CAP: '3', AEO_UNLOCK_COOKIE_SECRET: 'c',
});
});
afterEach(() => { for (const k of GATE_KEYS) delete process.env[k]; });
it('enforce: at the cap, refuses with 503 before any write or rate-limit spend', async () => {
process.env.AEO_GATE_MODE = 'enforce';
today.count = 3;
vi.spyOn(console, 'log').mockImplementation(() => {});
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'https://example.com' }));
expect(res.status).toBe(503);
expect((await res.json()).code).toBe('DAILY_CAP');
expect(supabaseInsertMock).not.toHaveBeenCalled();
expect(rateLimitMock).not.toHaveBeenCalled();
expect(today.since).toMatch(/T00:00:00\.000Z$/);
});
it('enforce: below the cap, the audit starts', async () => {
process.env.AEO_GATE_MODE = 'enforce';
today.count = 2;
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'https://example.com' }));
expect(res.status).toBe(201);
});
it('staff: at the cap, the audit starts and is marked as a staff audit (AC-019)', async () => {
process.env.AEO_GATE_MODE = 'enforce';
process.env.AEO_STAFF_EMAILS = 'staff@example.com';
today.count = 3;
staffMock.mockResolvedValueOnce('staff@example.com');
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'https://example.com' }, { authorization: 'Bearer t' }));
delete process.env.AEO_STAFF_EMAILS;
expect(res.status).toBe(201);
expect(supabaseInsertMock).toHaveBeenCalledWith(expect.objectContaining({ requested_by: 'staff' }));
});
it('with staff configured, the daily count leaves staff audits out', async () => {
process.env.AEO_GATE_MODE = 'enforce';
process.env.AEO_STAFF_EMAILS = 'staff@example.com';
today.publicOnly = false;
const { POST } = await import('./route');
await POST(makeRequest({ url: 'https://example.com' }));
delete process.env.AEO_STAFF_EMAILS;
expect(today.publicOnly).toBe(true);
expect(supabaseInsertMock).toHaveBeenCalledWith(expect.not.objectContaining({ requested_by: expect.anything() }));
});
it('observe: at the cap, logs and still starts the audit', async () => {
process.env.AEO_GATE_MODE = 'observe';
today.count = 3;
const log = vi.spyOn(console, 'log').mockImplementation(() => {});
const { POST } = await import('./route');
const res = await POST(makeRequest({ url: 'https://example.com' }));
expect(res.status).toBe(201);
expect(log.mock.calls.flat().join(' ')).toContain('[gate] observe would-refuse DAILY_CAP');
});
});