web/website/tests/contact.test.mjs
unimatrixgit 9dcd9b084a Integrate shared planetary inventories and account sales with the current site
Merge the supplied inventory/exchange package into the latest DuneRealms source.
Keep community-reward receipts, verified UUID linking, native menus and current
website artwork, Dutch naming and account controls. Wire the inventory/sales
adapters into the real CP session and verified-link schema, with isolated reads
and honest unconfigured states. Preserve lobby/ender separation and Core-owned
atomic exchange checkpoints.

Validation: website build and 56 tests; 12 inventory/market adapter tests;
Core/Essentials and NPC/Terrain/Lobby builds and tests; local browser inspection
of the themed inventory, mannequin and History of sales with fictional fixtures.
Paper 1.21.8 source only: live 1.21.4 binaries and databases are unchanged.
2026-10-27 19:44:22 +01:00

33 lines
2.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {createContact} from '../node/contact.mjs';
import {solveChallenge} from 'altcha-lib';
import {deriveKey} from 'altcha-lib/algorithms/pbkdf2';
const base='https://example.test';
const challenge=async handler=>(await handler(new Request(base+'/api/contact/challenge'))).json();
const request=(payload,origin=base)=>new Request(base+'/api/contact/reveal',{method:'POST',headers:{Origin:origin,'Content-Type':'application/json'},body:JSON.stringify({payload})});
const proof=async c=>Buffer.from(JSON.stringify({challenge:c,solution:await solveChallenge({challenge:c,deriveKey})})).toString('base64');
test('Contact email requires an authentic, unexpired, single-use CAPTCHA proof',async()=>{
let time=Date.now();const handler=createContact({CONTACT_EMAIL:'operator@example.test'},{cost:1,counter:2,now:()=>time});
const c=await challenge(handler),token=await proof(c);
assert.ok(!JSON.stringify(c).includes('operator@example.test'));
assert.equal((await handler(request(token,'https://evil.test'))).status,403);
assert.equal((await handler(request('forged'))).status,400);
const response=await handler(request(token));assert.equal(response.status,200);assert.equal((await response.json()).email,'operator@example.test');
assert.equal((await handler(request(token))).status,403);
const expired=await proof(await challenge(handler));time+=121000;
assert.equal((await handler(request(expired))).status,403);
assert.equal((await handler(new Request(base+'/api/contact/reveal'))).status,405);
});
test('Forged proof and concurrent replay never reveal the email twice',async()=>{
const handler=createContact({CONTACT_EMAIL:'operator@example.test'},{cost:1,counter:2});
const c=await challenge(handler);
const invalid=Buffer.from(JSON.stringify({challenge:c,solution:{counter:2,derivedKey:'0'.repeat(64)}})).toString('base64');
assert.equal((await handler(request(invalid))).status,403);
const token=await proof(await challenge(handler));
const replies=await Promise.all([handler(request(token)),handler(request(token))]);
assert.deepEqual(replies.map(r=>r.status).sort(),[200,403]);
});