web/website/tests/rebellion-api.test.mjs

14 lines
2.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {randomUUID} from 'node:crypto';
import {createRebellionAPI} from '../node/rebellion-api.mjs';
function fixture(){
const user={id:randomUUID()},clanId=randomUUID(),calls=[];let actor=user,nodes=['web.menu.game'],authCalls=0,change=false;
const service={options:async(a,id)=>{calls.push(['options',a.id,id]);return[];},list:async()=>[],declare:async(a,input)=>{calls.push(['declare',a.id,input]);return {keyId:randomUUID()};},cancel:async()=>({revoked:true})};
const api=createRebellionAPI({prepare:()=>({bind:()=>({first:async()=>({clan_id:clanId})})})},service,{authenticate:async()=>{authCalls++;return change&&authCalls>1?null:actor;},access:async()=>({nodes})});
const call=(body,extra={})=>api(new Request('https://cp.dunerealms.com/api/clans/decisions/rebellion'+(extra.query||''),body===undefined?{}:{method:'POST',headers:{origin:extra.origin||'https://cp.dunerealms.com'},body:JSON.stringify(body)}));
return{call,calls,user,clanId,logout:()=>actor=null,deny:()=>nodes=[],expire:()=>change=true};
}
test('rebellion reads remain private and use the signed-in clan leader',async()=>{const f=fixture(),r=await f.call();assert.equal(r.status,200);assert.equal(r.headers.get('Cache-Control'),'private, no-store');assert.deepEqual(f.calls[0],['options',f.user.id,f.clanId]);assert.equal((await f.call(undefined,{query:'?accountId=other'})).status,400);f.logout();assert.equal((await f.call()).status,401);});
test('browser can declare intent but cannot attest a capture or award an office',async()=>{const f=fixture();assert.equal((await f.call({action:'complete',winnerId:f.user.id})).status,400);assert.equal((await f.call({action:'recordCapture',eventId:randomUUID()})).status,400);assert.equal(f.calls.length,0);assert.equal((await f.call({action:'declare',clanId:f.clanId})).status,201);assert.equal(f.calls[0][0],'declare');});
test('writes reject foreign origins, changed sessions and missing game access',async()=>{const a=fixture();assert.equal((await a.call({action:'declare'},{origin:'https://evil.example'})).status,403);assert.equal(a.calls.length,0);const b=fixture();b.expire();assert.equal((await b.call({action:'declare'})).status,403);assert.equal(b.calls.length,0);const c=fixture();c.deny();assert.equal((await c.call()).status,403);});