Просмотр исходного кода

test(ita): align error contract and boundaries

zhangweijp 2 месяцев назад
Родитель
Сommit
9126cf61e4
2 измененных файлов с 40 добавлено и 18 удалено
  1. 17 9
      test/ita/test_ita_compact.py
  2. 23 9
      test/ita/test_ita_otp.py

+ 17 - 9
test/ita/test_ita_compact.py

@@ -78,6 +78,11 @@ class CompactProtocolTests(unittest.TestCase):
 
 
 class CompactCalendarTests(unittest.TestCase):
+    def test_runtime_service_id_rejects_missing_boolean_and_zero_values(self):
+        for value in (None, True, False, 0, "0", ""):
+            with self.subTest(value=value), self.assertRaises(ita.BizLogicError):
+                ita._runtime_id(value)
+
     def test_query_stops_at_first_positive_month(self):
         empty = ()
         positive = (ita.CalendarDay(date(2026, 9, 2), 1, 0),)
@@ -129,10 +134,11 @@ class CompactCalendarTests(unittest.TestCase):
         self.assertEqual(select.call_args.kwargs["target_day"], date(2026, 10, 2))
 
     def test_service_mismatch_fails_instead_of_timing_out(self):
-        with patch.object(ita, "read_calendar_service", return_value="9999"):
+        with patch.object(ita, "read_calendar_context",
+                          return_value=("9999", date(2026, 8, 1))):
             with self.assertRaisesRegex(ita.CalendarBookError, "service changed"):
                 ita.wait_for_calendar_service(
-                    object(), session_is_current=lambda: True,
+                    unittest.mock.ANY, session_is_current=lambda: True,
                     expected_service=4937, timeout=0.1)
 
 
@@ -185,13 +191,15 @@ class CompactPreflightTests(unittest.TestCase):
         raw = self.snapshot(otpValuePresent=True, otpSendEnabled=False)
         ita.validate_booking_preflight(raw, after_otp=True)
 
-    def test_query_failure_keeps_the_safe_calendar_reason(self):
-        plugin = object.__new__(ita.ItaPlugin)
-        plugin._prepared_calendar = object()
-        error = plugin._calendar_failure(
-            ita.CalendarGateError("Italy booking verification timed out: form_pending"),
-            "Query")
-        self.assertIn("form_pending", str(error))
+    def test_preflight_failure_uses_framework_business_error(self):
+        with self.assertRaisesRegex(ita.BizLogicError, "verification is not ready"):
+            ita.validate_booking_preflight({
+                "formReady": True,
+                "turnstileReady": True,
+                "otpValuePresent": False,
+                "otpSendEnabled": False,
+                "avantiEnabled": True,
+            })
 
 
 class CompactFinalTests(unittest.TestCase):

+ 23 - 9
test/ita/test_ita_otp.py

@@ -4,9 +4,11 @@ from unittest import TestCase
 from unittest.mock import Mock
 
 from plugins import ita_plugin as otp
-from plugins.ita_plugin import CalendarGateError
 from plugins.ita_plugin import OtpMailConfig, ItaProtocolError, parse_otp_body, resolve_route_config
-from vs_types import BizLogicError, PermissionDeniedError, SessionExpiredOrInvalidError
+from vs_types import (
+    BizLogicError, PermissionDeniedError, RateLimiteddError,
+    SessionExpiredOrInvalidError,
+)
 
 
 class OtpParserTests(TestCase):
@@ -95,7 +97,7 @@ class CloudOtpTests(TestCase):
     def test_account_mailbox_failure_does_not_read_another_inbox(self):
         config = OtpMailConfig(authorization_source="account")
         self.client.fetch_mail_content.side_effect = PermissionDeniedError("private-provider-details")
-        with self.assertRaisesRegex(CalendarGateError, "not permitted"):
+        with self.assertRaisesRegex(PermissionDeniedError, "not permitted"):
             otp.fetch_otp_from_cloud(config, recipient="second@example.test",
                 sent_at="2026-08-31 01:02:03", api=self.client)
         self.client.fetch_mail_content.assert_called_once()
@@ -111,12 +113,18 @@ class CloudOtpTests(TestCase):
 
     def test_provider_failures_are_redacted_and_not_site_session_errors(self):
         marker = 'PRIVATE_MAIL_CONTENT_001234'
-        for error in (BizLogicError(marker), PermissionDeniedError(marker),
-                      SessionExpiredOrInvalidError(marker), ValueError(marker), TimeoutError(marker)):
+        cases = (
+            (BizLogicError(marker), BizLogicError),
+            (PermissionDeniedError(marker), PermissionDeniedError),
+            (SessionExpiredOrInvalidError(marker), SessionExpiredOrInvalidError),
+            (ValueError(marker), BizLogicError),
+            (TimeoutError(marker), BizLogicError),
+        )
+        for error, expected in cases:
             self.client.reset_mock()
             self.client.fetch_mail_content.side_effect = error
             with self.subTest(error=type(error).__name__):
-                with self.assertRaises(CalendarGateError) as caught:
+                with self.assertRaises(expected) as caught:
                     self.fetch()
                 rendered = ''.join(traceback.format_exception(caught.exception))
                 self.assertNotIn(marker, rendered)
@@ -124,7 +132,7 @@ class CloudOtpTests(TestCase):
                 self.client.fetch_mail_content_from_top.assert_not_called()
 
     def test_invalid_input_never_reads_mail(self):
-        with self.assertRaises(CalendarGateError):
+        with self.assertRaises(BizLogicError):
             otp.fetch_otp_from_cloud(self.config, recipient='not-an-address', sent_at='invalid', api=self.client)
         self.client.fetch_mail_content.assert_not_called()
 
@@ -145,7 +153,13 @@ class CloudOtpTests(TestCase):
                     self.assertEqual(otp.fetch_otp_from_cloud(self.config,
                         recipient="applicant@example.test", sent_at="2026-08-31 01:02:03", api=client), "001234")
                 else:
-                    with self.assertRaises(CalendarGateError) as raised:
+                    expected = {
+                        401: SessionExpiredOrInvalidError,
+                        403: PermissionDeniedError,
+                        429: RateLimiteddError,
+                        504: BizLogicError,
+                    }[status]
+                    with self.assertRaises(expected) as raised:
                         otp.fetch_otp_from_cloud(self.config, recipient="applicant@example.test",
                                                 sent_at="2026-08-31 01:02:03", api=client)
                     rendered = ''.join(traceback.format_exception(raised.exception))
@@ -160,6 +174,6 @@ class CloudOtpTests(TestCase):
         for body in (None, "unknown template", "OTP Code:123456 OTP Code:654321"):
             with self.subTest(body=body):
                 self.client.fetch_mail_content.return_value = body
-                with self.assertRaises(CalendarGateError):
+                with self.assertRaises(BizLogicError):
                     self.fetch()
                 self.client.fetch_mail_content_from_top.assert_not_called()