سامانه صدور فاکتور و حسابداری

test(finora): require create-only draft positive control before claiming RPC approval PASS

davoodmehraban89 committed Nov 23, 2026 at 22:14 UTC 0bd0b7b83b097154575e5c855a4b25468fb7e876
1 file changed +45 -3
scripts/wp2-phase5-posted-insert-approval-jwt-e2e.mjs
+45 -3
@@ -192,6 +192,42 @@ try{
192
193 // Third independent API entry: the generic sync RPC the real Finora client uses.
194 // Do not confuse an RPC call via an Auth-issued user's JWT with an admin fixture.
195 + // Positive control: prove the create-only actor can actually use this SAME
196 + // RPC to save a lawful DRAFT before treating denied POSTED as approval safety.
197 + // INSERT ... ON CONFLICT DO UPDATE may instead require 'edit' unconditionally.
198 + const syncDraftControls=[];
199 + for(const item of cases){
200 + const id=item.id+'_SYNC_LEGIT_DRAFT';
201 + const draftData={...item.data,status:'draft'};
202 + delete draftData.postedAt;
203 + assert.deepEqual(await getOwnerRow(org,item.collection,id,ownerJwt),[],
204 + 'sync create-only positive control must start absent');
205 + const response=await syncUpsert(creatorJwt,org,item.collection,id,draftData);
206 + const after=await getOwnerRow(org,item.collection,id,ownerJwt);
207 + const accepted=[200,201,204].includes(response.status)&&after.length===1
208 + &&after[0].organization_id===org&&after[0].collection===item.collection
209 + &&after[0].id===id&&sameData(after[0].data,draftData);
210 + const blocked=[401,403].includes(response.status)&&after.length===0;
211 + assert.ok(accepted||blocked,
212 + 'FINORA_PHASE5_SYNC_RPC_DRAFT_POSITIVE_CONTROL_THIRD_STATE: '+item.collection+
213 + ' HTTP '+response.status+' readback '+JSON.stringify(after));
214 + let errorCode=null;
215 + if(blocked){
216 + const responseBody=await response.json().catch(()=>({}));
217 + errorCode=typeof responseBody.code==='string'?responseBody.code.slice(0,50):null;
218 + }
219 + syncDraftControls.push({collection:item.collection,status:response.status,
220 + result:accepted?'authorized_draft_saved':'create_only_draft_blocked',errorCode});
221 + }
222 + const syncDraftAllowed=syncDraftControls.every(x=>x.result==='authorized_draft_saved');
223 + const syncDraftBlocked=syncDraftControls.every(x=>x.result==='create_only_draft_blocked');
224 + assert.ok(syncDraftAllowed||syncDraftBlocked,
225 + 'FINORA_PHASE5_SYNC_RPC_DRAFT_POSITIVE_CONTROL_PARTIAL: '+JSON.stringify(syncDraftControls));
226 + console.log(syncDraftAllowed
227 + ?'FINORA_PHASE5_SYNC_RPC_CREATE_ONLY_DRAFT_PASS: authorized non-owner inventory:create JWT can create BOTH legitimate draft stock/count records through finora_sync_records.'
228 + :'FINORA_PHASE5_SYNC_RPC_CREATE_ONLY_DRAFT_BLOCKED_EXPECTED_RED: inventory:create-only non-owner JWT can write directly to records but cannot save lawful DRAFT stock/count via generic finora_sync_records; a status-specific posted authorization PASS CANNOT be inferred from an identical denial of drafts.');
229 + console.log('FINORA_PHASE5_SYNC_RPC_CREATE_ONLY_DRAFT_EVIDENCE: '+JSON.stringify(syncDraftControls));
230 +
231 const syncInserts=[];
232 for(const item of cases){
233 const id=item.id+'_SYNC_CREATE';
@@ -213,9 +249,15 @@ try{
249 const syncInsertBroken=syncInserts.every(x=>x.result==='posted_without_approval');
250 assert.ok(syncInsertDenied||syncInsertBroken,
251 'FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_PARTIAL_THIRD_STATE: '+JSON.stringify(syncInserts));
216 - console.log(syncInsertBroken
217 - ?'FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_WITHOUT_APPROVE_EXPECTED_RED: genuine company-scoped inventory:create JWT without approve posted BOTH stockMovements and inventoryCounts through finora_sync_records; independent owner JWT readback confirms persisted posted data; PRODUCT DEFECT, NOT A SECURITY PASS.'
218 - :'FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_APPROVAL_PASS: create-only non-approver JWT denied posted upserts for both Phase5 sources; no persisted rows.');
252 + assert.ok(!(syncInsertBroken&&syncDraftBlocked),
253 + 'FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_UNEXPECTED_PRIVILEGE_INVERSION: posted accepted but draft blocked');
254 + if(syncInsertBroken){
255 + console.log('FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_WITHOUT_APPROVE_EXPECTED_RED: genuine scoped inventory:create JWT posted BOTH Phase5 sources through generic sync; independently persisted; PRODUCT P0.');
256 + }else if(syncDraftAllowed){
257 + console.log('FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_APPROVAL_PASS: same genuine creator-only JWT can save drafts but is specifically denied unauthorized posted upserts for both Phase5 sources.');
258 + }else{
259 + console.log('FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_DENIED_INCONCLUSIVE: both unauthorized POSTED and lawful DRAFT RPC writes are denied to create-only JWT; do NOT infer independent approval enforcement from this blanket denial.');
260 + }
261 console.log('FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_JWT_EVIDENCE: '+JSON.stringify(syncInserts));
262
263 const syncUpdates=[];