@davoodmehraban89 / Factor-easy / commits / 83e031e
Fork 0

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

test(stage1): preserve unique account roles and cover sale/purchase semantic negatives

davoodmehraban89 committed Oct 17, 2026 at 10:49 UTC 83e031e1cda4b38662d37d61b01b6031634e854f
3 files changed +138 -2
.github/workflows/phase1a-supabase-parity.yml
+2
@@ -99,6 +99,8 @@ jobs:
99 run: PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f supabase/tests/phase1a_stage1_account_role_controls.sql
100 - name: PURCHASE RED FIRST — valid purchase must use canonical account mapping
101 run: PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f supabase/tests/phase1a_stage1_purchase_red.sql
102 + - name: Stage 1 sale/purchase wrong-balanced-account, unsupported, cross-org and reversal-idempotence negatives
103 + run: PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f supabase/tests/phase1a_stage1_sale_purchase_negatives.sql
104 - name: Run 1A green-after-red authenticated adversarial tests
105 run: node scripts/phase1a-adversarial-red.mjs
106 - name: Test 1A versioned CAS and two-tenant RLS
supabase/tests/phase1a_stage1_account_role_controls.sql
+5 -2
@@ -184,13 +184,16 @@ BEGIN
184 UPDATE public.records SET data=data||'{"postingContractRule":"sale-v1","postingContractVersion":1}'::jsonb
185 WHERE organization_id=org AND collection='journalVouchers' AND id='ROLE_DRAFT_BEFORE_CHANGE';
186 END $t$;
187 +UPDATE public.records SET data=data-'systemRole'
188 + WHERE organization_id=current_setting('finora_test.org_a')::uuid
189 + AND collection='accounts' AND id='ACC_STAGE_1';
190 INSERT INTO public.records(organization_id,owner_id,collection,id,data)
191 SELECT organization_id,owner_id,'accounts','ACC_STAGE_3',
189 - data||'{"code":"1105","title":"صندوق جایگزین تست"}'::jsonb
192 + data||'{"code":"1105","title":"صندوق جایگزین تست","systemRole":"cash_default"}'::jsonb
193 FROM public.records WHERE organization_id=current_setting('finora_test.org_a')::uuid
194 AND collection='accounts' AND id='ACC_STAGE_1';
195 INSERT INTO public.records(organization_id,owner_id,collection,id,data)
193 -SELECT organization_id,owner_id,'accounts','ACC_ONLY_B',data||'{"code":"1109"}'::jsonb
196 +SELECT organization_id,owner_id,'accounts','ACC_ONLY_B',(data-'systemRole')||'{"code":"1109"}'::jsonb
197 FROM public.records WHERE organization_id=current_setting('finora_test.org_b')::uuid
198 AND collection='accounts' AND id='ACC_STAGE_1';
199 SET LOCAL ROLE authenticated;
supabase/tests/phase1a_stage1_sale_purchase_negatives.sql new
+131
@@ -0,0 +1,131 @@
1 +-- Stage 1 SQL behavioral suite, disposable Supabase CLI ONLY.
2 +-- Roll back ALL synthetic users and ledger rows after checks.
3 +\set ON_ERROR_STOP on
4 +BEGIN;
5 +INSERT INTO auth.users(id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data)
6 +VALUES
7 +('aaaa1111-1111-4111-8111-111111111111','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-owner-a@example.invalid',now(),'{}'::jsonb),
8 +('bbbb2222-2222-4222-8222-222222222222','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-owner-b@example.invalid',now(),'{}'::jsonb),
9 +('cccc3333-3333-4333-8333-333333333333','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-member-c@example.invalid',now(),'{}'::jsonb);
10 +SELECT set_config('finora_test.org_a',(SELECT organization_id::text FROM public.organization_members WHERE user_id='aaaa1111-1111-4111-8111-111111111111' LIMIT 1),true);
11 +SELECT set_config('finora_test.org_b',(SELECT organization_id::text FROM public.organization_members WHERE user_id='bbbb2222-2222-4222-8222-222222222222' LIMIT 1),true);
12 +-- Admin creates synthetic base chart so client/server validations use real scopes.
13 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
14 +SELECT m.organization_id,m.user_id,'companies','COMP_STAGE_1',jsonb_build_object('name','Stage1 Test Co')
15 +FROM public.organization_members m WHERE m.user_id IN
16 +('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
17 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
18 +SELECT m.organization_id,m.user_id,'fiscalYears','FY_STAGE_1',
19 + jsonb_build_object('companyId','COMP_STAGE_1','startDate','1405/01/01','endDate','1405/12/29','status','open')
20 +FROM public.organization_members m WHERE m.user_id IN
21 +('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
22 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
23 +SELECT m.organization_id,m.user_id,'accounts','ACC_STAGE_'||n,
24 + jsonb_build_object('companyId','COMP_STAGE_1','postingAllowed',true,'active',true,
25 + 'code',CASE WHEN n=1 THEN '1101' ELSE '4101' END,
26 + 'title',CASE WHEN n=1 THEN 'صندوق و بانک' ELSE 'درآمد فروش' END,
27 + 'normalBalance',CASE WHEN n=1 THEN 'debit' ELSE 'credit' END,
28 + 'accountType',CASE WHEN n=1 THEN 'asset' ELSE 'revenue' END,
29 + 'systemRole',CASE WHEN n=1 THEN 'cash_default' ELSE 'revenue_default' END,
30 + 'level','detail','parentId',CASE WHEN n=1 THEN '11' ELSE '41' END)
31 +FROM public.organization_members m CROSS JOIN generate_series(1,2) n WHERE m.user_id IN
32 +('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
33 +INSERT INTO private.finora_phase1a_guard_modes(organization_id,mode)
34 +SELECT organization_id,'enforce' FROM public.organization_members
35 +WHERE user_id IN ('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
36 +-- Fixture seat capacity is raised ONLY inside this rolling-back local test;
37 +-- the production license and its max_users are never accessed or changed.
38 +UPDATE public.licenses SET max_users=GREATEST(max_users,3)
39 + WHERE organization_id=current_setting('finora_test.org_a')::uuid;
40 +-- C joins org A and has commerce:create ONLY; no accounting:approve.
41 +INSERT INTO public.organization_members(organization_id,user_id,status,is_owner)
42 +VALUES (current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333','active',false);
43 +INSERT INTO public.organization_roles(organization_id,role_key,title,is_system,active)
44 +VALUES(current_setting('finora_test.org_a')::uuid,'phase1a_commerce_only','Test commerce create',false,true);
45 +INSERT INTO public.organization_role_permissions(organization_id,role_id,module_key,capabilities)
46 +SELECT organization_id,id,'commerce',ARRAY['create','read']::text[]
47 +FROM public.organization_roles WHERE role_key='phase1a_commerce_only'
48 +AND organization_id=current_setting('finora_test.org_a')::uuid;
49 +INSERT INTO public.organization_member_roles(organization_id,member_id,role_id)
50 +SELECT m.organization_id,m.id,r.id FROM public.organization_members m
51 +JOIN public.organization_roles r ON r.organization_id=m.organization_id
52 +WHERE m.user_id='cccc3333-3333-4333-8333-333333333333'
53 +AND m.organization_id=current_setting('finora_test.org_a')::uuid AND r.role_key='phase1a_commerce_only';
54 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
55 +VALUES(current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333',
56 +'invoices','SOURCE_C_1','{"companyId":"COMP_STAGE_1","kind":"formal","paymentMethod":"cash","vatMode":"none","vatType":"percent","vatRate":0,"items":[{"qty":1,"price":100,"lineTotal":100}],"subtotal":100,"discount":0,"discountType":"fixed","discountInput":0,"vat":0,"grandTotal":100,"accountingVersion":1}'::jsonb);
57 +-- A pro-forma document must never post as a sale.
58 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
59 +VALUES(current_setting('finora_test.org_a')::uuid,
60 + 'cccc3333-3333-4333-8333-333333333333',
61 + 'invoices','SOURCE_C_PROFORMA','{"companyId":"COMP_STAGE_1","kind":"proforma","paymentMethod":"cash","vatMode":"none","vatType":"percent","vatRate":0,"items":[{"qty":1,"price":100,"lineTotal":100}],"subtotal":100,"discount":0,"discountType":"fixed","discountInput":0,"vat":0,"grandTotal":100,"accountingVersion":1}'::jsonb);
62 +
63 +
64 +INSERT INTO public.records(organization_id,owner_id,collection,id,data) VALUES
65 +(current_setting('finora_test.org_a')::uuid,'aaaa1111-1111-4111-8111-111111111111',
66 + 'accounts','ACC_PUR','{"companyId":"COMP_STAGE_1","code":"5101","title":"خرید کالا","systemRole":"purchase_default","accountType":"expense","normalBalance":"debit","level":"detail","parentId":"51","active":true,"postingAllowed":true}'),
67 +(current_setting('finora_test.org_a')::uuid,'aaaa1111-1111-4111-8111-111111111111',
68 + 'accounts','ACC_AR','{"companyId":"COMP_STAGE_1","code":"1102","title":"حساب‌های دریافتنی","systemRole":"receivable_control","accountType":"asset","normalBalance":"debit","level":"detail","parentId":"11","active":true,"postingAllowed":true}'),
69 +(current_setting('finora_test.org_a')::uuid,'aaaa1111-1111-4111-8111-111111111111',
70 + 'accounts','ACC_AP','{"companyId":"COMP_STAGE_1","code":"2101","title":"حساب‌های پرداختنی","systemRole":"payable_control","accountType":"liability","normalBalance":"credit","level":"detail","parentId":"21","active":true,"postingAllowed":true}');
71 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
72 +VALUES(current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333',
73 +'purchases','PUR_NEG_1','{"companyId":"COMP_STAGE_1","kind":"formal","paymentMethod":"credit","vatMode":"none","vatType":"percent","vatRate":0,"items":[{"qty":1,"price":100,"lineTotal":100}],"subtotal":100,"discount":0,"discountType":"fixed","discountInput":0,"vat":0,"grandTotal":100,"accountingVersion":1,"currencyCode":"IRR"}'::jsonb);
74 +DO $t$ BEGIN IF private.finora_phase1a_mode(current_setting('finora_test.org_a')::uuid)<>'enforce' THEN
75 + RAISE EXCEPTION 'FINORA_NEGATIVE_GUARD_NOT_ENFORCE';END IF;END $t$;
76 +SET LOCAL ROLE authenticated;
77 +SELECT set_config('request.jwt.claim.sub','aaaa1111-1111-4111-8111-111111111111',true);
78 +SELECT public.finora_phase1a_publish_role_assignments(current_setting('finora_test.org_a')::uuid,
79 + 'COMP_STAGE_1','{"cash_default":"ACC_STAGE_1","revenue_default":"ACC_STAGE_2",
80 + "receivable_control":"ACC_AR","purchase_default":"ACC_PUR","payable_control":"ACC_AP"}'::jsonb,
81 + 0,'Owner approves sale and purchase semantic mapping');
82 +SELECT set_config('request.jwt.claim.sub','cccc3333-3333-4333-8333-333333333333',true);
83 +DO $t$
84 +DECLARE org uuid:=current_setting('finora_test.org_a')::uuid;
85 + hdr_s jsonb:='{"companyId":"COMP_STAGE_1","fiscalYearId":"FY_STAGE_1","date":"1405/07/24","sourceType":"sale","sourceId":"SOURCE_C_1","sourceVersion":"1"}';
86 + hdr_p jsonb:='{"companyId":"COMP_STAGE_1","fiscalYearId":"FY_STAGE_1","date":"1405/07/24","sourceType":"purchase","sourceId":"PUR_NEG_1","sourceVersion":"1"}';
87 + r jsonb;v bigint;
88 +BEGIN
89 + BEGIN
90 + PERFORM public.finora_journal_autopost(org,'BAD_SALE_ACCOUNTS','invoices','SOURCE_C_1',
91 + hdr_s,'[{"accountId":"ACC_STAGE_2","debit":100,"credit":0},{"accountId":"ACC_STAGE_1","debit":0,"credit":100}]'::jsonb);
92 + RAISE EXCEPTION 'FINORA_BALANCED_WRONG_SALE_ACCEPTED';
93 + EXCEPTION WHEN check_violation THEN
94 + IF SQLERRM NOT LIKE '%FINORA_POSTING_LINES_MISMATCH%' THEN RAISE;END IF;
95 + END;
96 + BEGIN
97 + PERFORM public.finora_journal_autopost(org,'BAD_PURCHASE_ACCOUNTS','purchases','PUR_NEG_1',
98 + hdr_p,'[{"accountId":"ACC_STAGE_1","debit":100,"credit":0},{"accountId":"ACC_AP","debit":0,"credit":100}]'::jsonb);
99 + RAISE EXCEPTION 'FINORA_BALANCED_WRONG_PURCHASE_ACCEPTED';
100 + EXCEPTION WHEN check_violation THEN
101 + IF SQLERRM NOT LIKE '%FINORA_POSTING_LINES_MISMATCH%' THEN RAISE;END IF;
102 + END;
103 + BEGIN
104 + PERFORM public.finora_journal_autopost(org,'UNSUPPORTED_KIND','expenses','SRC',
105 + '{"companyId":"COMP_STAGE_1","sourceType":"expense","sourceId":"SRC"}'::jsonb,'[]'::jsonb);
106 + RAISE EXCEPTION 'FINORA_UNSUPPORTED_SOURCE_ACCEPTED';
107 + EXCEPTION WHEN check_violation THEN
108 + IF SQLERRM NOT LIKE '%FINORA_AUTOPOST_CONTRACT_UNSUPPORTED%' THEN RAISE;END IF;
109 + END;
110 + BEGIN
111 + PERFORM public.finora_journal_autopost(org,'WRONG_TENANT_SOURCE','invoices','NOT_IN_ORG',
112 + jsonb_set(hdr_s,'{sourceId}','"NOT_IN_ORG"'),'[]'::jsonb);
113 + RAISE EXCEPTION 'FINORA_CROSS_TENANT_SOURCE_ACCEPTED';
114 + EXCEPTION WHEN foreign_key_violation THEN
115 + IF SQLERRM NOT LIKE '%FINORA_SOURCE_NOT_COMMITTED_IN_ORG%' THEN RAISE;END IF;
116 + END;
117 + r:=public.finora_journal_autopost(org,'S_POST_1','invoices','SOURCE_C_1',hdr_s,NULL);
118 + v:=(r#>>'{voucher,version}')::bigint;
119 + IF r#>>'{voucher,status}'<>'posted' THEN RAISE EXCEPTION 'FINORA_SALE_NOT_POSTED';END IF;
120 + PERFORM public.finora_journal_reverse(org,'S_POST_1','S_REVERSE_1','1405/07/24',
121 + 'برگشت حسابداری تست',v,NULL);
122 + r:=public.finora_journal_autopost(org,'S_POST_AGAIN','invoices','SOURCE_C_1',hdr_s,NULL);
123 + IF r->>'replayed'<>'true' OR r#>>'{voucher,id}'<>'S_POST_1' THEN
124 + RAISE EXCEPTION 'FINORA_REVERSE_SOURCE_RETRY_NOT_IDEMPOTENT';END IF;
125 + IF EXISTS(SELECT 1 FROM public.records WHERE organization_id=org
126 + AND collection='journalVouchers' AND id IN('BAD_SALE_ACCOUNTS','BAD_PURCHASE_ACCOUNTS',
127 + 'UNSUPPORTED_KIND','WRONG_TENANT_SOURCE','S_POST_AGAIN')) THEN
128 + RAISE EXCEPTION 'FINORA_NEGATIVE_OR_RETRY_LEFT_VOUCHER';END IF;
129 + RAISE NOTICE 'FINORA_SALE_PURCHASE_NEGATIVES_AND_REVERSED_IDEMPOTENCY_PASS';
130 +END $t$;
131 +ROLLBACK;