v1
This commit is contained in:
39
tests/test_contact_api.py
Normal file
39
tests/test_contact_api.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
|
||||
server_app_module = importlib.import_module("server.app")
|
||||
|
||||
# Expose app and init_db from the imported module
|
||||
app = server_app_module.app
|
||||
init_db = server_app_module.init_db
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with app.test_client() as client:
|
||||
yield client
|
||||
|
||||
|
||||
def post(client, data):
|
||||
return client.post("/api/contact", data=data)
|
||||
|
||||
|
||||
def test_valid_submission_creates_record_and_returns_201(client):
|
||||
resp = post(
|
||||
client,
|
||||
{"name": "Test User", "email": "test@example.com",
|
||||
"message": "Hello", "consent": "on"},
|
||||
)
|
||||
assert resp.status_code in (201, 202)
|
||||
body = resp.get_json()
|
||||
assert body["status"] == "ok"
|
||||
assert isinstance(body.get("id"), int)
|
||||
|
||||
|
||||
def test_missing_required_fields_returns_400(client):
|
||||
resp = post(client, {"name": "", "email": "", "message": ""})
|
||||
assert resp.status_code == 400
|
||||
body = resp.get_json()
|
||||
assert body["status"] == "error"
|
||||
assert "errors" in body
|
||||
Reference in New Issue
Block a user