108 lines
4.3 KiB
Python
108 lines
4.3 KiB
Python
import pytest
|
|
from django.core import mail
|
|
from outreach.models import EmailTemplate, Campaign, CampaignRecipient
|
|
from outreach.engine import inject_tracking_pixel, dispatch_campaign
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestOutreachEngine:
|
|
def test_merge_tag_substitution(self):
|
|
template = EmailTemplate.objects.create(
|
|
name='Test Promo',
|
|
subject='Hello {{ first_name }}!',
|
|
body_html='<p>Hi {{ first_name }}, welcome to {{ company }}! Your code is {{ promo_code }}.</p>',
|
|
body_text='Hi {{ first_name }}, welcome to {{ company }}!'
|
|
)
|
|
context = {
|
|
'first_name': 'Alice',
|
|
'company': 'Acme Corp',
|
|
'promo_code': 'SAVE20'
|
|
}
|
|
subj, html, text = template.render(context)
|
|
assert subj == 'Hello Alice!'
|
|
assert 'Hi Alice, welcome to Acme Corp! Your code is SAVE20.' in html
|
|
assert 'Hi Alice, welcome to Acme Corp!' in text
|
|
|
|
def test_missing_merge_tag_fallback(self):
|
|
template = EmailTemplate.objects.create(
|
|
name='Missing Tag Test',
|
|
subject='Welcome {{ first_name }} to {{ nonexistent_var }}',
|
|
body_html='<p>Hello {{ first_name }}, your plan is {{ missing_plan }}</p>',
|
|
body_text='Hello {{ first_name }}, plan: {{ missing_plan }}'
|
|
)
|
|
context = {'first_name': 'Bob'}
|
|
subj, html, text = template.render(context)
|
|
# Missing tags are not provided in context, so unrendered placeholders remain or are handled safely
|
|
assert 'Welcome Bob' in subj
|
|
assert 'Hello Bob' in html
|
|
assert 'Hello Bob' in text
|
|
|
|
def test_inject_tracking_pixel_with_body_tag(self):
|
|
html = '<html><body><p>Test</p></body></html>'
|
|
token = '12345678-1234-5678-1234-567812345678'
|
|
result = inject_tracking_pixel(html, token, base_url='https://crm.example.com')
|
|
assert f'<img src="https://crm.example.com/api/outreach/track/open/{token}/"' in result
|
|
assert '</body>' in result
|
|
assert result.endswith('</body></html>')
|
|
|
|
def test_inject_tracking_pixel_without_body_tag(self):
|
|
html = '<p>Just a fragment</p>'
|
|
token = '12345678-1234-5678-1234-567812345678'
|
|
result = inject_tracking_pixel(html, token, base_url='https://crm.example.com')
|
|
assert f'<img src="https://crm.example.com/api/outreach/track/open/{token}/"' in result
|
|
assert result.startswith('<p>Just a fragment</p><img')
|
|
|
|
def test_multi_recipient_dispatch(self):
|
|
mail.outbox = []
|
|
template = EmailTemplate.objects.create(
|
|
name='Outreach Blast',
|
|
subject='Special Offer for {{ first_name }}',
|
|
body_html='<html><body><p>Hey {{ first_name }}, check this out!</p></body></html>',
|
|
body_text='Hey {{ first_name }}, check this out!'
|
|
)
|
|
campaign = Campaign.objects.create(
|
|
name='Q3 Outreach',
|
|
template=template,
|
|
status='draft'
|
|
)
|
|
rec1 = CampaignRecipient.objects.create(
|
|
campaign=campaign,
|
|
email='alice@example.com',
|
|
first_name='Alice',
|
|
last_name='Smith',
|
|
status='queued',
|
|
merge_data={'company': 'AliceCo'}
|
|
)
|
|
rec2 = CampaignRecipient.objects.create(
|
|
campaign=campaign,
|
|
email='bob@example.com',
|
|
first_name='Bob',
|
|
last_name='Jones',
|
|
status='queued',
|
|
merge_data={'company': 'BobCo'}
|
|
)
|
|
|
|
sent_count = dispatch_campaign(campaign.id, base_url='http://testserver')
|
|
assert sent_count == 2
|
|
assert len(mail.outbox) == 2
|
|
|
|
rec1.refresh_from_db()
|
|
rec2.refresh_from_db()
|
|
campaign.refresh_from_db()
|
|
|
|
assert rec1.status == 'sent'
|
|
assert rec1.sent_at is not None
|
|
assert rec2.status == 'sent'
|
|
assert rec2.sent_at is not None
|
|
assert campaign.status == 'completed'
|
|
|
|
# Verify email contents & tracking pixel
|
|
email1 = mail.outbox[0]
|
|
assert email1.to == ['alice@example.com']
|
|
assert email1.subject == 'Special Offer for Alice'
|
|
assert str(rec1.tracking_token) in email1.alternatives[0][0]
|
|
|
|
email2 = mail.outbox[1]
|
|
assert email2.to == ['bob@example.com']
|
|
assert email2.subject == 'Special Offer for Bob'
|
|
assert str(rec2.tracking_token) in email2.alternatives[0][0]
|