forked from unimatrix/web
50 lines
4 KiB
JavaScript
50 lines
4 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {randomUUID} from 'node:crypto';
|
|
import {createProgressionAPI} from '../node/progression-api.mjs';
|
|
|
|
function fixture(){
|
|
const user={id:randomUUID()},calls=[];let signedIn=true;
|
|
const quests={list:async actor=>({accountId:actor.id,quests:[]}),start:async(actor,body)=>{calls.push(['start',actor.id,body]);return{id:'instrument',created:true};},claim:async(actor,body)=>{calls.push(['claim',actor.id,body]);return{keyId:'instrument'};},processCompletions:async scope=>calls.push(['complete',scope])};
|
|
const db={query:async(sql,args)=>{calls.push(['query',sql,args]);return[];}};
|
|
const api=createProgressionAPI(db,{quests,authenticate:async()=>signedIn?user:null});
|
|
const call=(path='',body,headers={})=>api(new Request('https://cp.dunerealms.com/api/progression'+path,body===undefined?{}:{method:'POST',headers:{origin:'https://cp.dunerealms.com','Content-Type':'application/json',...headers},body:JSON.stringify(body)}));
|
|
return {user,calls,call,logout:()=>{signedIn=false;}};
|
|
}
|
|
test('progression reads require identity and history is scoped to the current account',async()=>{
|
|
const f=fixture();assert.equal((await f.call()).headers.get('cache-control'),'private, no-store');
|
|
assert.equal((await (await f.call()).json()).accountId,f.user.id);
|
|
assert.equal((await f.call('/history?page=2')).status,200);
|
|
const read=f.calls.find(c=>c[0]==='query');assert.deepEqual(read[2],[f.user.id]);assert.match(read[1],/OFFSET 25$/);
|
|
assert.equal((await f.call('/history?accountId=other')).status,400);
|
|
f.logout();assert.equal((await f.call()).status,401);
|
|
});
|
|
test('mission writes reject foreign origins and client supplied XP or account overrides',async()=>{
|
|
const f=fixture(),body={definitionId:'S1-A-01',confirmed:true};
|
|
assert.equal((await f.call('/start',body,{origin:'https://other.example'})).status,403);
|
|
assert.equal((await f.call('/start',{...body,accountId:randomUUID()})).status,400);
|
|
assert.equal((await f.call('/start',{...body,xp:999999})).status,400);
|
|
assert.equal((await f.call('/start',{...body,confirmed:false})).status,400);
|
|
assert.equal(f.calls.length,0);
|
|
assert.equal((await f.call('/start',body)).status,200);assert.equal(f.calls[0][1],f.user.id);
|
|
});
|
|
test('claim completion processing never broadens to another player',async()=>{
|
|
const f=fixture();const response=await f.call('/claim',{definitionId:'S1-A-01',revision:1,requestId:randomUUID(),confirmed:true});
|
|
assert.equal(response.status,200);assert.deepEqual(f.calls.at(-1),['complete',{accountId:f.user.id}]);
|
|
assert.equal((await f.call('/xp/redeem',{code:'DEV-forged',requestId:randomUUID(),confirmed:true})).status,400);
|
|
});
|
|
|
|
test('POL API routes use only the POL history store and reject cross-namespace paths',async()=>{
|
|
const user={id:randomUUID()},queries=[];
|
|
const {polProgressionStore}=await import('../node/progression-pol.mjs');
|
|
const central={query:async(sql,args)=>{queries.push([sql,args]);return[];}};
|
|
const quests={list:async()=>({xpType:'pol',quests:[]}),start:async()=>({id:'pol',created:true})};
|
|
const api=createProgressionAPI(polProgressionStore(central),{quests,authenticate:async()=>user,basePath:'/api/progression/pol'});
|
|
assert.equal((await api(new Request('https://cp.dunerealms.com/api/progression/pol/history?page=1'))).status,200);
|
|
assert.match(queries[0][0],/FROM jarvis_pol_progression_history/);assert.deepEqual(queries[0][1],[user.id]);
|
|
assert.equal((await api(new Request('https://cp.dunerealms.com/api/progression/history'))).status,404);
|
|
assert.equal((await api(new Request('https://cp.dunerealms.com/api/progression/pol?accountId='+randomUUID()))).status,404);
|
|
const post=(body,origin='https://cp.dunerealms.com')=>api(new Request('https://cp.dunerealms.com/api/progression/pol/start',{method:'POST',headers:{origin,'Content-Type':'application/json'},body:JSON.stringify(body)}));
|
|
assert.equal((await post({definitionId:'POL-ATREIDES-01',confirmed:true,xp:9999})).status,400);
|
|
assert.equal((await post({definitionId:'POL-ATREIDES-01',confirmed:true},'https://foreign.example')).status,403);
|
|
});
|

Staff