1
0
Fork 0
fahrPool/supabase/tests/notification_outbox_rls_tes...

103 lines
3.7 KiB
PL/PgSQL

begin;
select plan(8);
insert into auth.users (id, email) values
('c1111111-1111-1111-1111-111111111111', 'admin-outbox@auftraege.test.local'),
('c2222222-2222-2222-2222-222222222222', 'disponent-outbox@auftraege.test.local');
update public.profiles set role = 'admin' where id = 'c1111111-1111-1111-1111-111111111111';
update public.profiles set role = 'disponent' where id = 'c2222222-2222-2222-2222-222222222222';
insert into public.orte (id, adresse, ansprechpartner) values
('c3111111-1111-1111-1111-111111111111', 'Startstr. 1, Teststadt', 'Frau A'),
('c3222222-2222-2222-2222-222222222222', 'Zielstr. 2, Teststadt', 'Herr B');
insert into public.fahrzeuge (id, kennzeichen) values
('c3333333-0000-0000-0000-000000000001', 'S-WA 42');
set local role authenticated;
set local request.jwt.claims to '{"sub": "c2222222-2222-2222-2222-222222222222", "role": "authenticated"}';
-- 1) Disponent legt einen Auftrag an -> Enqueue-Trigger erzeugt genau eine Zeile.
insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status)
values (
'c4000000-0000-0000-0000-00000000000a',
'c3111111-1111-1111-1111-111111111111',
'c3222222-2222-2222-2222-222222222222',
'c3333333-0000-0000-0000-000000000001',
'c2222222-2222-2222-2222-222222222222',
'offen'
);
reset role;
reset request.jwt.claims;
select is(
(select count(*)::int from public.notification_outbox
where auftrag_id = 'c4000000-0000-0000-0000-00000000000a'),
1,
'Enqueue-Trigger legt genau eine notification_outbox-Zeile pro neuem Auftrag an'
);
select is(
(select status from public.notification_outbox
where auftrag_id = 'c4000000-0000-0000-0000-00000000000a'),
'pending',
'Neue Outbox-Zeile hat status = pending'
);
select is(
(select event_type from public.notification_outbox
where auftrag_id = 'c4000000-0000-0000-0000-00000000000a'),
'auftrag_erstellt',
'Neue Outbox-Zeile hat event_type = auftrag_erstellt'
);
select is(
(select payload->>'kennzeichen' from public.notification_outbox
where auftrag_id = 'c4000000-0000-0000-0000-00000000000a'),
'S-WA 42',
'payload.kennzeichen kommt aus dem verknuepften Fahrzeug'
);
select is(
(select payload->>'abholort_adresse' from public.notification_outbox
where auftrag_id = 'c4000000-0000-0000-0000-00000000000a'),
'Startstr. 1, Teststadt',
'payload.abholort_adresse kommt aus dem Abholort (nicht dem Zielort)'
);
-- 2) SELECT-RLS: Disponent sieht die Outbox nicht.
set local role authenticated;
set local request.jwt.claims to '{"sub": "c2222222-2222-2222-2222-222222222222", "role": "authenticated"}';
select is_empty(
$$ select id from public.notification_outbox $$,
'Disponent sieht keine notification_outbox-Zeilen (keine SELECT-Policy fuer disponent)'
);
-- 3) SELECT-RLS: Admin sieht die Outbox.
reset request.jwt.claims;
set local request.jwt.claims to '{"sub": "c1111111-1111-1111-1111-111111111111", "role": "authenticated"}';
select isnt_empty(
$$ select id from public.notification_outbox $$,
'Admin sieht notification_outbox-Zeilen'
);
-- 4) EXECUTE-Recht: authenticated darf die Batch-Claim-Funktion nicht aufrufen
-- (revoke von authenticated, grant nur an service_role).
set local request.jwt.claims to '{"sub": "c2222222-2222-2222-2222-222222222222", "role": "authenticated"}';
-- 2-arg throws_ok(sql, sqlstate): die 3-arg-Form (sql, sqlstate, description)
-- kollidiert mit pgTAPs throws_ok(sql, errcode, errmsg)-Signatur und wuerde die
-- Beschreibung als erwartete Fehlermeldung interpretieren. Getestet wird
-- unveraendert: authenticated laeuft in SQLSTATE 42501 (insufficient_privilege).
select throws_ok(
$$ select public.claim_notification_batch(5) $$,
'42501'
);
reset role;
reset request.jwt.claims;
select * from finish();
rollback;