@davoodmehraban89 / Factor-easy / commits / 4f362f9
Fork 0

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

test: inspect private D3a state only as database owner

davoodmehraban89 committed Oct 1, 2026 at 22:50 UTC 4f362f925de7afb4f29008b1d670aa9ad340578d
1 file changed +25 -1
supabase/tests/request_d3a_workflow_behavior.sql
+25 -1
@@ -20,6 +20,7 @@ select 'type',request_type_version_id from public.request_publish_type_version(
20 );
21 insert into d3_ids values('instance',public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from d3_ids where k='type'),'{"subject":"خرید"}'::jsonb,'d3-create-000001'));
22
23 +reset role;
24 do $$ declare i uuid; begin
25 i=(select v from d3_ids where k='instance');
26 if (select count(*) from private.request_step_instances where request_instance_id=i)<>2 then raise exception 'expected two D3 step instances'; end if;
@@ -27,9 +28,13 @@ do $$ declare i uuid; begin
28 if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='finance' and state='pending' and required_approvals=2) then raise exception 'parallel step was not initialized pending'; end if;
29 end $$;
30
31 +set role authenticated;
32 select set_config('request.jwt.claim.sub','22222222-2222-2222-2222-222222222222',false);
33 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
34 +select set_config('request.jwt.claim.entitled','true',false);
35 select set_config('request.jwt.claim.capabilities','read,approve',false);
36 select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',1,'d3-vote-000001','manager ok');
37 +reset role;
38 do $$ declare i uuid; begin
39 i=(select v from d3_ids where k='instance');
40 if (select revision from public.request_instances where id=i)<>2 then raise exception 'manager approval did not advance revision'; end if;
@@ -38,7 +43,13 @@ do $$ declare i uuid; begin
43 if not exists(select 1 from private.request_step_instances where request_instance_id=i and step_key='finance' and state='active') then raise exception 'finance step not activated'; end if;
44 end $$;
45
46 +set role authenticated;
47 +select set_config('request.jwt.claim.sub','22222222-2222-2222-2222-222222222222',false);
48 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
49 +select set_config('request.jwt.claim.entitled','true',false);
50 +select set_config('request.jwt.claim.capabilities','read,approve',false);
51 select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',2,'d3-vote-000002','finance 1');
52 +reset role;
53 do $$ declare i uuid; begin
54 i=(select v from d3_ids where k='instance');
55 if (select status from public.request_instances where id=i)<>'submitted' then raise exception 'parallel quorum completed too early'; end if;
@@ -46,12 +57,22 @@ do $$ declare i uuid; begin
57 if (select count(*) from private.request_step_votes v join private.request_step_instances s on s.id=v.step_instance_id where s.request_instance_id=i and s.step_key='finance' and v.decision='approve')<>1 then raise exception 'first parallel vote missing'; end if;
58 end $$;
59
60 +set role authenticated;
61 +select set_config('request.jwt.claim.sub','22222222-2222-2222-2222-222222222222',false);
62 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
63 +select set_config('request.jwt.claim.entitled','true',false);
64 +select set_config('request.jwt.claim.capabilities','read,approve',false);
65 select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',2,'d3-vote-000002','retry');
66 +reset role;
67 do $$ declare i uuid; begin i=(select v from d3_ids where k='instance'); if (select revision from public.request_instances where id=i)<>3 then raise exception 'idempotent vote retry changed revision'; end if; end $$;
68
69 +set role authenticated;
70 select set_config('request.jwt.claim.sub','33333333-3333-3333-3333-333333333333',false);
71 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
72 +select set_config('request.jwt.claim.entitled','true',false);
73 select set_config('request.jwt.claim.capabilities','read,approve',false);
74 select * from public.request_step_decide((select v from d3_ids where k='instance'),'approve',3,'d3-vote-000003','finance 2');
75 +reset role;
76 do $$ declare i uuid; begin
77 i=(select v from d3_ids where k='instance');
78 if (select status from public.request_instances where id=i)<>'approved' then raise exception 'parallel quorum did not approve request'; end if;
@@ -60,11 +81,14 @@ do $$ declare i uuid; begin
81 if (select count(*) from public.request_instance_events where request_instance_id=i and action like 'step_approve:%')<>3 then raise exception 'step decision evidence incomplete'; end if;
82 end $$;
83
84 +set role authenticated;
85 select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
86 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
87 +select set_config('request.jwt.claim.entitled','true',false);
88 select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
89 do $$ begin
90 begin
67 - perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3_BAD','bad',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"x","mode":"parallel","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,0);
91 + perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','D3_BAD','bad',null,'{}'::jsonb,'{"schemaVersion":2,"steps":[{"key":"x","title":"X","mode":"parallel","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,0);
92 raise exception 'invalid parallel schema unexpectedly published';
93 exception when others then if position('parallel step requires at least two approvals' in sqlerrm)=0 then raise; end if; end;
94 end $$;