upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/integration/test-reset-auth.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/test-reset-auth.mjs')
-rw-r--r--tests/integration/test-reset-auth.mjs101
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/integration/test-reset-auth.mjs b/tests/integration/test-reset-auth.mjs
new file mode 100644
index 0000000..279b2f9
--- /dev/null
+++ b/tests/integration/test-reset-auth.mjs
@@ -0,0 +1,101 @@
1import { execSync } from 'child_process';
2
3const IP = process.env.TOLLGATE_IP || '10.192.45.1';
4const API = `http://${IP}:2121`;
5const SUDO_PW = process.env.SUDO_PW || 'c03rad0r123';
6let passed = 0, failed = 0;
7
8function assert(cond, msg) {
9 if (cond) { console.log(` ✓ ${msg}`); passed++; }
10 else { console.log(` ✗ ${msg}`); failed++; }
11}
12
13function run(cmd) {
14 try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); }
15 catch { return null; }
16}
17
18function runJson(cmd) {
19 const out = run(cmd);
20 try { return out ? JSON.parse(out) : null; }
21 catch { return null; }
22}
23
24function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
25
26function mintToken(amount = 21) {
27 run('cashu -h https://testnut.cashu.space invoice ' + amount + ' 2>&1');
28 const out = run('cashu -h https://testnut.cashu.space send --legacy ' + amount + ' 2>&1');
29 const match = out && out.match(/cashuA[a-zA-Z0-9_-]+/);
30 return match ? match[0] : null;
31}
32
33function canPing(host = '8.8.8.8') {
34 const result = run(`ping -c 1 -W 2 -I wlp59s0 ${host} 2>/dev/null`);
35 return result && !result.includes('100% packet loss');
36}
37
38console.log(`\n=== Reset Auth Integration Test (target: ${IP}) ===\n`);
39
40console.log('1. Reset auth to clear state');
41const reset1 = run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`);
42assert(reset1 && reset1.includes('reset'), 'Reset returns {"status":"reset"}');
43
44await sleep(1000);
45
46console.log('\n2. Verify no session');
47const usage1 = run(`curl -s --connect-timeout 10 ${API}/usage`);
48assert(usage1 && usage1.includes('-1/-1'), 'Usage is -1/-1 before payment');
49
50console.log('\n3. Verify internet blocked');
51assert(!canPing(), 'Ping blocked before payment');
52
53console.log('\n4. Pay with valid token');
54const token = mintToken(21);
55assert(token !== null, 'Token generated');
56if (token) {
57 const payResult = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token}' -H "Content-Type: application/cashu" ${API}/`);
58 assert(payResult && payResult.kind === 1022, 'Payment accepted (kind=1022)');
59 const allotment = payResult && payResult.tags && payResult.tags.find(t => t[0] === 'allotment');
60 assert(allotment && parseInt(allotment[1]) > 0, `Allotment: ${allotment ? allotment[1] : 'N/A'}ms`);
61}
62
63await sleep(1000);
64
65console.log('\n5. Verify session active');
66const usage2 = run(`curl -s --connect-timeout 10 ${API}/usage`);
67assert(usage2 && !usage2.includes('-1/-1'), `Usage: ${usage2}`);
68
69console.log('\n6. Verify internet allowed');
70assert(canPing(), 'Ping works with active session');
71
72console.log('\n7. Reset auth while session active');
73const reset2 = run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`);
74assert(reset2 && reset2.includes('reset'), 'Reset returns {"status":"reset"}');
75
76await sleep(1000);
77
78console.log('\n8. Verify session cleared');
79const usage3 = run(`curl -s --connect-timeout 10 ${API}/usage`);
80assert(usage3 && usage3.includes('-1/-1'), 'Usage is -1/-1 after reset');
81
82console.log('\n9. Verify internet blocked again');
83assert(!canPing(), 'Ping blocked after reset');
84
85console.log('\n10. Pay again (new token)');
86const token2 = mintToken(21);
87if (token2) {
88 const pay2 = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token2}' -H "Content-Type: application/cashu" ${API}/`);
89 assert(pay2 && pay2.kind === 1022, 'Second payment accepted');
90}
91
92await sleep(1000);
93
94console.log('\n11. Verify internet works again');
95assert(canPing(), 'Ping works with new session');
96
97console.log('\n12. Final reset');
98run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`);
99
100console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`);
101process.exit(failed > 0 ? 1 : 0);