-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e-test.mjs
More file actions
245 lines (216 loc) · 7.93 KB
/
Copy pathe2e-test.mjs
File metadata and controls
245 lines (216 loc) · 7.93 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const BASE = 'http://localhost:3000';
const ALERT_SECRET = 'super-secret-alert-key-change-me';
let accessToken, refreshToken, userId, projectId, apiKey, logRequestId;
async function req(method, path, opts = {}) {
const url = `${BASE}${path}`;
const headers = { 'Content-Type': 'application/json', ...opts.headers };
if (opts.token) headers['Authorization'] = `Bearer ${opts.token}`;
if (opts.apiKey) headers['X-API-Key'] = opts.apiKey;
const body = opts.body ? JSON.stringify(opts.body) : undefined;
const res = await fetch(url, { method, headers, body });
const text = await res.text();
let data;
try { data = JSON.parse(text); } catch { data = text; }
return { status: res.status, headers: res.headers, data };
}
function assert(condition, msg) {
if (!condition) throw new Error(`FAIL: ${msg}`);
}
async function testHealth() {
const { status, data } = await req('GET', '/health');
assert(status === 200, `health status ${status}`);
assert(data.status === 'ok', 'health status ok');
console.log(' ✓ GET /health');
}
async function testRegister(email) {
const { status, data } = await req('POST', '/api/v1/auth/register', {
body: { email, password: 'TestPass1', name: 'E2E User' },
});
assert(status === 201, `register status ${status}`);
assert(data.accessToken, 'register has accessToken');
accessToken = data.accessToken;
refreshToken = data.refreshToken;
userId = data.user.id;
console.log(' ✓ POST /api/v1/auth/register');
}
async function testLogin(email) {
const { status, data } = await req('POST', '/api/v1/auth/login', {
body: { email, password: 'TestPass1' },
});
assert(status === 200, `login status ${status} ${status !== 200 ? JSON.stringify(data) : ''}`);
assert(data.accessToken, 'login has accessToken');
accessToken = data.accessToken;
refreshToken = data.refreshToken;
console.log(' ✓ POST /api/v1/auth/login');
}
async function testProfile() {
const { status, data } = await req('GET', '/api/v1/auth/profile', { token: accessToken });
assert(status === 200, `profile status ${status}`);
assert(data.email, 'profile has email');
console.log(' ✓ GET /api/v1/auth/profile');
}
async function testRefresh() {
const { status, data } = await req('POST', '/api/v1/auth/refresh', {
body: { refreshToken },
});
assert(status === 200, `refresh status ${status}`);
assert(data.accessToken, 'refresh has accessToken');
accessToken = data.accessToken;
refreshToken = data.refreshToken;
console.log(' ✓ POST /api/v1/auth/refresh');
}
async function testCreateProject() {
const { status, data } = await req('POST', '/api/v1/projects', {
token: accessToken,
body: { name: 'E2E Project', description: 'E2E test project' },
});
assert(status === 201, `create project status ${status}`);
assert(data.id, 'project has id');
projectId = data.id;
console.log(' ✓ POST /api/v1/projects');
}
async function testListProjects() {
const { status, data } = await req('GET', '/api/v1/projects', { token: accessToken });
assert(status === 200, `list projects status ${status}`);
assert(Array.isArray(data.projects), 'projects is array');
console.log(' ✓ GET /api/v1/projects');
}
async function testCreateApiKey() {
const { status, data } = await req('POST', `/api/v1/projects/${projectId}/keys`, {
token: accessToken,
body: { name: 'E2E Test Key' },
});
assert(status === 201, `create api key status ${status}`);
assert(data.rawKey, 'key has rawKey');
apiKey = data.rawKey;
console.log(' ✓ POST /api/v1/projects/' + projectId + '/keys');
}
async function testListKeys() {
const { status, data } = await req('GET', `/api/v1/projects/${projectId}/keys`, { token: accessToken });
assert(status === 200, `list keys status ${status}`);
assert(Array.isArray(data), 'keys is array');
console.log(' ✓ GET /api/v1/projects/' + projectId + '/keys');
}
async function testCreateRouteConfig() {
const { status } = await req('POST', `/api/v1/projects/${projectId}/routes`, {
token: accessToken,
body: { path: '/api/v1/test-e2e', method: 'GET', service: 'auth-service', rateLimit: 50, cacheTTL: 30 },
});
assert(status === 201, `create route status ${status}`);
console.log(' ✓ POST /api/v1/projects/' + projectId + '/routes');
}
async function testListRoutes() {
const { status, data } = await req('GET', `/api/v1/projects/${projectId}/routes`, { token: accessToken });
assert(status === 200, `list routes status ${status}`);
assert(Array.isArray(data), 'routes is array');
console.log(' ✓ GET /api/v1/projects/' + projectId + '/routes');
}
async function testLogIngestion() {
logRequestId = globalThis.crypto.randomUUID();
const { status } = await req('POST', '/api/v1/logs', {
body: {
requestId: logRequestId,
timestamp: new Date().toISOString(),
method: 'GET',
path: '/e2e/test',
statusCode: 200,
latency: 42,
ip: '127.0.0.1',
},
});
assert(status === 201, `log ingest status ${status}`);
console.log(' ✓ POST /api/v1/logs');
}
async function testLogQuery() {
const { status, data } = await req('GET', '/api/v1/logs', { token: accessToken });
assert(status === 200, `log query status ${status}`);
assert(Array.isArray(data.entries), 'log entries is array');
console.log(' ✓ GET /api/v1/logs');
}
async function testLogById() {
const { status, data } = await req('GET', `/api/v1/logs/${logRequestId}`, { token: accessToken });
assert(status === 200, `log by id status ${status}`);
assert(data.requestId === logRequestId, 'log by id matches');
console.log(' ✓ GET /api/v1/logs/:requestId');
}
async function testLogErrors() {
const { status, data } = await req('GET', '/api/v1/logs/errors', { token: accessToken });
assert(status === 200, `log errors status ${status}`);
assert(Array.isArray(data.entries), 'error entries is array');
console.log(' ✓ GET /api/v1/logs/errors');
}
async function testAnalytics() {
const { status } = await req('GET', '/api/v1/analytics/summary', { token: accessToken });
assert(status === 200, `analytics summary status ${status}`);
console.log(' ✓ GET /api/v1/analytics/summary');
}
async function testAlertEndpoints() {
const { status, data } = await req('POST', '/api/v1/alerts/emit', {
headers: { 'X-Alert-Secret': ALERT_SECRET },
body: {
type: 'HIGH_ERROR_RATE',
severity: 'WARNING',
message: 'E2E test alert',
value: 15.5,
threshold: 10.0,
service: 'e2e-test',
},
});
assert(status === 200, `alert emit status ${status} ${status !== 200 ? JSON.stringify(data) : ''}`);
console.log(' ✓ POST /api/v1/alerts/emit');
}
async function testSwagger() {
const { status } = await req('GET', '/api-docs/');
assert(status === 200, `swagger status ${status}`);
console.log(' ✓ GET /api-docs/');
}
async function testLogout() {
const { status } = await req('POST', '/api/v1/auth/logout', {
token: accessToken,
body: { refreshToken },
});
assert(status === 200, `logout status ${status}`);
console.log(' ✓ POST /api/v1/auth/logout');
}
async function runAll() {
console.log('\n🔷 E2E Full Pipeline Test\n');
const email = `e2e-${Date.now()}@test.com`;
const tests = [
testHealth,
() => testRegister(email),
() => testLogin(email),
testProfile,
testRefresh,
testCreateProject,
testListProjects,
testCreateApiKey,
testListKeys,
testCreateRouteConfig,
testListRoutes,
testLogIngestion,
testLogQuery,
testLogById,
testLogErrors,
testAnalytics,
testAlertEndpoints,
testSwagger,
testLogout,
];
let passed = 0;
let failed = 0;
for (const test of tests) {
try {
await test();
passed++;
} catch (err) {
failed++;
console.log(` ✗ ${err.message}`);
}
}
console.log(`\n📊 Results: ${passed} passed, ${failed} failed, ${tests.length} total\n`);
process.exit(failed > 0 ? 1 : 0);
}
runAll().catch((err) => {
console.error('Fatal:', err);
process.exit(1);
});