Revalidate captive Wi-Fi with one portal navigation after API login
build-image / build-push (push) Successful in 2m44s

This commit is contained in:
jfeng committed 2026-10-22 17:23:16 +00:00
1 parent 4c7440e233
commit 8d38a9136b
3 files changed
+56 -3

No files matched your search

+39 -2
View File
@@ -8,8 +8,10 @@ const code=html.match(/<script>([\s\S]*?)<\/script>/)[1];
async function portal({search,storage={},access={ok:true,authenticated:true,seconds:120},externalAvailable=true}) {
const ids=[...html.matchAll(/\bid="([^"]+)"/g)].map(m=>m[1]);
const elements=new Map(ids.map(id=>[id,{textContent:'',hidden:false,disabled:false,value:'',innerHTML:'',classList:{toggle(){},add(){},remove(){}},addEventListener(){},appendChild(){}}]));
const requests=[],forms=[],intervals=[];
const requests=[],forms=[],intervals=[],navigations=[];
const sandbox={URL,URLSearchParams,Date,AbortController,console,location:{search},navigator:{},sessionStorage:{getItem:k=>storage[k]??null,setItem:(k,v)=>storage[k]=v,removeItem:k=>delete storage[k]},document:{getElementById:id=>{assert.ok(elements.has(id),'Missing element '+id);return elements.get(id);},body:{append(){}},createElement:tag=>({append(){},submit(){forms.push(tag);}})},setTimeout(){},clearTimeout(){},setInterval:fn=>{intervals.push(fn);return intervals.length;},clearInterval(){},window:{addEventListener(){},scrollTo(){}}};
sandbox.location.href='https://hotspot.example.test/'+(search || '');
sandbox.window.location={replace:url=>navigations.push(url)};
sandbox.fetch=async(path,options)=>{
requests.push({path,options});
if(path==='/api/config')return {ok:true,json:async()=>({hotspotIp:'10.10.10.1',ssid:'Test'})};
@@ -28,7 +30,7 @@ async function portal({search,storage={},access={ok:true,authenticated:true,seco
};
vm.runInNewContext(code,sandbox);
for(let i=0;i<10;i++)await new Promise(r=>setImmediate(r));
return {requests,elements,forms,intervals,checkout:async()=>{
return {requests,elements,forms,intervals,navigations,checkout:async()=>{
elements.get('payerName').value='Teste Usuario';elements.get('payerEmail').value='[email protected]';
await vm.runInNewContext('checkout("plano-1h")',sandbox);
for(let i=0;i<10;i++)await new Promise(r=>setImmediate(r));
@@ -90,3 +92,38 @@ test('unsupported API login falls back to the hotspot form without claiming conf
assert.match(p.elements.get('freeAccessMessage').textContent,/ainda não confirmou/);
assert.equal(p.requests.filter(r=>r.path==='/api/access/report').length,0);
});
test('API login navigates once to trigger captive-network revalidation and restores the PIX',async()=>{
const storage={};
const expiresAt=Date.now()+120000;
const access={ok:true,authenticated:true,seconds:120,expiresAt,probeToken:'private-probe'};
const p=await portal({search:'?mac=AA:BB:CC:DD:EE:11',storage,access});
await p.checkout();
assert.equal(p.navigations.length,1);
const target=new URL(p.navigations[0]);
assert.equal(target.origin,'https://hotspot.example.test');
assert.equal(target.searchParams.get('mac'),'AA:BB:CC:DD:EE:11');
assert.equal(target.searchParams.get('free'),'1');
assert.equal(target.searchParams.get('access'),String(expiresAt));
assert.ok(!target.href.includes('private-probe'));
assert.equal(JSON.parse(storage.pendingPix).paymentId,'Ptest');
assert.equal(p.requests.filter(r=>r.path==='/api/access/report').length,0);
const back=await portal({search:target.search,storage,access:{...access,seconds:103,reused:true}});
assert.equal(back.navigations.length,0);
assert.equal(back.elements.get('copiaCola').textContent,'test-pix');
assert.match(back.elements.get('freeAccessMessage').textContent,/103 segundos/);
assert.ok(back.requests.some(r=>r.path==='/api/access/report'));
assert.equal(storage.freeAccessConfirmedUntil,String(expiresAt));
});
test('an expired or denied window cannot navigate, and a new window can revalidate again',async()=>{
const previous=Date.now()-1000;
const storage={freeAccessConfirmedUntil:String(previous)};
const search='?mac=AA:BB:CC:DD:EE:11&free=1';
const expired=await portal({search,storage,access:{ok:true,authenticated:true,seconds:0,expiresAt:previous}});
assert.equal(expired.navigations.length,0);
const denied=await portal({search,storage,access:{ok:false,error:'Aguarde',expiresAt:Date.now()+120000}});
assert.equal(denied.navigations.length,0);
const next=await portal({search,storage,access:{ok:true,authenticated:true,seconds:120,expiresAt:Date.now()+120000}});
assert.equal(next.navigations.length,1);
});