fix(goal): bound public calculator inputs

This commit is contained in:
nyan404 committed 2026-08-25 03:34:58 +03:00
1 parent 041d717439
commit 5d6e5987d1
8 files changed
+58 -8

No files matched your search

+7
View File
@@ -44,6 +44,13 @@ class ApiTests(unittest.TestCase):
self.assertEqual(response["result"]["total_hours"], 2)
self.assertIn("Осталось добрать: 24", response["message"])
def test_goal_calculator_rejects_non_finite_json_number(self):
with self.assertRaisesRegex(ApiError, "конечным числом"):
calculate_payload({
"mode": "goal", "current_points": "NaN", "target_points": 34,
"minutes_per_session": 20, "kitten_count": 1,
})
def test_calculate_and_store_uses_calculation_store(self):
with TemporaryDirectory() as directory:
with patch.dict("os.environ", {"BELKI_DATABASE_URL": "", "BELKI_HISTORY_DB": f"{directory}/history.sqlite3"}, clear=False):
+13
View File
@@ -1,4 +1,5 @@
import unittest
from math import inf, nan
from app.belki.domain import (
CoachingSession,
@@ -6,6 +7,7 @@ from app.belki.domain import (
TrainingSession,
calculate_coaching,
calculate_inspection,
calculate_goal,
calculate_training,
format_clock,
parse_clock,
@@ -28,6 +30,17 @@ class ClockTests(unittest.TestCase):
class CalculationTests(unittest.TestCase):
def test_goal_rejects_non_finite_and_unbounded_input(self):
for current_points, target_points in ((nan, 1), (1, inf)):
with self.assertRaisesRegex(ValueError, "конечным числом"):
calculate_goal(
current_points=current_points, target_points=target_points,
minutes_per_session=20, kitten_count=1,
)
with self.assertRaisesRegex(ValueError, "от 1 до 1440"):
calculate_goal(current_points=0, target_points=1, minutes_per_session=1441)
with self.assertRaisesRegex(ValueError, "от 1 до 50"):
calculate_goal(current_points=0, target_points=1, minutes_per_session=20, kitten_count=51)
def test_self_sufficient_kitten(self):
result = calculate_coaching(
CoachingSession(
+8
View File
@@ -18,6 +18,14 @@ class VkConversationTests(unittest.TestCase):
self.assertIn("/отмена", HELP_TEXT)
self.assertNotIn("Для детей", HELP_TEXT)
def test_goal_dialog_rejects_non_finite_and_unbounded_values(self):
dialog = BelkiDialog()
self.assertIn("баллов", dialog.handle(110, "/цель").lower())
self.assertIn("конечным", dialog.handle(110, "NaN").lower())
dialog.handle(110, "0")
dialog.handle(110, "10")
self.assertIn("от 1 до 1440", dialog.handle(110, "1441"))
def test_step_by_step_coaching_dialog(self):
dialog = BelkiDialog()
self.assertIn("дат", dialog.handle(77, "/лу").lower())