@marktiderman/genesis-supabase 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -1
- package/dist/blocks.d.ts +9 -1
- package/dist/blocks.js +1 -1
- package/dist/{chunk-FWZIWKI4.js → chunk-4IVEBFZM.js} +26 -5
- package/dist/chunk-4IVEBFZM.js.map +1 -0
- package/dist/chunk-7SCMHKCR.js +33 -0
- package/dist/chunk-7SCMHKCR.js.map +1 -0
- package/dist/chunk-XGAXPUFY.js +124 -0
- package/dist/chunk-XGAXPUFY.js.map +1 -0
- package/dist/{index-D73YuwEY.d.ts → index-CaYX9z8D.d.ts} +16 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/jobs.d.ts +3 -1
- package/dist/jobs.js +48 -34
- package/dist/jobs.js.map +1 -1
- package/dist/native.d.ts +2 -2
- package/dist/runner.d.ts +179 -0
- package/dist/runner.js +3 -0
- package/dist/runner.js.map +1 -0
- package/dist/web-handoff-path-C_EEl_BC.d.ts +16 -0
- package/dist/web-handoff-server.d.ts +225 -0
- package/dist/web-handoff-server.js +262 -0
- package/dist/web-handoff-server.js.map +1 -0
- package/dist/web-handoff.d.ts +133 -0
- package/dist/web-handoff.js +104 -0
- package/dist/web-handoff.js.map +1 -0
- package/migrations/0015_genesis_web_handoff.sql +270 -0
- package/migrations/0016_genesis_viewer_people.sql +70 -0
- package/migrations/0017_genesis_person_entitlements.sql +284 -0
- package/migrations/tests/0015_genesis_web_handoff.test.sql +203 -0
- package/migrations/tests/0016_genesis_viewer_people.test.sql +77 -0
- package/migrations/tests/0017_genesis_person_entitlements.test.sql +96 -0
- package/package.json +63 -5
- package/dist/chunk-FWZIWKI4.js.map +0 -1
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
-- Genesis feature registry: entitlements held by one person (0008 and 0014, extended here).
|
|
2
|
+
--
|
|
3
|
+
-- 0008's feature_entitlements says which tenant may use a paid feature. A membership, a class pack,
|
|
4
|
+
-- a course or a coaching package is bought by one person, so the gate also needs grants that belong
|
|
5
|
+
-- to a person record. They live in their own table, beside the tenant's, and the gate reads both:
|
|
6
|
+
--
|
|
7
|
+
-- public.feature_person_entitlements
|
|
8
|
+
-- Which person may use which paid feature in a tenant, by source (a billing webhook, a manual
|
|
9
|
+
-- grant) and source_ref (the subscription item or purchase it came from, so a webhook can be
|
|
10
|
+
-- replayed), optionally from starts_at and until ends_at. person_id is the app's person record,
|
|
11
|
+
-- not a login, so a grant bought before sign-up counts once the person signs in. Written by
|
|
12
|
+
-- the service role only, through grant_person_entitlement and revoke_person_entitlement; a
|
|
13
|
+
-- signed-in person reads their own.
|
|
14
|
+
--
|
|
15
|
+
-- Who "their own" is comes from 0016's private.genesis_viewer_person_ids(), the app's answer to
|
|
16
|
+
-- which person records a login is.
|
|
17
|
+
--
|
|
18
|
+
-- What changes for the gate:
|
|
19
|
+
--
|
|
20
|
+
-- feature_on(tenant, feature)
|
|
21
|
+
-- A paid feature is on for a signed-in person when the tenant holds it or they do. Server code
|
|
22
|
+
-- still gets the tenant's answer.
|
|
23
|
+
-- feature_entitled_tenants(feature)
|
|
24
|
+
-- For a signed-in person, also the tenants where they hold it themselves, so a policy over many
|
|
25
|
+
-- rows answers "may this person use it here".
|
|
26
|
+
-- feature_gate_state(tenant)
|
|
27
|
+
-- `entitled` is the tenant's grants plus the signed-in person's own in that tenant.
|
|
28
|
+
-- feature_gate_state(tenant, person) (new, server code only)
|
|
29
|
+
-- The same for a person server code names, for a snapshot built on the server.
|
|
30
|
+
--
|
|
31
|
+
-- Credits, balances and which offer unlocks a feature are billing's, not the registry's: an
|
|
32
|
+
-- entitlement only says on or off.
|
|
33
|
+
--
|
|
34
|
+
-- Needs 0004, 0008, 0014 and 0016. Verify: tests/0017_genesis_person_entitlements.test.sql.
|
|
35
|
+
|
|
36
|
+
create table public.feature_person_entitlements (
|
|
37
|
+
tenant_id uuid not null,
|
|
38
|
+
person_id uuid not null,
|
|
39
|
+
-- No foreign key: a billing webhook may grant a feature before the deploy that adds it syncs.
|
|
40
|
+
feature_id text not null
|
|
41
|
+
constraint feature_person_entitlements_feature_id_format check (feature_id ~ '^[a-z][a-z0-9]*([-_.][a-z0-9]+)*$' and char_length(feature_id) <= 100),
|
|
42
|
+
-- Where the grant comes from: 'stripe', 'manual', 'trial' …
|
|
43
|
+
source text not null
|
|
44
|
+
constraint feature_person_entitlements_source_format check (source ~ '^[a-z][a-z0-9_-]*$' and char_length(source) <= 40),
|
|
45
|
+
-- What in the source it comes from (a subscription item, a purchase); '' when there's only one.
|
|
46
|
+
source_ref text not null default ''
|
|
47
|
+
constraint feature_person_entitlements_source_ref_length check (char_length(source_ref) <= 200),
|
|
48
|
+
active boolean not null,
|
|
49
|
+
-- Optional start and end of the grant; outside them it doesn't count.
|
|
50
|
+
starts_at timestamptz,
|
|
51
|
+
ends_at timestamptz,
|
|
52
|
+
updated_at timestamptz not null default now(),
|
|
53
|
+
primary key (tenant_id, person_id, feature_id, source, source_ref),
|
|
54
|
+
constraint feature_person_entitlements_window check (starts_at is null or ends_at is null or starts_at < ends_at)
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
comment on table public.feature_person_entitlements is
|
|
58
|
+
'Which person may use which paid feature in a tenant, by source and source_ref, optionally from starts_at until ends_at. person_id is the app''s person record. Written by the service role; a person reads their own. Genesis feature registry.';
|
|
59
|
+
|
|
60
|
+
create index feature_person_entitlements_feature_active on public.feature_person_entitlements (feature_id) where active;
|
|
61
|
+
|
|
62
|
+
-- Writing grants (server code) -------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
-- Grant, or renew, one person's entitlement. Idempotent on (tenant, person, feature, source,
|
|
65
|
+
-- source_ref): a replayed webhook changes nothing but updated_at.
|
|
66
|
+
create or replace function public.grant_person_entitlement(
|
|
67
|
+
tenant uuid, person uuid, feature text, source text, source_ref text default '',
|
|
68
|
+
starts_at timestamptz default null, ends_at timestamptz default null)
|
|
69
|
+
returns void
|
|
70
|
+
language sql
|
|
71
|
+
set search_path = ''
|
|
72
|
+
as $$
|
|
73
|
+
insert into public.feature_person_entitlements as e
|
|
74
|
+
(tenant_id, person_id, feature_id, source, source_ref, active, starts_at, ends_at, updated_at)
|
|
75
|
+
values (tenant, person, feature, source, coalesce(source_ref, ''), true, starts_at, ends_at, now())
|
|
76
|
+
on conflict (tenant_id, person_id, feature_id, source, source_ref) do update
|
|
77
|
+
set active = true, starts_at = excluded.starts_at, ends_at = excluded.ends_at, updated_at = now()
|
|
78
|
+
$$;
|
|
79
|
+
|
|
80
|
+
comment on function public.grant_person_entitlement(uuid, uuid, text, text, text, timestamptz, timestamptz) is
|
|
81
|
+
'Grant or renew a person''s entitlement to a feature, idempotently. Service role only. Genesis feature registry.';
|
|
82
|
+
|
|
83
|
+
-- Revoke one person's entitlement. The row stays, inactive, as a record; false when there was none.
|
|
84
|
+
create or replace function public.revoke_person_entitlement(
|
|
85
|
+
tenant uuid, person uuid, feature text, source text, source_ref text default '')
|
|
86
|
+
returns boolean
|
|
87
|
+
language sql
|
|
88
|
+
set search_path = ''
|
|
89
|
+
as $$
|
|
90
|
+
with revoked as (
|
|
91
|
+
update public.feature_person_entitlements e
|
|
92
|
+
set active = false, updated_at = now()
|
|
93
|
+
where e.tenant_id = tenant and e.person_id = person and e.feature_id = feature
|
|
94
|
+
and e.source = revoke_person_entitlement.source and e.source_ref = coalesce(revoke_person_entitlement.source_ref, '')
|
|
95
|
+
returning 1
|
|
96
|
+
)
|
|
97
|
+
select exists (select 1 from revoked)
|
|
98
|
+
$$;
|
|
99
|
+
|
|
100
|
+
comment on function public.revoke_person_entitlement(uuid, uuid, text, text, text) is
|
|
101
|
+
'Revoke a person''s entitlement to a feature; the row stays inactive. Service role only. Genesis feature registry.';
|
|
102
|
+
|
|
103
|
+
-- Reading grants ---------------------------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
-- The features a person holds in a tenant right now.
|
|
106
|
+
create or replace function private.genesis_person_entitled(tenant uuid, person uuid)
|
|
107
|
+
returns setof text
|
|
108
|
+
language sql
|
|
109
|
+
stable
|
|
110
|
+
security definer
|
|
111
|
+
set search_path = ''
|
|
112
|
+
as $$
|
|
113
|
+
select distinct p.feature_id from public.feature_person_entitlements p
|
|
114
|
+
where p.tenant_id = tenant and p.person_id = person and p.active
|
|
115
|
+
and (p.starts_at is null or p.starts_at <= now()) and (p.ends_at is null or p.ends_at > now())
|
|
116
|
+
$$;
|
|
117
|
+
|
|
118
|
+
-- The features the signed-in person holds in a tenant, through any of their person records there.
|
|
119
|
+
create or replace function private.genesis_viewer_entitled(tenant uuid)
|
|
120
|
+
returns setof text
|
|
121
|
+
language sql
|
|
122
|
+
stable
|
|
123
|
+
security definer
|
|
124
|
+
set search_path = ''
|
|
125
|
+
as $$
|
|
126
|
+
select distinct p.feature_id from public.feature_person_entitlements p
|
|
127
|
+
join private.genesis_viewer_person_ids() v on v.tenant_id = p.tenant_id and v.person_id = p.person_id
|
|
128
|
+
where p.tenant_id = tenant and p.active
|
|
129
|
+
and (p.starts_at is null or p.starts_at <= now()) and (p.ends_at is null or p.ends_at > now())
|
|
130
|
+
$$;
|
|
131
|
+
|
|
132
|
+
create or replace function public.feature_on(tenant uuid, feature text)
|
|
133
|
+
returns boolean
|
|
134
|
+
language plpgsql
|
|
135
|
+
stable
|
|
136
|
+
security definer
|
|
137
|
+
set search_path = ''
|
|
138
|
+
as $$
|
|
139
|
+
begin
|
|
140
|
+
if tenant is null or feature is null then
|
|
141
|
+
return false;
|
|
142
|
+
end if;
|
|
143
|
+
if not private.genesis_feature_may_read(tenant) then
|
|
144
|
+
return false;
|
|
145
|
+
end if;
|
|
146
|
+
return exists (
|
|
147
|
+
select 1 from public.features f
|
|
148
|
+
where f.id = feature and f.in_registry and f.status in ('beta', 'live', 'deprecated')
|
|
149
|
+
and (f.access_plan is null
|
|
150
|
+
or exists (
|
|
151
|
+
select 1 from public.feature_entitlements e
|
|
152
|
+
where e.tenant_id = tenant and e.feature_id = f.id and e.active and (e.ends_at is null or e.ends_at > now())
|
|
153
|
+
)
|
|
154
|
+
or f.id in (select private.genesis_viewer_entitled(tenant)))
|
|
155
|
+
);
|
|
156
|
+
end;
|
|
157
|
+
$$;
|
|
158
|
+
|
|
159
|
+
comment on function public.feature_on(uuid, text) is
|
|
160
|
+
'The gate in SQL: shipped (beta, live, deprecated) and, when paid, entitled to the tenant or to the signed-in person. For row security. A signed-in caller outside the tenant gets false. Genesis feature registry.';
|
|
161
|
+
|
|
162
|
+
create or replace function public.feature_entitled_tenants(feature text)
|
|
163
|
+
returns setof uuid
|
|
164
|
+
language plpgsql
|
|
165
|
+
stable
|
|
166
|
+
security definer
|
|
167
|
+
set search_path = ''
|
|
168
|
+
as $$
|
|
169
|
+
begin
|
|
170
|
+
return query
|
|
171
|
+
select e.tenant_id from public.feature_entitlements e
|
|
172
|
+
where e.feature_id = feature and e.active and (e.ends_at is null or e.ends_at > now())
|
|
173
|
+
and private.genesis_feature_may_read(e.tenant_id)
|
|
174
|
+
union
|
|
175
|
+
select p.tenant_id from public.feature_person_entitlements p
|
|
176
|
+
join private.genesis_viewer_person_ids() v on v.tenant_id = p.tenant_id and v.person_id = p.person_id
|
|
177
|
+
where p.feature_id = feature and p.active
|
|
178
|
+
and (p.starts_at is null or p.starts_at <= now()) and (p.ends_at is null or p.ends_at > now())
|
|
179
|
+
and private.genesis_feature_may_read(p.tenant_id);
|
|
180
|
+
end;
|
|
181
|
+
$$;
|
|
182
|
+
|
|
183
|
+
comment on function public.feature_entitled_tenants(text) is
|
|
184
|
+
'Tenants where a feature is entitled: every tenant holding it, for server code; for a signed-in person, their own tenants that hold it or where they hold it themselves. For row security, once per statement. Genesis feature registry.';
|
|
185
|
+
|
|
186
|
+
-- The snapshot's state, for the signed-in person (person grants are theirs) or for server code
|
|
187
|
+
-- naming a person (null: the tenant's alone).
|
|
188
|
+
create or replace function private.genesis_feature_gate_state(tenant uuid, person_entitled text[])
|
|
189
|
+
returns jsonb
|
|
190
|
+
language sql
|
|
191
|
+
stable
|
|
192
|
+
security definer
|
|
193
|
+
set search_path = ''
|
|
194
|
+
as $$
|
|
195
|
+
select jsonb_build_object(
|
|
196
|
+
'off', coalesce((
|
|
197
|
+
select jsonb_agg(t.feature_id order by t.feature_id) from public.tenant_features t
|
|
198
|
+
where t.tenant_id = tenant and not t.enabled
|
|
199
|
+
), '[]'::jsonb),
|
|
200
|
+
'entitled', coalesce((
|
|
201
|
+
select jsonb_agg(x.feature_id order by x.feature_id) from (
|
|
202
|
+
select e.feature_id from public.feature_entitlements e
|
|
203
|
+
where e.tenant_id = tenant and e.active and (e.ends_at is null or e.ends_at > now())
|
|
204
|
+
union
|
|
205
|
+
select unnest(person_entitled)
|
|
206
|
+
) x
|
|
207
|
+
), '[]'::jsonb),
|
|
208
|
+
-- The tenant's own values, then platform values on top: a platform value (a kill switch)
|
|
209
|
+
-- applies to every tenant, whatever the tenant's own value.
|
|
210
|
+
'flags', coalesce((
|
|
211
|
+
select jsonb_object_agg(v.flag_key, v.value) from public.tenant_flag_values v
|
|
212
|
+
join public.feature_flags fl on fl.key = v.flag_key and fl.in_registry
|
|
213
|
+
where v.tenant_id = tenant
|
|
214
|
+
), '{}'::jsonb) || coalesce((
|
|
215
|
+
select jsonb_object_agg(fl.key, fl.platform_value) from public.feature_flags fl
|
|
216
|
+
where fl.in_registry and fl.platform_value is not null
|
|
217
|
+
), '{}'::jsonb)
|
|
218
|
+
)
|
|
219
|
+
$$;
|
|
220
|
+
|
|
221
|
+
create or replace function public.feature_gate_state(tenant uuid)
|
|
222
|
+
returns jsonb
|
|
223
|
+
language plpgsql
|
|
224
|
+
stable
|
|
225
|
+
security definer
|
|
226
|
+
set search_path = ''
|
|
227
|
+
as $$
|
|
228
|
+
begin
|
|
229
|
+
if tenant is null then
|
|
230
|
+
raise exception 'feature_gate_state: tenant is required' using errcode = '22004';
|
|
231
|
+
end if;
|
|
232
|
+
if not private.genesis_feature_may_read(tenant) then
|
|
233
|
+
raise exception 'feature_gate_state: only the tenant''s members can read its features' using errcode = '42501';
|
|
234
|
+
end if;
|
|
235
|
+
return private.genesis_feature_gate_state(tenant, array(select private.genesis_viewer_entitled(tenant)));
|
|
236
|
+
end;
|
|
237
|
+
$$;
|
|
238
|
+
|
|
239
|
+
comment on function public.feature_gate_state(uuid) is
|
|
240
|
+
'A tenant''s switches off, entitlements held (the tenant''s and the signed-in person''s own) and flag values, for the gate''s snapshot. Its members and the service role. Genesis feature registry.';
|
|
241
|
+
|
|
242
|
+
create or replace function public.feature_gate_state(tenant uuid, person uuid)
|
|
243
|
+
returns jsonb
|
|
244
|
+
language plpgsql
|
|
245
|
+
stable
|
|
246
|
+
security definer
|
|
247
|
+
set search_path = ''
|
|
248
|
+
as $$
|
|
249
|
+
begin
|
|
250
|
+
if tenant is null then
|
|
251
|
+
raise exception 'feature_gate_state: tenant is required' using errcode = '22004';
|
|
252
|
+
end if;
|
|
253
|
+
if not private.genesis_feature_caller_is_server() then
|
|
254
|
+
raise exception 'feature_gate_state: only server code can name a person' using errcode = '42501';
|
|
255
|
+
end if;
|
|
256
|
+
return private.genesis_feature_gate_state(tenant,
|
|
257
|
+
case when person is null then '{}'::text[] else array(select private.genesis_person_entitled(tenant, person)) end);
|
|
258
|
+
end;
|
|
259
|
+
$$;
|
|
260
|
+
|
|
261
|
+
comment on function public.feature_gate_state(uuid, uuid) is
|
|
262
|
+
'A tenant''s gate state with one named person''s entitlements added. Server code only. Genesis feature registry.';
|
|
263
|
+
|
|
264
|
+
-- Access ---------------------------------------------------------------------------------------
|
|
265
|
+
|
|
266
|
+
alter table public.feature_person_entitlements enable row level security;
|
|
267
|
+
|
|
268
|
+
revoke all on table public.feature_person_entitlements from public, anon, authenticated, service_role;
|
|
269
|
+
grant select, insert, update, delete on table public.feature_person_entitlements to service_role;
|
|
270
|
+
grant select (tenant_id, person_id, feature_id, source, active, starts_at, ends_at) on table public.feature_person_entitlements to authenticated;
|
|
271
|
+
create policy "People read their own entitlements" on public.feature_person_entitlements for select to authenticated
|
|
272
|
+
using ((tenant_id, person_id) in (select v.tenant_id, v.person_id from private.genesis_viewer_person_ids() v));
|
|
273
|
+
|
|
274
|
+
revoke execute on function public.grant_person_entitlement(uuid, uuid, text, text, text, timestamptz, timestamptz) from public, anon, authenticated;
|
|
275
|
+
grant execute on function public.grant_person_entitlement(uuid, uuid, text, text, text, timestamptz, timestamptz) to service_role;
|
|
276
|
+
revoke execute on function public.revoke_person_entitlement(uuid, uuid, text, text, text) from public, anon, authenticated;
|
|
277
|
+
grant execute on function public.revoke_person_entitlement(uuid, uuid, text, text, text) to service_role;
|
|
278
|
+
|
|
279
|
+
revoke execute on function private.genesis_person_entitled(uuid, uuid) from public, anon, authenticated;
|
|
280
|
+
revoke execute on function private.genesis_viewer_entitled(uuid) from public, anon, authenticated;
|
|
281
|
+
revoke execute on function private.genesis_feature_gate_state(uuid, text[]) from public, anon, authenticated;
|
|
282
|
+
|
|
283
|
+
revoke execute on function public.feature_gate_state(uuid, uuid) from public, anon, authenticated;
|
|
284
|
+
grant execute on function public.feature_gate_state(uuid, uuid) to service_role;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
-- @feat:web-handoff
|
|
2
|
+
-- pgTAP test for 0015_genesis_web_handoff.sql.
|
|
3
|
+
-- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
|
|
4
|
+
-- It writes only its own users, sessions and handoffs and rolls everything back.
|
|
5
|
+
begin;
|
|
6
|
+
create extension if not exists pgtap with schema extensions;
|
|
7
|
+
select plan(38);
|
|
8
|
+
|
|
9
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
10
|
+
select ('00000000-0000-4000-8000-0000000015' || n)::uuid, 'authenticated', 'authenticated', 'genesis-handoff-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
11
|
+
from unnest(array['a1', 'b2']) n;
|
|
12
|
+
|
|
13
|
+
create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-0000000015' || n)::uuid $$;
|
|
14
|
+
create function pg_temp.session(n int) returns uuid language sql immutable as $$ select ('00000000-0000-4000-9000-0000000150' || lpad(n::text, 2, '0'))::uuid $$;
|
|
15
|
+
create function pg_temp.digest(n int) returns text language sql immutable as $$ select repeat(to_hex(n % 16), 64) $$;
|
|
16
|
+
create function pg_temp.nonce(n int) returns text language sql immutable as $$ select repeat(chr(96 + n), 64) $$;
|
|
17
|
+
grant execute on function pg_temp.person(text), pg_temp.session(int), pg_temp.digest(int), pg_temp.nonce(int) to anon, authenticated, service_role;
|
|
18
|
+
|
|
19
|
+
-- a1's app session (1), the web session a handoff makes (2), and a1's other device (3).
|
|
20
|
+
insert into auth.sessions (id, user_id, created_at, updated_at)
|
|
21
|
+
values (pg_temp.session(1), pg_temp.person('a1'), now(), now()),
|
|
22
|
+
(pg_temp.session(2), pg_temp.person('a1'), now(), now()),
|
|
23
|
+
(pg_temp.session(3), pg_temp.person('a1'), now(), now());
|
|
24
|
+
|
|
25
|
+
-- Shape and access --------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
select has_table('private', 'genesis_web_handoffs', 'the handoff table is in private');
|
|
28
|
+
select ok(
|
|
29
|
+
(select relrowsecurity from pg_class where oid = 'private.genesis_web_handoffs'::regclass),
|
|
30
|
+
'row level security is on'
|
|
31
|
+
);
|
|
32
|
+
select ok(
|
|
33
|
+
not has_table_privilege('authenticated', 'private.genesis_web_handoffs', 'select')
|
|
34
|
+
and not has_table_privilege('anon', 'private.genesis_web_handoffs', 'select'),
|
|
35
|
+
'anon and authenticated cannot read the table'
|
|
36
|
+
);
|
|
37
|
+
select ok(
|
|
38
|
+
not exists (
|
|
39
|
+
select 1
|
|
40
|
+
from unnest(array[
|
|
41
|
+
'public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid)',
|
|
42
|
+
'public.genesis_web_handoff_claim(text, text, integer)',
|
|
43
|
+
'public.genesis_web_handoff_continue(text)',
|
|
44
|
+
'public.genesis_web_handoff_discard(text)',
|
|
45
|
+
'public.genesis_web_handoff_bind_session(text, uuid, uuid)',
|
|
46
|
+
'public.genesis_web_handoff_revoke_sessions(uuid, uuid)'
|
|
47
|
+
]) f
|
|
48
|
+
cross join unnest(array['anon', 'authenticated']) r
|
|
49
|
+
where has_function_privilege(r, f, 'execute')
|
|
50
|
+
),
|
|
51
|
+
'anon and authenticated can execute none of the functions'
|
|
52
|
+
);
|
|
53
|
+
select ok(
|
|
54
|
+
has_function_privilege('service_role', 'public.genesis_web_handoff_continue(text)', 'execute'),
|
|
55
|
+
'service_role can'
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
-- Even granted, the functions refuse any role but service_role, whatever the JWT claims.
|
|
59
|
+
grant execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) to authenticated;
|
|
60
|
+
grant execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) to authenticated;
|
|
61
|
+
set local role authenticated;
|
|
62
|
+
select set_config('request.jwt.claims', json_build_object('sub', pg_temp.person('a1'), 'role', 'service_role')::text, true);
|
|
63
|
+
select throws_ok(
|
|
64
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(1), '/x') $$,
|
|
65
|
+
'42501', 'genesis_web_handoff_issue is for the server only',
|
|
66
|
+
'issue: authenticated is refused even with a service_role claim in its JWT'
|
|
67
|
+
);
|
|
68
|
+
select throws_ok(
|
|
69
|
+
$$ select public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)) $$,
|
|
70
|
+
'42501', 'genesis_web_handoff_revoke_sessions is for the server only',
|
|
71
|
+
'revoke: the same'
|
|
72
|
+
);
|
|
73
|
+
reset role;
|
|
74
|
+
revoke execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) from authenticated;
|
|
75
|
+
revoke execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) from authenticated;
|
|
76
|
+
|
|
77
|
+
-- Issue, claim, continue ---------------------------------------------------------------
|
|
78
|
+
|
|
79
|
+
set local role service_role;
|
|
80
|
+
-- (Through a subquery: BETWEEN would call the function twice.)
|
|
81
|
+
select ok(
|
|
82
|
+
(select e between now() + interval '59 seconds' and now() + interval '61 seconds'
|
|
83
|
+
from (select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(1), '/clients/1/forms', 60, 10, 60, pg_temp.session(1)) as e) issued),
|
|
84
|
+
'a handoff expires in 60 seconds by default'
|
|
85
|
+
);
|
|
86
|
+
select results_eq(
|
|
87
|
+
$$ select user_id, path from public.genesis_web_handoff_claim(pg_temp.digest(1), pg_temp.nonce(1)) $$,
|
|
88
|
+
$$ values (pg_temp.person('a1'), '/clients/1/forms') $$,
|
|
89
|
+
'claiming returns the person and path'
|
|
90
|
+
);
|
|
91
|
+
select is_empty(
|
|
92
|
+
$$ select * from public.genesis_web_handoff_claim(pg_temp.digest(1), pg_temp.nonce(2)) $$,
|
|
93
|
+
'a link is claimed once'
|
|
94
|
+
);
|
|
95
|
+
select is_empty(
|
|
96
|
+
$$ select * from public.genesis_web_handoff_claim(pg_temp.digest(9), pg_temp.nonce(3)) $$,
|
|
97
|
+
'an unknown link claims nothing'
|
|
98
|
+
);
|
|
99
|
+
select results_eq(
|
|
100
|
+
$$ select user_id, path from public.genesis_web_handoff_continue(pg_temp.nonce(1)) $$,
|
|
101
|
+
$$ values (pg_temp.person('a1'), '/clients/1/forms') $$,
|
|
102
|
+
'Continue with the nonce returns the person and path'
|
|
103
|
+
);
|
|
104
|
+
select is_empty(
|
|
105
|
+
$$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(1)) $$,
|
|
106
|
+
'a nonce continues once'
|
|
107
|
+
);
|
|
108
|
+
select ok(
|
|
109
|
+
not public.genesis_web_handoff_discard(pg_temp.nonce(1)),
|
|
110
|
+
'a continued handoff cannot be discarded'
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
-- Bind and revoke.
|
|
114
|
+
select ok(
|
|
115
|
+
not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('b2'), pg_temp.session(2)),
|
|
116
|
+
'bind refuses another person'
|
|
117
|
+
);
|
|
118
|
+
select ok(
|
|
119
|
+
not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(1)),
|
|
120
|
+
'bind refuses the app session itself'
|
|
121
|
+
);
|
|
122
|
+
select ok(
|
|
123
|
+
public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(2)),
|
|
124
|
+
'bind keeps the web session'
|
|
125
|
+
);
|
|
126
|
+
select ok(
|
|
127
|
+
not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(3)),
|
|
128
|
+
'bind happens once'
|
|
129
|
+
);
|
|
130
|
+
select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('b2'), pg_temp.session(1)), 0, 'another person ends nothing');
|
|
131
|
+
select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(3)), 0, 'another app session ends nothing');
|
|
132
|
+
select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)), 1, 'the app session that handed off ends one web session');
|
|
133
|
+
reset role;
|
|
134
|
+
select is(
|
|
135
|
+
(select array_agg(id order by id) from auth.sessions where user_id = pg_temp.person('a1')),
|
|
136
|
+
array[pg_temp.session(1), pg_temp.session(3)],
|
|
137
|
+
'only the handed-off web session is gone; the app and the other device stay'
|
|
138
|
+
);
|
|
139
|
+
set local role service_role;
|
|
140
|
+
select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)), 0, 'ending again ends nothing');
|
|
141
|
+
|
|
142
|
+
-- Cancel, and the nonce's life.
|
|
143
|
+
select lives_ok(
|
|
144
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(2), '/a') $$,
|
|
145
|
+
'a second handoff'
|
|
146
|
+
);
|
|
147
|
+
select lives_ok($$ select * from public.genesis_web_handoff_claim(pg_temp.digest(2), pg_temp.nonce(4)) $$, 'claimed');
|
|
148
|
+
select ok(public.genesis_web_handoff_discard(pg_temp.nonce(4)), 'Cancel discards it');
|
|
149
|
+
select is_empty($$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(4)) $$, 'Continue after Cancel is refused');
|
|
150
|
+
|
|
151
|
+
select lives_ok(
|
|
152
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(3), '/a') $$,
|
|
153
|
+
'a third handoff'
|
|
154
|
+
);
|
|
155
|
+
select lives_ok($$ select * from public.genesis_web_handoff_claim(pg_temp.digest(3), pg_temp.nonce(5), 5) $$, 'claimed with a 5-second nonce');
|
|
156
|
+
reset role;
|
|
157
|
+
update private.genesis_web_handoffs set nonce_expires_at = now() - interval '1 second' where nonce_digest = pg_temp.nonce(5);
|
|
158
|
+
set local role service_role;
|
|
159
|
+
select is_empty($$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(5)) $$, 'Continue after the nonce expired is refused');
|
|
160
|
+
|
|
161
|
+
-- Expired links, refusals and the rate limit (b2).
|
|
162
|
+
select lives_ok(
|
|
163
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(4), '/a', 10) $$,
|
|
164
|
+
'a 10-second handoff is issued'
|
|
165
|
+
);
|
|
166
|
+
reset role;
|
|
167
|
+
update private.genesis_web_handoffs set created_at = now() - interval '1 minute', expires_at = now() - interval '1 second' where token_digest = pg_temp.digest(4);
|
|
168
|
+
set local role service_role;
|
|
169
|
+
select is_empty(
|
|
170
|
+
$$ select * from public.genesis_web_handoff_claim(pg_temp.digest(4), pg_temp.nonce(6)) $$,
|
|
171
|
+
'an expired link claims nothing'
|
|
172
|
+
);
|
|
173
|
+
select throws_ok(
|
|
174
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(5), '/a', 301) $$,
|
|
175
|
+
'22023', null, 'a lifetime over 300 seconds is refused'
|
|
176
|
+
);
|
|
177
|
+
select throws_ok(
|
|
178
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(5), '//evil.example') $$,
|
|
179
|
+
'23514', null, 'a protocol-relative path is refused'
|
|
180
|
+
);
|
|
181
|
+
select lives_ok(
|
|
182
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(6), '/a', 60, 2, 60) $$,
|
|
183
|
+
'first in the window'
|
|
184
|
+
);
|
|
185
|
+
select lives_ok(
|
|
186
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(7), '/a', 60, 2, 60) $$,
|
|
187
|
+
'second in the window, at the limit'
|
|
188
|
+
);
|
|
189
|
+
select throws_ok(
|
|
190
|
+
$$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(8), '/a', 60, 2, 60) $$,
|
|
191
|
+
'GS429', null, 'the third in the window is refused'
|
|
192
|
+
);
|
|
193
|
+
reset role;
|
|
194
|
+
|
|
195
|
+
-- Deleting the login deletes its handoffs.
|
|
196
|
+
delete from auth.users where id = pg_temp.person('b2');
|
|
197
|
+
select is(
|
|
198
|
+
(select count(*)::int from private.genesis_web_handoffs where user_id = pg_temp.person('b2')),
|
|
199
|
+
0, 'a deleted login leaves no handoffs'
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
select * from finish();
|
|
203
|
+
rollback;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
-- @feat:owner-access
|
|
2
|
+
-- pgTAP test for 0016_genesis_viewer_people.sql.
|
|
3
|
+
-- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
|
|
4
|
+
-- It writes only its own users and hook and rolls everything back. If your app already has the
|
|
5
|
+
-- hook, it is dropped and replaced inside this transaction only.
|
|
6
|
+
begin;
|
|
7
|
+
create extension if not exists pgtap with schema extensions;
|
|
8
|
+
select plan(9);
|
|
9
|
+
|
|
10
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
11
|
+
select ('00000000-0000-4000-8000-0000000016' || n)::uuid, 'authenticated', 'authenticated', 'genesis-viewer-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
12
|
+
from unnest(array['a1', 'b2']) n;
|
|
13
|
+
|
|
14
|
+
create function pg_temp.login(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-0000000016' || n)::uuid $$;
|
|
15
|
+
-- The wrapper is called as the owner with a person's claims, the way a policy calls it for that
|
|
16
|
+
-- person (signed-in callers can't name the private schema themselves).
|
|
17
|
+
create function pg_temp.claims(n text) returns void language plpgsql as $$
|
|
18
|
+
begin
|
|
19
|
+
perform set_config('request.jwt.claims', json_build_object('sub', pg_temp.login(n), 'role', 'authenticated')::text, true);
|
|
20
|
+
perform set_config('request.jwt.claim.sub', pg_temp.login(n)::text, true);
|
|
21
|
+
end $$;
|
|
22
|
+
create function pg_temp.no_one() returns void language plpgsql as $$
|
|
23
|
+
begin
|
|
24
|
+
perform set_config('request.jwt.claims', '', true);
|
|
25
|
+
perform set_config('request.jwt.claim.sub', '', true);
|
|
26
|
+
end $$;
|
|
27
|
+
|
|
28
|
+
drop function if exists private.genesis_viewer_people(uuid);
|
|
29
|
+
|
|
30
|
+
select ok(not has_function_privilege('anon', 'private.genesis_viewer_person_ids()', 'execute'), 'anon can''t call genesis_viewer_person_ids');
|
|
31
|
+
select ok(has_function_privilege('authenticated', 'private.genesis_viewer_person_ids()', 'execute'), 'authenticated can, for row security');
|
|
32
|
+
|
|
33
|
+
-- No hook: fail closed, without an error.
|
|
34
|
+
select pg_temp.claims('a1');
|
|
35
|
+
select lives_ok($$select * from private.genesis_viewer_person_ids()$$, 'without the hook, no error');
|
|
36
|
+
select is((select count(*)::int from private.genesis_viewer_person_ids()), 0, '... and an empty set');
|
|
37
|
+
|
|
38
|
+
-- The app's hook: a1 has a personal record (its tenant is its own id) and records in two
|
|
39
|
+
-- organizations, one listed twice and one row with a null person; b2 has none.
|
|
40
|
+
create function private.genesis_viewer_people(viewer uuid)
|
|
41
|
+
returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
|
|
42
|
+
select t, p from (values
|
|
43
|
+
('00000000-0000-4000-9000-0000000016c1'::uuid, '00000000-0000-4000-9000-0000000016c1'::uuid),
|
|
44
|
+
('00000000-0000-4000-9000-0000000016d1'::uuid, '00000000-0000-4000-a000-0000000016d1'::uuid),
|
|
45
|
+
('00000000-0000-4000-9000-0000000016d2'::uuid, '00000000-0000-4000-a000-0000000016d2'::uuid),
|
|
46
|
+
('00000000-0000-4000-9000-0000000016d2'::uuid, '00000000-0000-4000-a000-0000000016d2'::uuid),
|
|
47
|
+
('00000000-0000-4000-9000-0000000016d3'::uuid, null::uuid)
|
|
48
|
+
) r (t, p)
|
|
49
|
+
where viewer = '00000000-0000-4000-8000-0000000016a1'::uuid
|
|
50
|
+
$$;
|
|
51
|
+
|
|
52
|
+
select set_eq(
|
|
53
|
+
$$select tenant_id, person_id from private.genesis_viewer_person_ids()$$,
|
|
54
|
+
$$values ('00000000-0000-4000-9000-0000000016c1'::uuid, '00000000-0000-4000-9000-0000000016c1'::uuid),
|
|
55
|
+
('00000000-0000-4000-9000-0000000016d1'::uuid, '00000000-0000-4000-a000-0000000016d1'::uuid),
|
|
56
|
+
('00000000-0000-4000-9000-0000000016d2'::uuid, '00000000-0000-4000-a000-0000000016d2'::uuid)$$,
|
|
57
|
+
'a personal record and two organizations, without duplicates or null ids');
|
|
58
|
+
select is((select count(*)::int from private.genesis_viewer_person_ids()), 3, '... once each');
|
|
59
|
+
|
|
60
|
+
select pg_temp.claims('b2');
|
|
61
|
+
select is((select count(*)::int from private.genesis_viewer_person_ids()), 0, 'a login with no people gets an empty set');
|
|
62
|
+
|
|
63
|
+
select pg_temp.no_one();
|
|
64
|
+
select is((select count(*)::int from private.genesis_viewer_person_ids()), 0, 'nobody signed in gets an empty set');
|
|
65
|
+
|
|
66
|
+
-- A hook that errors fails closed.
|
|
67
|
+
drop function private.genesis_viewer_people(uuid);
|
|
68
|
+
create function private.genesis_viewer_people(viewer uuid)
|
|
69
|
+
returns table (tenant_id uuid, person_id uuid) language plpgsql stable set search_path = '' as $$
|
|
70
|
+
begin
|
|
71
|
+
raise exception 'broken hook';
|
|
72
|
+
end $$;
|
|
73
|
+
select pg_temp.claims('a1');
|
|
74
|
+
select is((select count(*)::int from private.genesis_viewer_person_ids()), 0, 'a hook that errors gives an empty set');
|
|
75
|
+
|
|
76
|
+
select * from finish();
|
|
77
|
+
rollback;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
-- @feat:feature-mirror
|
|
2
|
+
-- pgTAP test for 0017_genesis_person_entitlements.sql (needs 0004, 0008, 0014 and 0016).
|
|
3
|
+
-- Copy into your app's `supabase/tests/database/` and run `supabase test db`. Rolls back. If your
|
|
4
|
+
-- app already has the membership or viewer-people hooks, they are replaced inside this
|
|
5
|
+
-- transaction only.
|
|
6
|
+
begin;
|
|
7
|
+
create extension if not exists pgtap with schema extensions;
|
|
8
|
+
select plan(25);
|
|
9
|
+
|
|
10
|
+
-- Two logins, both members of tenant T. a1 is person P1 in T; b2 is person P2 in T.
|
|
11
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
12
|
+
select ('00000000-0000-4000-8000-0000000017' || n)::uuid, 'authenticated', 'authenticated', 'person-ent-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
13
|
+
from unnest(array['a1', 'b2']) n;
|
|
14
|
+
|
|
15
|
+
create function pg_temp.t() returns uuid language sql immutable as $$ select '00000000-0000-4000-9000-0000000017c1'::uuid $$;
|
|
16
|
+
create function pg_temp.p(n int) returns uuid language sql immutable as $$ select ('00000000-0000-4000-a000-00000000170' || n)::uuid $$;
|
|
17
|
+
create function pg_temp.as_login(n text) returns void language plpgsql as $$
|
|
18
|
+
begin
|
|
19
|
+
perform set_config('request.jwt.claims', json_build_object('sub', '00000000-0000-4000-8000-0000000017' || n, 'role', 'authenticated')::text, true);
|
|
20
|
+
perform set_config('request.jwt.claim.sub', '00000000-0000-4000-8000-0000000017' || n, true);
|
|
21
|
+
end $$;
|
|
22
|
+
|
|
23
|
+
drop function if exists private.genesis_tenant_member(uuid, uuid);
|
|
24
|
+
create function private.genesis_tenant_member(tenant uuid, person uuid) returns boolean language sql stable set search_path = '' as $$
|
|
25
|
+
select tenant = '00000000-0000-4000-9000-0000000017c1'::uuid
|
|
26
|
+
and person in ('00000000-0000-4000-8000-0000000017a1'::uuid, '00000000-0000-4000-8000-0000000017b2'::uuid)
|
|
27
|
+
$$;
|
|
28
|
+
revoke execute on function private.genesis_tenant_member(uuid, uuid) from public, anon, authenticated;
|
|
29
|
+
|
|
30
|
+
drop function if exists private.genesis_viewer_people(uuid);
|
|
31
|
+
create function private.genesis_viewer_people(viewer uuid)
|
|
32
|
+
returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
|
|
33
|
+
select '00000000-0000-4000-9000-0000000017c1'::uuid,
|
|
34
|
+
case viewer when '00000000-0000-4000-8000-0000000017a1'::uuid then '00000000-0000-4000-a000-000000001701'::uuid
|
|
35
|
+
when '00000000-0000-4000-8000-0000000017b2'::uuid then '00000000-0000-4000-a000-000000001702'::uuid end
|
|
36
|
+
$$;
|
|
37
|
+
revoke execute on function private.genesis_viewer_people(uuid) from public, anon, authenticated;
|
|
38
|
+
|
|
39
|
+
select public.sync_feature_registry(9300, 'person-1',
|
|
40
|
+
'[{"id": "classes", "name": "Classes", "status": "live", "access": {"plan": "member"}},
|
|
41
|
+
{"id": "coaching", "name": "Coaching", "status": "live", "access": {"plan": "coaching"}},
|
|
42
|
+
{"id": "free", "name": "Free", "status": "live"}]', '[]');
|
|
43
|
+
|
|
44
|
+
-- Privileges ---------------------------------------------------------------------------------
|
|
45
|
+
select ok(not has_function_privilege('authenticated', 'public.grant_person_entitlement(uuid,uuid,text,text,text,timestamptz,timestamptz)', 'execute'), 'a signed-in person can''t grant');
|
|
46
|
+
select ok(not has_function_privilege('authenticated', 'public.revoke_person_entitlement(uuid,uuid,text,text,text)', 'execute'), '... nor revoke');
|
|
47
|
+
select ok(not has_function_privilege('authenticated', 'public.feature_gate_state(uuid,uuid)', 'execute'), '... nor name a person for gate state');
|
|
48
|
+
select ok(not has_table_privilege('authenticated', 'public.feature_person_entitlements', 'insert'), '... nor write the table');
|
|
49
|
+
select ok(not has_function_privilege('anon', 'public.feature_gate_state(uuid)', 'execute'), 'anon still reads no gate state');
|
|
50
|
+
|
|
51
|
+
-- Granting -----------------------------------------------------------------------------------
|
|
52
|
+
set local role service_role;
|
|
53
|
+
select lives_ok($$select public.grant_person_entitlement(pg_temp.t(), pg_temp.p(1), 'classes', 'stripe', 'si_1')$$, 'server code grants P1 classes');
|
|
54
|
+
select lives_ok($$select public.grant_person_entitlement(pg_temp.t(), pg_temp.p(1), 'classes', 'stripe', 'si_1')$$, '... and a replayed webhook is fine');
|
|
55
|
+
select is((select count(*)::int from public.feature_person_entitlements where person_id = pg_temp.p(1)), 1, '... with one row');
|
|
56
|
+
select public.grant_person_entitlement(pg_temp.t(), pg_temp.p(1), 'coaching', 'stripe', 'pi_1', now() + interval '1 day', null);
|
|
57
|
+
select public.grant_person_entitlement(pg_temp.t(), pg_temp.p(2), 'coaching', 'manual', '', null, now() - interval '1 day');
|
|
58
|
+
select is(public.feature_gate_state(pg_temp.t(), pg_temp.p(1)) -> 'entitled', '["classes"]'::jsonb, 'server code reads a named person''s state; a grant not started yet doesn''t count');
|
|
59
|
+
select is(public.feature_gate_state(pg_temp.t(), pg_temp.p(2)) -> 'entitled', '[]'::jsonb, '... nor one that ended');
|
|
60
|
+
select is(public.feature_gate_state(pg_temp.t(), null) -> 'entitled', '[]'::jsonb, '... and no person means the tenant''s alone');
|
|
61
|
+
select is(public.feature_on(pg_temp.t(), 'classes'), false, 'server code''s feature_on stays the tenant''s answer');
|
|
62
|
+
reset role;
|
|
63
|
+
|
|
64
|
+
-- Reading, as each person ----------------------------------------------------------------------
|
|
65
|
+
set local role authenticated;
|
|
66
|
+
select pg_temp.as_login('a1');
|
|
67
|
+
select is(public.feature_on(pg_temp.t(), 'classes'), true, 'P1 may use classes');
|
|
68
|
+
select is(public.feature_on(pg_temp.t(), 'coaching'), false, '... but not coaching before it starts');
|
|
69
|
+
select is(public.feature_gate_state(pg_temp.t()) -> 'entitled', '["classes"]'::jsonb, 'P1''s gate state holds their own grant');
|
|
70
|
+
select ok(pg_temp.t() in (select public.feature_entitled_tenants('classes')), 'feature_entitled_tenants includes the tenant where P1 holds it');
|
|
71
|
+
select is((select count(*)::int from public.feature_person_entitlements), 2, 'P1 reads their own rows');
|
|
72
|
+
select throws_ok($$select public.feature_gate_state(pg_temp.t(), pg_temp.p(2))$$, '42501', null, 'P1 can''t name another person');
|
|
73
|
+
|
|
74
|
+
select pg_temp.as_login('b2');
|
|
75
|
+
select is(public.feature_on(pg_temp.t(), 'classes'), false, 'P2, in the same tenant, may not use P1''s classes');
|
|
76
|
+
select is((select count(*)::int from public.feature_person_entitlements), 1, 'P2 reads only their own row');
|
|
77
|
+
select is(public.feature_on(pg_temp.t(), 'free'), true, 'a free feature is on for everyone');
|
|
78
|
+
reset role;
|
|
79
|
+
select set_config('request.jwt.claims', '', true);
|
|
80
|
+
select set_config('request.jwt.claim.sub', '', true);
|
|
81
|
+
|
|
82
|
+
-- Revoking, and the tenant's own grants ---------------------------------------------------------
|
|
83
|
+
set local role service_role;
|
|
84
|
+
select is(public.revoke_person_entitlement(pg_temp.t(), pg_temp.p(1), 'classes', 'stripe', 'si_1'), true, 'revoking a grant');
|
|
85
|
+
select is(public.revoke_person_entitlement(pg_temp.t(), pg_temp.p(1), 'classes', 'stripe', 'si_nope'), false, '... and one that doesn''t exist is false');
|
|
86
|
+
insert into public.feature_entitlements (tenant_id, feature_id, source, active) values (pg_temp.t(), 'coaching', 'manual', true);
|
|
87
|
+
reset role;
|
|
88
|
+
|
|
89
|
+
set local role authenticated;
|
|
90
|
+
select pg_temp.as_login('a1');
|
|
91
|
+
select is(public.feature_on(pg_temp.t(), 'classes'), false, 'a revoked grant no longer counts');
|
|
92
|
+
select is(public.feature_on(pg_temp.t(), 'coaching'), true, 'a tenant grant still counts for every member');
|
|
93
|
+
reset role;
|
|
94
|
+
|
|
95
|
+
select * from finish();
|
|
96
|
+
rollback;
|