web/website/tests/account-test-identity.test.mjs
unimatrixgit 3cd5510f03
All checks were successful
DuneRealms Web CI / web (push) Successful in 3m7s
Register scoped Jarvis account test identity action with synthetic UUID adapter
2026-11-19 20:57:23 +01:00

59 lines
7 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
import {companyFixture} from './company-deed-fixture.mjs';
import {createMasterKeyGenerator,initializeJarvisGenerator} from '../node/jarvis-master-generator.mjs';
import {TEST_IDENTITY_ACTION,TEST_IDENTITY_PERMISSION} from '../node/account-test-identity.mjs';
import {createOrganisationKeys} from '../node/organisation-keys.mjs';
async function fixture(){
const f=await companyFixture();
f.db.sqlite.exec("ALTER TABLE site_game_links ADD COLUMN minecraft_name TEXT;ALTER TABLE site_game_links ADD COLUMN linked_at INTEGER;CREATE TABLE site_game_identities(minecraft_uuid TEXT PRIMARY KEY,first_linked_at INTEGER,last_name TEXT);CREATE TABLE site_link_challenges(user_id TEXT PRIMARY KEY,token_hash TEXT,expires_at INTEGER);CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT,created_at INTEGER);");
await f.db.query('DELETE FROM site_game_links WHERE user_id=?',[f.people.Other.id]);
await f.db.query("UPDATE site_accounts SET username='gsmaccount' WHERE id=?",[f.people.Other.id]);
let allowed=true,fail=false;
const central={prepare:f.db.prepare.bind(f.db),query:f.db.query.bind(f.db),transaction:work=>f.db.transaction(tx=>work({...tx,query:async(sql,args)=>{if(fail&&sql.startsWith('INSERT INTO site_test_game_identities'))throw Error('Storage failure');return tx.query(sql,args);}}))};
await initializeJarvisGenerator(central,{mysql:false});
const keys=createOrganisationKeys(central,{secret:'ab'.repeat(32)}),authorize=async(actor,node)=>allowed&&actor?.id===f.people.Owner.id;
return {...f,central,keys,authorize,service:createMasterKeyGenerator(central,keys,{authorize}),input:{actionId:TEST_IDENTITY_ACTION,templateVersion:1,requestId:randomUUID(),values:{ticketId:'unimatrix override',accountName:'gsmaccount',reference:'Owner-requested test identity'}},revoke:()=>{allowed=false;},fail:()=>{fail=true;}};
}
test('saved master template generates one scoped key; redemption assigns a labelled test UUID once',async()=>{
const f=await fixture();try{
const others=await f.db.query('SELECT * FROM site_game_links ORDER BY user_id'),accounts=await f.db.query('SELECT * FROM site_accounts ORDER BY id');
const key=await f.service.issue(f.people.Owner,f.input);assert.match(key.code,/^ACC-TEST-/);assert.deepEqual(await f.db.query('SELECT * FROM site_game_links ORDER BY user_id'),others);
assert.equal((await f.service.issue(f.people.Owner,f.input)).keyId,key.keyId);
const request={code:key.code,requestId:randomUUID()};await f.service.redeem(f.people.Owner,request);assert.equal((await f.service.redeem(f.people.Owner,request)).replayed,true);
const link=await f.db.first('SELECT * FROM site_game_links WHERE user_id=?',[f.people.Other.id]);assert.match(link.minecraft_uuid,/^[a-f0-9-]{14}4/);assert.equal(link.minecraft_name,'TEST_gsmaccount');
const record=await f.db.first('SELECT * FROM site_test_game_identities WHERE minecraft_uuid=?',[link.minecraft_uuid]);assert.equal(record.key_id,key.keyId);assert.equal(record.issued_by,f.people.Owner.id);
assert.deepEqual(await f.db.query('SELECT * FROM site_game_links WHERE user_id<>? ORDER BY user_id',[f.people.Other.id]),others);assert.deepEqual(await f.db.query('SELECT * FROM site_accounts ORDER BY id'),accounts);
assert.equal((await f.db.first("SELECT COUNT(*) n FROM account_audit WHERE action='jarvis-test-game-identity-assigned'")).n,1);
}finally{f.db.close();}
});
for(const scenario of ['linked','pending','renamed','disabled','role-change'])test('target change is rejected without consuming the key: '+scenario,async()=>{
const f=await fixture();try{const key=await f.service.issue(f.people.Owner,f.input),id=f.people.Other.id;
if(scenario==='linked')await f.db.query('INSERT INTO site_game_links(user_id,minecraft_uuid) VALUES(?,?)',[id,randomUUID()]);
if(scenario==='pending')await f.db.query('INSERT INTO site_link_challenges VALUES(?,?,?)',[id,'pending',Date.now()+60000]);
if(scenario==='renamed')await f.db.query("UPDATE site_accounts SET username='changed' WHERE id=?",[id]);
if(scenario==='disabled')await f.db.query("UPDATE site_accounts SET password_hash='!locked' WHERE id=?",[id]);
if(scenario==='role-change')await f.db.query("UPDATE site_accounts SET role='owners' WHERE id=?",[id]);
const before=await f.db.query('SELECT * FROM site_game_links ORDER BY user_id');await assert.rejects(f.service.redeem(f.people.Owner,{code:key.code,requestId:randomUUID()}),/Instruction/);assert.deepEqual(await f.db.query('SELECT * FROM site_game_links ORDER BY user_id'),before);assert.equal((await f.keys.list(f.people.Owner.id)).find(k=>k.id===key.keyId).status,'available');
}finally{f.db.close();}
});
test('wrong issuer, revoked permission and injected fields cannot assign an identity',async()=>{
const f=await fixture();try{
await assert.rejects(f.service.issue(f.people.Member,f.input),/authority/);
await assert.rejects(f.service.issue(f.people.Owner,{...f.input,values:{...f.input.values,uuid:randomUUID()}}),/Unknown/);
const key=await f.service.issue(f.people.Owner,f.input);await assert.rejects(f.service.redeem(f.people.Member,{code:key.code,requestId:randomUUID()}),/authority/);f.revoke();await assert.rejects(f.service.redeem(f.people.Owner,{code:key.code,requestId:randomUUID()}),/authority/);
}finally{f.db.close();}
});
test('UUID collision and partial write failure roll back the entire identity assignment',async()=>{
for(const collision of [true,false]){const f=await fixture();try{
const key=await f.service.issue(f.people.Owner,f.input),row=await f.db.first('SELECT manifest_json FROM jarvis_generated_instructions WHERE key_id=?',[key.keyId]),manifest=JSON.parse(row.manifest_json);
if(collision)await f.db.query('INSERT INTO site_game_identities VALUES(?,?,?)',[manifest.minecraftUuid,1,'already-used']);else f.fail();
await assert.rejects(f.service.redeem(f.people.Owner,{code:key.code,requestId:randomUUID()}));assert.equal(await f.db.first('SELECT * FROM site_game_links WHERE user_id=?',[f.people.Other.id]),null);assert.equal(await f.db.first('SELECT * FROM site_test_game_identities WHERE key_id=?',[key.keyId]),null);assert.equal((await f.keys.list(f.people.Owner.id)).find(k=>k.id===key.keyId).status,'available');
}finally{f.db.close();}}
});
test('catalogue availability follows this action permission, not unrelated clan permissions',async()=>{
const f=await fixture();try{const service=createMasterKeyGenerator(f.central,f.keys,{authorize:async(actor,node)=>node!==TEST_IDENTITY_PERMISSION}),catalogue=await service.catalogue(f.people.Owner);assert.equal(catalogue.templates.find(t=>t.id==='accounts').actions[0].available,false);await assert.rejects(service.issue(f.people.Owner,f.input),/authority/);}finally{f.db.close();}
});
test('the core key service refuses unscoped or adapter-less account identity grants',async()=>{
const f=await fixture();try{await assert.rejects(f.keys.issue({accountId:f.people.Owner.id,keyType:'account-test-identity',scopeId:'original'}),/bound dossier/);}finally{f.db.close();}
});