feat: add manual feedback AI summaries and secure leaderboard scoring
This commit is contained in:
1 parent
34a3a88376
commit
6bad96ca03
37 files changed
+1640
-77
No files matched your search
@@ -0,0 +1,61 @@
|
||||
\set ON_ERROR_STOP on
|
||||
begin;
|
||||
create function pg_temp.assert_summary(condition boolean, label text) returns void
|
||||
language plpgsql as $$ begin
|
||||
if condition is distinct from true then raise exception 'FAIL: %', label; end if;
|
||||
raise notice 'PASS: %', label;
|
||||
end; $$;
|
||||
|
||||
create temp table ai_fixture as select gen_random_uuid() event_id,
|
||||
'ai-admin-' || gen_random_uuid() || '@example.invalid' admin_email,
|
||||
'ai-attendee-' || gen_random_uuid() || '@example.invalid' attendee_email;
|
||||
insert into public.events(id, name, start_date, end_date, is_active)
|
||||
select event_id, 'AI test event', '2026-10-23', '2026-10-23', false from ai_fixture;
|
||||
insert into public.contacts(email, full_name, is_admin)
|
||||
select admin_email, 'AI test admin', true from ai_fixture
|
||||
union all select attendee_email, 'AI test attendee', false from ai_fixture;
|
||||
|
||||
select pg_temp.assert_summary(
|
||||
(select bool_and(relrowsecurity) from pg_class where oid in ('public.llm_settings'::regclass, 'public.feedback_summaries'::regclass)),
|
||||
'RLS enabled on both AI tables');
|
||||
select pg_temp.assert_summary(
|
||||
not has_table_privilege('anon', 'public.llm_settings', 'select')
|
||||
and not has_table_privilege('authenticated', 'public.llm_settings', 'select')
|
||||
and not has_table_privilege('authenticated', 'public.llm_settings', 'insert,update,delete'),
|
||||
'client roles cannot access LLM settings or credentials');
|
||||
select pg_temp.assert_summary(
|
||||
not has_table_privilege('anon', 'public.feedback_summaries', 'select')
|
||||
and not has_table_privilege('authenticated', 'public.feedback_summaries', 'insert,update,delete'),
|
||||
'anonymous reads and all client summary writes are denied');
|
||||
select pg_temp.assert_summary(
|
||||
not has_function_privilege('authenticated', 'public.evaluate_session_feedback(uuid)', 'execute')
|
||||
and not has_function_privilege('anon', 'public.evaluate_session_feedback(uuid)', 'execute'),
|
||||
'obsolete AI stub cannot be called by clients');
|
||||
|
||||
select event_id::text as ai_event_id, admin_email as ai_admin_email, attendee_email as ai_attendee_email from ai_fixture \gset
|
||||
set local role service_role;
|
||||
insert into public.llm_settings(id, endpoint, model, api_key)
|
||||
values (true, 'https://llm.example/v1/chat/completions', 'test', 'fixture-only-key')
|
||||
on conflict (id) do update set model = excluded.model;
|
||||
insert into public.feedback_summaries(scope, target_id, event_id, summary, response_count, source_fingerprint, model)
|
||||
values ('event', :'ai_event_id', :'ai_event_id', 'Fixture summary', 1, 'fixture-hash', 'test');
|
||||
reset role;
|
||||
select pg_temp.assert_summary(
|
||||
exists(select 1 from public.feedback_summaries where event_id = :'ai_event_id'),
|
||||
'service role can persist summaries');
|
||||
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', json_build_object('email', :'ai_attendee_email')::text, true);
|
||||
select pg_temp.assert_summary(
|
||||
not exists(select 1 from public.feedback_summaries where event_id = :'ai_event_id'),
|
||||
'non-admin cannot read a summary');
|
||||
select set_config('request.jwt.claims', json_build_object('email', :'ai_admin_email')::text, true);
|
||||
select pg_temp.assert_summary(
|
||||
(select summary = 'Fixture summary' from public.feedback_summaries where event_id = :'ai_event_id'),
|
||||
'admin can read the saved summary');
|
||||
reset role;
|
||||
delete from public.events where id = :'ai_event_id';
|
||||
select pg_temp.assert_summary(
|
||||
not exists(select 1 from public.feedback_summaries where event_id = :'ai_event_id'),
|
||||
'deleting the event cascades its summaries');
|
||||
rollback;
|
||||
@@ -0,0 +1,182 @@
|
||||
\set ON_ERROR_STOP on
|
||||
begin;
|
||||
|
||||
create temporary table leaderboard_results (name text);
|
||||
create function pg_temp.assert_board(p_condition boolean, p_name text) returns void
|
||||
language plpgsql as $$
|
||||
begin
|
||||
if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if;
|
||||
insert into leaderboard_results values (p_name);
|
||||
end;
|
||||
$$;
|
||||
|
||||
update public.events set is_active = false where is_active;
|
||||
insert into public.events (id, name, start_date, end_date, is_active)
|
||||
values ('a0150000-0000-0000-0000-000000000001', 'Leaderboard regression', current_date, current_date, true),
|
||||
('a0150000-0000-0000-0000-000000000002', 'Other event', current_date, current_date, false);
|
||||
insert into public.contacts (email, full_name, is_admin)
|
||||
values ('[email protected]', 'Player', false), ('[email protected]', 'Peer', false),
|
||||
('[email protected]', 'Other', false), ('[email protected]', 'Speaker', false),
|
||||
('[email protected]', 'Staff', false), ('[email protected]', 'Admin', true),
|
||||
('[email protected]', 'Domain Admin', false), ('[email protected]', 'Orphan', false);
|
||||
insert into public.registrations (event_id, contact_email, type, qr_token, points)
|
||||
select 'a0150000-0000-0000-0000-000000000001'::uuid, email,
|
||||
case when email = '[email protected]' then 'staff'
|
||||
when email = '[email protected]' then 'speaker' else 'attendee' end::public.registration_type,
|
||||
'LB-' || email, case when is_admin or email = '[email protected]' then 99 else 0 end
|
||||
from public.contacts where email in ('[email protected]', '[email protected]', '[email protected]',
|
||||
'[email protected]', '[email protected]', '[email protected]', '[email protected]');
|
||||
insert into public.registrations (event_id, contact_email, type, qr_token, points)
|
||||
values ('a0150000-0000-0000-0000-000000000002', '[email protected]', 'attendee', 'LB-OTHER-EVENT', 50);
|
||||
insert into public.slots (id, event_id, name, slot_date, start_time, end_time, type)
|
||||
values (-151001, 'a0150000-0000-0000-0000-000000000001', 'Talk', current_date, '09:00', '10:00', 'session'),
|
||||
(-151002, 'a0150000-0000-0000-0000-000000000001', 'Service', current_date, '10:00', '11:00', 'service_session');
|
||||
insert into public.sessions (id, event_id, slot_id, title)
|
||||
values ('a0150000-0000-0000-0000-000000000011', 'a0150000-0000-0000-0000-000000000001', -151001, 'Talk one'),
|
||||
('a0150000-0000-0000-0000-000000000012', 'a0150000-0000-0000-0000-000000000001', -151001, 'Talk two'),
|
||||
('a0150000-0000-0000-0000-000000000013', 'a0150000-0000-0000-0000-000000000001', -151002, 'Service');
|
||||
|
||||
do $$
|
||||
declare
|
||||
event_one uuid := 'a0150000-0000-0000-0000-000000000001';
|
||||
talk_one uuid := 'a0150000-0000-0000-0000-000000000011';
|
||||
talk_two uuid := 'a0150000-0000-0000-0000-000000000012';
|
||||
excluded_email text;
|
||||
feedback_result record;
|
||||
begin
|
||||
perform pg_temp.assert_board((select bool_and(points = 0) from public.registrations
|
||||
where contact_email in ('[email protected]','[email protected]','[email protected]')), 'excluded inserts force zero');
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform public.checkin_attendee(talk_one, '[email protected]');
|
||||
perform pg_temp.assert_board((select points = 10 and checked_in from public.registrations where contact_email = '[email protected]'), 'check-in awards ten');
|
||||
perform pg_temp.assert_board((select already and points = 10 from public.checkin_attendee(talk_one, '[email protected]')), 'check-in idempotent');
|
||||
perform public.checkin_attendee(talk_one, '[email protected]');
|
||||
perform public.checkin_attendee(talk_one, '[email protected]');
|
||||
perform pg_temp.assert_board((select points = 10 from public.registrations where contact_email = '[email protected]'), 'nonadmin speaker earns points');
|
||||
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
|
||||
perform public.checkin_attendee(talk_one, 'LB-' || excluded_email);
|
||||
end loop;
|
||||
begin
|
||||
perform public.checkin_attendee('a0150000-0000-0000-0000-000000000013', '[email protected]');
|
||||
raise exception 'service check-in accepted';
|
||||
exception when raise_exception then
|
||||
if sqlerrm <> 'service sessions cannot be checked in' then raise; end if;
|
||||
end;
|
||||
perform pg_temp.assert_board(true, 'service check-in rejected');
|
||||
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
|
||||
perform set_config('request.jwt.claims', json_build_object('email', excluded_email)::text, true);
|
||||
select * into feedback_result from public.submit_feedback(talk_one, 5, 'Review');
|
||||
perform pg_temp.assert_board(feedback_result.earned = 0 and feedback_result.points = 0, excluded_email || ' feedback earns zero');
|
||||
perform public._award_points(event_one, excluded_email, 7, 'connection');
|
||||
perform public._award_points(event_one, excluded_email, 2, 'talk_networking');
|
||||
perform public._award_points(event_one, excluded_email, 100, 'admin');
|
||||
end loop;
|
||||
perform pg_temp.assert_board(not exists (select 1 from public.points_log where contact_email in
|
||||
('[email protected]','[email protected]','[email protected]')), 'no excluded award logs');
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform pg_temp.assert_board((select earned = 5 and points = 15 from public.submit_feedback(talk_one, 4, null)), 'rating awards five');
|
||||
perform pg_temp.assert_board((select earned = 0 and points = 15 from public.submit_feedback(talk_one, 5, 'Updated')), 'feedback idempotent');
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform public.checkin_attendee(talk_two, '[email protected]');
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform pg_temp.assert_board((select earned = 10 and points = 35 from public.submit_feedback(talk_two, 5, 'Useful')), 'comment awards ten');
|
||||
perform pg_temp.assert_board((select points = 35 from public.submit_event_feedback(5, 'Good event')), 'event feedback does not award points');
|
||||
end;
|
||||
$$;
|
||||
|
||||
insert into public.connections (id, event_id, from_email, to_email)
|
||||
values ('a0150000-0000-0000-0000-000000000021', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]'),
|
||||
('a0150000-0000-0000-0000-000000000022', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]'),
|
||||
('a0150000-0000-0000-0000-000000000023', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]');
|
||||
do $$
|
||||
begin
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform pg_temp.assert_board((select shared_talk from public.accept_connection('a0150000-0000-0000-0000-000000000021')), 'shared talk detected');
|
||||
perform pg_temp.assert_board((select points = 42 from public.registrations where contact_email = '[email protected]'), 'eligible peer earns seven with staff connection');
|
||||
perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'staff connection earns zero');
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform public.accept_connection('a0150000-0000-0000-0000-000000000022');
|
||||
perform pg_temp.assert_board((select points = 49 from public.registrations where contact_email = '[email protected]'), 'sender earns connection plus bonus');
|
||||
perform pg_temp.assert_board((select points = 17 from public.registrations where contact_email = '[email protected]' and event_id = 'a0150000-0000-0000-0000-000000000001'), 'recipient earns connection plus bonus');
|
||||
begin
|
||||
perform public.accept_connection('a0150000-0000-0000-0000-000000000022');
|
||||
raise exception 'repeated acceptance allowed';
|
||||
exception when raise_exception then
|
||||
if sqlerrm <> 'request already resolved' then raise; end if;
|
||||
end;
|
||||
perform pg_temp.assert_board(true, 'connection one-shot');
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform pg_temp.assert_board((select not shared_talk from public.accept_connection('a0150000-0000-0000-0000-000000000023')), 'no shared-talk bonus without shared check-in');
|
||||
perform pg_temp.assert_board((select points = 5 from public.registrations where contact_email = '[email protected]'), 'ordinary connection earns five');
|
||||
perform pg_temp.assert_board((select points = 50 from public.registrations where event_id = 'a0150000-0000-0000-0000-000000000002'), 'other event untouched');
|
||||
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
||||
perform public.adjust_points('[email protected]', -999, 'Floor test');
|
||||
perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'points floored at zero');
|
||||
perform public.adjust_points('[email protected]', 3, 'Correction');
|
||||
perform pg_temp.assert_board((select points = 57 from public.registrations where contact_email = '[email protected]'), 'admin adjustment updates player');
|
||||
perform public.adjust_points('[email protected]', 100, 'Must not award');
|
||||
perform public.adjust_points('[email protected]', 100, 'Must not award');
|
||||
perform public._award_points('a0150000-0000-0000-0000-000000000001', '[email protected]', 100, 'admin');
|
||||
perform pg_temp.assert_board(not exists (select 1 from public.points_log where contact_email in
|
||||
('[email protected]','[email protected]','[email protected]','[email protected]')), 'adjustments cannot award excluded or missing registrations');
|
||||
perform pg_temp.assert_board(not exists (select 1 from public.leaderboard l join public.registrations r using (event_id, contact_email) where l.points <> r.points), 'leaderboard matches registration totals');
|
||||
perform pg_temp.assert_board((select contact_email = '[email protected]' from public.get_leaderboard() limit 1), 'highest points rank first');
|
||||
perform pg_temp.assert_board(not exists (select 1 from public.get_leaderboard() where event_id <> 'a0150000-0000-0000-0000-000000000001'), 'active event only');
|
||||
end;
|
||||
$$;
|
||||
|
||||
update public.registrations set points = 99 where contact_email = '[email protected]';
|
||||
select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'direct staff edits forced to zero');
|
||||
update public.registrations set type = 'staff' where contact_email = '[email protected]';
|
||||
select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'staff promotion clears points');
|
||||
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'staff promotion removes ranking');
|
||||
update public.contacts set is_admin = true where email = '[email protected]';
|
||||
select pg_temp.assert_board((select bool_and(points = 0) from public.registrations where contact_email = '[email protected]'), 'admin promotion clears all event balances');
|
||||
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'admin promotion removes all rankings');
|
||||
select pg_temp.assert_board(exists (select 1 from public.points_log where contact_email = '[email protected]'), 'historical audit retained');
|
||||
update public.registrations set type = 'attendee', points = 7 where contact_email = '[email protected]';
|
||||
update public.registrations set points = 7 where contact_email = '[email protected]';
|
||||
update public.contacts set full_name = 'Tied' where email in ('[email protected]', '[email protected]');
|
||||
select pg_temp.assert_board((select bool_and(full_name = 'Tied') from public.leaderboard where contact_email in ('[email protected]','[email protected]')), 'name changes refresh mirror');
|
||||
select pg_temp.assert_board((select array_agg(contact_email) = array['[email protected]','[email protected]'] from public.get_leaderboard() where points = 7), 'deterministic tie order');
|
||||
insert into public.leaderboard (event_id, contact_email, full_name, points)
|
||||
values ('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray staff', 999),
|
||||
('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray admin', 999),
|
||||
('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray orphan', 999);
|
||||
select pg_temp.assert_board(not exists (select 1 from public.get_leaderboard() where contact_email in ('[email protected]','[email protected]','[email protected]')), 'read defense excludes stale staff admin and orphan rows');
|
||||
select public._sync_leaderboard('a0150000-0000-0000-0000-000000000001', '[email protected]');
|
||||
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'sync removes stale ranking');
|
||||
delete from public.registrations where contact_email = '[email protected]';
|
||||
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'registration deletion removes ranking');
|
||||
select pg_temp.assert_board(not has_function_privilege('authenticated','public._award_points(uuid,text,integer,text)','execute')
|
||||
and not has_function_privilege('anon','public._award_points(uuid,text,integer,text)','execute'), 'API roles cannot forge awards');
|
||||
select pg_temp.assert_board(not has_function_privilege('authenticated','public._sync_leaderboard(uuid,text)','execute'), 'API roles cannot call sync helper');
|
||||
select pg_temp.assert_board(has_function_privilege('authenticated','public.get_leaderboard()','execute')
|
||||
and not has_function_privilege('anon','public.get_leaderboard()','execute'), 'rankings require authentication');
|
||||
select pg_temp.assert_board(exists (select 1 from pg_publication_tables where pubname = 'supabase_realtime' and tablename = 'leaderboard' and schemaname = 'public'), 'Realtime publication enabled');
|
||||
|
||||
grant insert on leaderboard_results to authenticated;
|
||||
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
|
||||
set local role authenticated;
|
||||
select pg_temp.assert_board(exists (select 1 from public.get_leaderboard()), 'authenticated API role can read rankings');
|
||||
do $$
|
||||
begin
|
||||
begin
|
||||
perform public._award_points('a0150000-0000-0000-0000-000000000001', '[email protected]', 9999, 'admin');
|
||||
raise exception 'forged award accepted';
|
||||
exception when insufficient_privilege then null;
|
||||
end;
|
||||
perform pg_temp.assert_board(true, 'direct helper invocation denied');
|
||||
begin
|
||||
perform public.adjust_points('[email protected]', 9999, 'Not admin');
|
||||
raise exception 'unauthorized adjustment accepted';
|
||||
exception when raise_exception then
|
||||
if sqlerrm <> 'admin only' then raise; end if;
|
||||
end;
|
||||
perform pg_temp.assert_board(true, 'nonadmin adjustment rejected');
|
||||
end;
|
||||
$$;
|
||||
reset role;
|
||||
|
||||
select count(*) as passed_assertions from leaderboard_results;
|
||||
rollback;
|
||||
@@ -86,7 +86,7 @@ create temporary table merge_result as select public.merge_tickets(source_id,tar
|
||||
reset role;
|
||||
select pg_temp.assert_merge((select (result->>'sessions_transferred')::integer=2 from merge_result), 'two source assignments transferred');
|
||||
select pg_temp.assert_merge(not exists(select 1 from public.registrations where id=(select source_id from merge_fixture)), 'source registration deleted');
|
||||
select pg_temp.assert_merge((select type='staff' and checked_in and points=17 and qr_token=id::text and external_id='pretix-target'
|
||||
select pg_temp.assert_merge((select type='staff' and checked_in and points=0 and qr_token=id::text and external_id='pretix-target'
|
||||
from public.registrations where id=(select target_id from merge_fixture)), 'target staff status QR and attendance preserved');
|
||||
select pg_temp.assert_merge((select count(*)=2 from public.session_speakers where contact_email=(select target_email from merge_fixture)
|
||||
and session_id in(select session_a from merge_fixture union all select session_shared from merge_fixture)), 'replacement links inserted without duplicates');
|
||||
|
||||
@@ -356,7 +356,7 @@ select pg_temp.assert_sessionize_guard(
|
||||
'stale slot/session outside range are retained after successful imports');
|
||||
select pg_temp.assert_sessionize_guard(
|
||||
exists (select 1 from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id
|
||||
where r.contact_email = staff_email and r.type = 'staff' and r.points = 22 and r.checked_in)
|
||||
where r.contact_email = staff_email and r.type = 'staff' and r.points = 0 and r.checked_in)
|
||||
and exists (select 1 from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id
|
||||
where r.contact_email = sponsor_email and r.type = 'sponsor' and r.points = 33 and r.checked_in)
|
||||
and exists (select 1 from public.registrations r join sessionize_guard_fixture f on f.event_id = r.event_id
|
||||
|
||||
Reference in new issue
Block a user