66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
def contact_payload(**overrides):
|
|
payload = {
|
|
"name": "Jane Smith",
|
|
"email": "jane@example.com",
|
|
"phone": "07123 456789",
|
|
"enquiry_type": "aviation_business",
|
|
"subject": "Basing a maintenance business at Swansea",
|
|
"message": "We would like to explore operating from Swansea Airport.",
|
|
"source_page": "/contact/",
|
|
}
|
|
payload.update(overrides)
|
|
return payload
|
|
|
|
|
|
def test_public_contact_request_emails_tower_and_logs(client, monkeypatch, capsys):
|
|
sent_emails = []
|
|
|
|
async def fake_send_email(**kwargs):
|
|
sent_emails.append(kwargs)
|
|
return True
|
|
|
|
monkeypatch.setattr("app.api.endpoints.contact_requests.email_service.send_email", fake_send_email)
|
|
|
|
response = client.post(
|
|
"/api/v1/contact-requests/public",
|
|
json=contact_payload(),
|
|
headers={"X-Forwarded-For": "203.0.113.10, 10.0.0.1"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "received"}
|
|
assert len(sent_emails) == 1
|
|
|
|
email = sent_emails[0]
|
|
assert email["to_email"] == "tower@swansea-airport.wales"
|
|
assert email["reply_to"] == "Jane Smith <jane@example.com>"
|
|
assert email["subject"] == "Website contact: Basing a maintenance business at Swansea"
|
|
assert email["template_name"] == "contact_request.html"
|
|
assert email["template_vars"]["name"] == "Jane Smith"
|
|
assert email["template_vars"]["enquiry_type"] == "aviation_business"
|
|
assert email["template_vars"]["client_ip"] == "203.0.113.10"
|
|
|
|
log_output = capsys.readouterr().out
|
|
assert "Public contact request received" in log_output
|
|
assert "aviation_business" in log_output
|
|
assert "jane@example.com" in log_output
|
|
|
|
|
|
def test_public_contact_request_validation(client, monkeypatch):
|
|
async def fake_send_email(**kwargs):
|
|
return True
|
|
|
|
monkeypatch.setattr("app.api.endpoints.contact_requests.email_service.send_email", fake_send_email)
|
|
|
|
invalid_category = client.post(
|
|
"/api/v1/contact-requests/public",
|
|
json=contact_payload(enquiry_type="sales"),
|
|
)
|
|
blank_required = client.post(
|
|
"/api/v1/contact-requests/public",
|
|
json=contact_payload(name=" ", subject="", message=" "),
|
|
)
|
|
|
|
assert invalid_category.status_code == 422
|
|
assert blank_required.status_code == 422
|