سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 15:45 UTC
test: cover C8 publication catalog and immutability
davoodmehraban89 committed
Oct 1, 2026 at 21:06 UTC
c9da86df97cc5bc65a6da60b76bff8ced29f9911
1 file changed
+75
-58
supabase/tests/office_c8_workflow.sql
renamed
+75
-58
@@ -1,6 +1,6 @@
1
+-- Office Slice C8 policy validation/publication behavioral contract.
2
+-- Run against the zero-cost disposable PostgreSQL CI harness after applying the C8 migration.
3
+-- The test is transaction-safe and leaves no fixtures.
4
5
begin;
6
@@ -9,83 +9,97 @@ declare
9
valid jsonb := '{"stages":[{"id":"start","label":"شروع","start":true,"terminal":false},{"id":"done","label":"پایان","start":false,"terminal":true}],"edges":[{"id":"approve","from":"start","to":"done","label":"تأیید","capability":"approve"}]}'::jsonb;
10
bad jsonb;
11
begin
12
- -- Persian labels and the minimal valid graph must pass.
12
perform private.office_workflow_validate_definition(valid);
14
-
15
- begin perform private.office_workflow_validate_definition(null); raise exception 'expected null rejection';
16
- exception when sqlstate '22023' then null; end;
17
-
13
+ begin perform private.office_workflow_validate_definition(null); raise exception 'expected null rejection'; exception when sqlstate '22023' then null; end;
14
bad := '{"stages":[{"id":"only","label":"تنها","start":true,"terminal":true}],"edges":[]}'::jsonb;
19
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected one-stage rejection';
20
- exception when sqlstate '22023' then null; end;
21
-
15
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected one-stage rejection'; exception when sqlstate '22023' then null; end;
16
bad := jsonb_build_object('stages',(select jsonb_agg(jsonb_build_object('id','s'||g,'label','مرحله','start',g=1,'terminal',g=21)) from generate_series(1,21) g),'edges','[]'::jsonb);
23
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected 21-stage rejection';
24
- exception when sqlstate '22023' then null; end;
25
-
17
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected 21-stage rejection'; exception when sqlstate '22023' then null; end;
18
bad := jsonb_build_object('stages',valid->'stages','edges',(select jsonb_agg(jsonb_build_object('id','e'||g,'from','start','to','done','label','عبور','capability','edit')) from generate_series(1,61) g));
27
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected 61-edge rejection';
28
- exception when sqlstate '22023' then null; end;
29
-
19
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected 61-edge rejection'; exception when sqlstate '22023' then null; end;
20
bad := jsonb_set(valid,'{edges,0,capability}','"delete"'::jsonb);
31
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected capability rejection';
32
- exception when sqlstate '22023' then null; end;
33
-
21
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected capability rejection'; exception when sqlstate '22023' then null; end;
22
bad := jsonb_set(valid,'{edges,0,to}','"missing"'::jsonb);
35
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-target rejection';
36
- exception when sqlstate '22023' then null; end;
37
-
23
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-target rejection'; exception when sqlstate '22023' then null; end;
24
bad := jsonb_set(valid,'{edges,0,to}','"start"'::jsonb);
39
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected self-edge rejection';
40
- exception when sqlstate '22023' then null; end;
41
-
25
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected self-edge rejection'; exception when sqlstate '22023' then null; end;
26
bad := jsonb_set(valid,'{stages,1,terminal}','false'::jsonb);
43
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected no-terminal rejection';
44
- exception when sqlstate '22023' then null; end;
45
-
27
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected no-terminal rejection'; exception when sqlstate '22023' then null; end;
28
bad := valid || '{"unknown":true}'::jsonb;
47
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-root-key rejection';
48
- exception when sqlstate '22023' then null; end;
49
-
29
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-root-key rejection'; exception when sqlstate '22023' then null; end;
30
bad := jsonb_set(valid,'{stages,0}',(valid->'stages'->0)||'{"unknown":true}'::jsonb);
51
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-stage-key rejection';
52
- exception when sqlstate '22023' then null; end;
53
-
31
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-stage-key rejection'; exception when sqlstate '22023' then null; end;
32
bad := jsonb_set(valid,'{edges,0}',(valid->'edges'->0)||'{"unknown":true}'::jsonb);
55
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-edge-key rejection';
56
- exception when sqlstate '22023' then null; end;
57
-
33
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unknown-edge-key rejection'; exception when sqlstate '22023' then null; end;
34
bad := jsonb_set(valid,'{stages,0,start}','"true"'::jsonb);
59
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected non-boolean flag rejection';
60
- exception when sqlstate '22023' then null; end;
61
-
35
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected non-boolean flag rejection'; exception when sqlstate '22023' then null; end;
36
bad := jsonb_set(valid,'{stages,1,id}','"start"'::jsonb);
63
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected duplicate-stage-id rejection';
64
- exception when sqlstate '22023' then null; end;
65
-
37
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected duplicate-stage-id rejection'; exception when sqlstate '22023' then null; end;
38
bad := jsonb_set(valid,'{edges}',(valid->'edges')||jsonb_build_array((valid->'edges'->0)||'{"label":"دوباره"}'::jsonb));
67
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected duplicate-edge-id rejection';
68
- exception when sqlstate '22023' then null; end;
69
-
39
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected duplicate-edge-id rejection'; exception when sqlstate '22023' then null; end;
40
bad := jsonb_set(valid,'{edges}',(valid->'edges')||jsonb_build_array((valid->'edges'->0)||'{"id":"again"}'::jsonb));
71
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected duplicate-edge-pair rejection';
72
- exception when sqlstate '22023' then null; end;
73
-
41
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected duplicate-edge-pair rejection'; exception when sqlstate '22023' then null; end;
42
bad := jsonb_set(valid,'{edges}',jsonb_build_array((valid->'edges'->0),jsonb_build_object('id','back','from','done','to','start','label','بازگشت','capability','edit')));
75
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected terminal-outgoing rejection';
76
- exception when sqlstate '22023' then null; end;
77
-
43
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected terminal-outgoing rejection'; exception when sqlstate '22023' then null; end;
44
bad := jsonb_set(valid,'{stages,1}',jsonb_build_object('id','orphan','label','یتیم','start',false,'terminal',true));
79
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unreachable-stage rejection';
80
- exception when sqlstate '22023' then null; end;
81
-
45
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected unreachable-stage rejection'; exception when sqlstate '22023' then null; end;
46
bad := '{"stages":[{"id":"start","label":"شروع","start":true,"terminal":false},{"id":"done","label":"پایان","start":false,"terminal":true},{"id":"loop1","label":"حلقه ۱","start":false,"terminal":false},{"id":"loop2","label":"حلقه ۲","start":false,"terminal":false}],"edges":[{"id":"finish","from":"start","to":"done","label":"پایان","capability":"approve"},{"id":"branch","from":"start","to":"loop1","label":"شاخه","capability":"edit"},{"id":"l12","from":"loop1","to":"loop2","label":"بعدی","capability":"edit"},{"id":"l21","from":"loop2","to":"loop1","label":"برگشت","capability":"edit"}]}'::jsonb;
83
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected no-exit-cycle rejection';
84
- exception when sqlstate '22023' then null; end;
85
-
47
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected no-exit-cycle rejection'; exception when sqlstate '22023' then null; end;
48
bad := jsonb_build_object('stages',valid->'stages','edges',valid->'edges','padding',repeat('x',70000));
87
- begin perform private.office_workflow_validate_definition(bad); raise exception 'expected size rejection';
88
- exception when sqlstate '22023' then null; end;
49
+ begin perform private.office_workflow_validate_definition(bad); raise exception 'expected size rejection'; exception when sqlstate '22023' then null; end;
50
+end $$;
51
+
52
+do $$
53
+declare
54
+ v_user constant uuid := '11111111-1111-4111-8111-111111111111';
55
+ v_org constant uuid := '22222222-2222-4222-8222-222222222222';
56
+ valid constant jsonb := '{"stages":[{"id":"start","label":"شروع","start":true,"terminal":false},{"id":"done","label":"پایان","start":false,"terminal":true}],"edges":[{"id":"approve","from":"start","to":"done","label":"تأیید","capability":"approve"}]}'::jsonb;
57
+ v_version integer;
58
+ v_catalog jsonb;
59
+ v_rls boolean;
60
+begin
61
+ insert into public.profiles(id) values(v_user);
62
+ insert into public.organizations(id,owner_user_id) values(v_org,v_user);
63
+ perform set_config('request.jwt.claim.sub',v_user::text,true);
64
+ perform set_config('request.jwt.claim.org',v_org::text,true);
65
+ perform set_config('request.jwt.claim.write','true',true);
66
+ perform set_config('request.jwt.claim.capabilities','configure,read',true);
67
+
68
+ v_version := public.office_workflow_publish(v_org,'approval_flow','گردش تأیید',valid);
69
+ if v_version <> 1 then raise exception 'expected first policy version 1'; end if;
70
+ v_version := public.office_workflow_publish(v_org,'approval_flow','گردش تأیید نسخه دوم',valid);
71
+ if v_version <> 2 then raise exception 'expected second policy version 2'; end if;
72
+
73
+ perform set_config('request.jwt.claim.capabilities','read',true);
74
+ v_catalog := public.office_workflow_catalog(v_org);
75
+ if jsonb_array_length(v_catalog) <> 1 or v_catalog->0->>'version' <> '2' or v_catalog->0->>'title' <> 'گردش تأیید نسخه دوم' then
76
+ raise exception 'catalog must return latest policy version only';
77
+ end if;
78
+
79
+ perform set_config('request.jwt.claim.write','false',true);
80
+ v_catalog := public.office_workflow_catalog(v_org);
81
+ if jsonb_array_length(v_catalog) <> 1 then raise exception 'expired/read-only catalog access must remain available'; end if;
82
+ perform set_config('request.jwt.claim.capabilities','configure,read',true);
83
+ begin perform public.office_workflow_publish(v_org,'blocked','نباید منتشر شود',valid); raise exception 'expected read-only publication denial';
84
+ exception when sqlstate '42501' then null; end;
85
+
86
+ perform set_config('request.jwt.claim.sub','',true);
87
+ begin perform public.office_workflow_publish(v_org,'anon','ناشناس',valid); raise exception 'expected anonymous denial';
88
+ exception when sqlstate '42501' then null; end;
89
+
90
+ begin update private.office_workflow_policy_versions set title='mutated' where organization_id=v_org and policy_key='approval_flow' and version=1; raise exception 'expected immutable update rejection';
91
+ exception when sqlstate '55000' then null; end;
92
+ begin delete from private.office_workflow_policy_versions where organization_id=v_org and policy_key='approval_flow' and version=1; raise exception 'expected immutable delete rejection';
93
+ exception when sqlstate '55000' then null; end;
94
+
95
+ select c.relrowsecurity into v_rls from pg_catalog.pg_class c join pg_catalog.pg_namespace n on n.oid=c.relnamespace where n.nspname='private' and c.relname='office_workflow_policy_versions';
96
+ if not coalesce(v_rls,false) then raise exception 'policy table RLS must be enabled'; end if;
97
+ if has_table_privilege('authenticated','private.office_workflow_policy_versions','select')
98
+ or has_table_privilege('authenticated','private.office_workflow_policy_versions','insert')
99
+ or has_table_privilege('authenticated','private.office_workflow_policy_versions','update')
100
+ or has_table_privilege('authenticated','private.office_workflow_policy_versions','delete') then
101
+ raise exception 'authenticated must not have direct workflow-policy table DML';
102
+ end if;
103
end $$;
104
105
rollback;