Init Project Flexly Estimator
This commit is contained in:
@@ -0,0 +1,792 @@
|
||||
import re
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
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.passwords import hash_password
|
||||
from tests.fakes import AutoGateway
|
||||
from tests.unit.agent.conftest import (
|
||||
DOC_WBS,
|
||||
clarify_out,
|
||||
decompose_out,
|
||||
estimate_out,
|
||||
q,
|
||||
)
|
||||
|
||||
PW = {"alex": "alex-password-1", "bob": "bob-password-22"}
|
||||
TOKEN_RE = re.compile(r'name="csrf_token" value="([^"]+)"')
|
||||
SECRET = "s" * 48
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
|
||||
|
||||
def users_env() -> str:
|
||||
return ",".join(f"{u}:{hash_password(p, iterations=1000)}" for u, p in PW.items())
|
||||
|
||||
|
||||
def make_ui(monkeypatch, dsn, gateway=None, **env):
|
||||
base = {
|
||||
"EST_DATABASE__DSN": dsn,
|
||||
"EST_LLM__ANTHROPIC__API_KEY": "sk-test",
|
||||
"EST_API__KEYS": "bot:botkey",
|
||||
"EST_WEB__ENABLED": "true",
|
||||
"EST_WEB__SESSION_SECRET": SECRET,
|
||||
"EST_WEB__USERS": users_env(),
|
||||
}
|
||||
base.update(env)
|
||||
for k, v in base.items():
|
||||
monkeypatch.setenv(k, v)
|
||||
# Secure cookies are only sent back over https, so that test client talks https
|
||||
base = "https://testserver" if env.get("EST_WEB__COOKIE_SECURE") == "true" else "http://testserver"
|
||||
return TestClient(
|
||||
create_app(Settings(), gateway=gateway or AutoGateway()), base_url=base, follow_redirects=False
|
||||
)
|
||||
|
||||
|
||||
def token(client, path="/ui/login") -> str:
|
||||
r = client.get(path)
|
||||
found = TOKEN_RE.search(r.text)
|
||||
assert found, f"no csrf token on {path}: {r.status_code}"
|
||||
return found.group(1)
|
||||
|
||||
|
||||
def login(client, user="alex", next_="/ui/estimates/new"):
|
||||
t = token(client)
|
||||
r = client.post("/ui/login", data={"username": user, "password": PW[user], "csrf_token": t, "next": next_})
|
||||
assert r.status_code == 303, r.text
|
||||
return r
|
||||
|
||||
|
||||
def page_token(client, path="/ui/estimates/new") -> str:
|
||||
return token(client, path)
|
||||
|
||||
|
||||
def create_via_ui(client, text="Бот для Telegram с ИИ", **extra):
|
||||
t = page_token(client)
|
||||
r = client.post("/ui/estimates", data={"requirements": text, "csrf_token": t, **extra})
|
||||
assert r.status_code == 303, r.text
|
||||
return r.headers["location"].rsplit("/", 1)[-1].split("?")[0]
|
||||
|
||||
|
||||
def wait_done(client, eid, timeout=8.0, until=("done", "failed", "needs_input")):
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
html = client.get(f"/ui/estimates/{eid}/panel").text
|
||||
for s in until:
|
||||
if 'class="badge' in html and _status_label(s) in html.split("</div>")[0]:
|
||||
return html
|
||||
time.sleep(0.03)
|
||||
raise AssertionError("timeout waiting for the panel")
|
||||
|
||||
|
||||
def _status_label(status):
|
||||
return {"done": "Готово", "failed": "Ошибка", "needs_input": "Нужны ответы", "running": "Считается", "pending": "В очереди"}[status]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ui(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
login(c)
|
||||
yield c
|
||||
|
||||
|
||||
# ================================================================== authentication
|
||||
|
||||
|
||||
def test_anonymous_is_sent_to_login_with_a_safe_next(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
r = c.get("/ui/estimates?status=done")
|
||||
assert r.status_code == 303
|
||||
assert r.headers["location"] == "/ui/login?next=%2Fui%2Festimates%3Fstatus%3Ddone"
|
||||
assert c.get("/ui/estimates/new").status_code == 303
|
||||
assert c.get("/ui/").status_code == 303 and "/login" in c.get("/ui/").headers["location"]
|
||||
|
||||
|
||||
def test_htmx_request_without_session_gets_hx_redirect_not_a_fragment(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
r = c.get("/ui/estimates/x/panel", headers={"HX-Request": "true"})
|
||||
assert r.status_code == 200 and r.headers["HX-Redirect"].startswith("/ui/login")
|
||||
assert r.text == ""
|
||||
|
||||
|
||||
def test_login_page_is_public_and_has_no_nav(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
r = c.get("/ui/login")
|
||||
assert r.status_code == 200 and "Пароль" in r.text and "Новая оценка" not in r.text
|
||||
|
||||
|
||||
def test_login_requires_csrf(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
r = c.post("/ui/login", data={"username": "alex", "password": PW["alex"]})
|
||||
assert r.status_code == 403
|
||||
c.get("/ui/login") # session now has a token, but the form did not send it
|
||||
assert c.post("/ui/login", data={"username": "alex", "password": PW["alex"], "csrf_token": "forged"}).status_code == 403
|
||||
|
||||
|
||||
def test_successful_login_sets_a_hardened_cookie(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn, EST_WEB__COOKIE_SECURE="true") as c:
|
||||
t = token(c)
|
||||
r = c.post("/ui/login", data={"username": "alex", "password": PW["alex"], "csrf_token": t})
|
||||
assert r.status_code == 303 and r.headers["location"] == "/ui/estimates/new"
|
||||
cookie = r.headers["set-cookie"].lower()
|
||||
assert "est_session=" in cookie and "httponly" in cookie and "samesite=lax" in cookie and "secure" in cookie
|
||||
assert "max-age=28800" in cookie
|
||||
|
||||
|
||||
def test_login_follows_safe_next_and_ignores_evil_next(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
r = login(c, next_="/ui/market")
|
||||
assert r.headers["location"] == "/ui/market"
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
r = login(c, next_="https://evil.example/")
|
||||
assert r.headers["location"] == "/ui/estimates/new"
|
||||
|
||||
|
||||
def test_wrong_password_and_unknown_user_look_the_same(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
t = token(c)
|
||||
a = c.post("/ui/login", data={"username": "alex", "password": "nope", "csrf_token": t})
|
||||
b = c.post("/ui/login", data={"username": "ghost", "password": "nope", "csrf_token": t})
|
||||
assert a.status_code == b.status_code == 401
|
||||
assert "Неверное имя пользователя или пароль" in a.text and "Неверное имя пользователя или пароль" in b.text
|
||||
assert "est_session" not in (a.headers.get("set-cookie", "").replace("est_session=;", "")) or "user" not in a.text
|
||||
|
||||
|
||||
def test_brute_force_is_throttled(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn, EST_WEB__LOGIN_MAX_ATTEMPTS="3") as c:
|
||||
t = token(c)
|
||||
for _ in range(3):
|
||||
assert c.post("/ui/login", data={"username": "alex", "password": "bad", "csrf_token": t}).status_code == 401
|
||||
r = c.post("/ui/login", data={"username": "alex", "password": PW["alex"], "csrf_token": t})
|
||||
assert r.status_code == 429 and int(r.headers["Retry-After"]) > 0
|
||||
assert "Слишком много попыток" in r.text # even the right password is refused while blocked
|
||||
|
||||
|
||||
def test_logout_ends_the_session(ui):
|
||||
t = page_token(ui)
|
||||
assert ui.get("/ui/estimates/new").status_code == 200
|
||||
assert ui.post("/ui/logout", data={}).status_code == 403 # needs CSRF too
|
||||
assert ui.post("/ui/logout", data={"csrf_token": t}).status_code == 303
|
||||
assert ui.get("/ui/estimates/new").status_code == 303
|
||||
|
||||
|
||||
def test_login_page_redirects_when_already_signed_in(ui):
|
||||
assert ui.get("/ui/login").status_code == 303
|
||||
|
||||
|
||||
def test_tampered_cookie_is_anonymous(ui):
|
||||
ui.cookies.set("est_session", "garbage.value.here", domain="testserver.local")
|
||||
ui.cookies.clear()
|
||||
assert ui.get("/ui/estimates/new").status_code == 303
|
||||
|
||||
|
||||
def test_removed_user_loses_access_even_with_a_valid_cookie(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c1:
|
||||
login(c1)
|
||||
cookie = c1.cookies.get("est_session")
|
||||
only_bob = f"bob:{hash_password(PW['bob'], iterations=1000)}"
|
||||
with make_ui(monkeypatch, dsn, EST_WEB__USERS=only_bob) as c2:
|
||||
c2.cookies.set("est_session", cookie)
|
||||
assert c2.get("/ui/estimates/new").status_code == 303
|
||||
|
||||
|
||||
# ================================================================== CSRF everywhere
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"path",
|
||||
[
|
||||
"/ui/estimates",
|
||||
"/ui/estimates/abc/answers",
|
||||
"/ui/estimates/abc/recalculate",
|
||||
"/ui/estimates/abc/tasks/t1",
|
||||
"/ui/logout",
|
||||
],
|
||||
)
|
||||
def test_state_changing_routes_reject_missing_or_wrong_csrf(ui, path):
|
||||
assert ui.post(path, data={"requirements": "x"}).status_code == 403
|
||||
assert ui.post(path, data={"requirements": "x", "csrf_token": "wrong"}).status_code == 403
|
||||
assert ui.post(path, headers={"X-CSRF-Token": "wrong"}).status_code == 403
|
||||
|
||||
|
||||
def test_csrf_error_for_htmx_is_an_alert_in_the_flash_area(ui):
|
||||
r = ui.post("/ui/estimates/abc/recalculate", headers={"HX-Request": "true"})
|
||||
assert r.status_code == 403 and r.headers["HX-Retarget"] == "#flash"
|
||||
assert "Обновите страницу" in r.text
|
||||
|
||||
|
||||
# ================================================================== the main flow
|
||||
|
||||
|
||||
def test_new_page_and_create_redirects_to_the_detail_page(ui):
|
||||
page = ui.get("/ui/estimates/new")
|
||||
assert page.status_code == 200 and "Описание задачи" in page.text and "Новая оценка" in page.text
|
||||
eid = create_via_ui(ui)
|
||||
assert len(eid) == 32
|
||||
|
||||
|
||||
def test_result_page_shows_the_numbers_from_the_doc_example(ui):
|
||||
eid = create_via_ui(ui, "Бот Telegram, MAX и VK с ИИ и amoCRM")
|
||||
html = wait_done(ui, eid)
|
||||
assert "370\u202f000\u00a0₽" in html # the fixed price, reserve included
|
||||
assert "336\u202f000\u00a0₽" in html # without the reserve
|
||||
assert "397\u202f000" not in html # the old double-counted figure
|
||||
assert "144,7\u00a0ч" in html and "16,7%" in html # hours and the margin implied by a 20% markup
|
||||
assert "минимальный резерв" in html and "Фиксированная цена" in html
|
||||
assert "В границах рынка" in html and "badge ok" in html
|
||||
assert html.count('<tr id="task-t') == 8 and "Интеграция amoCRM" in html
|
||||
assert "Backend" in html and "Аналитик" in html
|
||||
assert "Мультиканальный ИИ-агент" in html
|
||||
assert "<html" not in html # the panel is a fragment, not a page
|
||||
|
||||
|
||||
def test_detail_page_is_a_full_document_with_the_panel(ui):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
page = ui.get(f"/ui/estimates/{eid}")
|
||||
assert page.status_code == 200 and "<html" in page.text and 'id="panel"' in page.text
|
||||
assert "hx-headers" in page.text # CSRF header for htmx requests
|
||||
|
||||
|
||||
def test_polling_runs_while_calculating_and_stops_when_done(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
gate = gw.block()
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
running = c.get(f"/ui/estimates/{eid}/panel").text
|
||||
assert f'hx-get="/ui/estimates/{eid}/panel"' in running and 'hx-trigger="every 2s"' in running
|
||||
assert "progress" in running
|
||||
gate.set()
|
||||
done = wait_done(c, eid)
|
||||
assert "hx-trigger" not in done.split("<table")[0] # the root element stops polling
|
||||
|
||||
|
||||
def test_poll_interval_comes_from_settings(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
gw.block()
|
||||
with make_ui(monkeypatch, dsn, gw, EST_WEB__POLL_INTERVAL_S="5") as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
assert 'hx-trigger="every 5s"' in c.get(f"/ui/estimates/{eid}/panel").text
|
||||
|
||||
|
||||
def test_double_submit_with_the_same_form_makes_one_estimate(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
t = page_token(c)
|
||||
body = {"requirements": "бот", "csrf_token": t, "request_id": "same-form-1"}
|
||||
a = c.post("/ui/estimates", data=body)
|
||||
b = c.post("/ui/estimates", data=body)
|
||||
assert a.headers["location"] == b.headers["location"]
|
||||
wait_done(c, a.headers["location"].rsplit("/", 1)[-1])
|
||||
assert gw.steps().count("clarify") == 1
|
||||
|
||||
|
||||
def test_region_choice_changes_currency_and_price(ui):
|
||||
eid = create_via_ui(ui, "бот", region="eu")
|
||||
html = wait_done(ui, eid)
|
||||
assert "18\u202f000\u00a0€" in html and "ЕС" in html
|
||||
|
||||
|
||||
def test_validation_error_keeps_what_the_user_typed(ui):
|
||||
t = page_token(ui)
|
||||
r = ui.post("/ui/estimates", data={"requirements": " ", "csrf_token": t})
|
||||
assert r.status_code == 422 and "Опишите задачу" in r.text
|
||||
r2 = ui.post("/ui/estimates", data={"requirements": "x" * 25000, "csrf_token": t})
|
||||
assert r2.status_code == 422 and "слишком длинное" in r2.text and "x" * 100 in r2.text
|
||||
|
||||
|
||||
# ================================================================== questions
|
||||
|
||||
|
||||
def test_blocking_questions_are_shown_and_answers_complete_the_estimate(monkeypatch, dsn):
|
||||
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)]), clarify_out([])])
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
html = wait_done(c, eid, until=("needs_input",))
|
||||
assert "Какая CRM?" in html and "нужно для оценки" in html and 'name="a_crm"' in html
|
||||
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
empty = c.post(f"/ui/estimates/{eid}/answers", data={"csrf_token": t})
|
||||
assert empty.status_code == 422 and "Ответьте хотя бы на один вопрос" in empty.text
|
||||
|
||||
ok = c.post(f"/ui/estimates/{eid}/answers", data={"csrf_token": t, "a_crm": "amoCRM"})
|
||||
assert ok.status_code == 303
|
||||
assert "Готово" in wait_done(c, eid)
|
||||
assert "<q>Какая CRM?</q><a>amoCRM</a>" in gw.calls[1]["user"]
|
||||
# the earlier version is still there and still asks the question
|
||||
old = c.get(f"/ui/estimates/{eid}?version=1").text
|
||||
assert "версия 1" in old and "Открыть последнюю" in old and 'name="a_crm"' not in old
|
||||
|
||||
|
||||
def test_proceed_without_answers_continues_on_assumptions(monkeypatch, dsn):
|
||||
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)])] * 2)
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
wait_done(c, eid, until=("needs_input",))
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
assert c.post(f"/ui/estimates/{eid}/answers", data={"csrf_token": t, "proceed": "1"}).status_code == 303
|
||||
html = wait_done(c, eid)
|
||||
assert "Не уточнено: Какая CRM?" in html
|
||||
|
||||
|
||||
# ================================================================== recalculation and editing
|
||||
|
||||
|
||||
def test_recalculate_with_changed_requirements_makes_version_2(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c, "Первое описание")
|
||||
wait_done(c, eid)
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
r = c.post(f"/ui/estimates/{eid}/recalculate", data={"csrf_token": t, "requirements": "Второе описание"})
|
||||
assert r.status_code == 303
|
||||
html = wait_done(c, eid)
|
||||
assert "версия 2" in html and "пересчёт" in html and "Второе описание" in html
|
||||
assert f'href="/ui/estimates/{eid}?version=1"' in html
|
||||
assert "Второе описание" in gw.calls[4]["user"]
|
||||
|
||||
|
||||
def test_recalculate_with_unchanged_text_sends_no_requirements(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c, "Описание")
|
||||
wait_done(c, eid)
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
assert c.post(f"/ui/estimates/{eid}/recalculate", data={"csrf_token": t, "requirements": "Описание"}).status_code == 303
|
||||
wait_done(c, eid)
|
||||
assert c.get(f"/ui/estimates/{eid}").text.count("версия 2") >= 1
|
||||
|
||||
|
||||
def test_recalculate_while_running_shows_a_russian_error(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
gate = gw.block()
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
r = c.post(f"/ui/estimates/{eid}/recalculate", data={"csrf_token": t}, headers={"HX-Request": "true"})
|
||||
assert r.status_code == 409 and r.headers["HX-Retarget"] == "#flash"
|
||||
assert "ещё считается" in r.text
|
||||
plain = c.post(f"/ui/estimates/{eid}/recalculate", data={"csrf_token": t})
|
||||
assert plain.status_code == 409 and "<html" in plain.text and "ещё считается" in plain.text
|
||||
gate.set()
|
||||
wait_done(c, eid)
|
||||
|
||||
|
||||
def test_inline_task_edit_flow(ui):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
base = f"/ui/estimates/{eid}/tasks/t4"
|
||||
|
||||
row = ui.get(base).text
|
||||
assert row.lstrip().startswith("<tr") and "Интеграция amoCRM" in row and "Изменить" in row
|
||||
edit = ui.get(base + "/edit").text
|
||||
assert 'name="likely" value="16"' in edit and "Сохранить" in edit and "Отмена" in edit
|
||||
|
||||
t = page_token(ui, f"/ui/estimates/{eid}")
|
||||
hx = {"HX-Request": "true", "X-CSRF-Token": t}
|
||||
saved = ui.post(base, data={"optimistic": "16", "likely": "32,5", "pessimistic": "64"}, headers=hx)
|
||||
assert saved.status_code == 200 and saved.headers["HX-Redirect"] == f"/ui/estimates/{eid}"
|
||||
html = wait_done(ui, eid)
|
||||
assert "версия 2" in html and "ручная правка" in html and "32,5" in html
|
||||
assert "Скорректировано вручную" in html
|
||||
assert "370\u202f000\u00a0₽" not in html # repriced
|
||||
# without htmx the same form is a plain post with a redirect
|
||||
plain = ui.post(base, data={"optimistic": "1", "likely": "2", "pessimistic": "3", "csrf_token": t})
|
||||
assert plain.status_code == 303
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fields,message",
|
||||
[
|
||||
({"optimistic": "abc", "likely": "2", "pessimistic": "3"}, "введите число"),
|
||||
({"optimistic": "-1", "likely": "2", "pessimistic": "3"}, "неотрицательным"),
|
||||
({"optimistic": "5", "likely": "2", "pessimistic": "3"}, "оптимистично ≤ вероятно ≤ пессимистично"),
|
||||
({"optimistic": "1", "likely": "2", "pessimistic": "99999"}, "допустимого предела"),
|
||||
],
|
||||
)
|
||||
def test_bad_task_edit_shows_a_message_and_changes_nothing(ui, fields, message):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
t = page_token(ui, f"/ui/estimates/{eid}")
|
||||
r = ui.post(f"/ui/estimates/{eid}/tasks/t1", data=fields, headers={"HX-Request": "true", "X-CSRF-Token": t})
|
||||
assert r.status_code == 422 and message in r.text and r.headers["HX-Retarget"] == "#flash"
|
||||
assert "версия 2" not in ui.get(f"/ui/estimates/{eid}/panel").text
|
||||
|
||||
|
||||
def test_unknown_task_is_404(ui):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
assert ui.get(f"/ui/estimates/{eid}/tasks/t99").status_code == 404
|
||||
assert ui.get(f"/ui/estimates/{eid}/tasks/t99/edit").status_code == 404
|
||||
|
||||
|
||||
# ================================================================== failures
|
||||
|
||||
|
||||
def test_failed_estimate_shows_the_reason_and_a_retry_button(monkeypatch, dsn):
|
||||
from estimator.domain.errors import LlmTransientError
|
||||
|
||||
gw = AutoGateway(estimate=[LlmTransientError("down")])
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
html = wait_done(c, eid, until=("failed",))
|
||||
assert "временно недоступен" in html and "llm_unavailable" in html and "Повторить расчёт" in html
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
assert c.post(f"/ui/estimates/{eid}/recalculate", data={"csrf_token": t, "requirements": ""}).status_code == 303
|
||||
assert "Готово" in wait_done(c, eid)
|
||||
|
||||
|
||||
def test_api_5xx_is_not_leaked_to_the_browser(ui, monkeypatch):
|
||||
from estimator.web.client import ApiError, EstimatorApi
|
||||
|
||||
async def boom(self, *a, **k):
|
||||
raise ApiError(500, "Traceback: secret internals")
|
||||
|
||||
monkeypatch.setattr(EstimatorApi, "list", boom)
|
||||
r = ui.get("/ui/estimates")
|
||||
assert r.status_code == 502 and "Сервис временно недоступен" in r.text and "secret internals" not in r.text
|
||||
|
||||
|
||||
def test_unknown_estimate_is_a_404_page(ui):
|
||||
r = ui.get("/ui/estimates/does-not-exist")
|
||||
assert r.status_code == 404 and "Не найдено" in r.text and "<html" in r.text
|
||||
hx = ui.get("/ui/estimates/does-not-exist/panel", headers={"HX-Request": "true"})
|
||||
assert hx.status_code == 404 and hx.headers["HX-Retarget"] == "#flash"
|
||||
assert ui.get("/ui/estimates/x?version=0").status_code in (404, 422)
|
||||
|
||||
|
||||
# ================================================================== XSS and injection
|
||||
|
||||
|
||||
EVIL = '<script>alert("xss")</script>'
|
||||
EVIL2 = '"><img src=x onerror=alert(1)>'
|
||||
|
||||
|
||||
def test_untrusted_text_is_escaped_everywhere(monkeypatch, dsn):
|
||||
rows = [(f"{EVIL} Аналитика", Role.ANALYST, 8, 12, 20), *DOC_WBS[1:]]
|
||||
est = estimate_out(rows)
|
||||
est.tasks[0].assumptions = [EVIL2]
|
||||
est.tasks[0].risks = ["<b>risk</b>"]
|
||||
gw = AutoGateway(
|
||||
clarify=[clarify_out(summary=f"Сводка {EVIL}")],
|
||||
decompose=[decompose_out(rows)],
|
||||
estimate=[est],
|
||||
)
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c, f"Требования </textarea>{EVIL}")
|
||||
html = wait_done(c, eid)
|
||||
page = c.get(f"/ui/estimates/{eid}").text
|
||||
for doc in (html, page):
|
||||
assert EVIL not in doc and EVIL2 not in doc and "<b>risk</b>" not in doc
|
||||
assert "<script>" in doc
|
||||
assert "</textarea>" in page # the requirements cannot close their own textarea
|
||||
history = c.get("/ui/estimates").text
|
||||
assert EVIL not in history and "<script>alert" not in history
|
||||
|
||||
|
||||
def test_questions_from_the_model_are_escaped(monkeypatch, dsn):
|
||||
gw = AutoGateway(clarify=[clarify_out([q("x", f"Вопрос {EVIL}", blocking=True)])])
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
html = wait_done(c, eid, until=("needs_input",))
|
||||
assert EVIL not in html and "<script>" in html
|
||||
|
||||
|
||||
def test_error_messages_are_escaped_in_alerts(ui):
|
||||
t = page_token(ui)
|
||||
r = ui.post(
|
||||
f"/ui/estimates/{EVIL}/answers", data={"csrf_token": t, "a_x": "y"}, headers={"HX-Request": "true"}
|
||||
)
|
||||
assert EVIL not in r.text
|
||||
|
||||
|
||||
def test_security_headers(ui):
|
||||
for path in ("/ui/estimates/new", "/ui/login", "/ui/market", "/ui/estimates/nope"):
|
||||
r = ui.get(path)
|
||||
csp = r.headers["content-security-policy"]
|
||||
assert "script-src 'self'" in csp and "frame-ancestors 'none'" in csp and "default-src 'none'" in csp
|
||||
assert "unsafe-inline" not in csp and "unsafe-eval" not in csp
|
||||
assert r.headers["x-content-type-options"] == "nosniff"
|
||||
assert r.headers["cache-control"] == "no-store"
|
||||
assert "content-security-policy" not in ui.get("/health").headers
|
||||
|
||||
|
||||
def test_pages_have_no_inline_scripts_or_styles(ui):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
for path in ("/ui/estimates/new", f"/ui/estimates/{eid}", "/ui/estimates", "/ui/market"):
|
||||
html = ui.get(path).text
|
||||
assert not re.search(r"<script(?![^>]*\bsrc=)", html), path
|
||||
assert " style=" not in html and "<style" not in html and "javascript:" not in html
|
||||
|
||||
|
||||
def test_templates_load_nothing_from_other_hosts():
|
||||
for f in (ROOT / "src/estimator/web/templates").rglob("*.html"):
|
||||
assert not re.search(r"https?://", f.read_text()), f
|
||||
css = (ROOT / "src/estimator/web/static/app.css").read_text()
|
||||
assert "http" not in css and "@import" not in css
|
||||
|
||||
|
||||
def test_static_files_are_served_and_cacheable(ui):
|
||||
js = ui.get("/ui/static/htmx.min.js")
|
||||
assert js.status_code == 200 and "htmx" in js.text[:200] and js.headers.get("cache-control") != "no-store"
|
||||
assert ui.get("/ui/static/app.css").status_code == 200
|
||||
assert ui.get("/ui/static/../../config/default.toml").status_code in (404, 400)
|
||||
|
||||
|
||||
# ================================================================== isolation and secrets
|
||||
|
||||
|
||||
def test_ui_and_api_clients_do_not_see_each_other(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
login(c)
|
||||
ui_id = create_via_ui(c)
|
||||
wait_done(c, ui_id)
|
||||
bot = c.post("/api/estimates", json={"requirements": "бот от клиента API"}, headers={"X-API-Key": "botkey"}).json()
|
||||
# the API client sees none of the UI's estimates and the reverse
|
||||
assert c.get("/api/estimates", headers={"X-API-Key": "botkey"}).json()["items"][0]["id"] == bot["id"]
|
||||
assert c.get(f"/api/estimates/{ui_id}", headers={"X-API-Key": "botkey"}).status_code == 404
|
||||
assert c.get(f"/ui/estimates/{bot['id']}").status_code == 404
|
||||
assert bot["id"][:8] not in c.get("/ui/estimates").text
|
||||
|
||||
|
||||
def test_shared_workspace_lets_users_see_each_others_estimates(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as alex, make_ui(monkeypatch, dsn) as bob:
|
||||
login(alex, "alex")
|
||||
login(bob, "bob")
|
||||
eid = create_via_ui(alex)
|
||||
wait_done(alex, eid)
|
||||
assert bob.get(f"/ui/estimates/{eid}").status_code == 200
|
||||
assert eid[:8] in bob.get("/ui/estimates").text
|
||||
|
||||
|
||||
def test_per_user_workspace_separates_users(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn, EST_WEB__WORKSPACE="per_user") as alex:
|
||||
login(alex, "alex")
|
||||
eid = create_via_ui(alex)
|
||||
wait_done(alex, eid)
|
||||
bob_app = make_ui(monkeypatch, dsn, EST_WEB__WORKSPACE="per_user")
|
||||
with bob_app as bob:
|
||||
login(bob, "bob")
|
||||
assert bob.get(f"/ui/estimates/{eid}").status_code == 404
|
||||
assert eid[:8] not in bob.get("/ui/estimates").text
|
||||
assert alex.get(f"/ui/estimates/{eid}").status_code == 200
|
||||
|
||||
|
||||
def test_internal_api_tokens_never_reach_the_browser(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
bodies = [c.get(p) for p in ("/ui/estimates/new", f"/ui/estimates/{eid}", "/ui/estimates", "/ui/market")]
|
||||
tokens = list(c.app.state.ui_tokens.values())
|
||||
assert tokens and all(len(t) >= 32 for t in tokens)
|
||||
blob = " ".join(r.text + str(r.headers) for r in bodies) + str(c.cookies)
|
||||
assert not any(t in blob for t in tokens)
|
||||
# and the API does not accept a guessed or empty key
|
||||
assert c.get("/api/estimates", headers={"X-API-Key": "ui"}).status_code == 401
|
||||
|
||||
|
||||
def test_session_cookie_does_not_contain_the_password_or_hash(ui):
|
||||
cookie = ui.cookies.get("est_session")
|
||||
assert PW["alex"] not in cookie and "pbkdf2" not in cookie
|
||||
|
||||
|
||||
# ================================================================== history and market
|
||||
|
||||
|
||||
def test_history_lists_filters_and_pages(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn, EST_WEB__PAGE_SIZE="2") as c:
|
||||
login(c)
|
||||
ids = []
|
||||
for i in range(3):
|
||||
ids.append(create_via_ui(c, f"бот {i}"))
|
||||
for eid in ids:
|
||||
wait_done(c, eid)
|
||||
page = c.get("/ui/estimates")
|
||||
assert page.status_code == 200 and "370\u202f000\u00a0₽" in page.text
|
||||
assert ids[2][:8] in page.text and ids[1][:8] in page.text and ids[0][:8] not in page.text
|
||||
more = re.search(r'hx-get="(/ui/estimates\?cursor=[^"]+)"', page.text)
|
||||
assert more and "Показать ещё" in page.text
|
||||
rows = c.get(more.group(1).replace("&", "&"), headers={"HX-Request": "true"})
|
||||
assert ids[0][:8] in rows.text and "<html" not in rows.text and "Показать ещё" not in rows.text
|
||||
assert "Оценок пока нет" in c.get("/ui/estimates?status=failed").text
|
||||
assert "Оценок пока нет" not in c.get("/ui/estimates?status=weird").text # unknown filter is ignored
|
||||
|
||||
|
||||
def test_empty_history_invites_to_create(ui):
|
||||
assert "Оценок пока нет" in ui.get("/ui/estimates").text
|
||||
|
||||
|
||||
def test_market_page_shows_rates_and_benchmarks(ui):
|
||||
html = ui.get("/ui/market").text
|
||||
assert "2026Q4" in html and "1\u202f800\u00a0₽" in html and "85\u00a0€" in html
|
||||
assert "400\u202f000\u00a0₽" in html and "Мультиканальный ИИ-агент" in html
|
||||
assert "стартовые" in html.lower() or "Starting priors" in html
|
||||
|
||||
|
||||
def test_timezone_setting_is_applied(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn, EST_WEB__TIMEZONE="Pacific/Kiritimati") as c: # UTC+14, no DST
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
html = wait_done(c, eid)
|
||||
created = re.search(r"создана (\d\d)\.(\d\d)\.(\d{4}) (\d\d):(\d\d)", html)
|
||||
assert created
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
shown = datetime(int(created[3]), int(created[2]), int(created[1]), int(created[4]), int(created[5]))
|
||||
assert abs((shown - (datetime.now(UTC).replace(tzinfo=None) + timedelta(hours=14))).total_seconds()) < 300
|
||||
|
||||
|
||||
def test_ui_is_absent_when_disabled(monkeypatch, dsn):
|
||||
for k, v in {"EST_DATABASE__DSN": dsn, "EST_LLM__ANTHROPIC__API_KEY": "sk-test"}.items():
|
||||
monkeypatch.setenv(k, v)
|
||||
with TestClient(create_app(Settings(), gateway=AutoGateway())) as c:
|
||||
assert c.get("/ui/login").status_code == 404 and c.get("/ui/static/app.css").status_code == 404
|
||||
assert c.get("/health").status_code == 200
|
||||
|
||||
|
||||
|
||||
# ================================================================== engagement model in the UI
|
||||
|
||||
|
||||
def test_new_form_offers_the_engagement_choice_with_the_configured_default(monkeypatch, dsn):
|
||||
with make_ui(monkeypatch, dsn) as c:
|
||||
login(c)
|
||||
html = c.get("/ui/estimates/new").text
|
||||
assert 'name="engagement"' in html and "Time & Materials" in html
|
||||
assert re.search(r'value="fixed_price"\s+selected', html)
|
||||
with make_ui(monkeypatch, dsn, EST_PRICING__DEFAULT_ENGAGEMENT="time_and_materials") as c:
|
||||
login(c)
|
||||
assert re.search(r'value="time_and_materials"\s+selected', c.get("/ui/estimates/new").text)
|
||||
|
||||
|
||||
def test_time_and_materials_is_shown_with_a_ceiling_and_client_rates(ui):
|
||||
eid = create_via_ui(ui, "бот", engagement="time_and_materials")
|
||||
html = wait_done(ui, eid)
|
||||
assert "Time & Materials" in html
|
||||
assert "336\u202f000\u00a0₽" in html and "Потолок бюджета" in html and "370\u202f000\u00a0₽" in html
|
||||
assert "закрепить в договоре" in html # the advice to cap the budget
|
||||
assert "2\u202f484\u00a0₽" in html # the backend's client rate: 1800 x 1.15 x 1.20
|
||||
assert "Показать как фиксированную цену" in html
|
||||
|
||||
|
||||
def test_engagement_can_be_switched_in_the_ui_without_a_new_calculation(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c)
|
||||
wait_done(c, eid)
|
||||
calls = len(gw.calls)
|
||||
assert "Показать как Time & Materials" in c.get(f"/ui/estimates/{eid}").text
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
r = c.post(f"/ui/estimates/{eid}/engagement", data={"csrf_token": t, "engagement": "time_and_materials"})
|
||||
assert r.status_code == 303
|
||||
html = c.get(f"/ui/estimates/{eid}").text
|
||||
assert "версия 2" in html and "смена модели оплаты" in html and "Потолок бюджета" in html
|
||||
assert len(gw.calls) == calls
|
||||
assert "Показать как фиксированную цену" in html
|
||||
assert "Фиксированная цена" in c.get(f"/ui/estimates/{eid}?version=1").text
|
||||
# an unknown value is a message, not an error page
|
||||
bad = c.post(f"/ui/estimates/{eid}/engagement", data={"csrf_token": t, "engagement": "hourly"}, headers={"HX-Request": "true"})
|
||||
assert bad.status_code == 422 and bad.headers["HX-Retarget"] == "#flash"
|
||||
|
||||
|
||||
def test_engagement_switch_requires_csrf(ui):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
assert ui.post(f"/ui/estimates/{eid}/engagement", data={"engagement": "time_and_materials"}).status_code == 403
|
||||
|
||||
|
||||
def test_history_shows_the_model_next_to_the_amount(ui):
|
||||
fp = create_via_ui(ui, "один")
|
||||
tm = create_via_ui(ui, "два", engagement="time_and_materials")
|
||||
wait_done(ui, fp)
|
||||
wait_done(ui, tm)
|
||||
html = ui.get("/ui/estimates").text
|
||||
assert "370\u202f000\u00a0₽" in html and "336\u202f000\u00a0₽" in html
|
||||
assert "Фиксированная цена" in html and "Time & Materials" in html
|
||||
|
||||
|
||||
# ================================================================== VAT in the UI
|
||||
|
||||
|
||||
def test_result_shows_the_price_without_vat_and_the_amount_to_pay_with_it(ui):
|
||||
html = wait_done(ui, create_via_ui(ui))
|
||||
assert "370\u202f000\u00a0₽" in html and "цена для клиента без НДС" in html
|
||||
assert "К оплате с НДС 22%: 451\u202f400\u00a0₽" in html and "НДС 81\u202f400\u00a0₽" in html
|
||||
|
||||
|
||||
def test_eu_client_shows_no_vat(ui):
|
||||
html = wait_done(ui, create_via_ui(ui, "бот", region="eu"))
|
||||
assert "НДС не применяется" in html and "К оплате с НДС" not in html
|
||||
|
||||
|
||||
def test_new_form_lists_the_vat_choices_from_the_config(ui):
|
||||
html = ui.get("/ui/estimates/new").text
|
||||
assert 'name="vat"' in html and "По региону клиента (РФ 22%, ЕС без НДС)" in html
|
||||
assert 'value="0"' in html and "Без НДС" in html
|
||||
for rate in ("22%", "10%", "7%", "5%"):
|
||||
assert f"{rate} сверху" in html
|
||||
|
||||
|
||||
def test_vat_can_be_chosen_when_creating_and_switched_later(monkeypatch, dsn):
|
||||
gw = AutoGateway()
|
||||
with make_ui(monkeypatch, dsn, gw) as c:
|
||||
login(c)
|
||||
eid = create_via_ui(c, "бот", vat="0")
|
||||
html = wait_done(c, eid)
|
||||
assert "НДС не применяется" in html
|
||||
calls = len(gw.calls)
|
||||
t = page_token(c, f"/ui/estimates/{eid}")
|
||||
assert c.post(f"/ui/estimates/{eid}/vat", data={"csrf_token": t, "vat": "0.22"}).status_code == 303
|
||||
page = c.get(f"/ui/estimates/{eid}").text
|
||||
assert "версия 2" in page and "смена модели оплаты" in page
|
||||
assert "К оплате с НДС 22%: 451\u202f400\u00a0₽" in page
|
||||
assert re.search(r'<option value="0\.22"\s+selected', page)
|
||||
assert len(gw.calls) == calls # no new calculation
|
||||
assert "НДС не применяется" in c.get(f"/ui/estimates/{eid}?version=1").text
|
||||
|
||||
|
||||
def test_bad_vat_values_are_messages_not_crashes(ui):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
t = page_token(ui, f"/ui/estimates/{eid}")
|
||||
hx = {"HX-Request": "true"}
|
||||
for bad in ("", "abc", "0.2", "5"):
|
||||
r = ui.post(f"/ui/estimates/{eid}/vat", data={"csrf_token": t, "vat": bad}, headers=hx)
|
||||
assert r.status_code == 422 and r.headers["HX-Retarget"] == "#flash", bad
|
||||
page = ui.post("/ui/estimates", data={"requirements": "бот", "vat": "0.2", "csrf_token": page_token(ui)})
|
||||
assert page.status_code == 422 and "Ставка НДС 20% не разрешена" in page.text and "22%" in page.text
|
||||
assert "not allowed" not in page.text # the message is shown in Russian
|
||||
|
||||
|
||||
def test_vat_switch_requires_csrf(ui):
|
||||
eid = create_via_ui(ui)
|
||||
wait_done(ui, eid)
|
||||
assert ui.post(f"/ui/estimates/{eid}/vat", data={"vat": "0"}).status_code == 403
|
||||
|
||||
|
||||
def test_time_and_materials_shows_vat_on_the_amount_and_the_ceiling(ui):
|
||||
html = wait_done(ui, create_via_ui(ui, "бот", engagement="time_and_materials"))
|
||||
assert "С НДС 22%: ожидаемо 409\u202f920\u00a0₽, потолок 451\u202f400\u00a0₽" in html
|
||||
|
||||
|
||||
def test_market_check_card_says_what_it_compares(ui):
|
||||
html = wait_done(ui, create_via_ui(ui))
|
||||
assert "ценой с резервом без НДС" in html and "(без НДС)" in html
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from estimator.config.settings import Settings
|
||||
from estimator.passwords import hash_password, is_valid_hash, verify_password
|
||||
from estimator.web import format as fmt
|
||||
from estimator.web.client import ApiError
|
||||
from estimator.web.routes.estimates import parse_hours
|
||||
from estimator.web.security import LoginLimiter, safe_next
|
||||
|
||||
FAST = {"iterations": 1000}
|
||||
|
||||
|
||||
def test_password_roundtrip_and_wrong_password():
|
||||
h = hash_password("correct horse battery", **FAST)
|
||||
assert verify_password("correct horse battery", h)
|
||||
assert not verify_password("wrong", h) and not verify_password("", h)
|
||||
assert h.startswith("pbkdf2_sha256$1000$") and "correct" not in h
|
||||
|
||||
|
||||
def test_hashes_are_salted():
|
||||
assert hash_password("same", **FAST) != hash_password("same", **FAST)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad", ["", "plain", "md5$1$a$b", "pbkdf2_sha256$x$a$b", "pbkdf2_sha256$0$a$b", "a$b$c"])
|
||||
def test_malformed_hash_never_verifies(bad):
|
||||
assert not is_valid_hash(bad) and not verify_password("x", bad)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("target", [
|
||||
"/ui/estimates/abc", "/ui/estimates?status=done", "/ui/market",
|
||||
])
|
||||
def test_safe_next_allows_local_ui_paths(target):
|
||||
assert safe_next(target) == target
|
||||
|
||||
|
||||
@pytest.mark.parametrize("target", [
|
||||
None, "", "https://evil.example/ui/x", "//evil.example/ui/x", "/api/estimates", "javascript:alert(1)",
|
||||
"/ui/..\\evil", "/ui/x\nSet-Cookie: a=b", "ui/relative", "///ui/x",
|
||||
])
|
||||
def test_safe_next_rejects_everything_else(target):
|
||||
assert safe_next(target) == "/ui/estimates/new"
|
||||
|
||||
|
||||
def test_limiter_blocks_after_max_and_expires():
|
||||
now = [0.0]
|
||||
lim = LoginLimiter(3, 60, clock=lambda: now[0])
|
||||
for _ in range(3):
|
||||
assert lim.retry_after("1.1.1.1", "bob") == 0
|
||||
lim.record_failure("1.1.1.1", "bob")
|
||||
assert 0 < lim.retry_after("1.1.1.1", "bob") <= 61
|
||||
assert lim.retry_after("1.1.1.1", "alice") == 0 # another user from the same address is fine
|
||||
assert lim.retry_after("2.2.2.2", "bob") == 0 # same user from another address too
|
||||
now[0] = 61
|
||||
assert lim.retry_after("1.1.1.1", "bob") == 0
|
||||
|
||||
|
||||
def test_limiter_blocks_an_address_spraying_many_users():
|
||||
lim = LoginLimiter(2, 60, clock=lambda: 0.0)
|
||||
for i in range(8):
|
||||
lim.record_failure("9.9.9.9", f"user{i}")
|
||||
assert lim.retry_after("9.9.9.9", "someone-new") > 0
|
||||
|
||||
|
||||
def test_limiter_success_resets_the_user_counter():
|
||||
lim = LoginLimiter(2, 60, clock=lambda: 0.0)
|
||||
lim.record_failure("1.1.1.1", "bob")
|
||||
lim.reset("1.1.1.1", "bob")
|
||||
assert lim.retry_after("1.1.1.1", "bob") == 0
|
||||
|
||||
|
||||
def test_limiter_memory_is_bounded():
|
||||
lim = LoginLimiter(3, 60, clock=lambda: 0.0)
|
||||
for i in range(12_000):
|
||||
lim.record_failure(f"ip{i}", "u")
|
||||
assert len(lim._fails) <= 20_000
|
||||
|
||||
|
||||
def test_money_hours_num_formatting():
|
||||
assert fmt.money(370000, "RUB") == "370\u202f000\u00a0₽"
|
||||
assert fmt.money(17730.4, "EUR") == "17\u202f730\u00a0€"
|
||||
assert fmt.money(None, "RUB") == "—" and fmt.money(5, "XYZ") == "5\u00a0XYZ"
|
||||
assert fmt.hours(144.7) == "144,7\u00a0ч" and fmt.hours(12.0) == "12\u00a0ч"
|
||||
assert fmt.num(25.3333) == "25,33" and fmt.num(8.0) == "8" and fmt.num(1.5) == "1,5"
|
||||
|
||||
|
||||
def test_datetime_filter_uses_the_configured_zone():
|
||||
dt = fmt.make_datetime_filter("Europe/Helsinki")
|
||||
assert dt("2026-10-30T12:00:00+00:00") == "30.10.2026 14:00" # EET, UTC+2 in late October
|
||||
assert dt(datetime(2026, 7, 1, 12, 0, tzinfo=UTC)) == "01.07.2026 15:00" # EEST, UTC+3
|
||||
assert dt("2026-10-30T12:00:00") == "30.10.2026 14:00" # naive is taken as UTC
|
||||
assert dt(None) == "—"
|
||||
|
||||
|
||||
def test_labels_fall_back_to_the_raw_key():
|
||||
assert fmt.label(fmt.ROLES)("backend") == "Backend"
|
||||
assert fmt.label(fmt.ROLES)("wizard") == "wizard" and fmt.label(fmt.ROLES)(None) == "—"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw,expected", [("12", 12.0), (" 1,5 ", 1.5), ("0", 0.0), ("2.25", 2.25)])
|
||||
def test_parse_hours_accepts_comma_and_dot(raw, expected):
|
||||
assert parse_hours(raw, "x") == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw", ["", "abc", "-1", "nan", "inf", "1e999"])
|
||||
def test_parse_hours_rejects_garbage(raw):
|
||||
with pytest.raises(ApiError) as e:
|
||||
parse_hours(raw, "Вероятно")
|
||||
assert e.value.status == 422 and "Вероятно" in e.value.detail
|
||||
|
||||
|
||||
# ----------------------------------------------------------- settings validation
|
||||
|
||||
|
||||
def _env(monkeypatch, **extra):
|
||||
base = {"EST_DATABASE__DSN": "sqlite:///x.db", "EST_LLM__ANTHROPIC__API_KEY": "sk-test"}
|
||||
for k, v in {**base, **extra}.items():
|
||||
monkeypatch.setenv(k, v)
|
||||
|
||||
|
||||
def test_web_disabled_needs_nothing(monkeypatch):
|
||||
_env(monkeypatch)
|
||||
assert Settings().web.enabled is False
|
||||
|
||||
|
||||
def test_enabled_web_without_secrets_refuses_to_start(monkeypatch):
|
||||
_env(monkeypatch, EST_WEB__ENABLED="true")
|
||||
with pytest.raises(ValidationError) as e:
|
||||
Settings()
|
||||
text = str(e.value)
|
||||
assert "session_secret" in text and "web.users" in text
|
||||
|
||||
|
||||
def test_enabled_web_rejects_short_secret_and_bad_hash(monkeypatch):
|
||||
_env(monkeypatch, EST_WEB__ENABLED="true", EST_WEB__SESSION_SECRET="short", EST_WEB__USERS="alex:plaintext")
|
||||
with pytest.raises(ValidationError) as e:
|
||||
Settings()
|
||||
assert "at least 32" in str(e.value) and "valid password hash" in str(e.value)
|
||||
|
||||
|
||||
def test_enabled_web_rejects_unknown_timezone_and_odd_user_names(monkeypatch):
|
||||
h = hash_password("x" * 12, **FAST)
|
||||
_env(monkeypatch, EST_WEB__ENABLED="true", EST_WEB__SESSION_SECRET="s" * 40,
|
||||
EST_WEB__USERS=f"bad name:{h}", EST_WEB__TIMEZONE="Mars/Base") # fmt: skip
|
||||
with pytest.raises(ValidationError) as e:
|
||||
Settings()
|
||||
assert "timezone" in str(e.value) and "not valid" in str(e.value)
|
||||
|
||||
|
||||
def test_external_api_mode_needs_a_key(monkeypatch):
|
||||
h = hash_password("x" * 12, **FAST)
|
||||
_env(monkeypatch, EST_WEB__ENABLED="true", EST_WEB__SESSION_SECRET="s" * 40,
|
||||
EST_WEB__USERS=f"alex:{h}", EST_WEB__API_BASE_URL="https://api.example") # fmt: skip
|
||||
with pytest.raises(ValidationError, match="INTERNAL_API_KEY"):
|
||||
Settings()
|
||||
|
||||
|
||||
def test_valid_web_settings_load_and_hide_secrets(monkeypatch):
|
||||
h = hash_password("x" * 12, **FAST)
|
||||
_env(monkeypatch, EST_WEB__ENABLED="true", EST_WEB__SESSION_SECRET="s" * 40,
|
||||
EST_WEB__USERS=f"alex:{h},bob:{h}") # fmt: skip
|
||||
s = Settings()
|
||||
assert set(s.web.user_hashes) == {"alex", "bob"}
|
||||
assert "s" * 40 not in repr(s) and h not in repr(s)
|
||||
Reference in New Issue
Block a user