Coverage for app/backend/src/tests/test_email_zz.py: 100%
72 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-10 09:48 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-10 09:48 +0000
1import re
3from sqlalchemy import select
5from couchers.context import make_logged_out_context
6from couchers.crypto import random_hex
7from couchers.db import session_scope
8from couchers.email.zz import (
9 OPEN_PIXEL_GIF,
10 add_open_pixel,
11 add_zz_to_html_links,
12 generate_zz,
13 open_pixel_url,
14)
15from couchers.i18n import LocalizationContext
16from couchers.models import Notification, NotificationTopicAction, SignupFlow
17from couchers.models.logging import EventLog
18from couchers.proto import api_pb2, bugs_pb2
19from couchers.tasks import send_signup_email
20from tests.fixtures.db import generate_user, get_friend_relationship
21from tests.fixtures.feature_flags import FeatureFlags
22from tests.fixtures.misc import EmailCollector, Moderator
23from tests.fixtures.sessions import api_session, real_bugs_session
26def test_add_zz_to_html_links():
27 html = (
28 '<a href="http://localhost:3000/quick-link?payload=x&sig=y">a</a>'
29 '<a href="http://localhost:3000/dashboard#top">b</a>'
30 '<a href="https://example.com/">c</a>'
31 '<a href="http://localhost:3000.evil.com/x">d</a>'
32 '<img src="http://localhost:3000/logo512.png">'
33 "<p>http://localhost:3000/user/bob</p>"
34 )
35 assert add_zz_to_html_links(html, "abc") == (
36 '<a href="http://localhost:3000/quick-link?payload=x&sig=y&zz=abc">a</a>'
37 '<a href="http://localhost:3000/dashboard?zz=abc#top">b</a>'
38 '<a href="https://example.com/">c</a>'
39 '<a href="http://localhost:3000.evil.com/x">d</a>'
40 '<img src="http://localhost:3000/logo512.png">'
41 "<p>http://localhost:3000/user/bob</p>"
42 )
45def test_add_open_pixel():
46 html = add_open_pixel("<html><body><p>hi</p></body></html>", "abc")
47 assert html.startswith("<html><body><p>hi</p><img ")
48 assert html.endswith("></body></html>")
49 assert f'src="{open_pixel_url("abc")}"' in html
52def test_notification_email_gets_zz(
53 db, email_collector: EmailCollector, moderator: Moderator, feature_flags: FeatureFlags
54):
55 feature_flags.set("email_zz_links_enabled", True)
56 to_user, _ = generate_user()
57 from_user, from_token = generate_user()
59 with api_session(from_token) as api:
60 api.SendFriendRequest(api_pb2.SendFriendRequestReq(user_id=to_user.id))
61 friend_relationship = get_friend_relationship(from_user, to_user)
62 assert friend_relationship
63 moderator.approve_friend_request(friend_relationship.id)
65 email = email_collector.pop_for_recipient(to_user.email, last=True)
67 with session_scope() as session:
68 notification_id = session.execute(
69 select(Notification.id)
70 .where(Notification.user_id == to_user.id)
71 .where(Notification.topic_action == NotificationTopicAction.friend_request__create)
72 ).scalar_one()
73 properties = session.execute(
74 select(EventLog.properties).where(EventLog.event_type == "email.sent").where(EventLog.user_id == to_user.id)
75 ).scalar_one()
77 zz = properties["zz"]
78 assert properties == {"zz": zz, "source_data": email.source_data, "notification_id": notification_id}
80 hrefs = re.findall(r'href="(http://localhost:3000[^"]*)"', email.html)
81 assert hrefs
82 assert all(f"zz={zz}" in href for href in hrefs)
83 assert "zz=" not in email.plain
84 assert "/email/open/" not in email.html
87def test_userless_email_gets_open_pixel_without_zz_links(
88 db, email_collector: EmailCollector, feature_flags: FeatureFlags
89):
90 feature_flags.set("email_open_pixel_enabled", True)
91 recipient = f"{random_hex(12)}@couchers.org.invalid"
93 with session_scope() as session:
94 send_signup_email(
95 make_logged_out_context(LocalizationContext.en_utc()),
96 session,
97 SignupFlow(name="Frodo", email=recipient, flow_token=""),
98 )
100 email = email_collector.pop_for_recipient(recipient, last=True)
102 with session_scope() as session:
103 event = session.execute(select(EventLog).where(EventLog.event_type == "email.sent")).scalar_one()
104 assert event.user_id is None
105 zz = event.properties["zz"]
107 assert "zz=" not in email.html
108 assert "zz=" not in email.plain
109 assert f'src="{open_pixel_url(zz)}"' in email.html
112def test_record_email_open(db):
113 zz = generate_zz()
115 with real_bugs_session() as (bugs, metadata_interceptor):
116 res = bugs.RecordEmailOpen(bugs_pb2.RecordEmailOpenReq(zz=zz))
118 assert res.content_type == "image/gif"
119 assert res.data == OPEN_PIXEL_GIF
120 assert metadata_interceptor.latest_headers["cache-control"] == "no-store"
122 with session_scope() as session:
123 properties = session.execute(
124 select(EventLog.properties).where(EventLog.event_type == "email.opened")
125 ).scalar_one()
126 assert properties == {"zz": zz}
129def test_record_email_open_ignores_malformed_zz(db):
130 with real_bugs_session() as (bugs, _):
131 res = bugs.RecordEmailOpen(bugs_pb2.RecordEmailOpenReq(zz="not an zz"))
133 assert res.data == OPEN_PIXEL_GIF
135 with session_scope() as session:
136 assert not session.execute(select(EventLog).where(EventLog.event_type == "email.opened")).scalars().all()