upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/api.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/api.mjs')
-rw-r--r--tests/api.mjs79
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/api.mjs b/tests/api.mjs
new file mode 100644
index 0000000..5218d7b
--- /dev/null
+++ b/tests/api.mjs
@@ -0,0 +1,79 @@
1import { curl, curlBody, getPortalIP, canPing, canResolve, dnsResolvesToSelf } from './helpers/network.mjs';
2
3const IP = getPortalIP();
4let passed = 0, failed = 0;
5
6function assert(condition, test) {
7 if (condition) { console.log(` ✓ ${test}`); passed++; }
8 else { console.log(` ✗ ${test}`); failed++; }
9}
10
11async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
12
13console.log(`\n=== API Tests (target: ${IP}) ===\n`);
14
15// Test 3: Captive portal serves HTML
16console.log('Test 3: GET / returns portal HTML');
17const body3 = curlBody(`http://${IP}/`);
18assert(body3 && body3.includes('TollGate'), 'Portal HTML contains "TollGate"');
19assert(body3 && body3.includes('Grant Free Access'), 'Portal has Grant Access button');
20
21// Test 4: Captive detection URIs
22console.log('\nTest 4: Captive detection URIs');
23for (const uri of ['/generate_204', '/hotspot-detect.html', '/canonical.html', '/success.txt', '/ncsi.txt', '/connecttest.txt', '/wpad.dat', '/redirect']) {
24 const code = curl(`http://${IP}${uri}`);
25 assert(code === '200', `${uri} → 200`);
26}
27
28// Test 7: /whoami returns MAC
29console.log('\nTest 7: GET /whoami');
30const body7 = curlBody(`http://${IP}/whoami`);
31assert(body7 && body7.startsWith('mac='), '/whoami returns mac=...');
32
33// Test 8: /usage returns no session
34console.log('\nTest 8: GET /usage');
35const body8 = curlBody(`http://${IP}/usage`);
36assert(body8 && body8.includes('-1/-1'), '/usage returns -1/-1 before auth');
37
38// Test 5: DNS hijack before auth
39console.log('\nTest 5: DNS hijack before auth');
40assert(dnsResolvesToSelf('google.com'), 'DNS resolves google.com to AP IP');
41
42// Test 6: No internet before auth
43console.log('\nTest 6: No internet before auth');
44assert(!canPing('8.8.8.8', 1), 'ping 8.8.8.8 fails before auth');
45
46// Test 9: Grant access
47console.log('\nTest 9: GET /grant_access');
48const body9 = curlBody(`http://${IP}/grant_access`);
49assert(body9 && body9.includes('"granted"'), 'Grant access returns {"status":"granted"}');
50
51await sleep(2000);
52
53// Test 10: DNS forward after auth
54console.log('\nTest 10: DNS forward after auth');
55assert(canResolve('google.com'), 'DNS resolves normally after auth');
56
57// Test 11: Internet after auth
58console.log('\nTest 11: Internet after auth');
59assert(canPing('8.8.8.8'), 'ping 8.8.8.8 succeeds after auth');
60
61// Test 12: HTTP browsing works
62console.log('\nTest 12: HTTP browsing');
63const body12 = curlBody('http://example.com/');
64assert(body12 && (body12.includes('Example Domain') || body12.includes('example')), 'HTTP page loads');
65
66// Test 13: Reset auth
67console.log('\nTest 13: GET /reset_authentication');
68const body13 = curlBody(`http://${IP}/reset_authentication`);
69assert(body13 && body13.includes('"reset"'), 'Reset returns {"status":"reset"}');
70
71await sleep(2000);
72
73// Test 14: Internet blocked after reset
74console.log('\nTest 14: Internet blocked after reset');
75assert(!canPing('8.8.8.8', 1), 'ping fails after auth reset');
76
77// Summary
78console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`);
79process.exit(failed > 0 ? 1 : 0);