@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 553c366
Fork 0

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

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 22:00 UTC

test(stage1): explicit organization-scope role guard and purchase RED-first acceptance

davoodmehraban89 committed Oct 17, 2026 at 10:39 UTC 553c366cd00c0613098bea72ebefd111706813d0
3 files changed +93 -2
.github/workflows/phase1a-supabase-parity.yml
+2
@@ -95,6 +95,8 @@ jobs:
95 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_autopost_semantic_red.sql
96 - name: Stage 1 enforce/audit accounting role sync, owner RPC and append-only audit
97 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
98 + - name: PURCHASE RED FIRST — valid purchase must use canonical account mapping
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_purchase_red.sql
100 - name: Run 1A green-after-red authenticated adversarial tests
101 run: node scripts/phase1a-adversarial-red.mjs
102 - name: Test 1A versioned CAS and two-tenant RLS
supabase/tests/phase1a_stage1_account_role_controls.sql
+2 -2
@@ -65,8 +65,8 @@ INSERT INTO public.records(organization_id,owner_id,collection,id,data)
65 VALUES(current_setting('finora_test.org_a')::uuid,'aaaa1111-1111-4111-8111-111111111111',
66 'postingProfiles','PP_TEST',
67 '{"companyId":"COMP_STAGE_1","kind":"sale","debitAccountId":"ACC_STAGE_1","creditAccountId":"ACC_STAGE_2","active":true}'::jsonb);
68 -INSERT INTO public.member_module_permissions(organization_id,member_id,module_key,capabilities)
69 -SELECT organization_id,id,'accounting',ARRAY['read','create','edit']::text[]
68 +INSERT INTO public.member_module_permissions(organization_id,member_id,module_key,capabilities,scope_type)
69 +SELECT organization_id,id,'accounting',ARRAY['read','create','edit']::text[],'organization'
70 FROM public.organization_members
71 WHERE organization_id=current_setting('finora_test.org_a')::uuid
72 AND user_id='cccc3333-3333-4333-8333-333333333333';
supabase/tests/phase1a_stage1_purchase_red.sql new
+89
@@ -0,0 +1,89 @@
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}'::jsonb),
67 +(current_setting('finora_test.org_a')::uuid,'aaaa1111-1111-4111-8111-111111111111',
68 + '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}'::jsonb);
69 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
70 +VALUES(current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333',
71 +'purchases','PUR_RED_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);
72 +DO $t$ BEGIN IF private.finora_phase1a_mode(current_setting('finora_test.org_a')::uuid)<>'enforce' THEN
73 + RAISE EXCEPTION 'FINORA_GUARD_NOT_ENFORCE';END IF;END $t$;
74 +SET LOCAL ROLE authenticated;
75 +SELECT set_config('request.jwt.claim.sub','aaaa1111-1111-4111-8111-111111111111',true);
76 +SELECT public.finora_phase1a_publish_role_assignments(current_setting('finora_test.org_a')::uuid,'COMP_STAGE_1',
77 + '{"purchase_default":"ACC_PUR","payable_control":"ACC_AP"}'::jsonb,0,
78 + 'Owner confirms purchase semantic chart');
79 +SELECT set_config('request.jwt.claim.sub','cccc3333-3333-4333-8333-333333333333',true);
80 +DO $t$
81 +DECLARE result jsonb;
82 +BEGIN
83 + result:=public.finora_journal_autopost(current_setting('finora_test.org_a')::uuid,
84 + 'PUR_RED_VOUCHER','purchases','PUR_RED_1',
85 + '{"companyId":"COMP_STAGE_1","fiscalYearId":"FY_STAGE_1","date":"1405/07/24",
86 + "sourceType":"purchase","sourceId":"PUR_RED_1","sourceVersion":"1"}'::jsonb,NULL);
87 + IF result#>>'{voucher,status}'<>'posted' THEN RAISE EXCEPTION 'FINORA_PURCHASE_RED_NOT_POSTED';END IF;
88 +END $t$;
89 +ROLLBACK;