sightbox/backend/tests/test_contact_lead.py

133 lines
4.7 KiB
Python

"""
tests/test_contact_lead.py — tests for POST /contact-lead. Resend's REST
API is always mocked (via monkeypatch on requests.post) — no test hits it
live, same discipline ENTERPRISE_STANDARDS.md requires for Stripe/Frappe
in the other billing routes.
"""
from unittest.mock import MagicMock
import pytest
import app as app_module
from billing import contact_lead as contact_lead_module
@pytest.fixture
def client():
flask_app = app_module.app
flask_app.config["TESTING"] = True
with flask_app.test_client() as test_client:
yield test_client
@pytest.fixture
def configured_resend(monkeypatch):
"""Sets both required env vars so the happy path is reachable."""
monkeypatch.setenv("RESEND_API_KEY", "re_test_key")
monkeypatch.setenv("CONTACT_LEAD_NOTIFY_EMAIL", "owner@example.com")
# --- validation ------------------------------------------------------------
def test_missing_email_returns_400(client):
response = client.post("/contact-lead", json={})
assert response.status_code == 400
assert "trace_id" in response.get_json()
def test_malformed_email_returns_400(client):
response = client.post("/contact-lead", json={"email": "not-an-email"})
assert response.status_code == 400
def test_blank_email_returns_400(client):
response = client.post("/contact-lead", json={"email": " "})
assert response.status_code == 400
# --- happy path --------------------------------------------------------------
def test_valid_email_returns_ok_and_notifies_resend(client, configured_resend, monkeypatch):
mock_post = MagicMock(return_value=MagicMock(raise_for_status=MagicMock()))
monkeypatch.setattr(contact_lead_module.requests, "post", mock_post)
response = client.post("/contact-lead", json={"email": "lead@example.com"})
assert response.status_code == 200
assert response.get_json()["status"] == "ok"
assert "X-Trace-ID" in response.headers
mock_post.assert_called_once()
args, kwargs = mock_post.call_args
assert args[0] == "https://api.resend.com/emails"
assert kwargs["headers"]["Authorization"] == "Bearer re_test_key"
assert kwargs["json"]["to"] == ["owner@example.com"]
assert "lead@example.com" in kwargs["json"]["html"]
def test_from_email_defaults_to_resend_dev_sender(client, configured_resend, monkeypatch):
mock_post = MagicMock(return_value=MagicMock(raise_for_status=MagicMock()))
monkeypatch.setattr(contact_lead_module.requests, "post", mock_post)
client.post("/contact-lead", json={"email": "lead@example.com"})
assert mock_post.call_args.kwargs["json"]["from"] == "onboarding@resend.dev"
def test_from_email_override_is_respected(client, configured_resend, monkeypatch):
monkeypatch.setenv("CONTACT_LEAD_FROM_EMAIL", "hello@bettersight.io")
mock_post = MagicMock(return_value=MagicMock(raise_for_status=MagicMock()))
monkeypatch.setattr(contact_lead_module.requests, "post", mock_post)
client.post("/contact-lead", json={"email": "lead@example.com"})
assert mock_post.call_args.kwargs["json"]["from"] == "hello@bettersight.io"
# --- best-effort notify: failures never surface to the browser -------------
def test_missing_resend_config_still_returns_ok(client, monkeypatch):
monkeypatch.delenv("RESEND_API_KEY", raising=False)
monkeypatch.delenv("CONTACT_LEAD_NOTIFY_EMAIL", raising=False)
mock_capture = MagicMock()
monkeypatch.setattr(contact_lead_module.sentry_sdk, "capture_message", mock_capture)
response = client.post("/contact-lead", json={"email": "lead@example.com"})
assert response.status_code == 200
assert response.get_json()["status"] == "ok"
mock_capture.assert_called_once()
def test_resend_failure_still_returns_ok(client, configured_resend, monkeypatch):
mock_post = MagicMock(side_effect=Exception("network error"))
monkeypatch.setattr(contact_lead_module.requests, "post", mock_post)
mock_capture = MagicMock()
monkeypatch.setattr(contact_lead_module.sentry_sdk, "capture_exception", mock_capture)
response = client.post("/contact-lead", json={"email": "lead@example.com"})
assert response.status_code == 200
assert response.get_json()["status"] == "ok"
mock_capture.assert_called_once()
def test_resend_non_2xx_status_still_returns_ok(client, configured_resend, monkeypatch):
import requests as real_requests
bad_response = MagicMock()
bad_response.raise_for_status.side_effect = real_requests.HTTPError("422 Unprocessable")
monkeypatch.setattr(contact_lead_module.requests, "post", MagicMock(return_value=bad_response))
response = client.post("/contact-lead", json={"email": "lead@example.com"})
assert response.status_code == 200
assert response.get_json()["status"] == "ok"