-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadmin-api-killswitch.test.ts
More file actions
166 lines (139 loc) · 4.13 KB
/
admin-api-killswitch.test.ts
File metadata and controls
166 lines (139 loc) · 4.13 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
import { NextRequest } from 'next/server';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { vi } from 'vitest';
const BASE_URL = 'http://localhost';
const TEST_PRODUCT_ID = '00000000-0000-4000-8000-000000000001';
const TEST_ORDER_ID = '00000000-0000-4000-8000-000000000002';
const MUTATION_METHODS = ['POST', 'PUT', 'PATCH', 'DELETE'] as const;
type RouteCase =
| {
name: string;
importPath: string;
path: string;
kind: 'static';
}
| {
name: string;
importPath: string;
path: (id: string) => string;
kind: 'dynamic-id';
id: string;
};
const cases: RouteCase[] = [
{
name: 'admin/products',
importPath: '@/app/api/shop/admin/products/route',
path: '/api/shop/admin/products',
kind: 'static',
},
{
name: 'admin/products/[id]',
importPath: '@/app/api/shop/admin/products/[id]/route',
path: (id: string) => `/api/shop/admin/products/${id}`,
kind: 'dynamic-id',
id: TEST_PRODUCT_ID,
},
{
name: 'admin/products/[id]/status',
importPath: '@/app/api/shop/admin/products/[id]/status/route',
path: (id: string) => `/api/shop/admin/products/${id}/status`,
kind: 'dynamic-id',
id: TEST_PRODUCT_ID,
},
{
name: 'admin/orders',
importPath: '@/app/api/shop/admin/orders/route',
path: '/api/shop/admin/orders',
kind: 'static',
},
{
name: 'admin/orders/[id]',
importPath: '@/app/api/shop/admin/orders/[id]/route',
path: (id: string) => `/api/shop/admin/orders/${id}`,
kind: 'dynamic-id',
id: TEST_ORDER_ID,
},
{
name: 'admin/orders/[id]/refund',
importPath: '@/app/api/shop/admin/orders/[id]/refund/route',
path: (id: string) => `/api/shop/admin/orders/${id}/refund`,
kind: 'dynamic-id',
id: TEST_ORDER_ID,
},
{
name: 'admin/orders/reconcile-stale',
importPath: '@/app/api/shop/admin/orders/reconcile-stale/route',
path: '/api/shop/admin/orders/reconcile-stale',
kind: 'static',
},
];
function makeReq(path: string, method: string) {
const url = `${BASE_URL}${path}`;
const origin = process.env.APP_ORIGIN ?? 'http://localhost:3000';
const init: RequestInit = {
method,
headers: { 'content-type': 'application/json', origin },
};
if (method !== 'GET' && method !== 'HEAD') {
(init as any).body = '{';
}
return new NextRequest(url, init as any);
}
async function readCodeFromResponse(
res: Response
): Promise<{ status: number; code?: string; raw: string }> {
const status = res.status;
const raw = await res.text();
if (!raw) return { status, raw: '' };
try {
const parsed = JSON.parse(raw);
const code =
(parsed?.code as string | undefined) ??
(parsed?.error?.code as string | undefined);
return { status, code, raw };
} catch {
return { status, raw };
}
}
async function expectAdminDisabled(res: Response) {
const body = await readCodeFromResponse(res);
expect(body.status).toBe(403);
if (body.code) {
expect(body.code).toBe('ADMIN_API_DISABLED');
} else {
expect(body.raw).toContain('ADMIN_API_DISABLED');
}
}
async function runAllMutationMethods(mod: any, reqPath: string, ctx?: any) {
for (const m of MUTATION_METHODS) {
const handler = mod[m];
if (typeof handler !== 'function') continue;
const req = makeReq(reqPath, m);
const res: Response = ctx ? await handler(req, ctx) : await handler(req);
await expectAdminDisabled(res);
}
}
describe('P0-7.1 Admin API kill-switch coverage (production)', () => {
beforeEach(() => {
vi.resetModules();
vi.stubEnv('NODE_ENV', 'production');
vi.stubEnv('ENABLE_ADMIN_API', '');
vi.stubEnv('APP_ORIGIN', 'https://admin.example.test');
});
afterEach(() => {
vi.unstubAllEnvs();
});
it.each(cases)(
'returns 403 ADMIN_API_DISABLED for all mutating handlers: $name',
async c => {
const mod = await import(c.importPath);
if (c.kind === 'static') {
await runAllMutationMethods(mod, c.path);
return;
}
const path = c.path(c.id);
const ctx = { params: { id: c.id } };
await runAllMutationMethods(mod, path, ctx);
}
);
});