forked from unimatrix/web
12 lines
2.1 KiB
JavaScript
12 lines
2.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {createManualSealRoute} from '../node/manual-seal-route.mjs';
|
|
const origin='https://cp.dunerealms.com';
|
|
const request=(method='POST',body={action:'grant',targetId:'target',seal:'alpha-tester',reason:'Approved by staff'},headers={origin})=>new Request(origin+'/api/seals/manual',{method,headers:{...headers,'Content-Type':'application/json'},...(method==='POST'?{body:JSON.stringify(body)}:{})});
|
|
function fixture(actor={id:'staff'}){const calls=[];const route=createManualSealRoute({},origin,{authenticate:async()=>actor,readAccess:async()=>({nodes:['web.accounts.view']}),queue:async()=>{},manage:async(db,user,body)=>{calls.push({user,body});return {ok:true,created:true};}});return {route,calls};}
|
|
test('unauthenticated award request returns 401 and never calls service',async()=>{const x=fixture(null),res=await x.route(request());assert.equal(res.status,401);assert.equal(x.calls.length,0);});
|
|
test('Recognition read requires an explicit citizen identity',async()=>{const x=fixture(),res=await x.route(request('GET'));assert.equal(res.status,400);assert.equal(x.calls.length,0);});
|
|
test('wrong Origin is rejected before award service',async()=>{const x=fixture(),res=await x.route(request('POST',undefined,{origin:'https://evil.example'}));assert.equal(res.status,403);assert.equal(x.calls.length,0);});
|
|
test('invalid request body is rejected before service',async()=>{const x=fixture(),res=await x.route(request('POST',{action:'grant',targetId:'target',seal:'alpha-tester',reason:''}));assert.equal(res.status,400);assert.equal(x.calls.length,0);});
|
|
test('valid award reaches authenticated service exactly once',async()=>{const x=fixture(),res=await x.route(request());assert.equal(res.status,200);assert.equal(x.calls.length,1);assert.equal(x.calls[0].user.id,'staff');assert.equal(x.calls[0].body.seal,'alpha-tester');});
|
|
test('service denial status is preserved',async()=>{const route=createManualSealRoute({},origin,{authenticate:async()=>({id:'staff'}),manage:async()=>({ok:false,status:403,message:'Denied'})});const res=await route(request());assert.equal(res.status,403);});
|

Staff