@davoodmehraban89 / Factor-easy / commits / f165f87
Fork 0

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

test(auth): assert seeded license, seat cap, ordinary role and phone bootstrap

davoodmehraban89 committed Oct 12, 2026 at 11:16 UTC f165f87efb4aae5c4cdd129f3d9f8a13ed91e63f
1 file changed +56
supabase/tests/identity_bootstrap_seat_guard_behavior.sql new
+56
@@ -0,0 +1,56 @@
1 +\set ON_ERROR_STOP on
2 +-- Wave5a behavior confirms the owner is provisioned with the trial license.
3 +-- The additional checks ensure user-creation fix did not relax seat limits or privileges.
4 +do $$
5 +declare v_org uuid;v_member_count integer;
6 +begin
7 + select id into strict v_org from public.organizations
8 + where owner_user_id='11111111-1111-1111-1111-111111111111';
9 + select count(*) into v_member_count from public.organization_members
10 + where organization_id=v_org and status='active';
11 + if v_member_count<>1 then raise exception 'unexpected initial owner count'; end if;
12 + if not exists(select 1 from public.licenses where organization_id=v_org and max_users=1) then
13 + raise exception 'license was not seeded before member enforcement';
14 + end if;
15 + if not exists(select 1 from public.profiles where id='11111111-1111-1111-1111-111111111111' and role='user') then
16 + raise exception 'initial account was elevated to admin';
17 + end if;
18 +end $$;
19 +
20 +-- A second identity receives its own one-seat trial; it must never join
21 +-- the first organization as a second active named user.
22 +insert into auth.users(id,email,raw_user_meta_data)
23 +values('33333333-3333-3333-3333-333333333333','second@example.invalid','{"full_name":"Second QA"}'::jsonb);
24 +do $$
25 +declare v_org uuid;
26 +begin
27 + select id into strict v_org from public.organizations
28 + where owner_user_id='11111111-1111-1111-1111-111111111111';
29 + begin
30 + insert into public.organization_members(organization_id,user_id,status,is_owner)
31 + values(v_org,'33333333-3333-3333-3333-333333333333','active',false);
32 + raise exception 'second active user illegally bypassed named seat limit';
33 + exception when sqlstate '23514' then
34 + if position('named-user seat limit exceeded' in sqlerrm)=0 then raise;end if;
35 + end;
36 + if exists(select 1 from public.organization_members where organization_id=v_org
37 + and user_id='33333333-3333-3333-3333-333333333333') then
38 + raise exception 'failed extra membership was inserted';
39 + end if;
40 +end $$;
41 +
42 +-- The currently deployed bootstrap permits authenticated phone-only identities.
43 +insert into auth.users(id,email,phone,raw_user_meta_data)
44 +values('44444444-4444-4444-4444-444444444444',null,'+989123456789','{"full_name":"Phone QA"}'::jsonb);
45 +do $$
46 +declare v_org uuid;
47 +begin
48 + select id into strict v_org from public.organizations
49 + where owner_user_id='44444444-4444-4444-4444-444444444444';
50 + if not exists(select 1 from public.profiles where id='44444444-4444-4444-4444-444444444444' and phone='+989123456789' and role='user')
51 + or not exists(select 1 from public.licenses where organization_id=v_org and max_users=1 and status='trial')
52 + or not exists(select 1 from public.organization_members where organization_id=v_org
53 + and user_id='44444444-4444-4444-4444-444444444444' and status='active' and is_owner) then
54 + raise exception 'phone bootstrap regressed';
55 + end if;
56 +end $$;