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='

Hi {{ first_name }}, welcome to {{ company }}! Your code is {{ promo_code }}.

', 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='

Hello {{ first_name }}, your plan is {{ missing_plan }}

', 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 = '

Test

' token = '12345678-1234-5678-1234-567812345678' result = inject_tracking_pixel(html, token, base_url='https://crm.example.com') assert f'' in result assert result.endswith('') def test_inject_tracking_pixel_without_body_tag(self): html = '

Just a fragment

' token = '12345678-1234-5678-1234-567812345678' result = inject_tracking_pixel(html, token, base_url='https://crm.example.com') assert f'Just a fragment