@davoodmehraban89 / VELYQOR / commits / efe9ab0
Fork 0

Intelligence, Advanced Charting & Trading Platform

test(db): revoke queued recipient before claim, check suppression and active opt-in path

davoodmehraban89 committed Oct 22, 2026 at 23:48 UTC efe9ab02af9fc2623a511fd892206d62ebe9e898
1 file changed +37 -4
db/tests/alert_runtime_invariants.sql
+37 -4
@@ -114,10 +114,42 @@ BEGIN
114 SELECT count(*) INTO n FROM public.notification_outbox WHERE event_key=event_id;
115 IF n <> 1 THEN RAISE EXCEPTION 'duplicate outbox message created'; END IF;
116
117 - -- A revoked recipient never qualifies, even when the rule is valid.
117 + -- Consent may be revoked AFTER an informational alert was queued.
118 + -- The future sender must never receive the previously queued message.
119 + UPDATE public.alert_notification_consents
120 + SET revoked_at = t + interval '2 minutes'
121 + WHERE user_id=v_user AND recipient_ref=v_recipient AND channel='BALE';
122 +
123 + SELECT count(*) INTO n
124 + FROM public.alert_notification_claim_consented(t+interval '3 minutes',30);
125 + IF n <> 0 THEN RAISE EXCEPTION 'revoked recipient was returned to sender'; END IF;
126 + SELECT status INTO current_status FROM public.notification_outbox
127 + WHERE user_id=v_user AND event_key=event_id;
128 + IF current_status <> 'UNKNOWN' THEN
129 + RAISE EXCEPTION 'revoked queued message not terminally suppressed';
130 + END IF;
131 + SELECT count(*) INTO n
132 + FROM public.alert_notification_claim_consented(t+interval '4 minutes',30);
133 + IF n <> 0 THEN RAISE EXCEPTION 'revoked notification was re-claimed'; END IF;
134 +
135 + -- Active opt-in remains retrievable only through the consent-aware claim.
136 INSERT INTO public.alert_notification_consents
119 - (user_id,recipient_ref,channel,consented_at,revoked_at)
120 - VALUES(v_other,v_recipient,'BALE',t-interval '10 minutes',t-interval '1 minute');
137 + (user_id,recipient_ref,channel,consented_at)
138 + VALUES(v_other,v_recipient,'BALE',t-interval '10 minutes');
139 + PERFORM public.notification_outbox_enqueue(v_other,'consented-alert',
140 + 'BALE',v_recipient,3);
141 + SELECT * INTO result
142 + FROM public.alert_notification_claim_consented(t+interval '5 minutes',30);
143 + IF result.event_key <> 'consented-alert' OR result.claim_token IS NULL THEN
144 + RAISE EXCEPTION 'consented claimant did not receive authorized pending event';
145 + END IF;
146 + PERFORM public.notification_outbox_finish(result.outbox_id,result.claim_token,
147 + 'UNKNOWN',NULL,t+interval '5 minutes 5 seconds');
148 +
149 + -- A revoked recipient never qualifies, even when the rule is valid.
150 + UPDATE public.alert_notification_consents
151 + SET revoked_at = t-interval '1 minute'
152 + WHERE user_id=v_other AND recipient_ref=v_recipient AND channel='BALE';
153
154 BEGIN
155 PERFORM * FROM public.alert_rule_commit(v_other,'threshold1',0,
@@ -159,7 +191,8 @@ BEGIN
191 CROSS JOIN LATERAL pg_catalog.aclexplode(
192 COALESCE(p.proacl,pg_catalog.acldefault('f',p.proowner))
193 ) AS acl
162 - WHERE ns.nspname='public' AND p.proname='alert_rule_commit'
194 + WHERE ns.nspname='public' AND p.proname IN
195 + ('alert_rule_commit','alert_notification_claim_consented')
196 AND acl.grantee=0 AND acl.privilege_type='EXECUTE'
197 ) THEN
198 RAISE EXCEPTION 'unsafe PUBLIC execute grant';