begin; select plan(20); -- Firma B neben der Bestandsfirma Widmann (f001) insert into public.firmen (id, name) values ('00000000-0000-0000-0000-00000000f002', 'Firma B'); insert into auth.users (id, email) values ('a0000000-0000-0000-0000-0000000000a1', 'admin-a@mandant.test.local'), ('a0000000-0000-0000-0000-0000000000a3', 'fahrer-a@mandant.test.local'), ('b0000000-0000-0000-0000-0000000000b1', 'admin-b@mandant.test.local'), ('b0000000-0000-0000-0000-0000000000b3', 'fahrer-b@mandant.test.local'), ('5a000000-0000-0000-0000-000000000001', 'superadmin@mandant.test.local'); -- 1) Neues Profil hat keine Firma (handle_new_user bleibt unveraendert) select is( (select firma_id from public.profiles where id = 'a0000000-0000-0000-0000-0000000000a1'), null::uuid, 'Neues Profil startet ohne Firma' ); update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001', role = 'admin' where id = 'a0000000-0000-0000-0000-0000000000a1'; update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001', role = 'fahrer' where id = 'a0000000-0000-0000-0000-0000000000a3'; update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002', role = 'admin' where id = 'b0000000-0000-0000-0000-0000000000b1'; update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002', role = 'fahrer' where id = 'b0000000-0000-0000-0000-0000000000b3'; update public.profiles set role = 'superadmin' where id = '5a000000-0000-0000-0000-000000000001'; -- 2) Superadmin mit Firma ist verboten select throws_ok( $$ update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where id = '5a000000-0000-0000-0000-000000000001' $$, '23514', null, 'Superadmin darf keiner Firma angehoeren' ); -- 3) Firmenwechsel eines Profils ist verboten (auch ohne Session, wenn schon gesetzt) select throws_ok( $$ update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002' where id = 'a0000000-0000-0000-0000-0000000000a3' $$, '42501', 'firma_id darf nicht geaendert werden', 'firma_id eines Profils ist unveraenderlich' ); -- Stammdaten ohne Session: firma_id explizit insert into public.orte (id, adresse, ansprechpartner, firma_id) values ('a0000000-0000-0000-0000-0000000000c1', 'Ort A', 'Herr A', '00000000-0000-0000-0000-00000000f001'), ('b0000000-0000-0000-0000-0000000000c1', 'Ort B', 'Frau B', '00000000-0000-0000-0000-00000000f002'); insert into public.fahrzeuge (id, kennzeichen, firma_id) values ('a0000000-0000-0000-0000-0000000000d1', 'MT-AB 1', '00000000-0000-0000-0000-00000000f001'); -- 4) Ohne Session und ohne firma_id: Fehler select throws_ok( $$ insert into public.orte (adresse, ansprechpartner) values ('X', 'Y') $$, '23502', 'firma_id fehlt', 'Insert ohne Session braucht eine firma_id' ); -- 5) Gleiches Kennzeichen in zwei Firmen ist erlaubt select lives_ok( $$ insert into public.fahrzeuge (id, kennzeichen, firma_id) values ('b0000000-0000-0000-0000-0000000000d1', 'MT-AB 1', '00000000-0000-0000-0000-00000000f002') $$, 'Gleiches Kennzeichen in einer anderen Firma ist erlaubt' ); -- 6) Doppeltes Kennzeichen in derselben Firma nicht select throws_ok( $$ insert into public.fahrzeuge (kennzeichen, firma_id) values ('MT-AB 1', '00000000-0000-0000-0000-00000000f001') $$, '23505', null, 'Doppeltes Kennzeichen in derselben Firma scheitert' ); -- Session als Admin B set local role authenticated; set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; -- 7) Mitgeschickte fremde firma_id wird ueberschrieben select is( (select public.user_firma_id()), '00000000-0000-0000-0000-00000000f002'::uuid, 'user_firma_id liefert die Firma des angemeldeten Nutzers' ); reset role; reset request.jwt.claims; -- Insert mit Session, Rueckgabe ueber eine Hilfstabelle (RLS-Policies sind in Task 1 noch rollenbasiert) set local role authenticated; set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; insert into public.orte (id, adresse, ansprechpartner, firma_id) values ('b0000000-0000-0000-0000-0000000000c2', 'Ort B2', 'Herr B2', '00000000-0000-0000-0000-00000000f001'); reset role; reset request.jwt.claims; select is( (select firma_id from public.orte where id = 'b0000000-0000-0000-0000-0000000000c2'), '00000000-0000-0000-0000-00000000f002'::uuid, 'Mitgeschickte fremde firma_id wird durch die eigene Firma ersetzt' ); -- 9) Auftrag mit Ort einer fremden Firma: Exception select throws_ok( $$ insert into public.auftraege (abholort_id, zielort_id, fahrzeug_id, erstellt_von, firma_id) values ('a0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000d1', 'b0000000-0000-0000-0000-0000000000b1', '00000000-0000-0000-0000-00000000f002') $$, '42501', 'Firmenfremder Verweis', 'Auftrag mit Ort einer fremden Firma scheitert' ); -- 10) Auftrag mit Fahrer einer fremden Firma: Exception select throws_ok( $$ insert into public.auftraege (abholort_id, zielort_id, fahrzeug_id, erstellt_von, zugewiesen_an, status, firma_id) values ('b0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000d1', 'b0000000-0000-0000-0000-0000000000b1', 'a0000000-0000-0000-0000-0000000000a3', 'zugewiesen', '00000000-0000-0000-0000-00000000f002') $$, '42501', 'Firmenfremder Verweis', 'Auftrag mit Fahrer einer fremden Firma scheitert' ); -- Je ein gueltiger Auftrag pro Firma update public.firmen set whatsapp_empfaenger = '+4915100000001' where id = '00000000-0000-0000-0000-00000000f001'; insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, firma_id) values ('a0000000-0000-0000-0000-0000000000e1', 'a0000000-0000-0000-0000-0000000000c1', 'a0000000-0000-0000-0000-0000000000c1', 'a0000000-0000-0000-0000-0000000000d1', 'a0000000-0000-0000-0000-0000000000a1', '00000000-0000-0000-0000-00000000f001'), ('b0000000-0000-0000-0000-0000000000e1', 'b0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000c1', 'b0000000-0000-0000-0000-0000000000d1', 'b0000000-0000-0000-0000-0000000000b1', '00000000-0000-0000-0000-00000000f002'); -- 11) Auftragsnummer zaehlt pro Firma: der erste B-Auftrag des Tages endet auf -0001 select is( (select right(auftragsnummer, 4) from public.auftraege where id = 'b0000000-0000-0000-0000-0000000000e1'), '0001', 'Auftragsnummer zaehlt pro Firma' ); -- 12) firma_id eines Auftrags ist unveraenderlich select throws_ok( $$ update public.auftraege set firma_id = '00000000-0000-0000-0000-00000000f002' where id = 'a0000000-0000-0000-0000-0000000000e1' $$, '42501', 'firma_id darf nicht geaendert werden', 'firma_id eines Auftrags ist unveraenderlich' ); -- 13) WhatsApp-Outbox nur fuer Firma mit Empfaenger, mit firma_id aus dem Auftrag select is( (select count(*) from public.notification_outbox where event_type = 'auftrag_erstellt' and auftrag_id = 'a0000000-0000-0000-0000-0000000000e1' and firma_id = '00000000-0000-0000-0000-00000000f001'), 1::bigint, 'Firma mit WhatsApp-Empfaenger bekommt eine Outbox-Zeile mit ihrer firma_id' ); select is( (select count(*) from public.notification_outbox where auftrag_id = 'b0000000-0000-0000-0000-0000000000e1'), 0::bigint, 'Firma ohne WhatsApp-Empfaenger bekommt keine Outbox-Zeile' ); -- 14a) FIN ist pro Firma eindeutig: gleiche FIN in f001 und f002 ist erlaubt update public.fahrzeuge set fin = 'WVWZZZ1KZAW000002' where id = 'a0000000-0000-0000-0000-0000000000d1'; select lives_ok( $$ update public.fahrzeuge set fin = 'WVWZZZ1KZAW000002' where id = 'b0000000-0000-0000-0000-0000000000d1' $$, 'Gleiche FIN in einer anderen Firma ist erlaubt' ); -- 14b) doppelte FIN in derselben Firma nicht select throws_ok( $$ insert into public.fahrzeuge (kennzeichen, fin, firma_id) values ('MT-AB 9', 'WVWZZZ1KZAW000002', '00000000-0000-0000-0000-00000000f001') $$, '23505', null, 'Doppelte FIN in derselben Firma scheitert' ); -- 14c) fin_bei_fahrtbeginn sucht nur in der eigenen Firma: eine FIN, die nur in -- Firma B existiert, wird in Firma A nachgetragen und nicht als abweichend gemeldet. insert into public.fahrzeuge (id, kennzeichen, fin, firma_id) values ('b0000000-0000-0000-0000-0000000000d2', 'MT-BB 2', 'WDB9066331S555555', '00000000-0000-0000-0000-00000000f002'); insert into public.fahrzeuge (id, kennzeichen, firma_id) values ('a0000000-0000-0000-0000-0000000000d2', 'MT-AB 2', '00000000-0000-0000-0000-00000000f001'); insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, zugewiesen_an, status, firma_id) values ('a0000000-0000-0000-0000-0000000000e2', 'a0000000-0000-0000-0000-0000000000c1', 'a0000000-0000-0000-0000-0000000000c1', 'a0000000-0000-0000-0000-0000000000d2', 'a0000000-0000-0000-0000-0000000000a1', 'a0000000-0000-0000-0000-0000000000a3', 'zugewiesen', '00000000-0000-0000-0000-00000000f001'); set local role authenticated; set local request.jwt.claims to '{"sub": "a0000000-0000-0000-0000-0000000000a3", "role": "authenticated"}'; select is( public.fin_bei_fahrtbeginn('a0000000-0000-0000-0000-0000000000e2', 'WDB9066331S555555'), 'nachgetragen'::public.app_fin_abgleich, 'FIN-Abgleich ignoriert FINs fremder Firmen (nachgetragen statt abweichend)' ); reset role; reset request.jwt.claims; select is( (select fin from public.fahrzeuge where id = 'a0000000-0000-0000-0000-0000000000d2'), 'WDB9066331S555555', 'FIN steht am Fahrzeug der eigenen Firma' ); -- 15) Mit Nutzer-Session kann aktiv nicht geaendert werden. Nur Claims setzen, -- Rolle bleibt postgres: in Task 1 gibt es noch keine Policy auf firmen, der -- Trigger soll aber trotzdem greifen. set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; select throws_ok( $$ update public.firmen set aktiv = false where id = '00000000-0000-0000-0000-00000000f002' $$, '42501', 'Status und ID einer Firma kann nur der Plattform-Betreiber aendern', 'Admin kann seine Firma nicht deaktivieren' ); reset request.jwt.claims; -- 16) Deaktivierte Firma: user_firma_id() liefert null update public.firmen set aktiv = false where id = '00000000-0000-0000-0000-00000000f002'; set local role authenticated; set local request.jwt.claims to '{"sub": "b0000000-0000-0000-0000-0000000000b1", "role": "authenticated"}'; select is((select public.user_firma_id()), null::uuid, 'Deaktivierte Firma: user_firma_id() ist null'); reset role; reset request.jwt.claims; select * from finish(); rollback;