Files
estimator/tests/unit/api/test_estimates_api.py
T
2026-11-09 08:43:30 +03:00

610 lines
29 KiB
Python

import time
import pytest
from fastapi.testclient import TestClient
from estimator.api.app import create_app
from estimator.config.settings import Settings
from estimator.domain.enums import Role
from estimator.domain.errors import LlmAuthError, LlmTransientError
from tests.fakes import AutoGateway
from tests.unit.agent.conftest import (
DOC_WBS,
clarify_out,
critic_out,
decompose_out,
estimate_out,
q,
)
A = {"X-API-Key": "key-a"}
B = {"X-API-Key": "key-b"}
REQ = {"requirements": "Бот для Telegram, MAX и VK с ИИ и интеграцией amoCRM"}
def make_client(monkeypatch, dsn, gateway, **env):
base = {
"EST_DATABASE__DSN": dsn,
"EST_LLM__ANTHROPIC__API_KEY": "sk-test",
"EST_API__KEYS": "client_a:key-a,client_b:key-b",
}
base.update(env)
for k, v in base.items():
monkeypatch.setenv(k, v)
return TestClient(create_app(Settings(), gateway=gateway))
@pytest.fixture
def gw():
return AutoGateway()
@pytest.fixture
def client(monkeypatch, backend_dsn, gw):
with make_client(monkeypatch, backend_dsn, gw) as c:
yield c
def wait(client, eid, headers=A, until=("done", "needs_input", "failed"), timeout=8.0, version=None):
deadline = time.time() + timeout
url = f"/api/estimates/{eid}" + (f"?version={version}" if version else "")
while time.time() < deadline:
body = client.get(url, headers=headers).json()
if body["status"] in until:
return body
time.sleep(0.02)
raise AssertionError(f"timeout, last status: {body['status']} step={body.get('step')}")
def create(client, body=None, headers=A, **kw):
r = client.post("/api/estimates", json=body or REQ, headers=headers, **kw)
assert r.status_code == 202, r.text
return r.json()
# ---------------------------------------------------------------- happy path
def test_create_then_poll_to_done_with_doc_example_numbers(client):
first = create(client)
assert first["status"] in ("pending", "running") and first["version"] == 1
assert first["region"] == "ru" and first["currency"] == "RUB" and first["result"] is None
done = wait(client, first["id"])
assert done["status"] == "done" and done["step"] is None and done["error"] is None
r = done["result"]
assert [t["id"] for t in r["tasks"]] == [f"t{i}" for i in range(1, 9)]
assert r["segment"] == "multichannel_ai" and r["market_version"] == "2026Q4"
assert r["price"]["hours"] == pytest.approx(144.7)
assert r["price"]["cost_expected"] == 336_000 and r["price"]["cost_with_reserve"] == 370_000
assert r["price"]["reserve_basis"] == "floor" and r["price"]["reserve_pct"] == pytest.approx(0.10, abs=1e-3)
assert done["engagement"] == "fixed_price"
assert r["quote"]["amount"] == 370_000 and r["quote"]["cap"] is None
assert r["market_check"]["verdict"] == "within"
assert r["warnings"] == []
assert done["summary"].startswith("Бот в Telegram")
core = next(t for t in r["tasks"] if t["id"] == "t2")
assert core["expected"] == pytest.approx(25.33) and core["role"] == "backend"
# the audit trail (prompt hashes, tokens) is not part of the API
assert "steps" not in r and "steps" not in done
def test_progress_step_is_visible_while_running(monkeypatch, backend_dsn):
gw = AutoGateway()
gate = gw.block()
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
seen = wait(c, eid, until=("running",))
assert seen["step"] == "clarify" and seen["started_at"] is not None
gate.set()
assert wait(c, eid)["status"] == "done"
def test_eu_region_uses_eu_rates_and_currency(client):
eid = create(client, {**REQ, "region": "eu"})["id"]
done = wait(client, eid)
assert done["currency"] == "EUR"
assert done["result"]["price"]["cost_with_reserve"] == 18_000 # 17 730 rounded to the configured step of 1000
assert done["result"]["price"]["cost_expected"] == 16_000 # 16 118 rounded
assert done["result"]["market_check"]["verdict"] == "within"
def test_default_region_comes_from_settings(monkeypatch, backend_dsn, gw):
with make_client(monkeypatch, backend_dsn, gw, EST_PRICING__DEFAULT_REGION="eu") as c:
assert create(c)["region"] == "eu"
def test_currency_must_match_the_region(client):
r = client.post("/api/estimates", json={**REQ, "currency": "EUR"}, headers=A)
assert r.status_code == 422 and "conversion" in r.json()["detail"]
assert client.post("/api/estimates", json={**REQ, "currency": "rub"}, headers=A).status_code == 202
# ----------------------------------------------------------------- validation
def test_auth_required_everywhere(client):
assert client.post("/api/estimates", json=REQ).status_code == 401
assert client.get("/api/estimates").status_code == 401
assert client.get("/api/estimates/x").status_code == 401
assert client.post("/api/estimates/x/answers", json={"answers": []}).status_code == 401
assert client.post("/api/estimates/x/recalculate").status_code == 401
assert client.patch("/api/estimates/x/tasks/t1", json={}).status_code == 401
def test_bad_input_is_rejected_before_anything_runs(client, gw):
assert client.post("/api/estimates", json={"requirements": ""}, headers=A).status_code == 422
assert client.post("/api/estimates", json={"requirements": " "}, headers=A).status_code == 422
assert client.post("/api/estimates", json={"requirements": "x" * 25_000}, headers=A).status_code == 422
assert client.post("/api/estimates", json={**REQ, "region": "mars"}, headers=A).status_code == 422
assert gw.calls == []
assert client.get("/api/estimates", headers=A).json()["items"] == []
# ---------------------------------------------------------------- isolation
def test_clients_cannot_see_each_other(client):
eid = create(client)["id"]
wait(client, eid)
assert client.get(f"/api/estimates/{eid}", headers=B).status_code == 404
assert client.post(f"/api/estimates/{eid}/recalculate", headers=B).status_code == 404
assert client.patch(f"/api/estimates/{eid}/tasks/t1", json={"optimistic": 1, "likely": 2, "pessimistic": 3}, headers=B).status_code == 404
assert client.get("/api/estimates", headers=B).json()["items"] == []
assert len(client.get("/api/estimates", headers=A).json()["items"]) == 1
def test_unknown_estimate_404(client):
assert client.get("/api/estimates/nope", headers=A).status_code == 404
# ---------------------------------------------------------------- idempotency
def test_idempotency_key_prevents_duplicates(client, gw):
h = {**A, "Idempotency-Key": "abc"}
first = create(client, headers=h)
second = create(client, headers=h)
assert first["id"] == second["id"]
wait(client, first["id"])
assert gw.steps().count("clarify") == 1 # the work ran once
other = client.post("/api/estimates", json={"requirements": "другое"}, headers=h)
assert other.status_code == 409
# --------------------------------------------------------- questions and answers
def test_blocking_question_then_answer_completes(monkeypatch, backend_dsn):
gw = AutoGateway(
clarify=[
clarify_out([q("crm", "Какая CRM?", blocking=True), q("tone", "Какой тон бота?")]),
clarify_out([]),
]
)
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
waiting = wait(c, eid)
assert waiting["status"] == "needs_input" and waiting["result"] is None
assert [x["id"] for x in waiting["open_questions"]] == ["crm", "tone"]
assert waiting["summary"]
assert gw.steps() == ["clarify"] # nothing expensive ran
bad = c.post(f"/api/estimates/{eid}/answers", json={"answers": [{"question_id": "zzz", "answer": "x"}]}, headers=A)
assert bad.status_code == 422
missing = c.post(f"/api/estimates/{eid}/answers", json={"answers": [{"question_id": "tone", "answer": "деловой"}]}, headers=A)
assert missing.status_code == 422 and "crm" in missing.json()["detail"]
r = c.post(f"/api/estimates/{eid}/answers", json={"answers": [{"question_id": "crm", "answer": "amoCRM"}]}, headers=A)
assert r.status_code == 202 and r.json()["version"] == 2 and r.json()["source"] == "answers"
done = wait(c, eid)
assert done["status"] == "done" and done["version"] == 2
assert "<q>Какая CRM?</q><a>amoCRM</a>" in gw.calls[1]["user"]
# answering again is not possible: nothing is waiting
again = c.post(f"/api/estimates/{eid}/answers", json={"answers": [{"question_id": "crm", "answer": "x"}]}, headers=A)
assert again.status_code == 409
# v1 stays readable as it was
assert wait(c, eid, version=1)["status"] == "needs_input"
def test_proceed_with_assumptions_skips_the_answers(monkeypatch, backend_dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)])] * 2)
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c, {**REQ, "proceed_with_assumptions": True})["id"]
done = wait(c, eid)
assert done["status"] == "done"
assert "Не уточнено: Какая CRM?" in done["result"]["assumptions"]
# ----------------------------------------------------------------- recalculate
def test_recalculate_makes_a_new_version_and_keeps_the_old(client, gw):
eid = create(client)["id"]
wait(client, eid)
r = client.post(f"/api/estimates/{eid}/recalculate", json={"requirements": "Бот только для Telegram"}, headers=A)
assert r.status_code == 202 and r.json()["version"] == 2 and r.json()["source"] == "recalc"
done = wait(client, eid)
assert done["version"] == 2 and done["latest_version"] == 2
assert "Бот только для Telegram" in gw.calls[4]["user"]
assert wait(client, eid, version=1)["status"] == "done"
assert client.post(f"/api/estimates/{eid}/recalculate", headers=A).status_code == 202 # no body is fine
def test_recalculate_while_running_is_refused(monkeypatch, backend_dsn):
gw = AutoGateway()
gate = gw.block()
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
assert c.post(f"/api/estimates/{eid}/recalculate", headers=A).status_code == 409
gate.set()
wait(c, eid)
# --------------------------------------------------------------------- editing
def test_edit_task_creates_a_repriced_manual_version(client):
eid = create(client)["id"]
before = wait(client, eid)["result"]["price"]
r = client.patch(f"/api/estimates/{eid}/tasks/t4", json={"optimistic": 16, "likely": 32, "pessimistic": 64}, headers=A)
assert r.status_code == 200, r.text
body = r.json()
assert body["version"] == 2 and body["source"] == "manual_edit" and body["status"] == "done"
task = next(t for t in body["result"]["tasks"] if t["id"] == "t4")
assert (task["optimistic"], task["likely"], task["pessimistic"]) == (16, 32, 64)
assert "Скорректировано вручную" in task["assumptions"]
assert body["result"]["price"]["cost_with_reserve"] > before["cost_with_reserve"]
assert body["result"]["market_version"] == "2026Q4"
assert any("Правка вручную" in n for n in body["result"]["critic_notes"])
# the original stays available and unchanged
old = client.get(f"/api/estimates/{eid}?version=1", headers=A).json()
assert old["result"]["price"]["cost_with_reserve"] == before["cost_with_reserve"]
def test_edit_task_validation(client):
eid = create(client)["id"]
wait(client, eid)
ok = {"optimistic": 1, "likely": 2, "pessimistic": 3}
assert client.patch(f"/api/estimates/{eid}/tasks/t99", json=ok, headers=A).status_code == 404
assert client.patch(f"/api/estimates/{eid}/tasks/t1", json={"optimistic": 5, "likely": 2, "pessimistic": 3}, headers=A).status_code == 422
assert client.patch(f"/api/estimates/{eid}/tasks/t1", json={"optimistic": -1, "likely": 2, "pessimistic": 3}, headers=A).status_code == 422
assert client.patch(f"/api/estimates/{eid}/tasks/t1", json={"optimistic": 1, "likely": 2, "pessimistic": 9999}, headers=A).status_code == 422
def test_edit_requires_a_finished_estimate(monkeypatch, backend_dsn):
gw = AutoGateway()
gate = gw.block()
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
r = c.patch(f"/api/estimates/{eid}/tasks/t1", json={"optimistic": 1, "likely": 2, "pessimistic": 3}, headers=A)
assert r.status_code == 409
gate.set()
wait(c, eid)
# ---------------------------------------------------------------------- listing
def test_list_pagination_and_status_filter(client):
ids = []
for i in range(3):
ids.append(create(client, {"requirements": f"бот {i}"})["id"])
for eid in ids:
wait(client, eid)
page1 = client.get("/api/estimates?limit=2", headers=A).json()
assert [i["id"] for i in page1["items"]] == [ids[2], ids[1]] and page1["next_cursor"]
page2 = client.get(f"/api/estimates?limit=2&cursor={page1['next_cursor']}", headers=A).json()
assert [i["id"] for i in page2["items"]] == [ids[0]] and page2["next_cursor"] is None
item = page1["items"][0]
assert item["status"] == "done" and item["cost"] == 370_000 and item["summary"]
assert client.get("/api/estimates?status=failed", headers=A).json()["items"] == []
assert client.get("/api/estimates?cursor=garbage", headers=A).status_code == 422
assert client.get("/api/estimates?limit=0", headers=A).status_code == 422
assert client.get("/api/estimates?status=weird", headers=A).status_code == 422
# --------------------------------------------------------------------- failures
def fail_case(monkeypatch, dsn, gateway, **env):
with make_client(monkeypatch, dsn, gateway, **env) as c:
return wait(c, create(c)["id"])
def test_provider_outage_is_reported_without_internals(monkeypatch, backend_dsn):
gw = AutoGateway(estimate=[LlmTransientError("503 from api.openai.com key=sk-LEAK", provider="openai")])
body = fail_case(monkeypatch, backend_dsn, gw)
assert body["status"] == "failed" and body["error"]["code"] == "llm_unavailable"
assert "sk-LEAK" not in str(body) and "openai" not in str(body)
assert body["step"] is None and body["finished_at"]
def test_auth_failure_has_its_own_code(monkeypatch, backend_dsn):
body = fail_case(monkeypatch, backend_dsn, AutoGateway(clarify=[LlmAuthError("bad key", provider="x")]))
assert body["error"]["code"] == "llm_auth"
def test_unexpected_error_is_generic(monkeypatch, backend_dsn):
body = fail_case(monkeypatch, backend_dsn, AutoGateway(clarify=[RuntimeError("secret internal detail")]))
assert body["error"]["code"] == "internal" and "secret internal detail" not in str(body)
def test_timeout(monkeypatch, backend_dsn):
gw = AutoGateway()
gw.delay = 2.0
body = fail_case(monkeypatch, backend_dsn, gw, EST_JOBS__TIMEOUT_S="1")
assert body["error"]["code"] == "timeout"
def test_roles_without_a_rate_are_blocked_up_front(monkeypatch, backend_dsn, tmp_path):
import re
import shutil
# a market file without a designer rate: the model must not be allowed to plan designer work
shutil.copytree("config", tmp_path / "config")
market = tmp_path / "config" / "market" / "2026Q4.toml"
market.write_text(re.sub(r"^designer = .*\n", "", market.read_text(), flags=re.MULTILINE))
rows = [*DOC_WBS[:-1], ("Макеты", Role.DESIGNER, 8, 16, 24)]
gw = AutoGateway(decompose=[decompose_out(rows)] * 2)
body = fail_case(monkeypatch, backend_dsn, gw, EST_CONFIG_DIR=str(tmp_path / "config"))
assert body["error"]["code"] == "pipeline_invalid" and "roles not allowed" in body["error"]["message"]
assert "allowed roles: analyst, backend, qa, devops, frontend" in gw.calls[1]["user"]
def test_frontend_and_designer_roles_are_priced(monkeypatch, backend_dsn):
rows = [*DOC_WBS[:-1], ("Админка", Role.FRONTEND, 8, 16, 24), ("Макеты", Role.DESIGNER, 4, 8, 12)]
gw = AutoGateway(decompose=[decompose_out(rows)], estimate=[estimate_out(rows)])
body = fail_case(monkeypatch, backend_dsn, gw)
assert body["status"] == "done"
by_role = {x["role"]: x for x in body["result"]["price"]["by_role"]}
assert by_role["frontend"]["rate"] == 1800 and by_role["designer"]["rate"] == 1500
assert "allowed roles: analyst, backend, qa, devops, frontend, designer" in gw.calls[1]["user"]
def test_critic_outage_still_gives_an_estimate(monkeypatch, backend_dsn):
body = fail_case(monkeypatch, backend_dsn, AutoGateway(critic=[LlmTransientError("down")]))
assert body["status"] == "done"
assert any("критиком" in w for w in body["result"]["warnings"])
def test_critic_added_task_is_priced_and_has_an_id(monkeypatch, backend_dsn):
from tests.unit.agent.conftest import missing
gw = AutoGateway(critic=[critic_out(missing=[missing("Документация", Role.ANALYST, 4, 6, 10)])])
body = fail_case(monkeypatch, backend_dsn, gw)
ids = [t["id"] for t in body["result"]["tasks"]]
assert ids[-1] == "t9" and len(ids) == 9
assert body["result"]["price"]["cost_with_reserve"] > 370_000
# ---------------------------------------------------------------- engagement model
def test_default_engagement_is_fixed_price_and_comes_from_settings(monkeypatch, backend_dsn, gw):
with make_client(monkeypatch, backend_dsn, gw) as c:
assert create(c)["engagement"] == "fixed_price"
with make_client(monkeypatch, backend_dsn, AutoGateway(), EST_PRICING__DEFAULT_ENGAGEMENT="time_and_materials") as c:
assert create(c)["engagement"] == "time_and_materials"
def test_time_and_materials_quote_has_an_expected_amount_and_a_ceiling(client):
eid = create(client, {**REQ, "engagement": "time_and_materials"})["id"]
done = wait(client, eid)
r = done["result"]
assert done["engagement"] == "time_and_materials"
assert r["quote"]["amount"] == 336_000 and r["quote"]["cap"] == 370_000
assert r["quote"]["hours"] == pytest.approx(144.7) and r["quote"]["hours_cap"] == pytest.approx(159.1)
# the underlying numbers are the same whichever way it is sold
assert r["price"]["cost_expected"] == 336_000 and r["price"]["cost_with_reserve"] == 370_000
backend = next(x for x in r["price"]["by_role"] if x["role"] == "backend")
assert backend["billing_rate"] == pytest.approx(2484) # 1800 x 1.15 x 1.20
def test_unknown_engagement_is_rejected(client):
assert client.post("/api/estimates", json={**REQ, "engagement": "hourly"}, headers=A).status_code == 422
def test_switching_engagement_is_a_new_version_without_calling_the_model(client, gw):
eid = create(client)["id"]
first = wait(client, eid)
calls_before = len(gw.calls)
r = client.post(f"/api/estimates/{eid}/engagement", json={"engagement": "time_and_materials"}, headers=A)
assert r.status_code == 200, r.text
body = r.json()
assert body["version"] == 2 and body["source"] == "reprice" and body["engagement"] == "time_and_materials"
assert body["status"] == "done" and body["result"]["quote"]["amount"] == 336_000
assert body["result"]["price"] == first["result"]["price"] # not recalculated
assert [t["id"] for t in body["result"]["tasks"]] == [t["id"] for t in first["result"]["tasks"]]
assert len(gw.calls) == calls_before
# the earlier version still says fixed price
old = client.get(f"/api/estimates/{eid}?version=1", headers=A).json()
assert old["engagement"] == "fixed_price" and old["result"]["quote"]["amount"] == 370_000
def test_switching_to_the_same_engagement_makes_no_new_version(client):
eid = create(client)["id"]
wait(client, eid)
r = client.post(f"/api/estimates/{eid}/engagement", json={"engagement": "fixed_price"}, headers=A)
assert r.status_code == 200 and r.json()["version"] == 1 and r.json()["latest_version"] == 1
def test_engagement_switch_needs_a_finished_estimate_and_ownership(monkeypatch, backend_dsn):
gw = AutoGateway()
gate = gw.block()
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
body = {"engagement": "time_and_materials"}
assert c.post(f"/api/estimates/{eid}/engagement", json=body, headers=A).status_code == 409
gate.set()
wait(c, eid)
assert c.post(f"/api/estimates/{eid}/engagement", json=body, headers=B).status_code == 404
assert c.post(f"/api/estimates/{eid}/engagement", json={"engagement": "x"}, headers=A).status_code == 422
assert c.post(f"/api/estimates/{eid}/engagement", json=body).status_code == 401
def test_engagement_is_carried_through_answers_recalculation_and_edits(monkeypatch, backend_dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)]), clarify_out([])])
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c, {**REQ, "engagement": "time_and_materials"})["id"]
assert wait(c, eid)["status"] == "needs_input"
c.post(f"/api/estimates/{eid}/answers", json={"answers": [{"question_id": "crm", "answer": "amoCRM"}]}, headers=A)
assert wait(c, eid)["engagement"] == "time_and_materials"
r = c.post(f"/api/estimates/{eid}/recalculate", headers=A).json()
assert r["engagement"] == "time_and_materials"
wait(c, eid)
edit = c.patch(f"/api/estimates/{eid}/tasks/t4", json={"optimistic": 16, "likely": 32, "pessimistic": 64}, headers=A).json()
assert edit["engagement"] == "time_and_materials" and edit["result"]["quote"]["cap"] is not None
# recalculation may change the model explicitly
again = c.post(f"/api/estimates/{eid}/recalculate", json={"engagement": "fixed_price"}, headers=A).json()
assert again["engagement"] == "fixed_price"
def test_list_shows_the_headline_amount_of_each_model(client):
fp = create(client, {"requirements": "первый"})["id"]
tm = create(client, {"requirements": "второй", "engagement": "time_and_materials"})["id"]
wait(client, fp)
wait(client, tm)
items = {i["id"]: i for i in client.get("/api/estimates", headers=A).json()["items"]}
assert items[fp]["cost"] == 370_000 and items[fp]["engagement"] == "fixed_price"
assert items[tm]["cost"] == 336_000 and items[tm]["engagement"] == "time_and_materials"
def test_idempotency_distinguishes_engagement(client):
h = {**A, "Idempotency-Key": "same-key"}
first = client.post("/api/estimates", json=REQ, headers=h)
other = client.post("/api/estimates", json={**REQ, "engagement": "time_and_materials"}, headers=h)
assert first.status_code == 202 and other.status_code == 409
def test_edit_reprices_with_the_stored_snapshot_not_todays_settings(monkeypatch, backend_dsn):
"""Change the markup after the estimate was made: a manual edit must keep the original markup."""
gw = AutoGateway()
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
first = wait(c, eid)
assert first["result"]["price"]["inputs"]["markup_pct"] == 0.20
with make_client(monkeypatch, backend_dsn, gw, EST_PRICING__MARKUP_PCT="0.60") as c: # settings changed
edit = c.patch(
f"/api/estimates/{eid}/tasks/t1", json={"optimistic": 8, "likely": 12, "pessimistic": 20}, headers=A
).json()
price = edit["result"]["price"]
assert price["inputs"]["markup_pct"] == 0.20 # the snapshot wins
assert price["cost_with_reserve"] == first["result"]["price"]["cost_with_reserve"] # same hours, same price
assert price["inputs"]["rates"]["backend"] == 1800
# while a fresh estimate does use the new setting
fresh = wait(c, create(c, {"requirements": "новый"})["id"])
assert fresh["result"]["price"]["inputs"]["markup_pct"] == 0.60
assert fresh["result"]["price"]["cost_with_reserve"] > first["result"]["price"]["cost_with_reserve"]
# ---------------------------------------------------------------------- VAT
def test_russian_client_gets_22_percent_vat_on_top_by_default(client):
done = wait(client, create(client)["id"])
q = done["result"]["quote"]
assert done["vat_rate"] == pytest.approx(0.22)
assert q["amount"] == 370_000 and q["amount_gross"] == 451_400 and q["vat_rate"] == pytest.approx(0.22)
# the calculation and the market check are done without VAT
assert done["result"]["price"]["cost_with_reserve"] == 370_000
assert done["result"]["market_check"]["verdict"] == "within"
def test_eu_client_has_no_russian_vat_by_default(client):
done = wait(client, create(client, {**REQ, "region": "eu"})["id"])
q = done["result"]["quote"]
assert done["vat_rate"] == 0 and q["amount_gross"] == q["amount"] == 18_000
def test_vat_can_be_chosen_at_creation(client):
none = wait(client, create(client, {**REQ, "vat_rate": 0})["id"])
assert none["vat_rate"] == 0 and none["result"]["quote"]["amount_gross"] == 370_000
reduced = wait(client, create(client, {"requirements": "другой", "vat_rate": 0.07})["id"])
assert reduced["result"]["quote"]["amount_gross"] == pytest.approx(395_900)
@pytest.mark.parametrize("rate", [0.2, 0.5, 0.999])
def test_a_rate_outside_the_configured_list_is_refused(client, rate):
r = client.post("/api/estimates", json={**REQ, "vat_rate": rate}, headers=A)
assert r.status_code == 422 and "not allowed" in r.json()["detail"] and "22%" in r.json()["detail"]
@pytest.mark.parametrize("rate", [-0.1, 1, 22])
def test_nonsense_vat_values_are_refused(client, rate):
assert client.post("/api/estimates", json={**REQ, "vat_rate": rate}, headers=A).status_code == 422
def test_changing_vat_is_a_new_version_without_recalculation(client, gw):
eid = create(client)["id"]
first = wait(client, eid)
calls = len(gw.calls)
r = client.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0}, headers=A)
assert r.status_code == 200, r.text
body = r.json()
assert body["version"] == 2 and body["source"] == "reprice" and body["vat_rate"] == 0
assert body["result"]["quote"]["amount_gross"] == 370_000
assert body["result"]["price"] == first["result"]["price"] and len(gw.calls) == calls
old = client.get(f"/api/estimates/{eid}?version=1", headers=A).json()
assert old["vat_rate"] == pytest.approx(0.22) and old["result"]["quote"]["amount_gross"] == 451_400
# asking for what is already set makes no new version
same = client.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0}, headers=A).json()
assert same["version"] == 2 and same["latest_version"] == 2
def test_vat_and_engagement_can_be_switched_one_after_another(client):
eid = create(client)["id"]
wait(client, eid)
client.post(f"/api/estimates/{eid}/engagement", json={"engagement": "time_and_materials"}, headers=A)
last = client.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0.10}, headers=A).json()
assert last["version"] == 3 and last["engagement"] == "time_and_materials" and last["vat_rate"] == pytest.approx(0.10)
q = last["result"]["quote"]
assert q["amount"] == 336_000 and q["amount_gross"] == pytest.approx(369_600) and q["cap_gross"] == pytest.approx(407_000)
def test_vat_switch_checks_state_ownership_and_values(monkeypatch, backend_dsn):
gw = AutoGateway()
gate = gw.block()
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
assert c.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0}, headers=A).status_code == 409
gate.set()
wait(c, eid)
assert c.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0}, headers=B).status_code == 404
assert c.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0.2}, headers=A).status_code == 422
assert c.post(f"/api/estimates/{eid}/vat", json={}, headers=A).status_code == 422
assert c.post(f"/api/estimates/{eid}/vat", json={"vat_rate": 0}).status_code == 401
def test_vat_survives_answers_recalculation_and_edits(monkeypatch, backend_dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)]), clarify_out([])])
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c, {**REQ, "vat_rate": 0.10})["id"]
assert wait(c, eid)["status"] == "needs_input"
c.post(f"/api/estimates/{eid}/answers", json={"answers": [{"question_id": "crm", "answer": "amoCRM"}]}, headers=A)
assert wait(c, eid)["vat_rate"] == pytest.approx(0.10)
assert c.post(f"/api/estimates/{eid}/recalculate", headers=A).json()["vat_rate"] == pytest.approx(0.10)
wait(c, eid)
edit = c.patch(f"/api/estimates/{eid}/tasks/t4", json={"optimistic": 16, "likely": 32, "pessimistic": 64}, headers=A).json()
assert edit["vat_rate"] == pytest.approx(0.10)
again = c.post(f"/api/estimates/{eid}/recalculate", json={"vat_rate": 0.22}, headers=A).json()
assert again["vat_rate"] == pytest.approx(0.22)
def test_the_vat_rate_is_fixed_at_creation_not_read_from_todays_settings(monkeypatch, backend_dsn):
gw = AutoGateway()
with make_client(monkeypatch, backend_dsn, gw) as c:
eid = create(c)["id"]
assert wait(c, eid)["vat_rate"] == pytest.approx(0.22)
with make_client(monkeypatch, backend_dsn, gw, EST_PRICING__VAT__DEFAULT_RATES='{"ru": 0.0, "eu": 0.0}') as c:
assert c.get(f"/api/estimates/{eid}", headers=A).json()["vat_rate"] == pytest.approx(0.22)
assert wait(c, create(c, {"requirements": "новый"})["id"])["vat_rate"] == 0
def test_idempotency_distinguishes_vat(client):
h = {**A, "Idempotency-Key": "vat-key"}
assert client.post("/api/estimates", json=REQ, headers=h).status_code == 202
assert client.post("/api/estimates", json={**REQ, "vat_rate": 0}, headers=h).status_code == 409