154 lines
6.2 KiB
Python
154 lines
6.2 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]
|
|
|
|
def test_campaign_launch_modal_payload_and_tracking_open(self):
|
|
from rest_framework.test import APIClient
|
|
from django.contrib.auth.models import User
|
|
user = User.objects.create_user('outreach_user', 'outreach@test.com', 'pw')
|
|
client = APIClient()
|
|
client.force_authenticate(user=user)
|
|
|
|
# 1. UI payload with only {name, subject, target_object} without explicit template
|
|
res = client.post('/api/campaigns/', {
|
|
'name': 'UI Launched Campaign',
|
|
'subject': 'Special Offer For You',
|
|
'target_object': 'Lead'
|
|
}, format='json')
|
|
assert res.status_code == 201
|
|
campaign_id = res.data['id']
|
|
assert res.data['template'] is not None
|
|
|
|
# 2. Add recipient
|
|
add_res = client.post(f'/api/campaigns/{campaign_id}/add-recipients/', {
|
|
'recipients': [{'email': 'prospect@example.com', 'first_name': 'Prospect'}]
|
|
}, format='json')
|
|
assert add_res.status_code == 201
|
|
assert add_res.data['count'] == 1
|
|
|
|
# 3. Dispatch
|
|
dispatch_res = client.post(f'/api/campaigns/{campaign_id}/dispatch/')
|
|
assert dispatch_res.status_code == 200
|
|
|
|
# Check metrics before open
|
|
camp_res = client.get(f'/api/campaigns/{campaign_id}/')
|
|
assert camp_res.data['sent_count'] == 1
|
|
assert camp_res.data['open_count'] == 0
|
|
|
|
# 4. Open tracking pixel
|
|
recipient = CampaignRecipient.objects.get(campaign_id=campaign_id)
|
|
open_res = client.get(f'/api/outreach/track/open/{recipient.tracking_token}/')
|
|
assert open_res.status_code == 200
|
|
|
|
recipient.refresh_from_db()
|
|
assert recipient.opened_at is not None
|
|
assert recipient.status == 'opened'
|
|
|
|
# Check metrics after open
|
|
camp_res = client.get(f'/api/campaigns/{campaign_id}/')
|
|
assert camp_res.data['open_count'] == 1
|