- apps/solutions: SolutionIndexPage/SolutionPage (mirrors products/cases) - apps/home: HomePage.subpage_types now includes products/solutions/cases index pages (previously only blog, blocking admin page creation) - pytest.ini + conftest.py (root_page/home_page fixtures) - unit tests for core (SEOablePage, FormBlock API repr), products, cases, forms (lead submit API)
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
"""apps.forms 单元测试:线索提交 API(B2B 官网核心转化路径,测试覆盖率要求较高)。"""
|
|
import pytest
|
|
from rest_framework.test import APIClient
|
|
|
|
from apps.forms.models import FormDefinition, FormDefinitionField, Lead
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
@pytest.fixture
|
|
def contact_form():
|
|
form_def = FormDefinition.objects.create(
|
|
name="联系我们",
|
|
success_message="提交成功,我们会尽快联系您。",
|
|
)
|
|
FormDefinitionField.objects.create(
|
|
form=form_def, label="姓名", field_key="name", field_type="text", required=True
|
|
)
|
|
return form_def
|
|
|
|
|
|
def test_lead_submit_creates_lead_and_returns_success_message(contact_form):
|
|
client = APIClient()
|
|
response = client.post(
|
|
"/api/v1/custom/leads/",
|
|
{
|
|
"form_id": contact_form.pk,
|
|
"data": {"name": "张三"},
|
|
"source_url": "https://example.com/contact",
|
|
},
|
|
format="json",
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
assert response.data["success"] is True
|
|
assert response.data["message"] == contact_form.success_message
|
|
|
|
lead = Lead.objects.get()
|
|
assert lead.form_id == contact_form.pk
|
|
assert lead.data == {"name": "张三"}
|
|
assert lead.source_url == "https://example.com/contact"
|
|
|
|
|
|
def test_lead_submit_returns_404_for_unknown_form():
|
|
client = APIClient()
|
|
response = client.post(
|
|
"/api/v1/custom/leads/",
|
|
{"form_id": 9999, "data": {}},
|
|
format="json",
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
assert response.data["error"]["code"] == "NOT_FOUND"
|
|
|
|
|
|
def test_lead_submit_requires_form_id():
|
|
client = APIClient()
|
|
response = client.post(
|
|
"/api/v1/custom/leads/",
|
|
{"data": {"name": "张三"}},
|
|
format="json",
|
|
)
|
|
|
|
assert response.status_code == 400
|