1
0
Fork 0
fahrPool/supabase/tests/einsatzrollen_test.sql

215 lines
11 KiB
PL/PgSQL

begin;
select plan(18);
insert into public.firmen (id, name) values ('00000000-0000-0000-0000-00000000f002', 'Firma B');
insert into auth.users (id, email) values
('e1a00000-0000-0000-0000-000000000001', 'admin-einsatz@test.local'),
('e1a00000-0000-0000-0000-000000000002', 'dispo-einsatz@test.local'),
('e1a00000-0000-0000-0000-000000000003', 'fahrer-a-einsatz@test.local'),
('e1a00000-0000-0000-0000-000000000004', 'fahrer-b-einsatz@test.local'),
('e1a00000-0000-0000-0000-000000000005', 'readonly-einsatz@test.local'),
('e1a00000-0000-0000-0000-000000000006', 'fahrer-fremd-einsatz@test.local'),
('e1a00000-0000-0000-0000-000000000007', 'dispo-fremd-einsatz@test.local');
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001'
where id in ('e1a00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000002',
'e1a00000-0000-0000-0000-000000000003', 'e1a00000-0000-0000-0000-000000000004',
'e1a00000-0000-0000-0000-000000000005');
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f002'
where id in ('e1a00000-0000-0000-0000-000000000006', 'e1a00000-0000-0000-0000-000000000007');
update public.firmen set whatsapp_empfaenger = '+4915100000001' where id = '00000000-0000-0000-0000-00000000f001';
update public.profiles set role = 'admin' where id = 'e1a00000-0000-0000-0000-000000000001';
update public.profiles set role = 'disponent' where id = 'e1a00000-0000-0000-0000-000000000002';
update public.profiles set role = 'fahrer' where id = 'e1a00000-0000-0000-0000-000000000003';
update public.profiles set role = 'fahrer' where id = 'e1a00000-0000-0000-0000-000000000004';
-- ...05 bleibt readonly (Trigger-Default).
update public.profiles set role = 'fahrer' where id = 'e1a00000-0000-0000-0000-000000000006';
update public.profiles set role = 'disponent' where id = 'e1a00000-0000-0000-0000-000000000007';
-- Erwartete Anzahl Dispo-/Admin-Profile der Firma f001 (inkl. evtl. vorhandener), als Superuser berechnet.
create temp table erwartet_dispo_admin as
select count(*) as n from public.profiles
where role in ('disponent', 'admin') and firma_id = '00000000-0000-0000-0000-00000000f001';
grant select on erwartet_dispo_admin to authenticated;
insert into public.orte (id, adresse, ansprechpartner, firma_id) values
('e1b00000-0000-0000-0000-000000000001', 'Hauptstr. 1, Musterstadt', 'Frau Muster', '00000000-0000-0000-0000-00000000f001'),
('e1b00000-0000-0000-0000-000000000002', 'Nebenweg 2, Musterstadt', 'Herr Beispiel', '00000000-0000-0000-0000-00000000f001');
insert into public.fahrzeuge (id, kennzeichen, fin, firma_id) values
('e1c00000-0000-0000-0000-000000000001', 'M-EZ 001', 'WDB9066331S123456', '00000000-0000-0000-0000-00000000f001');
-- d1: Disponent, zugewiesen | d2: Admin, zugewiesen | d3: Fahrer A, zugewiesen
-- d4: Fahrer A (Zuweisungstests) | d5: Fahrer A, FIN bestaetigt | d6: Fahrer A, FIN abweichend
-- d7: Fahrer A (Fahrtbeginn) | d8: Disponent, unterwegs
insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, start_km, start_fin, start_fin_status, firma_id) values
('e1d00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'e1a00000-0000-0000-0000-000000000002', null, null, null, '00000000-0000-0000-0000-00000000f001'),
('e1d00000-0000-0000-0000-000000000002', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'e1a00000-0000-0000-0000-000000000001', null, null, null, '00000000-0000-0000-0000-00000000f001'),
('e1d00000-0000-0000-0000-000000000003', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'e1a00000-0000-0000-0000-000000000003', null, null, null, '00000000-0000-0000-0000-00000000f001'),
('e1d00000-0000-0000-0000-000000000004', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'e1a00000-0000-0000-0000-000000000003', null, null, null, '00000000-0000-0000-0000-00000000f001'),
('e1d00000-0000-0000-0000-000000000005', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'e1a00000-0000-0000-0000-000000000003', null, 'WDB9066331S123456', 'bestaetigt', '00000000-0000-0000-0000-00000000f001'),
('e1d00000-0000-0000-0000-000000000006', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'e1a00000-0000-0000-0000-000000000003', null, 'WVWZZZ1KZAW999999', 'abweichend', '00000000-0000-0000-0000-00000000f001'),
('e1d00000-0000-0000-0000-000000000007', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'zugewiesen', 'e1a00000-0000-0000-0000-000000000003', null, null, null, '00000000-0000-0000-0000-00000000f001'),
('e1d00000-0000-0000-0000-000000000008', 'e1b00000-0000-0000-0000-000000000001', 'e1b00000-0000-0000-0000-000000000002', 'e1c00000-0000-0000-0000-000000000001', 'e1a00000-0000-0000-0000-000000000001', 'unterwegs', 'e1a00000-0000-0000-0000-000000000002', 1000, null, null, '00000000-0000-0000-0000-00000000f001');
-- ====================================================== Disponent
set local role authenticated;
set local request.jwt.claims to '{"sub": "e1a00000-0000-0000-0000-000000000002", "role": "authenticated"}';
-- 1)
select lives_ok(
$$ insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by)
values ('e1d00000-0000-0000-0000-000000000001', 'front', 'e1d00000-0000-0000-0000-000000000001/front.jpg', 'e1a00000-0000-0000-0000-000000000002') $$,
'Disponent laedt Foto fuer eigenen zugewiesenen Auftrag hoch'
);
-- 3)
select throws_ok(
$$ insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by)
values ('e1d00000-0000-0000-0000-000000000003', 'front', 'e1d00000-0000-0000-0000-000000000003/front.jpg', 'e1a00000-0000-0000-0000-000000000002') $$,
'42501',
null,
'Disponent kann kein Foto fuer einen einem Fahrer zugewiesenen Auftrag hochladen'
);
-- 4)
select lives_ok(
$$ select public.fin_bei_fahrtbeginn('e1d00000-0000-0000-0000-000000000001', 'WDB9066331S123456') $$,
'Disponent fuehrt FIN-Abgleich fuer eigenen Auftrag durch'
);
-- 6)
select lives_ok(
$$ insert into public.arbeitszeiten (fahrer_id, kommt_um) values ('e1a00000-0000-0000-0000-000000000002', now()) $$,
'Disponent stempelt sich selbst ein'
);
-- 9)
select throws_ok(
$$ update public.auftraege set zugewiesen_an = 'e1a00000-0000-0000-0000-000000000005' where id = 'e1d00000-0000-0000-0000-000000000004' $$,
'42501',
'Zuweisung nur an Fahrer, Disponent oder Admin',
'Zuweisung an ReadOnly scheitert'
);
-- 10)
select throws_ok(
$$ update public.auftraege set zugewiesen_an = 'e1a00000-0000-0000-0000-000000000006' where id = 'e1d00000-0000-0000-0000-000000000004' $$,
'42501',
null,
'Zuweisung an Fahrer einer fremden Firma scheitert'
);
-- 11)
update public.auftraege set zugewiesen_an = 'e1a00000-0000-0000-0000-000000000004' where id = 'e1d00000-0000-0000-0000-000000000005';
select ok(
(select start_fin is null and start_fin_status is null and status = 'zugewiesen'
from public.auftraege where id = 'e1d00000-0000-0000-0000-000000000005'),
'Umzuweisung A -> B setzt bestaetigten FIN-Abgleich zurueck'
);
-- 12)
update public.auftraege set zugewiesen_an = 'e1a00000-0000-0000-0000-000000000004' where id = 'e1d00000-0000-0000-0000-000000000006';
select ok(
(select start_fin = 'WVWZZZ1KZAW999999' and start_fin_status = 'abweichend'
from public.auftraege where id = 'e1d00000-0000-0000-0000-000000000006'),
'Umzuweisung A -> B laesst abweichenden FIN-Vermerk stehen'
);
-- 15)
select is(
(select count(*) from public.profiles where role in ('disponent', 'admin')),
(select n from erwartet_dispo_admin),
'Disponent sieht die Dispo-/Admin-Profile der eigenen Firma'
);
-- 16)
select is(
(select count(*) from public.profiles where firma_id = '00000000-0000-0000-0000-00000000f002'),
0::bigint,
'Disponent sieht keine Profile der fremden Firma'
);
-- 17)
select lives_ok(
$$ insert into storage.objects (bucket_id, name, owner)
values ('auftrag-unterschriften', 'e1d00000-0000-0000-0000-000000000008/unterschrift.png', 'e1a00000-0000-0000-0000-000000000002') $$,
'Disponent laedt Unterschrift fuer eigenen Auftrag im Status unterwegs hoch'
);
-- 18)
select lives_ok(
$$ insert into storage.objects (bucket_id, name, owner)
values ('auftrag-fotos', 'e1d00000-0000-0000-0000-000000000001/front.jpg', 'e1a00000-0000-0000-0000-000000000002') $$,
'Disponent laedt Fahrtbeginn-Foto fuer eigenen zugewiesenen Auftrag hoch'
);
reset role;
-- ====================================================== Admin
set local role authenticated;
set local request.jwt.claims to '{"sub": "e1a00000-0000-0000-0000-000000000001", "role": "authenticated"}';
-- 2)
select lives_ok(
$$ insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by)
values ('e1d00000-0000-0000-0000-000000000002', 'front', 'e1d00000-0000-0000-0000-000000000002/front.jpg', 'e1a00000-0000-0000-0000-000000000001') $$,
'Admin laedt Foto fuer eigenen zugewiesenen Auftrag hoch'
);
reset role;
-- ====================================================== ReadOnly
set local role authenticated;
set local request.jwt.claims to '{"sub": "e1a00000-0000-0000-0000-000000000005", "role": "authenticated"}';
-- 5)
select throws_ok(
$$ select public.fin_bei_fahrtbeginn('e1d00000-0000-0000-0000-000000000001', 'WDB9066331S123456') $$,
'P0001',
'Nur Einsatzrollen duerfen den FIN-Abgleich durchfuehren',
'ReadOnly kann den FIN-Abgleich nicht aufrufen'
);
-- 7)
select throws_ok(
$$ insert into public.arbeitszeiten (fahrer_id, kommt_um) values ('e1a00000-0000-0000-0000-000000000005', now()) $$,
'42501',
null,
'ReadOnly kann sich nicht einstempeln'
);
reset role;
-- ====================================================== Fahrer A
set local role authenticated;
set local request.jwt.claims to '{"sub": "e1a00000-0000-0000-0000-000000000003", "role": "authenticated"}';
-- 8)
select lives_ok(
$$ insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by)
values ('e1d00000-0000-0000-0000-000000000003', 'front', 'e1d00000-0000-0000-0000-000000000003/front.jpg', 'e1a00000-0000-0000-0000-000000000003') $$,
'Fahrer laedt Foto fuer eigenen Auftrag hoch (unveraendert)'
);
-- 13)
update public.auftraege set status = 'unterwegs', start_km = 100 where id = 'e1d00000-0000-0000-0000-000000000007';
select isnt(
(select fahrt_beginn_at from public.auftraege where id = 'e1d00000-0000-0000-0000-000000000007'),
null,
'Statuswechsel zugewiesen -> unterwegs setzt fahrt_beginn_at'
);
-- 14)
select throws_ok(
$$ update public.auftraege set fahrt_beginn_at = now() - interval '1 day' where id = 'e1d00000-0000-0000-0000-000000000007' $$,
'P0001',
null,
'Fahrer kann fahrt_beginn_at nicht direkt aendern'
);
reset role;
select * from finish();
rollback;