sightbox/website/backend/tests/test_calculator.py

122 lines
3.6 KiB
Python

"""
tests/test_calculator.py — tests for POST /api/calculator/copy and
POST /api/calculator/contact. build_pdf() runs for real (pure, fast, no
network — see services/pdf_service.py); Resend is mocked at the
httpx.post level, same as test_contact.py.
"""
from unittest.mock import MagicMock
import httpx
import services.email_service as email_service
_PAYLOAD = {
"calculator": "self-assess",
"title": "Your Results",
"summary_text": "You could save <strong>6 hours/week</strong>.",
"results": [
{"label": "Hours saved / week", "value": "6"},
{"label": "Annual cost", "value": "$18,000"},
],
"basis_footnote": "Estimates based on inputs provided.",
}
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
# --- /api/calculator/copy ---------------------------------------------------
def test_copy_missing_email_returns_400(client):
response = client.post("/api/calculator/copy", json={**_PAYLOAD, "recipient_email": ""})
assert response.status_code == 400
def test_copy_valid_request_sends_pdf_to_visitor_only(client, configured_resend, monkeypatch):
mock_post = _mock_post(monkeypatch)
response = client.post(
"/api/calculator/copy",
json={**_PAYLOAD, "recipient_email": "visitor@example.com"},
)
assert response.status_code == 200
assert response.get_json()["status"] == "sent"
mock_post.assert_called_once()
sent = mock_post.call_args.kwargs["json"]
assert sent["to"] == ["visitor@example.com"]
assert len(sent["attachments"]) == 1
assert sent["attachments"][0]["filename"] == "bettersight-results.pdf"
def test_copy_resend_failure_returns_502(client, configured_resend, monkeypatch):
_mock_post(monkeypatch, status_code=500)
response = client.post(
"/api/calculator/copy",
json={**_PAYLOAD, "recipient_email": "visitor@example.com"},
)
assert response.status_code == 502
# --- /api/calculator/contact -------------------------------------------------
def test_contact_missing_fields_returns_400(client):
response = client.post(
"/api/calculator/contact", json={**_PAYLOAD, "contact": {"name": "Jason"}}
)
assert response.status_code == 400
def test_contact_valid_request_notifies_sales_with_reply_to(client, configured_resend, monkeypatch):
mock_post = _mock_post(monkeypatch)
response = client.post(
"/api/calculator/contact",
json={
**_PAYLOAD,
"contact": {
"name": "Jason Fraser",
"email": "jason@example.com",
"organization": "Acme Co",
},
},
)
assert response.status_code == 200
assert response.get_json()["status"] == "received"
sent = mock_post.call_args.kwargs["json"]
assert sent["to"] == ["sales@bettersight.io"]
assert sent["reply_to"] == "jason@example.com"
assert "Jason Fraser" in sent["subject"]
def test_contact_resend_failure_returns_502(client, configured_resend, monkeypatch):
_mock_post(monkeypatch, status_code=500)
response = client.post(
"/api/calculator/contact",
json={**_PAYLOAD, "contact": {"name": "Jason", "email": "jason@example.com"}},
)
assert response.status_code == 502