-
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
from email import message_from_bytes
|
||||
|
||||
from app.core.config import settings
|
||||
from app.services.quotation import DEFAULT_QUOTE_SETTINGS, calculate_quote, format_inr
|
||||
from tests.conftest import make_image
|
||||
|
||||
|
||||
def test_format_inr_indian_grouping():
|
||||
assert format_inr(59500) == "₹59,500"
|
||||
assert format_inr(197000) == "₹1,97,000"
|
||||
assert format_inr(8420000) == "₹84,20,000"
|
||||
assert format_inr(999) == "₹999"
|
||||
|
||||
|
||||
def test_quote_matches_design_rates_and_subsidy_rules():
|
||||
cfg = dict(DEFAULT_QUOTE_SETTINGS)
|
||||
q = calculate_quote(cfg, "residential", 250, "Bhubaneswar, India")
|
||||
assert q.capacity_kw == 2.5
|
||||
assert q.panel_cost == 92_500 and q.installation_cost == 22_500 # as in Figma
|
||||
assert q.subsidy == 69_000 # 2 kW x 30,000 + 0.5 kW x 18,000
|
||||
assert q.total_after_subsidy == 92_500 + 22_500 - 69_000
|
||||
|
||||
big = calculate_quote(cfg, "residential", 2000, "")
|
||||
assert big.capacity_kw == 20 and big.subsidy == 78_000 # capped
|
||||
assert big.panel_cost == 740_000 and big.installation_cost == 180_000 # as in Figma
|
||||
|
||||
com = calculate_quote(cfg, "commercial", 500, "")
|
||||
assert com.subsidy == 0 and com.total_after_subsidy == 230_000
|
||||
|
||||
clamped = calculate_quote(cfg, "residential", 999_999, "")
|
||||
assert clamped.roof_area_sqft == cfg["max_area_sqft"]
|
||||
|
||||
|
||||
async def test_quote_endpoints_and_admin_overrides(client, admin_headers):
|
||||
r = await client.post("/api/quotations/calculate", json={"property_type": "residential", "roof_area_sqft": 500})
|
||||
assert r.status_code == 200 and r.json()["capacity_kw"] == 5
|
||||
|
||||
await client.put("/api/admin/content/quote_settings", headers=admin_headers,
|
||||
json={"value": {"panel_cost_per_kw": 40000, "sqft_per_kw": "abc"}})
|
||||
s = (await client.get("/api/quotations/settings")).json()
|
||||
assert s["panel_cost_per_kw"] == 40000 and s["sqft_per_kw"] == 100 # invalid value ignored
|
||||
r = await client.post("/api/quotations/calculate", json={"roof_area_sqft": 500})
|
||||
assert r.json()["panel_cost"] == 200_000
|
||||
|
||||
bad = await client.post("/api/quotations/calculate", json={"property_type": "villa", "roof_area_sqft": 0})
|
||||
assert bad.status_code == 422
|
||||
|
||||
|
||||
async def test_quote_pdf(client):
|
||||
r = await client.post("/api/quotations/pdf", json={"property_type": "commercial", "roof_area_sqft": 750, "location": "Cuttack"})
|
||||
assert r.status_code == 200
|
||||
assert r.headers["content-type"] == "application/pdf"
|
||||
assert r.content.startswith(b"%PDF")
|
||||
|
||||
|
||||
async def test_email_quote_saves_lead_and_writes_outbox(client, admin_headers, tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(settings, "OUTBOX_DIR", tmp_path)
|
||||
r = await client.post("/api/quotations/email",
|
||||
json={"roof_area_sqft": 750, "location": "Puri", "email": "lead@example.com"})
|
||||
assert r.status_code == 200
|
||||
assert "sent to your email" in r.json()["message"]
|
||||
|
||||
files = list(tmp_path.glob("*.eml"))
|
||||
assert len(files) == 1
|
||||
msg = message_from_bytes(files[0].read_bytes())
|
||||
assert msg["To"] == "lead@example.com"
|
||||
assert any(p.get_filename() == "saura-shakti-quotation.pdf" for p in msg.walk())
|
||||
|
||||
enquiries = (await client.get("/api/admin/enquiries", headers=admin_headers)).json()
|
||||
assert enquiries["total"] == 1
|
||||
lead = enquiries["items"][0]
|
||||
assert lead["source"] == "quote_calculator" and lead["phone"] is None
|
||||
assert lead["required_capacity_kw"] == 7.5
|
||||
|
||||
# honeypot: nothing saved or sent
|
||||
await client.post("/api/quotations/email", json={"roof_area_sqft": 750, "email": "bot@example.com", "website": "x"})
|
||||
assert len(list(tmp_path.glob("*.eml"))) == 1
|
||||
bad = await client.post("/api/quotations/email", json={"roof_area_sqft": 750, "email": "nope"})
|
||||
assert bad.status_code == 422
|
||||
|
||||
|
||||
async def test_newsletter(client, admin_headers, editor_headers):
|
||||
assert (await client.post("/api/subscribe", json={"email": "Fan@Example.com"})).status_code == 201
|
||||
assert (await client.post("/api/subscribe", json={"email": "fan@example.com"})).status_code == 201 # idempotent
|
||||
assert (await client.post("/api/subscribe", json={"email": "bad"})).status_code == 422
|
||||
lst = (await client.get("/api/admin/subscribers", headers=editor_headers)).json()
|
||||
assert lst["total"] == 1 and lst["items"][0]["email"] == "fan@example.com"
|
||||
assert (await client.delete("/api/admin/subscribers/1", headers=editor_headers)).status_code == 403
|
||||
assert (await client.delete("/api/admin/subscribers/1", headers=admin_headers)).status_code == 204
|
||||
|
||||
|
||||
async def test_testimonial_video_and_image(client, editor_headers, upload_dir):
|
||||
bad = await client.post("/api/admin/testimonials", headers=editor_headers,
|
||||
json={"customer_name": "Rajesh Kumar", "video_url": "javascript:alert(1)"})
|
||||
assert bad.status_code == 422
|
||||
r = await client.post("/api/admin/testimonials", headers=editor_headers,
|
||||
json={"customer_name": "Rajesh Kumar", "designation": "Homeowner",
|
||||
"video_url": "https://www.youtube.com/watch?v=abc123"})
|
||||
assert r.status_code == 201 and r.json()["content"] is None
|
||||
tid = r.json()["id"]
|
||||
r = await client.post(f"/api/admin/testimonials/{tid}/image", headers=editor_headers,
|
||||
files={"file": ("still.jpg", make_image("JPEG"), "image/jpeg")})
|
||||
assert r.status_code == 200 and r.json()["thumbnail_url"]
|
||||
pub = (await client.get("/api/testimonials")).json()[0]
|
||||
assert pub["video_url"].startswith("https://") and pub["image_url"]
|
||||
assert (await client.delete(f"/api/admin/testimonials/{tid}", headers=editor_headers)).status_code == 204
|
||||
assert list((upload_dir / "watermarked").glob("*")) == []
|
||||
Reference in New Issue
Block a user