forked from unimatrix/web
10 lines
2.4 KiB
JavaScript
10 lines
2.4 KiB
JavaScript
import {test} from 'node:test';import assert from 'node:assert/strict';
|
|
import {createGameKitGrantsAPI} from '../node/game-kit-grants.mjs';
|
|
const uuid='49d2fe76-7977-4d49-bd39-e48fc8c648b8',settings={GAME_LINK_SECRET:'b'.repeat(64)};
|
|
function request(body,token=settings.GAME_LINK_SECRET){return new Request('https://cp.dunerealms.com/internal/game/kit-grants',{method:'POST',headers:{authorization:'Bearer '+token,'content-type':'application/json'},body:typeof body==='string'?body:JSON.stringify(body)});}
|
|
test('the private gift reader rejects unauthorised requests before reading any entitlement',async()=>{let calls=0;const api=createGameKitGrantsAPI({},settings,{pending:async()=>{calls++;return[];}});assert.equal((await api(request({uuid,serverKey:'lobby'},'wrong'))).status,401);assert.equal(calls,0);});
|
|
test('does not accept arbitrary actor/kit overrides',async()=>{const api=createGameKitGrantsAPI({},settings,{pending:async()=>{throw Error('Must not read');}});assert.equal((await api(request({uuid,serverKey:'lobby',actorId:uuid}))).status,400);});
|
|
test('enforces the streaming request bound',async()=>{const api=createGameKitGrantsAPI({},settings);assert.equal((await api(request('x'.repeat(1025)))).status,413);});
|
|
test('returns only owed gifts for the current UUID',async()=>{const api=createGameKitGrantsAPI({},settings,{pending:async(db,player,server)=>{assert.equal(player,uuid);assert.equal(server,'arrakis');return[];}}),r=await api(request({uuid,serverKey:'arrakis'}));assert.equal(r.status,200);assert.deepEqual(await r.json(),{ok:true,schema:1,playerUuid:uuid,grants:[]});});
|
|
test('native delivered receipts remove gifts from the read response',async()=>{const envelope={payload:Buffer.from(JSON.stringify({recipients:[{receiptId:'receipt'}]})).toString('base64')};const api=createGameKitGrantsAPI({},settings,{pending:async()=>[envelope],pool:{async query(q){assert.equal(q.values[0],uuid);assert.ok(q.text.includes("kind='kit-grant'"));return{rows:[{id:'receipt'}]};}}});const r=await api(request({uuid,serverKey:'lobby'}));assert.equal(r.status,200);assert.deepEqual((await r.json()).grants,[]);});
|
|
test('an unavailable register retains the gift and exposes no database detail',async()=>{const api=createGameKitGrantsAPI({},settings,{pending:async()=>{throw Error('database password secret');}}),r=await api(request({uuid,serverKey:'lobby'}));assert.equal(r.status,503);assert.ok(!(await r.text()).includes('password'));});
|

Staff