forked from fahrPool/fahrPool
130 lines
4.6 KiB
PL/PgSQL
130 lines
4.6 KiB
PL/PgSQL
begin;
|
|
select plan(15);
|
|
|
|
insert into auth.users (id, email) values
|
|
('11111111-1111-1111-1111-111111111111', 'admin@test.local'),
|
|
('22222222-2222-2222-2222-222222222222', 'disponent@test.local'),
|
|
('33333333-3333-3333-3333-333333333333', 'fahrer-a@test.local'),
|
|
('44444444-4444-4444-4444-444444444444', 'fahrer-b@test.local');
|
|
|
|
update public.profiles set firma_id = '00000000-0000-0000-0000-00000000f001' where firma_id is null;
|
|
update public.profiles set role = 'admin' where id = '11111111-1111-1111-1111-111111111111';
|
|
update public.profiles set role = 'disponent' where id = '22222222-2222-2222-2222-222222222222';
|
|
update public.profiles set role = 'fahrer' where id = '33333333-3333-3333-3333-333333333333';
|
|
update public.profiles set role = 'fahrer' where id = '44444444-4444-4444-4444-444444444444';
|
|
|
|
-- Fahrer B hat bereits Daten (als Superuser angelegt)
|
|
insert into public.nutzer_details (user_id, telefon) values
|
|
('44444444-4444-4444-4444-444444444444', '+491701234567');
|
|
|
|
-- ===== Fahrer A =====
|
|
set local role authenticated;
|
|
set local request.jwt.claims to '{"sub": "33333333-3333-3333-3333-333333333333", "role": "authenticated"}';
|
|
|
|
select lives_ok(
|
|
$$ insert into public.nutzer_details (user_id, telefon) values ('33333333-3333-3333-3333-333333333333', '+491751112233') $$,
|
|
'Fahrer kann seine eigene Zeile anlegen'
|
|
);
|
|
|
|
select results_eq(
|
|
$$ select count(*)::int from public.nutzer_details $$,
|
|
$$ values (1) $$,
|
|
'Fahrer sieht nur seine eigene Zeile'
|
|
);
|
|
|
|
select is_empty(
|
|
$$ select user_id from public.nutzer_details where user_id = '44444444-4444-4444-4444-444444444444' $$,
|
|
'Fahrer sieht die Zeile eines anderen Fahrers nicht'
|
|
);
|
|
|
|
select is_empty(
|
|
$$ update public.nutzer_details set telefon = '+491700000000' where user_id = '44444444-4444-4444-4444-444444444444' returning user_id $$,
|
|
'Fahrer kann die Zeile eines anderen Fahrers nicht ändern (0 Zeilen betroffen)'
|
|
);
|
|
|
|
select results_eq(
|
|
$$ update public.nutzer_details set telefon = '+491709998877' where user_id = '33333333-3333-3333-3333-333333333333' returning user_id $$,
|
|
$$ values ('33333333-3333-3333-3333-333333333333'::uuid) $$,
|
|
'Fahrer kann seine eigene Zeile ändern'
|
|
);
|
|
|
|
select throws_ok(
|
|
$$ insert into public.nutzer_details (user_id) values ('11111111-1111-1111-1111-111111111111') $$,
|
|
'42501',
|
|
null,
|
|
'Fahrer kann keine Zeile für einen anderen Nutzer anlegen (RLS)'
|
|
);
|
|
|
|
select throws_ok(
|
|
$$ update public.nutzer_details set fuehrerscheinklassen = array['XYZ'] where user_id = '33333333-3333-3333-3333-333333333333' $$,
|
|
'23514',
|
|
null,
|
|
'Ungültige Führerscheinklasse wird vom Check-Constraint abgelehnt'
|
|
);
|
|
|
|
select throws_ok(
|
|
$$ update public.nutzer_details set fahrerkarte = false, fahrerkarte_ablauf = '2027-03-01' where user_id = '33333333-3333-3333-3333-333333333333' $$,
|
|
'23514',
|
|
null,
|
|
'Ablaufdatum ohne Fahrerkarte wird vom Check-Constraint abgelehnt'
|
|
);
|
|
|
|
select throws_ok(
|
|
$$ update public.nutzer_details set telefon = '0170123' where user_id = '33333333-3333-3333-3333-333333333333' $$,
|
|
'23514',
|
|
null,
|
|
'Telefonnummer, die nicht im E.164-Format ist, wird vom Check-Constraint abgelehnt'
|
|
);
|
|
|
|
reset role;
|
|
|
|
-- ===== Disponent =====
|
|
set local role authenticated;
|
|
set local request.jwt.claims to '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';
|
|
|
|
select results_eq(
|
|
$$ select count(*)::int from public.nutzer_details $$,
|
|
$$ values (2) $$,
|
|
'Disponent sieht die Details aller Fahrer'
|
|
);
|
|
|
|
select is_empty(
|
|
$$ update public.nutzer_details set telefon = '+491700000000' where user_id = '44444444-4444-4444-4444-444444444444' returning user_id $$,
|
|
'Disponent kann Details nicht ändern (0 Zeilen betroffen)'
|
|
);
|
|
|
|
select throws_ok(
|
|
$$ insert into public.nutzer_details (user_id) values ('11111111-1111-1111-1111-111111111111') $$,
|
|
'42501',
|
|
null,
|
|
'Disponent kann keine Zeile anlegen (RLS)'
|
|
);
|
|
|
|
reset role;
|
|
|
|
-- ===== Admin =====
|
|
set local role authenticated;
|
|
set local request.jwt.claims to '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
|
|
|
|
select results_eq(
|
|
$$ update public.nutzer_details set telefon = '+491700000000' where user_id = '44444444-4444-4444-4444-444444444444' returning user_id $$,
|
|
$$ values ('44444444-4444-4444-4444-444444444444'::uuid) $$,
|
|
'Admin kann die Details eines Fahrers ändern'
|
|
);
|
|
|
|
select lives_ok(
|
|
$$ insert into public.nutzer_details (user_id) values ('22222222-2222-2222-2222-222222222222') $$,
|
|
'Admin kann eine Zeile für einen anderen Nutzer anlegen'
|
|
);
|
|
|
|
select results_eq(
|
|
$$ select count(*)::int from public.nutzer_details $$,
|
|
$$ values (3) $$,
|
|
'Admin sieht alle Zeilen'
|
|
);
|
|
|
|
reset role;
|
|
|
|
select * from finish();
|
|
rollback;
|