fix: confirm temporary hotspot login and show payment access countdown
build-image / build-push (push) Successful in 2m48s

This commit is contained in:
jfeng committed 2026-10-22 15:35:19 +00:00
1 parent bba6dcd37e
commit 0c01e5d88b
5 files changed
+105 -23

No files matched your search

+26 -2
View File
@@ -9,6 +9,9 @@ test('admin API authenticates, creates, lists, blocks, disconnects and exports w
const users=[{'.id':'*1',name:'existing',profile:'pix-1h',server:'guest-hotspot',disabled:'false',uptime:'1m','limit-uptime':'1h',comment:'=FORMULA()'}];
const active=[{'.id':'*A',user:'existing',server:'guest-hotspot',address:'10.10.10.10','mac-address':'AA:BB:CC:DD:EE:FF'}];
const cookies=[{'.id':'*B',user:'existing'}];
const profiles=[{'.id':'*C',name:'pix-1h','rate-limit':'5M/1M'}];
const commands=[];
let denyLogin=false;
let trap=false;
function encode(words) {
return Buffer.concat([...words.flatMap(w=>{const b=Buffer.from(w);return [b.length<128?Buffer.from([b.length]):Buffer.from([0x80 | b.length>>8,b.length&255]),b];}),Buffer.from([0])]);
@@ -25,13 +28,20 @@ test('admin API authenticates, creates, lists, blocks, disconnects and exports w
const args=Object.fromEntries(cmd.slice(1).filter(w=>w[0]==='=').map(w=>{const i=w.indexOf('=',1);return[w.slice(1,i),w.slice(i+1)];}));
if(cmd[0]==='/login'){socket.write(encode(['!done']));continue;}
if(trap){socket.write(encode(['!trap','=message=simulated failure']));continue;}
commands.push(cmd);
if(cmd[0]==='/ip/hotspot/active/login' && denyLogin){socket.write(encode(['!trap','=message=no such command']));continue;}
let rows=[];
if(cmd[0]==='/ip/hotspot/user/print') rows=users;
if(cmd[0]==='/ip/hotspot/active/print') rows=active;
if(cmd[0]==='/ip/hotspot/cookie/print') rows=cookies;
if(cmd[0]==='/ip/hotspot/user/profile/print') rows=[{name:'pix-1h','rate-limit':'5M/1M'}];
if(cmd[0]==='/ip/hotspot/user/profile/print') rows=profiles;
if(cmd[0]==='/ip/hotspot/host/print') rows=[{address:'10.10.10.20','mac-address':'AA:BB:CC:DD:EE:11'},{address:'10.10.10.21','mac-address':'AA:BB:CC:DD:EE:22'}];
for(const query of cmd.filter(w=>w[0]==='?')){const i=query.indexOf('=');rows=rows.filter(r=>r[query.slice(1,i)]===query.slice(i+1));}
if(cmd[0]==='/ip/hotspot/user/add') users.push({'.id':'*2',...args,disabled:'false'});
if(cmd[0]==='/ip/hotspot/user/add') users.push({'.id':'*'+(users.length+1).toString(16).toUpperCase(),...args,disabled:'false'});
if(cmd[0]==='/ip/hotspot/user/remove') users.splice(users.findIndex(u=>u['.id']===args.numbers),1);
if(cmd[0]==='/ip/hotspot/user/profile/add') profiles.push({'.id':'*D',...args});
if(cmd[0]==='/ip/hotspot/user/profile/set') Object.assign(profiles.find(p=>p['.id']===args.numbers),args);
if(cmd[0]==='/ip/hotspot/active/login') active.push({'.id':'*E',user:args.user,server:'guest-hotspot',address:args.ip,'mac-address':args['mac-address']});
if(cmd[0]==='/ip/hotspot/user/set') Object.assign(users.find(u=>u['.id']===args.numbers),{disabled:args.disabled==='yes'?'true':'false'});
if(cmd[0]==='/ip/hotspot/active/remove') active.splice(active.findIndex(a=>a['.id']===args.numbers),1);
if(cmd[0]==='/ip/hotspot/cookie/remove') cookies.splice(cookies.findIndex(a=>a['.id']===args.numbers),1);
@@ -65,6 +75,20 @@ test('admin API authenticates, creates, lists, blocks, disconnects and exports w
assert.equal((await request('/api/admin/plans','POST',{plans:[{price:-1}]})).status,400);
assert.equal((await request('/api/mp/webhook','POST',{data:{id:'ORD1',status:'processed'}})).status,503);
assert.equal((await fetch(base+'/api/admin/plans',{method:'POST',headers:{'Content-Type':'application/json'},body:'{'})).status,400);
// Temporary access is confirmed by RouterOS, bounded in seconds, and reused.
res=await request('/api/access/start','POST',{mac:'aa:bb:cc:dd:ee:11'});assert.equal(res.status,200);
const temporary=await res.json();assert.equal(temporary.authenticated,true);assert.ok(temporary.seconds>0 && temporary.seconds<=120);
const free=users.find(u=>u.name==='FREEAABBCCDDEE11');assert.equal(free['limit-uptime'],'120s');assert.equal(free['mac-address'],'AA:BB:CC:DD:EE:11');
assert.equal(profiles.find(p=>p.name==='pix-free')['session-timeout'],'120s');
const loginCount=commands.filter(c=>c[0]==='/ip/hotspot/active/login').length;
res=await request('/api/access/start','POST',{mac:'AA:BB:CC:DD:EE:11'});assert.equal((await res.json()).authenticated,true);
assert.equal(commands.filter(c=>c[0]==='/ip/hotspot/active/login').length,loginCount);
const {getDb}=await import('../src/db.js');getDb().tempAccess.find(t=>t.mac==='AA:BB:CC:DD:EE:11').expiresAt=Date.now()-1;
assert.equal((await request('/api/access/start','POST',{mac:'AA:BB:CC:DD:EE:11'})).status,429);
denyLogin=true;res=await request('/api/access/start','POST',{mac:'AA:BB:CC:DD:EE:22'});const fallback=await res.json();assert.equal(fallback.authenticated,false);assert.ok(fallback.loginUrl);
denyLogin=false;
active.push({'.id':'*F',user:'paid-voucher',server:'guest-hotspot','mac-address':'AA:BB:CC:DD:EE:11'});
const {revokePaymentAccess}=await import('../src/ros.js');await revokePaymentAccess('AA:BB:CC:DD:EE:11');assert.ok(active.some(a=>a.user==='paid-voucher'));assert.ok(!active.some(a=>a.user==='FREEAABBCCDDEE11'));
trap=true;res=await request('/api/admin/users');assert.equal(res.status,500);assert.ok(!(await res.text()).includes('simulated failure'));
const html=await fetch(base+'/admin');assert.equal(html.status,200);assert.ok((await html.text()).includes('Criar usuário'));
});