web/website/tests/kit-studio-fetch-policy.test.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

159 lines
9.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
test('kit studio arc and player calls refuse redirects and cached responses',()=>{
const calls=[...source.matchAll(/fetcher\('http:\/\/127\.0\.0\.1:15433\/kits\/(?:arc|player)\?uuid='[\s\S]*?AbortSignal\.timeout\(10000\)\}\)/g)];
assert.equal(calls.length,2,'Both authenticated native lookups must be guarded');
for(const [call] of calls){
assert.match(call,/redirect:'error'/);
assert.match(call,/cache:'no-store'/);
assert.match(call,/Authorization:'Bearer '/);
}
});
test('kit studio rejects malformed identities and permissions before database access',async()=>{
const {createKitStudio}=await import('../control-panel/kit-studio.mjs');
for(const actor of [null,{}, {id:0},{id:' '}]){
let accesses=0,reads=0;
const handler=createKitStudio({prepare(){reads++;throw Error('unexpected database access');}}, {}, {
authenticate:async()=>actor,access:async()=>{accesses++;return {nodes:['web.menu.inventory']};}
});
assert.equal((await handler(new Request('https://cp.dunerealms.com/api/kits/studio'))).status,401);
assert.equal(accesses,0);assert.equal(reads,0);
}
for(const rights of [null,{}, {nodes:null},{nodes:'web.menu.inventory'}, {nodes:[],ranks:[]},{nodes:['web.menu.inventory'],ranks:'admin'}]){
let reads=0;
const handler=createKitStudio({prepare(){reads++;throw Error('unexpected database access');}}, {}, {
authenticate:async()=>({id:'account-1',role:'user'}),access:async()=>rights
});
assert.equal((await handler(new Request('https://cp.dunerealms.com/api/kits/studio'))).status,403);
assert.equal(reads,0);
}
});
test('kit studio rejects missing or malformed linked Minecraft UUID before native requests',async()=>{
const {createKitStudio}=await import('../control-panel/kit-studio.mjs');
for(const linkedUuid of [null,'','not-a-uuid','../../admin',42]){
let calls=0;
const db={prepare(){return {bind(){return {async first(){return linkedUuid===null?null:{minecraft_uuid:linkedUuid};}}}};}};
const handler=createKitStudio(db,{SERVER_CONTROL_TOKEN:'x'.repeat(64)}, {
authenticate:async()=>({id:'account-1',role:'user'}),
access:async()=>({nodes:['web.menu.inventory'],ranks:[]}),
fetcher:async()=>{calls++;throw Error('Native service should not be called');}
});
const response=await handler(new Request('https://cp.dunerealms.com/api/kits/studio'));
assert.equal(response.status,409);assert.equal(calls,0);
}
});
test('kit studio rejects non-JSON native arc responses before reading kit records',async()=>{
const {createKitStudio}=await import('../control-panel/kit-studio.mjs');
const uuid='12345678-1234-4234-9234-123456789012';
let calls=0;
const db={prepare(){return {bind(){return {async first(){return {minecraft_uuid:uuid};}}}};}};
const handler=createKitStudio(db,{SERVER_CONTROL_TOKEN:'x'.repeat(64)}, {
authenticate:async()=>({id:'account-1',role:'user'}),
access:async()=>({nodes:['web.menu.inventory'],ranks:[]}),
fetcher:async()=>{calls++;return new Response('<html>invalid</html>',{headers:{'Content-Type':'text/html'}});}
});
const response=await handler(new Request('https://cp.dunerealms.com/api/kits/studio'));
assert.equal(response.status,503);assert.equal(calls,1);
});
test('server kit studio refuses missing or invalid registered owner identities',async()=>{
const {createKitStudio}=await import('../control-panel/kit-studio.mjs');
for(const owner of [null,{}, {id:null}, {id:' '}, {id:0}]){
let nativeCalls=0;
const db={prepare(){return {async first(){return owner;}};}};
const handler=createKitStudio(db,{SERVER_CONTROL_TOKEN:'x'.repeat(64)}, {
authenticate:async()=>({id:'admin',role:'owners'}),
access:async()=>({nodes:['web.menu.inventory'],ranks:['owners'],rank:'owners'}),
fetcher:async()=>{nativeCalls++;throw Error('unexpected native request');}
});
const response=await handler(new Request('https://cp.dunerealms.com/api/kits/server-studio'));
assert.equal(response.status,503);assert.equal(nativeCalls,0);
}
});
test('kit studio refuses oversized native arc bodies before database kit reads',async()=>{
const {createKitStudio}=await import('../control-panel/kit-studio.mjs');
const uuid='12345678-1234-4234-9234-123456789012';
let nativeCalls=0;
const db={prepare(){return {bind(){return {async first(){return {minecraft_uuid:uuid};}}}};}};
const handler=createKitStudio(db,{SERVER_CONTROL_TOKEN:'x'.repeat(64)},{
authenticate:async()=>({id:'account-1',role:'user'}),access:async()=>({nodes:['web.menu.inventory'],ranks:[]}),
fetcher:async()=>{nativeCalls++;return Response.json({playerUuid:uuid,chapters:[],padding:'x'.repeat(1048576)});}
});
assert.equal((await handler(new Request('https://cp.dunerealms.com/api/kits/studio'))).status,503);
assert.equal(nativeCalls,1);
});
test('kit studio write routes reject foreign origins before account and database access',async()=>{
const {createKitStudio}=await import('../control-panel/kit-studio.mjs');
for(const path of ['/api/kits/studio','/api/kits/server-studio']){
let auth=0,reads=0;
const handler=createKitStudio({prepare(){reads++;throw Error('No database reads');}}, {CP_ORIGIN:'https://cp.dunerealms.com'}, {
authenticate:async()=>{auth++;return {id:'account-1'};},access:async()=>({nodes:['web.menu.inventory'],ranks:[]})
});
const response=await handler(new Request('https://cp.dunerealms.com'+path,{method:'POST',headers:{Origin:'https://evil.example','Content-Type':'application/json'},body:'{}'}));
assert.equal(response.status,403);assert.equal(auth,0);assert.equal(reads,0);
}
});
test('unpublishing a kit preserves historical listing price, version and office metadata',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/UPDATE site_kit_listings SET version=version\+1,published=0,updated_at=\? WHERE id=\? AND version=\? AND published=1/);
assert.match(source,/UPDATE site_kit_listings SET blueprint_version=\?,price_cents=\?,version=version\+1,published=1/);
assert.doesNotMatch(source,/b\.action==='unpublish'\?0:1/);
});
test('saving a draft withdraws published listings without changing historical receipt fields',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/if\(b\.action==='save'\)await query\('UPDATE site_kit_listings SET published=0,version=version\+1,updated_at=\? WHERE blueprint_id=\? AND published=1'/);
assert.doesNotMatch(source,/UPDATE site_kit_listings SET price_cents=\?,published=0/);
});
test('restoring an archived design never republishes or reactivates its listings',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/if\(b\.action==='archive'\)await query\('UPDATE site_kit_listings SET active=0,published=0,version=version\+1,updated_at=\? WHERE blueprint_id=\? AND \(active=1 OR published=1\)'/);
assert.doesNotMatch(source,/b\.action==='restore'\?1:0,now,current\.id\]\);\s*await query\('UPDATE site_kit_listings/);
});
test('saving an existing kit does not enter the new-blueprint insertion branch',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/if\(current\)\{\s*await query\('UPDATE site_kit_blueprints SET/);
assert.match(source,/if\(b\.action==='save'\)await query\('UPDATE site_kit_listings SET published=0/);
assert.match(source,/\}else\{designId=randomUUID\(\);await query\('INSERT INTO site_kit_blueprints/);
});
test('unpublish refuses nonexistent listings rather than reporting a successful withdrawal',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/b\.action==='unpublish'&&\(\!listing\|\|Number\(listing\.published\)!==1\)\)throw Error\('conflict'\)/);
assert.match(source,/SELECT id,version,published FROM site_kit_listings WHERE blueprint_id=\? AND shop_type=\? AND clan_id <=> \? FOR UPDATE/);
});
test('unpublish refuses already withdrawn listings under the row lock',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/SELECT id,version,published FROM site_kit_listings WHERE blueprint_id=\? AND shop_type=\? AND clan_id <=> \? FOR UPDATE/);
assert.match(source,/b\.action==='unpublish'&&\(\!listing\|\|Number\(listing\.published\)!==1\)\)throw Error\('conflict'\)/);
});
test('Kit Studio rejects ambiguous blueprint rows instead of updating an arbitrary design',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/SELECT id,version,upgrade_json,active FROM site_kit_blueprints WHERE owner_user_id=\? AND quality=\? AND commerce_kind=\? FOR UPDATE/);
assert.match(source,/if\(rows\.length>1\)throw Error\('conflict'\);const current=rows\[0\]/);
});
test('listing writes require the expected revision and exactly one updated row',async()=>{
const {readFileSync}=await import('node:fs');
const source=readFileSync(new URL('../control-panel/kit-studio.mjs',import.meta.url),'utf8');
assert.match(source,/WHERE id=\? AND version=\? AND published=1/);
assert.match(source,/WHERE id=\? AND version=\?'\,\[version,b\.priceCents,now,officeKey,officeTerm,factionId,listing\.id,b\.listingVersion\]/);
assert.match(source,/if\(updated\.affectedRows!==1\)throw Error\('conflict'\)/);
});