from datetime import timedelta import pytest from app.common import ValidationError, clean_text, iso_after, iso_ago, now_iso def test_clean_text(): assert clean_text(" Milch \n frisch ", "Name", 20) == "Milch frisch" assert clean_text(" Zeile 1\nZeile 2 ", "Text", 20, collapse=False) == "Zeile 1\nZeile 2" assert clean_text(None, "Region", 10, required=False) == "" with pytest.raises(ValidationError, match="Name fehlt"): clean_text(" ", "Name", 10) with pytest.raises(ValidationError, match=r"max\. 3 Zeichen"): clean_text("abcd", "Name", 3) def test_zeitstempel_vergleichbar_als_text(): assert iso_ago(timedelta(days=1)) < now_iso() < iso_after(now_iso(), timedelta(seconds=1)) assert now_iso().endswith("Z") and len(now_iso()) == 24 @pytest.mark.parametrize("method, url, body", [ ("post", "/api/projects", {"title": ""}), # Artikel ("post", "/api/kanban/projekte", {"name": " "}), # Kanban ("post", "/api/haushalt/listen", {"name": ""}), # Haushalt ("put", "/api/links", {"jellyfin": "javascript:alert(1)"}), # Links ("post", "/api/reisen/staedte", {"geonameId": 1, "country": "DE", "name": "", "lat": 1, "lon": 1}), ]) def test_eingabefehler_einheitlich_400(login, method, url, body): res = getattr(login("anna"), method)(url, json=body) assert res.status_code == 400 assert res.json()["error"]