upleb.uk

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

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