From f435b64d3c50800b908280608202c02edab097b0 Mon Sep 17 00:00:00 2001 From: Nyan404 Date: Tue, 25 Aug 2026 04:10:17 +0300 Subject: [PATCH] fix(domain): validate participant name types --- app/belki/domain.py | 5 +++-- structure_and_data.md | 1 + tests/test_belki.py | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/belki/domain.py b/app/belki/domain.py index 133dcb7..48d4c04 100644 --- a/app/belki/domain.py +++ b/app/belki/domain.py @@ -103,7 +103,7 @@ class CoachingSession: if self.kitten_independence not in ("самостоятельно", "несамостоятельно"): raise ValueError("Некорректный статус самостоятельности") for name, kitten_id, independence in self.extra_kittens: - if not name.strip(): + if not isinstance(name, str) or not name.strip(): raise ValueError("Имя дополнительного котёнка не может быть пустым") _positive_id(kitten_id, "ID дополнительного котёнка") if independence not in ("самостоятельно", "несамостоятельно"): @@ -239,7 +239,8 @@ class TrainingSession: def __post_init__(self) -> None: """Валидирует данные сессии тренировки.""" - if not self.escort_name.strip() or not self.kitten_name.strip(): + if (not isinstance(self.escort_name, str) or not self.escort_name.strip() + or not isinstance(self.kitten_name, str) or not self.kitten_name.strip()): raise ValueError("Имена участников не могут быть пустыми") _positive_id(self.escort_id, "ID сопровождающего") _positive_id(self.kitten_id, "ID котёнка") diff --git a/structure_and_data.md b/structure_and_data.md index 2f8ed2e..daa2224 100644 --- a/structure_and_data.md +++ b/structure_and_data.md @@ -74,6 +74,7 @@ - 2026-08-25: admin history sorting now tolerates legacy records whose IDs were stored as strings, preserving numeric order for valid IDs instead of failing the complete history view. - 2026-08-25: coaching sessions now validate a real calendar date in `ДД.ММ` form before any calculation or persistence; free-form dates remain accepted after parser normalization. - 2026-08-25: coaching and training reports reject a repeated participant ID, including an escort submitted as their own kitten, so duplicate input cannot inflate a result. +- 2026-08-25: direct domain calls with non-text participant names now return the same clear validation errors as API and VK input, rather than an implementation-level attribute error. - 2026-08-25: one-line VK/web recognition now gives explicit role labels priority over position. `сопровождающий`/`наставник`/`учитель` identify the escort, while `ученик`/`котёнок`/`бельчонок`/`подопечный` identify the ward; unlabeled pairs retain the original order-based fallback. - 2026-08-25: a self-sufficiency marker directly beside a labelled ward now belongs to that ward, so one natural line can describe different statuses for several kittens. - 2026-08-25: corrected Russian UX copy in the VK help and attachment flow; the web identity hint now accurately says that a manually completed pair applies only to the current calculation. diff --git a/tests/test_belki.py b/tests/test_belki.py index 89b1488..7e25acf 100644 --- a/tests/test_belki.py +++ b/tests/test_belki.py @@ -102,6 +102,15 @@ class CalculationTests(unittest.TestCase): with self.assertRaisesRegex(ValueError, "разными персонажами"): TrainingSession("Белка", 1, "Белка", 1) + def test_sessions_reject_non_text_participant_names_with_a_domain_error(self): + with self.assertRaisesRegex(ValueError, "Имена участников"): + TrainingSession("Белка", 1, None, 2) # type: ignore[arg-type] + with self.assertRaisesRegex(ValueError, "дополнительного котёнка"): + CoachingSession( + "15.08", "ЛУ", "11:00", "12:00", "Белка", 1, "Первый", 2, + extra_kittens=((None, 3, "самостоятельно"),), # type: ignore[arg-type] + ) + def test_zu_rejects_non_self_kitten(self): with self.assertRaisesRegex(ValueError, "Для ЗУ все котята должны быть самостоятельными"): CoachingSession(