Repository navigation
Expand file tree
/
Copy pathtest-production-comprehensive.js
More file actions
executable file
Β·469 lines (386 loc) Β· 14.8 KB
/
Copy pathtest-production-comprehensive.js
File metadata and controls
executable file
Β·469 lines (386 loc) Β· 14.8 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env node
/**
* Production Comprehensive Test Suite
* Tests all production-ready features including crypto health, turn-taking, staging controls, etc.
*/
const axios = require('axios');
const API_BASE = 'http://localhost:3001';
// Colors for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function logTest(testName, status, details = '') {
const statusColor = status === 'PASS' ? 'green' : status === 'FAIL' ? 'red' : 'yellow';
const statusIcon = status === 'PASS' ? 'β
' : status === 'FAIL' ? 'β' : 'β οΈ';
log(`${statusIcon} ${testName}: ${status}`, statusColor);
if (details) {
log(` ${details}`, 'cyan');
}
}
async function callApi(method, endpoint, data = null, headers = {}) {
try {
const config = {
method,
url: `${API_BASE}${endpoint}`,
headers: {
'Content-Type': 'application/json',
...headers,
},
};
if (data) {
config.data = data;
}
const response = await axios(config);
return { success: true, data: response.data, status: response.status, headers: response.headers };
} catch (error) {
return {
success: false,
error: error.response?.data || error.message,
status: error.response?.status,
headers: error.response?.headers
};
}
}
async function testCryptoHealth() {
log('\nπ Testing Crypto Health Endpoint...', 'blue');
const result = await callApi('GET', '/health/crypto');
if (result.success) {
logTest('Crypto Health Endpoint', 'PASS');
// Check crypto health status
if (result.data.status === 'healthy') {
logTest('Crypto Health Status', 'PASS', 'Encryption system is healthy');
} else if (result.data.status === 'degraded') {
logTest('Crypto Health Status', 'WARN', 'Encryption system is degraded');
} else {
logTest('Crypto Health Status', 'FAIL', 'Encryption system is unhealthy');
}
// Check KMS connection
if (result.data.kmsConnection) {
logTest('KMS Connection', 'PASS');
} else {
logTest('KMS Connection', 'FAIL', 'KMS connection failed');
}
// Check DEK key status
if (result.data.dekKeyStatus === 'active') {
logTest('DEK Key Status', 'PASS');
} else if (result.data.dekKeyStatus === 'inactive') {
logTest('DEK Key Status', 'WARN', 'Using test key - not suitable for production');
} else {
logTest('DEK Key Status', 'FAIL', 'DEK key error');
}
// Check test vector decryption
if (result.data.testVectorDecrypt) {
logTest('Test Vector Decryption', 'PASS');
} else {
logTest('Test Vector Decryption', 'FAIL', 'Encryption/decryption test failed');
}
return true;
} else {
logTest('Crypto Health Endpoint', 'FAIL', result.error);
return false;
}
}
async function testTurnTakingLocks() {
log('\nπ― Testing Turn-Taking Locks...', 'blue');
// Setup test users
const aliceAuth = await callApi('POST', '/auth/verify-code', { email: 'alice@test.com', code: '123456' });
const bobAuth = await callApi('POST', '/auth/verify-code', { email: 'bob@test.com', code: '123456' });
if (!aliceAuth.success || !bobAuth.success) {
logTest('User Setup', 'FAIL', 'Failed to authenticate test users');
return false;
}
const aliceHeaders = { Authorization: `Bearer ${aliceAuth.data.accessToken}` };
const bobHeaders = { Authorization: `Bearer ${bobAuth.data.accessToken}` };
// Create couple and session
const coupleResult = await callApi('POST', '/couples', null, aliceHeaders);
if (!coupleResult.success) {
logTest('Couple Creation', 'FAIL', coupleResult.error);
return false;
}
// Create invite and accept
const inviteResult = await callApi('POST', '/invites', null, aliceHeaders);
if (!inviteResult.success) {
logTest('Invite Creation', 'FAIL', inviteResult.error);
return false;
}
const acceptResult = await callApi('POST', `/invites/${inviteResult.data.code}/accept`, null, bobHeaders);
if (!acceptResult.success) {
logTest('Invite Acceptance', 'FAIL', acceptResult.error);
return false;
}
// Create session
const sessionResult = await callApi('POST', '/sessions', null, aliceHeaders);
if (!sessionResult.success) {
logTest('Session Creation', 'FAIL', sessionResult.error);
return false;
}
const sessionId = sessionResult.data.sessionId;
logTest('Test Setup', 'PASS');
// Test turn-taking
const aliceMessage1 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Hello Bob, I want to talk about our weekend plans.',
client_message_id: 'alice_msg_1'
}, aliceHeaders);
if (aliceMessage1.success) {
logTest('Alice First Message', 'PASS');
} else {
logTest('Alice First Message', 'FAIL', aliceMessage1.error);
return false;
}
// Alice tries to send another message (should fail)
const aliceMessage2 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Let me add something else...',
client_message_id: 'alice_msg_2'
}, aliceHeaders);
if (aliceMessage2.status === 409) {
logTest('Turn-Taking Lock', 'PASS', 'Alice correctly blocked from consecutive messages');
} else {
logTest('Turn-Taking Lock', 'FAIL', 'Alice should have been blocked');
return false;
}
// Bob responds (should succeed)
const bobMessage1 = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Hi Alice! I\'d love to hear your ideas.',
client_message_id: 'bob_msg_1'
}, bobHeaders);
if (bobMessage1.success) {
logTest('Bob Response', 'PASS');
} else {
logTest('Bob Response', 'FAIL', bobMessage1.error);
return false;
}
// Test turn lock status endpoint
const lockStatusResult = await callApi('GET', `/turn-taking/status?sessionId=${sessionId}`, null, aliceHeaders);
if (lockStatusResult.success && lockStatusResult.data.hasLock) {
logTest('Turn Lock Status Endpoint', 'PASS');
} else {
logTest('Turn Lock Status Endpoint', 'FAIL', 'Turn lock status not found');
}
return true;
}
async function testLongPolling() {
log('\nβ³ Testing Long-Polling Behavior...', 'blue');
// Setup test environment
const authResult = await callApi('POST', '/auth/verify-code', { email: 'polling@test.com', code: '123456' });
if (!authResult.success) {
logTest('Authentication', 'FAIL', authResult.error);
return false;
}
const headers = { Authorization: `Bearer ${authResult.data.accessToken}` };
const coupleResult = await callApi('POST', '/couples', null, headers);
const sessionResult = await callApi('POST', '/sessions', null, headers);
if (!coupleResult.success || !sessionResult.success) {
logTest('Setup', 'FAIL', 'Failed to create couple or session');
return false;
}
const sessionId = sessionResult.data.sessionId;
// Test empty poll (should return empty array)
const emptyPollResult = await callApi('GET', `/sessions/${sessionId}/messages`, null, headers);
if (emptyPollResult.success && Array.isArray(emptyPollResult.data) && emptyPollResult.data.length === 0) {
logTest('Empty Poll', 'PASS');
} else {
logTest('Empty Poll', 'FAIL', 'Empty poll should return empty array');
}
// Test long-polling with timeout
const longPollPromise = callApi('GET', `/sessions/${sessionId}/messages?waitMs=2000`, null, headers);
// Send a message after a short delay
setTimeout(async () => {
await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Test message for long polling',
client_message_id: 'polling_test_msg'
}, headers);
}, 500);
const longPollResult = await longPollPromise;
if (longPollResult.success && Array.isArray(longPollResult.data)) {
logTest('Long-Polling', 'PASS', 'Long-polling returned messages');
} else {
logTest('Long-Polling', 'FAIL', 'Long-polling failed');
}
return true;
}
async function testStagingControls() {
log('\nπ§ Testing Staging Controls...', 'blue');
// Test staging config endpoint (if staging is enabled)
const configResult = await callApi('GET', '/staging/config');
if (configResult.success) {
logTest('Staging Config Endpoint', 'PASS');
if (configResult.data.enabled) {
logTest('Staging Mode', 'PASS', 'Staging mode is enabled');
} else {
logTest('Staging Mode', 'WARN', 'Staging mode is disabled');
}
} else {
logTest('Staging Config Endpoint', 'WARN', 'Staging config endpoint not available (normal in production)');
}
// Test robots.txt
const robotsResult = await callApi('GET', '/robots.txt');
if (robotsResult.success) {
logTest('Robots.txt', 'PASS', 'Robots.txt is available');
if (robotsResult.data.includes('Disallow: /')) {
logTest('Robots.txt Content', 'PASS', 'Robots.txt properly blocks crawlers');
} else {
logTest('Robots.txt Content', 'WARN', 'Robots.txt may not be blocking crawlers');
}
} else {
logTest('Robots.txt', 'WARN', 'Robots.txt not available (normal in production)');
}
return true;
}
async function testDeleteSession() {
log('\nποΈ Testing Delete Session...', 'blue');
// Setup test environment
const authResult = await callApi('POST', '/auth/verify-code', { email: 'delete@test.com', code: '123456' });
if (!authResult.success) {
logTest('Authentication', 'FAIL', authResult.error);
return false;
}
const headers = { Authorization: `Bearer ${authResult.data.accessToken}` };
const coupleResult = await callApi('POST', '/couples', null, headers);
const sessionResult = await callApi('POST', '/sessions', null, headers);
if (!coupleResult.success || !sessionResult.success) {
logTest('Setup', 'FAIL', 'Failed to create couple or session');
return false;
}
const sessionId = sessionResult.data.sessionId;
// Send some messages
await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Test message 1',
client_message_id: 'delete_test_msg_1'
}, headers);
await callApi('POST', `/sessions/${sessionId}/messages`, {
content: 'Test message 2',
client_message_id: 'delete_test_msg_2'
}, headers);
// Verify messages exist
const messagesBefore = await callApi('GET', `/sessions/${sessionId}/messages`, null, headers);
if (messagesBefore.success && messagesBefore.data.length > 0) {
logTest('Messages Before Delete', 'PASS', `${messagesBefore.data.length} messages found`);
} else {
logTest('Messages Before Delete', 'FAIL', 'No messages found before delete');
return false;
}
// Delete session
const deleteResult = await callApi('DELETE', `/sessions/${sessionId}`, null, headers);
if (deleteResult.success) {
logTest('Session Delete', 'PASS');
} else {
logTest('Session Delete', 'FAIL', deleteResult.error);
return false;
}
// Verify session is gone
const sessionCheck = await callApi('GET', `/sessions/${sessionId}/messages`, null, headers);
if (sessionCheck.status === 404) {
logTest('Session Cleanup', 'PASS', 'Session no longer exists');
} else {
logTest('Session Cleanup', 'FAIL', 'Session still exists after delete');
}
return true;
}
async function testLoggingSecurity() {
log('\nπ Testing Logging Security...', 'blue');
const result = await callApi('GET', '/admin/logging-security');
if (result.success) {
logTest('Logging Security Endpoint', 'PASS');
if (result.data.preventPlaintextLogging) {
logTest('Plaintext Logging Prevention', 'PASS', 'Plaintext logging is prevented');
} else {
logTest('Plaintext Logging Prevention', 'FAIL', 'Plaintext logging is not prevented');
}
if (result.data.sensitiveFieldsCount > 0) {
logTest('Sensitive Fields Protection', 'PASS', `${result.data.sensitiveFieldsCount} sensitive fields protected`);
} else {
logTest('Sensitive Fields Protection', 'FAIL', 'No sensitive fields configured');
}
return true;
} else {
logTest('Logging Security Endpoint', 'FAIL', result.error);
return false;
}
}
async function testAdminEndpoints() {
log('\nπ¨βπΌ Testing Admin Endpoints...', 'blue');
// Test rate limit status
const rateLimitStatus = await callApi('GET', '/rate-limit/status');
if (rateLimitStatus.success) {
logTest('Rate Limit Status', 'PASS');
} else {
logTest('Rate Limit Status', 'FAIL', rateLimitStatus.error);
}
// Test turn locks
const turnLocks = await callApi('GET', '/admin/turn-locks');
if (turnLocks.success) {
logTest('Turn Locks Admin', 'PASS', `${turnLocks.data.count} active locks`);
} else {
logTest('Turn Locks Admin', 'FAIL', turnLocks.error);
}
// Test audit logs
const auditLogs = await callApi('GET', '/delete/audit-logs');
if (auditLogs.success) {
logTest('Audit Logs', 'PASS', 'Audit logs accessible');
} else {
logTest('Audit Logs', 'FAIL', auditLogs.error);
}
return true;
}
async function runProductionTests() {
log('π Starting Production Comprehensive Tests...', 'bright');
log('Testing all production-ready features', 'cyan');
try {
const results = {
cryptoHealth: await testCryptoHealth(),
turnTaking: await testTurnTakingLocks(),
longPolling: await testLongPolling(),
stagingControls: await testStagingControls(),
deleteSession: await testDeleteSession(),
loggingSecurity: await testLoggingSecurity(),
adminEndpoints: await testAdminEndpoints(),
};
const passedTests = Object.values(results).filter(Boolean).length;
const totalTests = Object.keys(results).length;
log('\nπ Production Test Results:', 'blue');
log(`β
Passed: ${passedTests}/${totalTests}`, 'green');
if (passedTests === totalTests) {
log('\nπ All Production Tests Passed!', 'green');
log('Your Sync platform is production-ready!', 'cyan');
} else {
log('\nβ οΈ Some Production Tests Failed', 'yellow');
log('Review the failed tests before deploying to production', 'yellow');
}
// Summary of features tested
log('\nπ§ Features Tested:', 'blue');
log('β
Crypto Health Monitoring', 'green');
log('β
Turn-Taking Locks', 'green');
log('β
Long-Polling Behavior', 'green');
log('β
Staging Controls', 'green');
log('β
Session Deletion', 'green');
log('β
Logging Security', 'green');
log('β
Admin Endpoints', 'green');
} catch (error) {
log(`\nβ Test suite failed: ${error.message}`, 'red');
process.exit(1);
}
}
// Run tests if this script is executed directly
if (require.main === module) {
runProductionTests();
}
module.exports = {
runProductionTests,
testCryptoHealth,
testTurnTakingLocks,
testLongPolling,
testStagingControls,
testDeleteSession,
testLoggingSecurity,
testAdminEndpoints,
};