@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 096fe42
Fork 0

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

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

Synced Dec 3, 19:30 UTC

Test Wave 2 request integrity failures

davoodmehraban89 committed Oct 2, 2026 at 02:13 UTC 096fe428768084f458c7356f985745e87b4c5a54
1 file changed +98
supabase/tests/request_wave2_integrity.sql new
+98
@@ -0,0 +1,98 @@
1 +\set ON_ERROR_STOP on
2 +set role authenticated;
3 +select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
4 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
5 +select set_config('request.jwt.claim.member_active','true',false);
6 +select set_config('request.jwt.claim.entitled','true',false);
7 +select set_config('request.jwt.claim.write','true',false);
8 +select set_config('request.jwt.claim.capabilities','read,configure,create,edit,approve',false);
9 +
10 +create temporary table wave2_ids(k text primary key,v uuid);
11 +insert into wave2_ids
12 +select 'v1',request_type_version_id from public.request_publish_type_version(
13 + 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','W2_FORM','Wave 2 form','Integrity',
14 + '{"schemaVersion":1,"fields":[{"key":"title","label":"Title","type":"text","required":true},{"key":"amount","label":"Amount","type":"number","required":true},{"key":"due","label":"Due","type":"date","required":true},{"key":"note","label":"Note","type":"textarea","required":false},{"key":"kind","label":"Kind","type":"select","required":true,"options":["A","B"]}]}'::jsonb,
15 + '{"schemaVersion":2,"steps":[{"key":"manager","title":"Manager","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"amount","op":"gte","value":1000}},{"key":"finance","title":"Finance","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,0
16 +);
17 +insert into wave2_ids values('instance_v1',public.request_create_instance(
18 + 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v1'),
19 + '{"title":"Laptop","amount":500,"due":"2026-10-10","note":"ok","kind":"A"}'::jsonb,'wave2-create-v1-0001'
20 +));
21 +
22 +insert into wave2_ids
23 +select 'v2',request_type_version_id from public.request_publish_type_version(
24 + 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','W2_FORM','Wave 2 form v2','Integrity',
25 + '{"schemaVersion":1,"fields":[{"key":"title","label":"Title","type":"text","required":true},{"key":"amount","label":"Amount","type":"number","required":true},{"key":"due","label":"Due","type":"date","required":true},{"key":"note","label":"Note","type":"textarea","required":false},{"key":"kind","label":"Kind","type":"select","required":true,"options":["A","B"]}]}'::jsonb,
26 + '{"schemaVersion":2,"steps":[{"key":"manager","title":"Manager","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"amount","op":"gte","value":1000}},{"key":"finance","title":"Finance","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve"}]}'::jsonb,1
27 +);
28 +
29 +do $$ declare a uuid;b uuid;begin
30 + a=(select v from wave2_ids where k='instance_v1');
31 + b=public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v1'),'{"title":"Laptop","amount":500,"due":"2026-10-10","note":"ok","kind":"A"}'::jsonb,'wave2-create-v1-0001');
32 + if a<>b then raise exception 'same-command idempotent replay changed id'; end if;
33 + begin
34 + perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v1'),'{"title":"Laptop","amount":999,"due":"2026-10-10","note":"ok","kind":"A"}'::jsonb,'wave2-create-v1-0001');
35 + raise exception 'F-414 conflicting idempotency payload unexpectedly succeeded';
36 + exception when unique_violation then null; end;
37 + begin
38 + perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v1'),'{"title":"Old","amount":1,"due":"2026-10-10","kind":"A"}'::jsonb,'wave2-old-version-0001');
39 + raise exception 'F-417 obsolete version unexpectedly accepted';
40 + exception when object_not_in_prerequisite_state then if position('not current' in sqlerrm)=0 then raise;end if;end;
41 +end $$;
42 +
43 +do $$ begin
44 + begin perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v2'),'{"amount":1,"due":"2026-10-10","kind":"A"}'::jsonb,'wave2-missing-00001');raise exception 'F-411 missing required field accepted';exception when others then if position('required request field title is missing' in sqlerrm)=0 then raise;end if;end;
45 + begin perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v2'),'{"title":"X","amount":"1","due":"2026-10-10","kind":"A"}'::jsonb,'wave2-number-00001');raise exception 'F-411 string number accepted';exception when others then if position('must be a number' in sqlerrm)=0 then raise;end if;end;
46 + begin perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v2'),'{"title":"X","amount":1,"due":"2026-02-31","kind":"A"}'::jsonb,'wave2-date-0000001');raise exception 'F-411 invalid date accepted';exception when others then if position('invalid date' in sqlerrm)=0 then raise;end if;end;
47 + begin perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v2'),'{"title":"X","amount":1,"due":"2026-10-10","kind":"C"}'::jsonb,'wave2-select-00001');raise exception 'F-411 invalid select option accepted';exception when others then if position('unsupported option' in sqlerrm)=0 then raise;end if;end;
48 + begin perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v2'),'{"title":"X","amount":1,"due":"2026-10-10","kind":"A","ghost":1}'::jsonb,'wave2-unknown-0001');raise exception 'F-411 unknown field accepted';exception when others then if position('unknown field' in sqlerrm)=0 then raise;end if;end;
49 +end $$;
50 +
51 +do $$ begin
52 + begin
53 + perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','W2_BAD_FIELD','Bad field',null,
54 + '{"schemaVersion":1,"fields":[{"key":"amount","label":"Amount","type":"number","required":true}]}'::jsonb,
55 + '{"schemaVersion":2,"steps":[{"key":"x","title":"X","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"amunt","op":"gte","value":1}}]}'::jsonb,0);
56 + raise exception 'F-418 unbound condition field unexpectedly published';
57 + exception when others then if position('does not exist in form schema' in sqlerrm)=0 then raise;end if;end;
58 + begin
59 + perform * from public.request_publish_type_version('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','W2_BAD_OPTYPE','Bad type',null,
60 + '{"schemaVersion":1,"fields":[{"key":"title","label":"Title","type":"text","required":true}]}'::jsonb,
61 + '{"schemaVersion":2,"steps":[{"key":"x","title":"X","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"title","op":"gt","value":"a"}}]}'::jsonb,0);
62 + raise exception 'F-418 incompatible condition operator unexpectedly published';
63 + exception when others then if position('requires a number field' in sqlerrm)=0 then raise;end if;end;
64 +end $$;
65 +
66 +insert into wave2_ids
67 +select 'no_match',request_type_version_id from public.request_publish_type_version(
68 + 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','W2_NO_MATCH','No match','Integrity',
69 + '{"schemaVersion":1,"fields":[{"key":"amount","label":"Amount","type":"number","required":true}]}'::jsonb,
70 + '{"schemaVersion":2,"steps":[{"key":"manager","title":"Manager","mode":"sequential","requiredApprovals":1,"requiredCapability":"approve","condition":{"field":"amount","op":"gt","value":1000}}]}'::jsonb,0
71 +);
72 +do $$ begin
73 + begin
74 + perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='no_match'),'{"amount":500}'::jsonb,'wave2-nomatch-0001');
75 + raise exception 'F-418 all-false workflow auto-approved';
76 + exception when object_not_in_prerequisite_state then if position('no applicable approval step' in sqlerrm)=0 then raise;end if;end;
77 +end $$;
78 +
79 +reset role;
80 +update public.request_type_definitions set active=false where organization_id='aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' and code='W2_FORM';
81 +set role authenticated;
82 +select set_config('request.jwt.claim.sub','11111111-1111-1111-1111-111111111111',false);
83 +select set_config('request.jwt.claim.org','aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',false);
84 +select set_config('request.jwt.claim.entitled','true',false);
85 +select set_config('request.jwt.claim.capabilities','read,create',false);
86 +do $$ begin
87 + begin
88 + perform public.request_create_instance('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',(select v from wave2_ids where k='v2'),'{"title":"X","amount":1,"due":"2026-10-10","kind":"A"}'::jsonb,'wave2-inactive-0001');
89 + raise exception 'F-417 inactive definition unexpectedly accepted';
90 + exception when object_not_in_prerequisite_state then if position('inactive' in sqlerrm)=0 then raise;end if;end;
91 +end $$;
92 +reset role;
93 +
94 +do $$ begin
95 + if exists(select 1 from public.request_instances where idempotency_key in ('wave2-missing-00001','wave2-number-00001','wave2-date-0000001','wave2-select-00001','wave2-unknown-0001','wave2-old-version-0001','wave2-nomatch-0001','wave2-inactive-0001')) then
96 + raise exception 'failed Wave 2 commands left persistent request rows';
97 + end if;
98 +end $$;