Skip to content

Commit f3b81f3

Browse files
Merge pull request #482 from tutorcruncher/extra-details-spam-email-sentry-warnings
2 parents 018b713 + 8d67201 commit f3b81f3

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/spam/email_checker.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import logging
2-
from html import escape
2+
import re
3+
from html import unescape
4+
from typing import Optional, Union
35

46
from src.render.main import MessageDef, render_email
57
from src.schemas.messages import EmailSendModel
68
from src.spam.services import OpenAISpamEmailService, SpamCacheService
79

810
logger = logging.getLogger('spam.email_checker')
911

12+
_html_tag_re = re.compile(r'<[^>]+>')
13+
_white_space_re = re.compile(r'\s+')
14+
15+
16+
def _clean_html_body(
17+
html_body: Optional[str] = None,
18+
) -> Union[str, None]:
19+
'''
20+
Cleans the html body - removes HTML tags and whitespaces
21+
'''
22+
if isinstance(html_body, str) and html_body.strip():
23+
return _white_space_re.sub(' ', unescape(_html_tag_re.sub('', html_body)))
24+
1025

1126
class EmailSpamChecker:
1227
def __init__(self, spam_service: OpenAISpamEmailService, cache_service: SpamCacheService):
@@ -40,9 +55,7 @@ async def check_spam(self, m: EmailSendModel):
4055
)
4156
email_info = render_email(message_def)
4257
company_name = m.context.get("company_name", "no_company")
43-
escaped_html = escape(email_info.html_body)
4458
subject = email_info.subject
45-
recipients = [recipient.address for recipient in m.recipients]
4659

4760
spam_result = await self.spam_service.is_spam_email(email_info, company_name)
4861

@@ -55,10 +68,10 @@ async def check_spam(self, m: EmailSendModel):
5568
extra={
5669
"reason": spam_result.reason,
5770
"number of recipients": len(m.recipients),
58-
"to": recipients,
5971
"subject": subject,
6072
"company": company_name,
61-
"html_escaped": escaped_html,
73+
"company_code": m.company_code,
74+
"email_main_body": _clean_html_body(context.get('main_message__render')) or 'no main body',
6275
},
6376
)
6477
return spam_result

tests/test_email.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hashlib
33
import hmac
44
import json
5+
import logging
56
import pytest
67
import re
78
from arq import Retry
@@ -1168,3 +1169,47 @@ def test_get_cache_key_with_emojis_and_special_chars():
11681169
assert (
11691170
cache_key != cache_key3
11701171
), f"Different company codes should produce different cache keys for {test_case['name']}"
1172+
1173+
1174+
@pytest.mark.spam
1175+
def test_spam_logging_includes_body(cli: TestClient, sync_db: SyncDb, worker, caplog):
1176+
caplog.set_level(logging.ERROR, logger='spam.email_checker')
1177+
1178+
recipients = []
1179+
for i in range(21):
1180+
recipients.append(
1181+
{
1182+
'first_name': f'User{i}',
1183+
'last_name': f'Last{i}',
1184+
'address': f'user{i}@example.org',
1185+
'tags': ['test'],
1186+
}
1187+
)
1188+
1189+
data = {
1190+
'uid': str(uuid4()),
1191+
'company_code': 'foobar',
1192+
'from_address': 'Spammer <[email protected]>',
1193+
'method': 'email-test',
1194+
'subject_template': 'Urgent: {{ company_name }} Alert!',
1195+
'main_template': '{{{ main_message }}}',
1196+
'context': {
1197+
'main_message__render': 'Hi {{ recipient_first_name }},\n\nDont miss out on <b>FREE MONEY</b>! '
1198+
'Click [here]({{ login_link }}) now!\n\nRegards,\n{{ company_name }}',
1199+
'company_name': 'TestCorp',
1200+
'login_link': 'https://spam.example.com/click',
1201+
},
1202+
'recipients': recipients,
1203+
}
1204+
1205+
r = cli.post('/send/email/', json=data, headers={'Authorization': 'testing-key'})
1206+
assert r.status_code == 201, r.text
1207+
assert worker.test_run() == len(recipients)
1208+
1209+
records = [r for r in caplog.records if r.name == 'spam.email_checker' and r.levelno == logging.ERROR]
1210+
assert len(records) == 1
1211+
body = getattr(records[0], 'email_main_body')
1212+
assert (
1213+
body == 'Hi {{ recipient_first_name }}, Dont miss out on FREE MONEY! '
1214+
'Click [here]({{ login_link }}) now! Regards, {{ company_name }}'
1215+
)

0 commit comments

Comments
 (0)