|
2 | 2 | import hashlib |
3 | 3 | import hmac |
4 | 4 | import json |
| 5 | +import logging |
5 | 6 | import pytest |
6 | 7 | import re |
7 | 8 | from arq import Retry |
@@ -1168,3 +1169,47 @@ def test_get_cache_key_with_emojis_and_special_chars(): |
1168 | 1169 | assert ( |
1169 | 1170 | cache_key != cache_key3 |
1170 | 1171 | ), 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