web/website/tests/server-project-api.test.mjs
2026-11-27 23:43:05 +00:00

17 lines
2.1 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
import {createServerProjectAPI} from '../node/server-project-api.mjs';
function fixture(){let user={id:randomUUID()};const calls=[];const service={list:async actor=>({projects:[],accountId:actor.id}),submit:async(actor,body)=>{calls.push({actor,body});return {replayed:false};},review:async()=>{calls.push('review');return{};}};
const api=createServerProjectAPI({},service,{authenticate:async()=>user});
return{calls,logout:()=>{user=null;},call:(path='',body,origin='https://cp.dunerealms.com')=>api(new Request('https://cp.dunerealms.com/api/projects'+path,body?{method:'POST',headers:{origin,'Content-Type':'application/json'},body:JSON.stringify(body)}:{}))};}
test('project API scopes private requests to the authenticated account and rejects browser overrides',async()=>{
const f=fixture();assert.equal((await f.call()).status,200);assert.equal((await f.call('?accountId=other')).status,400);
const submit={requestId:randomUUID(),title:'Arena',proposal:'A proposal for a new public arena.',requestedMinor:25000,confirmed:true};
assert.equal((await f.call('/submit',{...submit,applicantId:randomUUID()})).status,400);assert.equal((await f.call('/submit',submit,'https://elsewhere.example')).status,403);assert.equal(f.calls.length,0);
assert.equal((await f.call('/submit',submit)).status,200);assert.equal(f.calls.length,1);assert.equal(typeof f.calls[0].actor.revalidate,'function');
f.logout();assert.equal((await f.call()).status,401);
});
test('review instructions require a boolean decision and cannot inject bank accounts or payment destinations',async()=>{
const f=fixture(),review={projectId:randomUUID(),requestId:randomUUID(),version:1,approve:true,approvedMinor:100,note:'Approved.',confirmed:true};
assert.equal((await f.call('/review',{...review,approve:'true'})).status,400);assert.equal((await f.call('/review',{...review,targetNumber:'any-bank-account'})).status,400);assert.equal(f.calls.length,0);
assert.equal((await f.call('/review',review)).status,200);
});