209 lines
6.5 KiB
Python
209 lines
6.5 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Integration test for newsletter with tracking.
|
|
Tests the full flow of generating a newsletter with tracking enabled.
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
# Add backend directory to path
|
|
backend_dir = Path(__file__).parent.parent / 'backend'
|
|
sys.path.insert(0, str(backend_dir))
|
|
|
|
# Mock the tracking service to avoid database dependency
|
|
class MockTrackingService:
|
|
"""Mock tracking service for testing"""
|
|
|
|
@staticmethod
|
|
def create_newsletter_tracking(newsletter_id, subscriber_email, article_links=None):
|
|
"""Mock create_newsletter_tracking function"""
|
|
link_tracking_map = {}
|
|
|
|
if article_links:
|
|
for i, article in enumerate(article_links):
|
|
link_tracking_map[article['url']] = f"mock-link-{i}"
|
|
|
|
return {
|
|
'pixel_tracking_id': 'mock-pixel-123',
|
|
'link_tracking_map': link_tracking_map,
|
|
'newsletter_id': newsletter_id,
|
|
'subscriber_email': subscriber_email
|
|
}
|
|
|
|
# Import after setting up path
|
|
from tracking_integration import inject_tracking_pixel, replace_article_links, generate_tracking_urls
|
|
from jinja2 import Template
|
|
|
|
|
|
def test_newsletter_with_tracking():
|
|
"""Test generating a newsletter with tracking enabled"""
|
|
print("\n" + "="*70)
|
|
print("NEWSLETTER TRACKING INTEGRATION TEST")
|
|
print("="*70)
|
|
|
|
# Mock article data
|
|
articles = [
|
|
{
|
|
'title': 'Munich Tech Summit Announces 2025 Dates',
|
|
'author': 'Tech Reporter',
|
|
'link': 'https://example.com/tech-summit',
|
|
'summary': 'The annual Munich Tech Summit will return in 2025 with exciting new features.',
|
|
'source': 'Munich Tech News',
|
|
'published_at': datetime.now()
|
|
},
|
|
{
|
|
'title': 'New Public Transport Routes Launched',
|
|
'author': 'Transport Desk',
|
|
'link': 'https://example.com/transport-routes',
|
|
'summary': 'MVG announces three new bus routes connecting suburban areas.',
|
|
'source': 'Munich Transport',
|
|
'published_at': datetime.now()
|
|
}
|
|
]
|
|
|
|
# Configuration
|
|
newsletter_id = 'test-newsletter-2025-11-11'
|
|
subscriber_email = 'test@example.com'
|
|
api_url = 'http://localhost:5001'
|
|
|
|
print(f"\nNewsletter ID: {newsletter_id}")
|
|
print(f"Subscriber: {subscriber_email}")
|
|
print(f"Articles: {len(articles)}")
|
|
print(f"API URL: {api_url}")
|
|
|
|
# Step 1: Generate tracking URLs
|
|
print("\n" + "-"*70)
|
|
print("Step 1: Generate tracking data")
|
|
print("-"*70)
|
|
|
|
tracking_data = generate_tracking_urls(
|
|
articles=articles,
|
|
newsletter_id=newsletter_id,
|
|
subscriber_email=subscriber_email,
|
|
tracking_service=MockTrackingService
|
|
)
|
|
|
|
print(f"✓ Pixel tracking ID: {tracking_data['pixel_tracking_id']}")
|
|
print(f"✓ Link tracking map: {len(tracking_data['link_tracking_map'])} links")
|
|
for url, tracking_id in tracking_data['link_tracking_map'].items():
|
|
print(f" - {url} → {tracking_id}")
|
|
|
|
# Step 2: Load and render template
|
|
print("\n" + "-"*70)
|
|
print("Step 2: Render newsletter template")
|
|
print("-"*70)
|
|
|
|
template_path = Path(__file__).parent / 'newsletter_template.html'
|
|
with open(template_path, 'r', encoding='utf-8') as f:
|
|
template_content = f.read()
|
|
|
|
template = Template(template_content)
|
|
|
|
now = datetime.now()
|
|
template_data = {
|
|
'date': now.strftime('%A, %B %d, %Y'),
|
|
'year': now.year,
|
|
'article_count': len(articles),
|
|
'articles': articles,
|
|
'unsubscribe_link': 'http://localhost:3000/unsubscribe',
|
|
'website_link': 'http://localhost:3000',
|
|
'tracking_enabled': True
|
|
}
|
|
|
|
html = template.render(**template_data)
|
|
print("✓ Template rendered")
|
|
|
|
# Step 3: Inject tracking pixel
|
|
print("\n" + "-"*70)
|
|
print("Step 3: Inject tracking pixel")
|
|
print("-"*70)
|
|
|
|
html = inject_tracking_pixel(
|
|
html,
|
|
tracking_data['pixel_tracking_id'],
|
|
api_url
|
|
)
|
|
|
|
pixel_url = f"{api_url}/api/track/pixel/{tracking_data['pixel_tracking_id']}"
|
|
if pixel_url in html:
|
|
print(f"✓ Tracking pixel injected: {pixel_url}")
|
|
else:
|
|
print(f"✗ Tracking pixel NOT found")
|
|
return False
|
|
|
|
# Step 4: Replace article links
|
|
print("\n" + "-"*70)
|
|
print("Step 4: Replace article links with tracking URLs")
|
|
print("-"*70)
|
|
|
|
html = replace_article_links(
|
|
html,
|
|
tracking_data['link_tracking_map'],
|
|
api_url
|
|
)
|
|
|
|
# Verify all article links were replaced
|
|
success = True
|
|
for article in articles:
|
|
original_url = article['link']
|
|
tracking_id = tracking_data['link_tracking_map'].get(original_url)
|
|
|
|
if tracking_id:
|
|
tracking_url = f"{api_url}/api/track/click/{tracking_id}"
|
|
if tracking_url in html:
|
|
print(f"✓ Link replaced: {original_url}")
|
|
print(f" → {tracking_url}")
|
|
else:
|
|
print(f"✗ Link NOT replaced: {original_url}")
|
|
success = False
|
|
|
|
# Verify original URL is NOT in the HTML (should be replaced)
|
|
if f'href="{original_url}"' in html:
|
|
print(f"✗ Original URL still present: {original_url}")
|
|
success = False
|
|
|
|
# Step 5: Verify privacy notice
|
|
print("\n" + "-"*70)
|
|
print("Step 5: Verify privacy notice")
|
|
print("-"*70)
|
|
|
|
if "This email contains tracking to measure engagement" in html:
|
|
print("✓ Privacy notice present in footer")
|
|
else:
|
|
print("✗ Privacy notice NOT found")
|
|
success = False
|
|
|
|
# Step 6: Save output for inspection
|
|
print("\n" + "-"*70)
|
|
print("Step 6: Save test output")
|
|
print("-"*70)
|
|
|
|
output_file = 'test_newsletter_with_tracking.html'
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
f.write(html)
|
|
|
|
print(f"✓ Test newsletter saved to: {output_file}")
|
|
print(f" Open it in your browser to inspect the tracking integration")
|
|
|
|
return success
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print("\n" + "="*70)
|
|
print("TESTING NEWSLETTER WITH TRACKING")
|
|
print("="*70)
|
|
|
|
success = test_newsletter_with_tracking()
|
|
|
|
print("\n" + "="*70)
|
|
if success:
|
|
print("✓ ALL TESTS PASSED")
|
|
print("="*70 + "\n")
|
|
sys.exit(0)
|
|
else:
|
|
print("✗ SOME TESTS FAILED")
|
|
print("="*70 + "\n")
|
|
sys.exit(1)
|