diff --git a/website/node/rebellion-quests.mjs b/website/node/rebellion-quests.mjs index 94ca0a5b..4d69546e 100644 --- a/website/node/rebellion-quests.mjs +++ b/website/node/rebellion-quests.mjs @@ -47,7 +47,7 @@ export function createRebellionQuests(central,keys,{clock=Date.now,authorizeProd const season=await readActiveXPSeason(polProgressionStore(central),clock()); const fields={clanId:input.clanId,officeKey:input.officeKey,expectedTermId:input.expectedTermId,expectedHomeBlockId:input.expectedHomeBlockId},scope=season.season_id+':REBELLION_'+input.requestId; const grant=await keys.issue({accountId:actor.id,keyType:'quest',scopeId:scope,authorize:async(tx,{keyId})=>{ - const prior=await tx.first('SELECT account_id,fields_json FROM jarvis_generated_instructions WHERE request_id=? AND action_id=?',[input.requestId,DECLARE]);if(prior){if(prior.account_id!==actor.id||prior.fields_json!==JSON.stringify(fields))reject('Rebellion reference conflict.');return;} + const prior=await tx.first('SELECT i.account_id,i.fields_json,k.scope_id FROM jarvis_generated_instructions i JOIN organisation_keys k ON k.id=i.key_id WHERE i.request_id=? AND i.action_id=?',[input.requestId,DECLARE]);if(prior){if(prior.scope_id!==scope||prior.account_id!==actor.id||prior.fields_json!==JSON.stringify(fields))reject('Rebellion reference conflict.');return;} const declaration={...await target(tx,actor,input.clanId,input.officeKey),seasonId:season.season_id,declaredAt:clock()};if(declaration.sourceTermId!==input.expectedTermId||declaration.homeBlockId!==input.expectedHomeBlockId)reject('The incumbent or Home Block changed. Review the current decision.'); const active=await tx.first("SELECT i.key_id FROM jarvis_generated_instructions i JOIN organisation_keys k ON k.id=i.key_id WHERE i.action_id=? AND i.account_id=? AND i.state='issued' AND k.revoked_at IS NULL AND i.fields_json=?",[DECLARE,actor.id,JSON.stringify(fields)]);if(active)reject('This clan already has an open rebellion quest for that office.'); await insert(tx,{keyId,requestId:input.requestId,accountId:actor.id,action:DECLARE,fields,manifest:{declaration},state:'issued'}); diff --git a/website/tests/rebellion-quests.test.mjs b/website/tests/rebellion-quests.test.mjs index 9e66089e..24a93a29 100644 --- a/website/tests/rebellion-quests.test.mjs +++ b/website/tests/rebellion-quests.test.mjs @@ -26,3 +26,13 @@ test('stale confirmation and revoked evidence cannot transfer a deed or consume assert.equal((await f.db.first('SELECT outcome FROM political_office_terms WHERE id=?',[f.term])).outcome,'winner'); }finally{f.db.close();} }); + +test('a request UUID from an earlier season cannot create an orphan declaration key',async()=>{ + const f=await fixture();try{ + await f.service.declare(f.winner,f.input);const before=(await f.keys.list(f.winner.id)).length; + await f.db.query("UPDATE jarvis_pol_progression_seasons SET state='closed' WHERE season_id='S01'"); + await f.db.query("INSERT INTO jarvis_pol_progression_seasons(season_id,starts_at,ends_at,level_cap,curve_base,state) VALUES('S02',0,?,0,25,'active')",[f.now()+100000]); + await assert.rejects(f.service.declare(f.winner,f.input),/reference conflict/); + assert.equal((await f.keys.list(f.winner.id)).length,before); + }finally{f.db.close();} +});