app.bettersight.io/backend/tests/services/test_email_service.py

85 lines
2.7 KiB
Python

import sys
import types
from services import email_service
def _install_fake_resend(monkeypatch, raise_error=False):
fake = types.ModuleType('resend')
fake.api_key = None
calls = []
class _Emails:
@staticmethod
def send(payload):
if raise_error:
raise RuntimeError('resend api down')
calls.append(payload)
fake.Emails = _Emails
monkeypatch.setitem(sys.modules, 'resend', fake)
return calls
def test_send_welcome_email_renders_and_sends(monkeypatch):
calls = _install_fake_resend(monkeypatch)
result = email_service.send_welcome_email(
'pm@gadventures.com', 'G Adventures', template_url='https://sheets.google.com/template'
)
assert result is True
assert len(calls) == 1
assert calls[0]['to'] == 'pm@gadventures.com'
assert 'G Adventures' in calls[0]['html']
assert 'template' in calls[0]['html']
def test_send_welcome_email_without_template_url(monkeypatch):
calls = _install_fake_resend(monkeypatch)
result = email_service.send_welcome_email('pm@gadventures.com', 'G Adventures')
assert result is True
assert len(calls) == 1
def test_send_trial_reminder_email(monkeypatch):
calls = _install_fake_resend(monkeypatch)
result = email_service.send_trial_reminder_email('pm@gadventures.com', 'G Adventures', 2, '2026-07-14')
assert result is True
assert '2' in calls[0]['subject']
assert '2026-07-14' in calls[0]['html']
def test_send_price_alert_email_computes_previous_price(monkeypatch):
calls = _install_fake_resend(monkeypatch)
result = email_service.send_price_alert_email('pm@gadventures.com', {
'competitor_name': 'Intrepid Travel', 'trip_name': 'Peru Classic',
'majority_price_usd': 2190, 'change_amount': -300, 'change_percent': -12,
})
assert result is True
assert 'Intrepid Travel' in calls[0]['subject']
assert '2490' in calls[0]['html'] # previous = 2190 - (-300)
assert '2190' in calls[0]['html']
def test_send_new_product_email(monkeypatch):
calls = _install_fake_resend(monkeypatch)
result = email_service.send_new_product_email('pm@gadventures.com', {
'competitor_name': 'Flash Pack', 'trip_name': 'Jordan Desert Explorer',
'destination': 'Jordan', 'duration_days': 8, 'majority_price_usd': 3200,
})
assert result is True
assert 'Flash Pack' in calls[0]['subject']
assert 'Jordan Desert Explorer' in calls[0]['html']
def test_send_returns_false_and_logs_on_failure(monkeypatch):
_install_fake_resend(monkeypatch, raise_error=True)
result = email_service.send_welcome_email('pm@gadventures.com', 'G Adventures')
assert result is False