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
blob: c8334abd29606fe1958415369574c10dea46ff5b (plain)
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
import { execSync } from 'child_process';

const IP = process.env.TOLLGATE_IP || '10.192.45.1';
const API = `http://${IP}:2121`;
let passed = 0, failed = 0;

function assert(cond, msg) {
  if (cond) { console.log(`  ✓ ${msg}`); passed++; }
  else { console.log(`  ✗ ${msg}`); failed++; }
}

function run(cmd) {
  try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); }
  catch { return null; }
}

function runJson(cmd) {
  const out = run(cmd);
  try { return out ? JSON.parse(out) : null; }
  catch { return null; }
}

function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }

function mintToken(amount = 21) {
  run('cashu -h https://testnut.cashu.space invoice ' + amount + ' 2>&1');
  const out = run('cashu -h https://testnut.cashu.space send --legacy ' + amount + ' 2>&1');
  const match = out && out.match(/cashuA[a-zA-Z0-9_-]+/);
  return match ? match[0] : null;
}

function canPing(host = '8.8.8.8') {
  const result = run(`ping -c 1 -W 2 -I wlp59s0 ${host} 2>/dev/null`);
  return result && !result.includes('100% packet loss');
}

console.log(`\n=== Session Expiry Integration Test (target: ${IP}) ===`);
console.log(`NOTE: This test waits 65s for session expiry. Total runtime ~80s.\n`);

console.log('1. Reset auth');
run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`);

await sleep(1000);

console.log('\n2. Verify blocked before payment');
assert(!canPing(), 'Ping blocked before payment');

const usage0 = run(`curl -s --connect-timeout 10 ${API}/usage`);
assert(usage0 && usage0.includes('-1/-1'), 'Usage is -1/-1');

console.log('\n3. Pay with valid token (21 sats = 60000ms)');
const token = mintToken(21);
assert(token !== null, 'Token generated');
if (token) {
  const payResult = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token}' -H "Content-Type: application/cashu" ${API}/`);
  assert(payResult && payResult.kind === 1022, 'Payment accepted');
}

await sleep(1000);

console.log('\n4. Verify session active');
const usage1 = run(`curl -s --connect-timeout 10 ${API}/usage`);
assert(usage1 && !usage1.includes('-1/-1'), `Usage: ${usage1}`);

console.log('\n5. Verify internet works');
assert(canPing(), 'Ping works with active session');

const httpResult = run(`curl -s --connect-timeout 10 -m 10 --interface wlp59s0 http://1.1.1.1/ 2>/dev/null`);
assert(httpResult && httpResult.length > 0, 'HTTP request reaches internet');

console.log('\n6. Waiting 65s for session expiry (allotment=60000ms)...');
for (let i = 65; i > 0; i -= 5) {
  process.stdout.write(`\r  ${i}s remaining...`);
  await sleep(Math.min(5000, i * 1000));
}
console.log('\r  Session should be expired now.         ');

console.log('\n7. Verify session expired');
const usage2 = run(`curl -s --connect-timeout 10 ${API}/usage`);
assert(usage2 && usage2.includes('-1/-1'), `Usage after expiry: ${usage2}`);

console.log('\n8. Verify internet blocked after expiry');
assert(!canPing(), 'Ping blocked after session expiry');

const httpResult2 = run(`curl -s --connect-timeout 5 -m 5 --interface wlp59s0 http://1.1.1.1/ 2>/dev/null`);
assert(!httpResult2 || httpResult2.length === 0, 'HTTP blocked after expiry');

console.log('\n9. Pay again to verify renewal works');
const token2 = mintToken(21);
if (token2) {
  const pay2 = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token2}' -H "Content-Type: application/cashu" ${API}/`);
  assert(pay2 && pay2.kind === 1022, 'Renewal payment accepted');
}

await sleep(1000);

console.log('\n10. Verify internet works after renewal');
assert(canPing(), 'Ping works after renewal');

run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`);

console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`);
process.exit(failed > 0 ? 1 : 0);