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

test(finora): real Auth JWT generic sync RPC posted source approval proof

davoodmehraban89 committed Nov 23, 2026 at 22:10 UTC f9255e2153b10b3dcf272e2478f0086bc94c6382
1 file changed +74
scripts/wp2-phase5-posted-insert-approval-jwt-e2e.mjs
+74
@@ -47,6 +47,16 @@ async function rest(path,token,init={}){
47 ...init,headers:{apikey:anon,Authorization:'Bearer '+token,...(init.headers||{})}
48 });
49 }
50 +async function syncUpsert(token,org,collection,id,data){
51 + // Actual app sync channel: Auth user invokes SECURITY INVOKER RPC, not service-role SQL.
52 + return rest('rpc/finora_sync_records',token,{
53 + method:'POST',headers:{'Content-Type':'application/json',Prefer:'return=minimal'},
54 + body:JSON.stringify({p_organization_id:org,
55 + p_upserts:[{collection,id,data}],p_deletes:[]})
56 + });
57 +}
58 +const sameData=(a,b)=>JSON.stringify(Object.entries(a||{}).sort())===
59 + JSON.stringify(Object.entries(b||{}).sort());
60 function rec(org,owner,collection,id,data){
61 return '('+[q(org),q(owner),q(collection),q(id),q(JSON.stringify(data))+'::jsonb'].join(',')+')';
62 }
@@ -179,6 +189,70 @@ try{
189 console.log('FINORA_PHASE5_POSTED_UPDATE_APPROVAL_PASS: same non-approver JWT denied draft-to-posted UPDATE in both stock/count collections, and both draft rows preserved.');
190 }
191 console.log('FINORA_PHASE5_POSTED_UPDATE_JWT_EVIDENCE: '+JSON.stringify(transitions));
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 + const syncInserts=[];
196 + for(const item of cases){
197 + const id=item.id+'_SYNC_CREATE';
198 + assert.deepEqual(await getOwnerRow(org,item.collection,id,ownerJwt),[],
199 + 'sync posted-insert fixture ID must be absent');
200 + const response=await syncUpsert(creatorJwt,org,item.collection,id,item.data);
201 + const after=await getOwnerRow(org,item.collection,id,ownerJwt);
202 + const denied=[401,403].includes(response.status)&&after.length===0;
203 + const bypass=[200,201,204].includes(response.status)&&after.length===1
204 + &&after[0].organization_id===org&&after[0].collection===item.collection
205 + &&after[0].id===id&&sameData(after[0].data,item.data);
206 + assert.ok(denied||bypass,
207 + 'FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_THIRD_STATE: '+item.collection+
208 + ' RPC HTTP '+response.status+' owner readback '+JSON.stringify(after));
209 + syncInserts.push({collection:item.collection,status:response.status,
210 + result:denied?'properly_denied':'posted_without_approval'});
211 + }
212 + const syncInsertDenied=syncInserts.every(x=>x.result==='properly_denied');
213 + const syncInsertBroken=syncInserts.every(x=>x.result==='posted_without_approval');
214 + assert.ok(syncInsertDenied||syncInsertBroken,
215 + '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.');
219 + console.log('FINORA_PHASE5_SYNC_RPC_POSTED_INSERT_JWT_EVIDENCE: '+JSON.stringify(syncInserts));
220 +
221 + const syncUpdates=[];
222 + for(const item of cases){
223 + const id=item.id+'_SYNC_EDIT';
224 + const draftData={...item.data,ownerUserId:editor.id,status:'draft'};
225 + delete draftData.postedAt;
226 + const postedData={...draftData,status:'posted',postedAt:'2026-11-23T00:00:00.000Z'};
227 + assert.deepEqual(await getOwnerRow(org,item.collection,id,ownerJwt),[],
228 + 'sync draft-to-posted fixture ID must be absent');
229 + const draftCreate=await syncUpsert(editorJwt,org,item.collection,id,draftData);
230 + assert.ok([200,201,204].includes(draftCreate.status),
231 + 'FINORA_PHASE5_SYNC_RPC_DRAFT_CREATE_FAILED: '+item.collection+
232 + ' HTTP '+draftCreate.status);
233 + const before=await getOwnerRow(org,item.collection,id,ownerJwt);
234 + assert.equal(before.length,1,'RPC draft must be visible via independent owner JWT');
235 + assert.ok(sameData(before[0].data,draftData),'RPC draft must preserve exact payload');
236 + const response=await syncUpsert(editorJwt,org,item.collection,id,postedData);
237 + const after=await getOwnerRow(org,item.collection,id,ownerJwt);
238 + const denied=[401,403].includes(response.status)&&after.length===1
239 + &&sameData(after[0].data,draftData);
240 + const bypass=[200,201,204].includes(response.status)&&after.length===1
241 + &&sameData(after[0].data,postedData);
242 + assert.ok(denied||bypass,
243 + 'FINORA_PHASE5_SYNC_RPC_POSTED_UPDATE_THIRD_STATE: '+item.collection+
244 + ' RPC HTTP '+response.status+' owner readback '+JSON.stringify(after));
245 + syncUpdates.push({collection:item.collection,status:response.status,
246 + result:denied?'properly_denied':'posted_without_approval'});
247 + }
248 + const syncUpdateDenied=syncUpdates.every(x=>x.result==='properly_denied');
249 + const syncUpdateBroken=syncUpdates.every(x=>x.result==='posted_without_approval');
250 + assert.ok(syncUpdateDenied||syncUpdateBroken,
251 + 'FINORA_PHASE5_SYNC_RPC_POSTED_UPDATE_PARTIAL_THIRD_STATE: '+JSON.stringify(syncUpdates));
252 + console.log(syncUpdateBroken
253 + ?'FINORA_PHASE5_SYNC_RPC_POSTED_UPDATE_WITHOUT_APPROVE_EXPECTED_RED: genuine company-scoped inventory:read/create/edit JWT WITHOUT approve converted BOTH stockMovements and inventoryCounts draft to posted through finora_sync_records; owner JWT verifies persisted status; PRODUCT DEFECT, NOT A FIX.'
254 + :'FINORA_PHASE5_SYNC_RPC_POSTED_UPDATE_APPROVAL_PASS: non-approver edit JWT denied draft-to-posted RPC for both Phase5 sources; owner JWT verifies original drafts unchanged.');
255 + console.log('FINORA_PHASE5_SYNC_RPC_POSTED_UPDATE_JWT_EVIDENCE: '+JSON.stringify(syncUpdates));
256 }finally{
257 console.log('FINORA_PHASE5_POSTED_INSERT_DISPOSABLE: '+usersCreated+' temporary Auth users; local WP2 teardown owns this database.');
258 }