begin; do $test$ declare fixture_email text := 'session-test-' || gen_random_uuid()::text || '@example.invalid'; fixture_uid uuid; fixture_session uuid := gen_random_uuid(); previous_hash text; previous_updated timestamptz; begin perform public._ensure_auth_user(fixture_email, 'Session Test', 'session-test-ticket'); select id, encrypted_password, updated_at into fixture_uid, previous_hash, previous_updated from auth.users where email = fixture_email; if fixture_uid is null then raise exception 'Fixture user was not created'; end if; insert into auth.sessions (id, user_id, created_at, updated_at) values (fixture_session, fixture_uid, now() - interval '6 days', now()); for iteration in 1..3 loop perform public._ensure_auth_user(fixture_email, 'Session Test', 'session-test-ticket'); end loop; if not exists (select 1 from auth.sessions where id = fixture_session) then raise exception 'Unchanged imports revoked an existing session'; end if; if not exists (select 1 from auth.users where id = fixture_uid and encrypted_password = previous_hash and updated_at = previous_updated) then raise exception 'Unchanged imports rewrote the password'; end if; raise notice 'PASS: repeated unchanged ticket imports preserve session and password'; perform public._ensure_auth_user(fixture_email, 'Session Test', 'session-test-new-ticket'); if exists (select 1 from auth.sessions where id = fixture_session) then raise exception 'Changed password did not revoke session'; end if; if not exists (select 1 from auth.users where id = fixture_uid and encrypted_password = extensions.crypt('session-test-new-ticket', encrypted_password)) then raise exception 'Changed password was not synchronized'; end if; raise notice 'PASS: changed ticket password updates hash and revokes session'; fixture_email := 'session-test-' || gen_random_uuid()::text || '@appe.at'; perform public._ensure_auth_user(fixture_email, 'Session Admin Test', 'first-ticket'); select id, encrypted_password into fixture_uid, previous_hash from auth.users where email = fixture_email; fixture_session := gen_random_uuid(); insert into auth.sessions (id, user_id, created_at, updated_at) values (fixture_session, fixture_uid, now(), now()); perform public._ensure_auth_user(fixture_email, 'Session Admin Test', 'different-ticket'); if not exists (select 1 from auth.sessions where id = fixture_session) or not exists (select 1 from auth.users where id = fixture_uid and encrypted_password = previous_hash) then raise exception 'Admin password session changed during ticket sync'; end if; raise notice 'PASS: existing admin password and session survive ticket changes'; end; $test$; rollback;