144 lines
6.6 KiB
Python
144 lines
6.6 KiB
Python
VALID = {
|
|
"name": "Priya Sharma",
|
|
"phone": "+91 98765-43210",
|
|
"email": "priya@example.com",
|
|
"city": "Bhubaneswar",
|
|
"property_type": "residential",
|
|
"monthly_bill": 3500,
|
|
"required_capacity_kw": 5,
|
|
"message": "Need rooftop solar for my house.",
|
|
}
|
|
|
|
|
|
async def test_enquiry_submit_and_validation(client, admin_headers):
|
|
r = await client.post("/api/enquiries", json=VALID)
|
|
assert r.status_code == 201
|
|
assert r.json()["success"] is True
|
|
assert "id" not in r.json() # public response doesn't leak ids
|
|
|
|
bad = await client.post("/api/enquiries", json={"name": "A", "phone": "abc", "email": "nope"})
|
|
assert bad.status_code == 422
|
|
fields = {e["field"] for e in bad.json()["errors"]}
|
|
assert {"name", "phone", "email"} <= fields
|
|
|
|
extra = await client.post("/api/enquiries", json={**VALID, "status": "converted"})
|
|
assert extra.status_code == 422 # clients cannot set status
|
|
|
|
lst = (await client.get("/api/admin/enquiries", headers=admin_headers)).json()
|
|
assert lst["total"] == 1
|
|
item = lst["items"][0]
|
|
assert item["phone"] == "+919876543210" # normalised
|
|
assert item["status"] == "new"
|
|
|
|
|
|
async def test_enquiry_honeypot_not_saved(client, admin_headers):
|
|
r = await client.post("/api/enquiries", json={**VALID, "website": "http://spam.example"})
|
|
assert r.status_code == 201
|
|
assert (await client.get("/api/admin/enquiries", headers=admin_headers)).json()["total"] == 0
|
|
|
|
|
|
async def test_enquiry_admin_workflow(client, admin_headers):
|
|
for i, city in enumerate(["Cuttack", "Puri", "Cuttack"]):
|
|
await client.post("/api/enquiries", json={**VALID, "name": f"Customer {i}", "city": city})
|
|
|
|
r = await client.get("/api/admin/enquiries", params={"search": "cuttack"}, headers=admin_headers)
|
|
assert r.json()["total"] == 2
|
|
r = await client.get("/api/admin/enquiries", params={"search": "100%_"}, headers=admin_headers)
|
|
assert r.json()["total"] == 0 # LIKE wildcards are escaped
|
|
|
|
r = await client.patch(
|
|
"/api/admin/enquiries/1",
|
|
headers=admin_headers,
|
|
json={"status": "site_visit_scheduled", "note": "Visit booked for Monday 11am"},
|
|
)
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["status"] == "site_visit_scheduled"
|
|
assert body["notes"][0]["content"] == "Visit booked for Monday 11am"
|
|
assert body["notes"][0]["author_name"] == "Admin"
|
|
|
|
r = await client.patch("/api/admin/enquiries/1", headers=admin_headers, json={"status": "bogus"})
|
|
assert r.status_code == 422
|
|
r = await client.patch("/api/admin/enquiries/1", headers=admin_headers, json={})
|
|
assert r.status_code == 422
|
|
|
|
r = await client.get("/api/admin/enquiries", params={"status": "site_visit_scheduled"}, headers=admin_headers)
|
|
assert r.json()["total"] == 1
|
|
|
|
detail = (await client.get("/api/admin/enquiries/1", headers=admin_headers)).json()
|
|
assert detail["monthly_bill"] == 3500 and detail["message"]
|
|
|
|
stats = (await client.get("/api/admin/dashboard/stats", headers=admin_headers)).json()
|
|
assert stats["total_enquiries"] == 3
|
|
assert stats["new_enquiries"] == 2
|
|
assert stats["site_visits_scheduled"] == 1
|
|
assert len(stats["recent_enquiries"]) == 3
|
|
|
|
assert (await client.delete("/api/admin/enquiries/1", headers=admin_headers)).status_code == 204
|
|
assert (await client.get("/api/admin/enquiries/1", headers=admin_headers)).status_code == 404
|
|
assert (await client.delete("/api/admin/enquiries/999", headers=admin_headers)).status_code == 404
|
|
|
|
|
|
async def test_contact_messages(client, admin_headers):
|
|
r = await client.post("/api/contact", json={"name": "Asha", "email": "asha@example.com", "message": "Hello there"})
|
|
assert r.status_code == 201
|
|
bad = await client.post("/api/contact", json={"name": "Asha", "email": "bad", "message": "hi"})
|
|
assert bad.status_code == 422
|
|
lst = (await client.get("/api/admin/contact-messages", headers=admin_headers)).json()
|
|
assert lst["total"] == 1 and lst["items"][0]["is_read"] is False
|
|
r = await client.patch("/api/admin/contact-messages/1", headers=admin_headers, json={"is_read": True})
|
|
assert r.json()["is_read"] is True
|
|
stats = (await client.get("/api/admin/dashboard/stats", headers=admin_headers)).json()
|
|
assert stats["unread_contact_messages"] == 0
|
|
assert (await client.delete("/api/admin/contact-messages/1", headers=admin_headers)).status_code == 204
|
|
|
|
|
|
async def test_testimonials(client, editor_headers):
|
|
r = await client.post(
|
|
"/api/admin/testimonials",
|
|
headers=editor_headers,
|
|
json={"customer_name": "Mr. Das", "content": "Excellent installation!", "rating": 5},
|
|
)
|
|
assert r.status_code == 201
|
|
hidden = await client.post(
|
|
"/api/admin/testimonials",
|
|
headers=editor_headers,
|
|
json={"customer_name": "Draft", "content": "Not yet public", "is_published": False},
|
|
)
|
|
assert hidden.status_code == 201
|
|
bad = await client.post(
|
|
"/api/admin/testimonials", headers=editor_headers, json={"customer_name": "X Y", "content": "Bad rating", "rating": 9}
|
|
)
|
|
assert bad.status_code == 422
|
|
missing_project = await client.post(
|
|
"/api/admin/testimonials", headers=editor_headers,
|
|
json={"customer_name": "X Y", "content": "Linked", "project_id": 999},
|
|
)
|
|
assert missing_project.status_code == 422
|
|
|
|
public = (await client.get("/api/testimonials")).json()
|
|
assert [t["customer_name"] for t in public] == ["Mr. Das"]
|
|
assert "is_published" not in public[0]
|
|
|
|
r = await client.put("/api/admin/testimonials/2", headers=editor_headers, json={"is_published": True})
|
|
assert r.status_code == 200
|
|
assert len((await client.get("/api/testimonials")).json()) == 2
|
|
assert (await client.delete("/api/admin/testimonials/2", headers=editor_headers)).status_code == 204
|
|
|
|
|
|
async def test_site_content(client, editor_headers, admin_headers):
|
|
r = await client.put(
|
|
"/api/admin/content/hero",
|
|
headers=editor_headers,
|
|
json={"value": {"title": "Power your home with the sun", "cta": "Get a free quote"}},
|
|
)
|
|
assert r.status_code == 200
|
|
assert (await client.get("/api/content/hero")).json()["value"]["cta"] == "Get a free quote"
|
|
assert (await client.get("/api/content")).json() == {
|
|
"hero": {"title": "Power your home with the sun", "cta": "Get a free quote"}
|
|
}
|
|
assert (await client.put("/api/admin/content/Bad Key!", headers=editor_headers, json={"value": {}})).status_code == 422
|
|
assert (await client.get("/api/content/missing")).status_code == 404
|
|
assert (await client.delete("/api/admin/content/hero", headers=editor_headers)).status_code == 403
|
|
assert (await client.delete("/api/admin/content/hero", headers=admin_headers)).status_code == 204
|