begin; select plan(25); -- Firma A (deaktiviert, wird geloescht) und Firma B (aktiv, muss bleiben) insert into public.firmen (id, name, aktiv) values ('00000000-0000-0000-0000-0000000fa001', 'Loesch A', false), ('00000000-0000-0000-0000-0000000fb001', 'Bleibt B', true); insert into auth.users (id, email) values ('aa000000-0000-0000-0000-000000000001', 'admin-a@loeschen.test.local'), ('aa000000-0000-0000-0000-000000000002', 'fahrer-a@loeschen.test.local'), ('bb000000-0000-0000-0000-000000000001', 'admin-b@loeschen.test.local'), ('5a000000-0000-0000-0000-0000000005a1', 'super@loeschen.test.local'); update public.profiles set firma_id = '00000000-0000-0000-0000-0000000fa001', role = 'admin' where id = 'aa000000-0000-0000-0000-000000000001'; update public.profiles set firma_id = '00000000-0000-0000-0000-0000000fa001', role = 'fahrer' where id = 'aa000000-0000-0000-0000-000000000002'; update public.profiles set firma_id = '00000000-0000-0000-0000-0000000fb001', role = 'admin' where id = 'bb000000-0000-0000-0000-000000000001'; update public.profiles set role = 'superadmin' where id = '5a000000-0000-0000-0000-0000000005a1'; insert into public.nutzer_details (user_id, firma_id) values ('aa000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-0000000fa001'); insert into public.orte (id, adresse, ansprechpartner, firma_id, created_by) values ('aa000000-0000-0000-0000-0000000000c1', 'Ort A1', 'Herr A', '00000000-0000-0000-0000-0000000fa001', 'aa000000-0000-0000-0000-000000000001'), ('aa000000-0000-0000-0000-0000000000c2', 'Ort A2', 'Frau A', '00000000-0000-0000-0000-0000000fa001', 'aa000000-0000-0000-0000-000000000001'), ('bb000000-0000-0000-0000-0000000000c1', 'Ort B1', 'Herr B', '00000000-0000-0000-0000-0000000fb001', null), ('bb000000-0000-0000-0000-0000000000c2', 'Ort B2', 'Frau B', '00000000-0000-0000-0000-0000000fb001', null); insert into public.fahrzeuge (id, kennzeichen, firma_id) values ('aa000000-0000-0000-0000-0000000000d1', 'LA-A 1', '00000000-0000-0000-0000-0000000fa001'), ('bb000000-0000-0000-0000-0000000000d1', 'LB-B 1', '00000000-0000-0000-0000-0000000fb001'); insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, zugewiesen_an, status, firma_id) values ('aa000000-0000-0000-0000-0000000000e1', 'aa000000-0000-0000-0000-0000000000c1', 'aa000000-0000-0000-0000-0000000000c2', 'aa000000-0000-0000-0000-0000000000d1', 'aa000000-0000-0000-0000-000000000001', 'aa000000-0000-0000-0000-000000000002', 'zugewiesen', '00000000-0000-0000-0000-0000000fa001'), ('bb000000-0000-0000-0000-0000000000e1', 'bb000000-0000-0000-0000-0000000000c1', 'bb000000-0000-0000-0000-0000000000c2', 'bb000000-0000-0000-0000-0000000000d1', 'bb000000-0000-0000-0000-000000000001', null, 'offen', '00000000-0000-0000-0000-0000000fb001'); -- Folgeauftrag in A (Selbstreferenz) insert into public.auftraege (abholort_id, zielort_id, fahrzeug_id, erstellt_von, vorgaenger_auftrag_id, firma_id) values ('aa000000-0000-0000-0000-0000000000c2', 'aa000000-0000-0000-0000-0000000000c1', 'aa000000-0000-0000-0000-0000000000d1', 'aa000000-0000-0000-0000-000000000001', 'aa000000-0000-0000-0000-0000000000e1', '00000000-0000-0000-0000-0000000fa001'); insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by, firma_id) values ('aa000000-0000-0000-0000-0000000000e1', 'front', 'aa000000-0000-0000-0000-0000000000e1/front.jpg', 'aa000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-0000000fa001'); insert into public.arbeitszeiten (fahrer_id, kommt_um, firma_id) values ('aa000000-0000-0000-0000-000000000002', now() - interval '2 hours', '00000000-0000-0000-0000-0000000fa001'); -- Firmenfremde Verweise aus B auf Nutzer von A (per RLS fuer B-Nutzer -- moeglich, z.B. PATCH orte.created_by / auftrag_fotos.uploaded_by). insert into public.orte (id, adresse, ansprechpartner, firma_id, created_by) values ('bb000000-0000-0000-0000-0000000000c3', 'Ort B3', 'Herr C', '00000000-0000-0000-0000-0000000fb001', 'aa000000-0000-0000-0000-000000000001'); insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by, firma_id) values ('bb000000-0000-0000-0000-0000000000e1', 'front', 'bb000000-0000-0000-0000-0000000000e1/front.jpg', 'aa000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-0000000fb001'); create temp table b_vorher as select (select array_agg(id order by id) from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fb001') as audit, (select count(*) from public.fahrzeuge where firma_id = '00000000-0000-0000-0000-0000000fb001') as fahrzeuge, (select count(*) from public.auftrag_fotos where firma_id = '00000000-0000-0000-0000-0000000fb001') as fotos, (select count(*) from auth.users where id in ('bb000000-0000-0000-0000-000000000001', '5a000000-0000-0000-0000-0000000005a1')) as logins; -- 1) aktive Firma laesst sich nicht loeschen select throws_ok( $$ select public.firma_endgueltig_loeschen('00000000-0000-0000-0000-0000000fb001', '5a000000-0000-0000-0000-0000000005a1') $$, 'P0001', 'Firma ist noch aktiv', 'Aktive Firma wird nicht geloescht' ); -- 2) unbekannte Firma select throws_ok( $$ select public.firma_endgueltig_loeschen(gen_random_uuid(), '5a000000-0000-0000-0000-0000000005a1') $$, 'P0001', 'Firma nicht gefunden', 'Unbekannte Firma meldet Fehler' ); -- 3) Rechte select ok(not has_function_privilege('authenticated', 'public.firma_endgueltig_loeschen(uuid, uuid)', 'execute'), 'authenticated darf die Funktion nicht ausfuehren'); select ok(not has_function_privilege('anon', 'public.firma_endgueltig_loeschen(uuid, uuid)', 'execute'), 'anon darf die Funktion nicht ausfuehren'); select ok(has_function_privilege('service_role', 'public.firma_endgueltig_loeschen(uuid, uuid)', 'execute'), 'service_role darf die Funktion ausfuehren'); -- 4) Audit-Log bleibt ohne Einstellung unveraenderlich select throws_ok( $$ delete from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fb001' $$, '42501', null, 'Audit-Log-Eintraege bleiben ohne Loesch-Einstellung unveraenderlich' ); -- 5) Loeschen von A create temp table ergebnis as select public.firma_endgueltig_loeschen('00000000-0000-0000-0000-0000000fa001', '5a000000-0000-0000-0000-0000000005a1') as z; select is((select (z ->> 'nutzer')::int from ergebnis), 2, 'Zaehler Nutzer'); select is((select (z ->> 'auftraege')::int from ergebnis), 2, 'Zaehler Auftraege'); select is((select (z ->> 'fotos')::int from ergebnis), 1, 'Zaehler Fotos'); select is((select count(*)::int from public.firmen where id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Firma A weg'); select is((select count(*)::int from public.auftraege where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Auftraege A weg'); select is((select count(*)::int from public.auftrag_fotos where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Fotos A weg'); select is((select count(*)::int from public.orte where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Orte A weg'); select is((select count(*)::int from public.fahrzeuge where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Fahrzeuge A weg'); select is((select count(*)::int from public.arbeitszeiten where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Arbeitszeiten A weg'); select is((select count(*)::int from public.nutzer_details where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Nutzerdetails A weg'); select is((select count(*)::int from public.notification_outbox where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Outbox A weg'); select is((select count(*)::int from public.auftragsnummer_zaehler where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Nummernzaehler A weg'); select is((select count(*)::int from public.profiles where firma_id = '00000000-0000-0000-0000-0000000fa001'), 0, 'Profile A weg'); select is((select count(*)::int from auth.users where id in ('aa000000-0000-0000-0000-000000000001', 'aa000000-0000-0000-0000-000000000002')), 0, 'Logins A weg'); -- 6) B unveraendert select is( (select count(*)::int from public.auftraege where firma_id = '00000000-0000-0000-0000-0000000fb001') + (select count(*)::int from public.orte where firma_id = '00000000-0000-0000-0000-0000000fb001') + (select count(*)::int from public.profiles where firma_id = '00000000-0000-0000-0000-0000000fb001') + (select count(*)::int from public.firmen where id = '00000000-0000-0000-0000-0000000fb001'), 6, 'Firma B bleibt vollstaendig erhalten' ); select is( (select row( (select array_agg(id order by id) from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fb001' and id in (select unnest(audit) from b_vorher)), (select count(*) from public.fahrzeuge where firma_id = '00000000-0000-0000-0000-0000000fb001'), (select count(*) from public.auftrag_fotos where firma_id = '00000000-0000-0000-0000-0000000fb001'), (select count(*) from auth.users where id in ('bb000000-0000-0000-0000-000000000001', '5a000000-0000-0000-0000-0000000005a1')) )::text), (select row(audit, fahrzeuge, fotos, logins)::text from b_vorher), 'Bestehende Audit-Eintraege, Fahrzeuge, Fotos und Logins von B sowie der Superadmin bleiben' ); select is( (select created_by from public.orte where id = 'bb000000-0000-0000-0000-0000000000c3'), null::uuid, 'Firmenfremder Verweis auf geloeschten Nutzer wird geleert' ); -- 7) genau ein Loesch-Eintrag im Audit-Log select is((select count(*)::int from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fa001'), 1, 'Nur der Loesch-Eintrag bleibt im Audit-Log'); select is( (select bereich || '/' || aktion || '/' || akteur_rolle from public.audit_log where firma_id = '00000000-0000-0000-0000-0000000fa001'), 'firma/geloescht/superadmin', 'Loesch-Eintrag mit Superadmin als Akteur' ); select * from finish(); rollback;