forked from fahrPool/fahrPool
160 lines
6.4 KiB
PL/PgSQL
160 lines
6.4 KiB
PL/PgSQL
begin;
|
|
select plan(11);
|
|
|
|
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, array['auftrag_erstellt']) $$,
|
|
'42501'
|
|
);
|
|
|
|
reset role;
|
|
reset request.jwt.claims;
|
|
|
|
-- 5) claim_notification_batch waehlt genau die faelligen Zeilen aus.
|
|
-- Zuerst die vom Enqueue-Trigger oben angelegte Zeile entfernen, damit die
|
|
-- erwartete Ergebnismenge exakt ist (als postgres, RLS-umgehend).
|
|
delete from public.notification_outbox
|
|
where auftrag_id = 'c4000000-0000-0000-0000-00000000000a';
|
|
|
|
insert into public.notification_outbox
|
|
(id, event_type, status, payload, naechster_versuch_ab, updated_at)
|
|
values
|
|
-- faellig: pending, Zeitpunkt erreicht
|
|
('c5000000-0000-0000-0000-000000000001', 'auftrag_erstellt', 'pending', '{}'::jsonb,
|
|
now() - interval '1 minute', now()),
|
|
-- nicht faellig: failed, Backoff noch in der Zukunft
|
|
('c5000000-0000-0000-0000-000000000002', 'auftrag_erstellt', 'failed', '{}'::jsonb,
|
|
now() + interval '1 hour', now()),
|
|
-- nicht reclaimt: sending, erst vor 2 Minuten uebernommen
|
|
('c5000000-0000-0000-0000-000000000003', 'auftrag_erstellt', 'sending', '{}'::jsonb,
|
|
now(), now() - interval '2 minutes'),
|
|
-- reclaimt: sending, seit 20 Minuten haengengeblieben
|
|
('c5000000-0000-0000-0000-000000000004', 'auftrag_erstellt', 'sending', '{}'::jsonb,
|
|
now(), now() - interval '20 minutes');
|
|
|
|
select results_eq(
|
|
$$ select id::text from public.claim_notification_batch(10, array['auftrag_erstellt']) order by id $$,
|
|
$$ values ('c5000000-0000-0000-0000-000000000001'::text),
|
|
('c5000000-0000-0000-0000-000000000004') $$,
|
|
'claim_notification_batch liefert genau die faellige pending- und die abgestandene sending-Zeile'
|
|
);
|
|
|
|
-- 6) Write-RLS: authenticated (disponent) darf nicht in die Outbox schreiben.
|
|
set local role authenticated;
|
|
set local request.jwt.claims to '{"sub": "c2222222-2222-2222-2222-222222222222", "role": "authenticated"}';
|
|
|
|
-- 2-arg throws_ok(sql, sqlstate): siehe Kommentar bei Assertion 4 -- die 3-arg-Form
|
|
-- wuerde die Beschreibung als erwartete Fehlermeldung interpretieren. Geprueft wird
|
|
-- nur, dass der INSERT in SQLSTATE 42501 (RLS: keine INSERT-Policy) laeuft.
|
|
select throws_ok(
|
|
$$ insert into public.notification_outbox (event_type, payload) values ('x', '{}'::jsonb) $$,
|
|
'42501'
|
|
);
|
|
|
|
-- RLS ohne UPDATE-Policy wirft nicht, sondern trifft schlicht 0 Zeilen: das
|
|
-- UPDATE laeuft durch, aendert aber keine Zeile. Danach (als postgres) pruefen,
|
|
-- dass wirklich keine der vier Zeilen auf 'sent' gekippt ist. (Ein
|
|
-- data-modifying UPDATE laesst sich nicht in einen Sub-SELECT/CTE-Ausdruck
|
|
-- packen, daher als eigenes Statement + separate Assertion.)
|
|
update public.notification_outbox set status = 'sent' where true;
|
|
|
|
reset role;
|
|
reset request.jwt.claims;
|
|
|
|
select is(
|
|
(select count(*)::int from public.notification_outbox where status = 'sent'),
|
|
0,
|
|
'authenticated-UPDATE auf notification_outbox aendert keine Zeile (keine Write-Policy)'
|
|
);
|
|
|
|
select * from finish();
|
|
rollback;
|