update
This commit is contained in:
187
tests/sender/test_tracking_integration.py
Normal file
187
tests/sender/test_tracking_integration.py
Normal file
@@ -0,0 +1,187 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Test script for tracking integration in newsletter sender.
|
||||
Tests tracking pixel injection and link replacement.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend directory to path
|
||||
backend_dir = Path(__file__).parent.parent / 'backend'
|
||||
sys.path.insert(0, str(backend_dir))
|
||||
|
||||
from tracking_integration import inject_tracking_pixel, replace_article_links
|
||||
|
||||
|
||||
def test_inject_tracking_pixel():
|
||||
"""Test that tracking pixel is correctly injected into HTML"""
|
||||
print("\n" + "="*70)
|
||||
print("TEST 1: Inject Tracking Pixel")
|
||||
print("="*70)
|
||||
|
||||
# Test HTML
|
||||
html = """<html>
|
||||
<body>
|
||||
<p>Newsletter content</p>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
tracking_id = "test-tracking-123"
|
||||
api_url = "http://localhost:5001"
|
||||
|
||||
# Inject pixel
|
||||
result = inject_tracking_pixel(html, tracking_id, api_url)
|
||||
|
||||
# Verify pixel is present
|
||||
expected_pixel = f'<img src="{api_url}/api/track/pixel/{tracking_id}" width="1" height="1" alt="" style="display:block;" />'
|
||||
|
||||
if expected_pixel in result:
|
||||
print("✓ Tracking pixel correctly injected")
|
||||
print(f" Pixel URL: {api_url}/api/track/pixel/{tracking_id}")
|
||||
return True
|
||||
else:
|
||||
print("✗ Tracking pixel NOT found in HTML")
|
||||
print(f" Expected: {expected_pixel}")
|
||||
print(f" Result: {result}")
|
||||
return False
|
||||
|
||||
|
||||
def test_replace_article_links():
|
||||
"""Test that article links are correctly replaced with tracking URLs"""
|
||||
print("\n" + "="*70)
|
||||
print("TEST 2: Replace Article Links")
|
||||
print("="*70)
|
||||
|
||||
# Test HTML with article links
|
||||
html = """<html>
|
||||
<body>
|
||||
<a href="https://example.com/article1">Article 1</a>
|
||||
<a href="https://example.com/article2">Article 2</a>
|
||||
<a href="https://example.com/untracked">Untracked Link</a>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
# Tracking map
|
||||
link_tracking_map = {
|
||||
"https://example.com/article1": "track-id-1",
|
||||
"https://example.com/article2": "track-id-2"
|
||||
}
|
||||
|
||||
api_url = "http://localhost:5001"
|
||||
|
||||
# Replace links
|
||||
result = replace_article_links(html, link_tracking_map, api_url)
|
||||
|
||||
# Verify replacements
|
||||
success = True
|
||||
|
||||
# Check article 1 link
|
||||
expected_url_1 = f"{api_url}/api/track/click/track-id-1"
|
||||
if expected_url_1 in result:
|
||||
print(f"✓ Article 1 link replaced: {expected_url_1}")
|
||||
else:
|
||||
print(f"✗ Article 1 link NOT replaced")
|
||||
success = False
|
||||
|
||||
# Check article 2 link
|
||||
expected_url_2 = f"{api_url}/api/track/click/track-id-2"
|
||||
if expected_url_2 in result:
|
||||
print(f"✓ Article 2 link replaced: {expected_url_2}")
|
||||
else:
|
||||
print(f"✗ Article 2 link NOT replaced")
|
||||
success = False
|
||||
|
||||
# Check untracked link remains unchanged
|
||||
if "https://example.com/untracked" in result:
|
||||
print(f"✓ Untracked link preserved: https://example.com/untracked")
|
||||
else:
|
||||
print(f"✗ Untracked link was modified (should remain unchanged)")
|
||||
success = False
|
||||
|
||||
return success
|
||||
|
||||
|
||||
def test_full_integration():
|
||||
"""Test full integration: pixel + link replacement"""
|
||||
print("\n" + "="*70)
|
||||
print("TEST 3: Full Integration (Pixel + Links)")
|
||||
print("="*70)
|
||||
|
||||
# Test HTML
|
||||
html = """<html>
|
||||
<body>
|
||||
<h1>Newsletter</h1>
|
||||
<a href="https://example.com/article">Read Article</a>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
api_url = "http://localhost:5001"
|
||||
pixel_tracking_id = "pixel-123"
|
||||
link_tracking_map = {
|
||||
"https://example.com/article": "link-456"
|
||||
}
|
||||
|
||||
# First inject pixel
|
||||
html = inject_tracking_pixel(html, pixel_tracking_id, api_url)
|
||||
|
||||
# Then replace links
|
||||
html = replace_article_links(html, link_tracking_map, api_url)
|
||||
|
||||
# Verify both are present
|
||||
success = True
|
||||
|
||||
pixel_url = f"{api_url}/api/track/pixel/{pixel_tracking_id}"
|
||||
if pixel_url in html:
|
||||
print(f"✓ Tracking pixel present: {pixel_url}")
|
||||
else:
|
||||
print(f"✗ Tracking pixel NOT found")
|
||||
success = False
|
||||
|
||||
link_url = f"{api_url}/api/track/click/link-456"
|
||||
if link_url in html:
|
||||
print(f"✓ Tracking link present: {link_url}")
|
||||
else:
|
||||
print(f"✗ Tracking link NOT found")
|
||||
success = False
|
||||
|
||||
if success:
|
||||
print("\n✓ Full integration successful!")
|
||||
print("\nFinal HTML:")
|
||||
print("-" * 70)
|
||||
print(html)
|
||||
print("-" * 70)
|
||||
|
||||
return success
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("\n" + "="*70)
|
||||
print("TRACKING INTEGRATION TEST SUITE")
|
||||
print("="*70)
|
||||
|
||||
results = []
|
||||
|
||||
# Run tests
|
||||
results.append(("Inject Tracking Pixel", test_inject_tracking_pixel()))
|
||||
results.append(("Replace Article Links", test_replace_article_links()))
|
||||
results.append(("Full Integration", test_full_integration()))
|
||||
|
||||
# Summary
|
||||
print("\n" + "="*70)
|
||||
print("TEST SUMMARY")
|
||||
print("="*70)
|
||||
|
||||
passed = sum(1 for _, result in results if result)
|
||||
total = len(results)
|
||||
|
||||
for test_name, result in results:
|
||||
status = "✓ PASS" if result else "✗ FAIL"
|
||||
print(f"{status}: {test_name}")
|
||||
|
||||
print("-" * 70)
|
||||
print(f"Results: {passed}/{total} tests passed")
|
||||
print("="*70 + "\n")
|
||||
|
||||
# Exit with appropriate code
|
||||
sys.exit(0 if passed == total else 1)
|
||||
Reference in New Issue
Block a user