110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
"""
|
|
tests/test_contact.py — tests for POST /api/contact. Resend is always
|
|
mocked at the httpx.post level (services/email_service.py's one outbound
|
|
call) — no test hits it live.
|
|
"""
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import httpx
|
|
|
|
import services.email_service as email_service
|
|
|
|
|
|
def _mock_post(monkeypatch, status_code=200):
|
|
response = MagicMock()
|
|
response.status_code = status_code
|
|
if status_code >= 400:
|
|
response.raise_for_status.side_effect = httpx.HTTPStatusError(
|
|
"error", request=MagicMock(), response=response
|
|
)
|
|
else:
|
|
response.raise_for_status.return_value = None
|
|
mock_post = MagicMock(return_value=response)
|
|
monkeypatch.setattr(email_service.httpx, "post", mock_post)
|
|
return mock_post
|
|
|
|
|
|
# --- validation ------------------------------------------------------------
|
|
|
|
|
|
def test_missing_name_returns_400(client):
|
|
response = client.post("/api/contact", json={"email": "lead@example.com"})
|
|
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "name is required"
|
|
|
|
|
|
def test_missing_email_returns_400(client):
|
|
response = client.post("/api/contact", json={"name": "Jason"})
|
|
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "a valid email is required"
|
|
|
|
|
|
def test_malformed_email_returns_400(client):
|
|
response = client.post("/api/contact", json={"name": "Jason", "email": "not-an-email"})
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
# --- happy path --------------------------------------------------------------
|
|
|
|
|
|
def test_valid_submission_sends_via_resend(client, configured_resend, monkeypatch):
|
|
mock_post = _mock_post(monkeypatch)
|
|
|
|
response = client.post(
|
|
"/api/contact",
|
|
json={
|
|
"name": "Jason Fraser",
|
|
"email": "jason@example.com",
|
|
"organization": "Acme Co",
|
|
"message": "Tell me more.",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 202
|
|
assert response.get_json()["status"] == "sent"
|
|
|
|
mock_post.assert_called_once()
|
|
args, kwargs = mock_post.call_args
|
|
assert args[0] == "https://api.resend.com/emails"
|
|
assert kwargs["json"]["to"] == ["sales@bettersight.io"]
|
|
assert "Jason Fraser" in kwargs["json"]["html"]
|
|
|
|
|
|
def test_html_in_submission_is_escaped(client, configured_resend, monkeypatch):
|
|
mock_post = _mock_post(monkeypatch)
|
|
|
|
client.post(
|
|
"/api/contact",
|
|
json={"name": "<script>alert(1)</script>", "email": "lead@example.com"},
|
|
)
|
|
|
|
sent_html = mock_post.call_args.kwargs["json"]["html"]
|
|
assert "<script>" not in sent_html
|
|
assert "<script>" in sent_html
|
|
|
|
|
|
# --- failure paths -----------------------------------------------------------
|
|
|
|
|
|
def test_missing_resend_config_returns_502(client):
|
|
response = client.post(
|
|
"/api/contact", json={"name": "Jason", "email": "lead@example.com"}
|
|
)
|
|
|
|
assert response.status_code == 502
|
|
assert "error" in response.get_json()
|
|
|
|
|
|
def test_resend_failure_returns_502(client, configured_resend, monkeypatch):
|
|
_mock_post(monkeypatch, status_code=422)
|
|
|
|
response = client.post(
|
|
"/api/contact", json={"name": "Jason", "email": "lead@example.com"}
|
|
)
|
|
|
|
assert response.status_code == 502
|