test_ita_query_recovery.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. """Offline recovery contracts; no browser, credentials, or website requests."""
  2. import json
  3. import sys
  4. import threading
  5. import time
  6. import types
  7. import unittest
  8. from dataclasses import fields, replace
  9. from pathlib import Path
  10. from types import SimpleNamespace
  11. from unittest.mock import Mock, patch
  12. from DrissionPage.errors import PageDisconnectedError
  13. from plugins.ita_plugin import RUNTIME_SERVICE_SCRIPT
  14. from plugins.ita_plugin import (
  15. AVAILABILITY_PATH, CalendarAccessDeniedError, CalendarAuthenticationExpiredError,
  16. CalendarGateError, CalendarRateLimitedError, CalendarTransientError,
  17. parse_availability_packet,
  18. )
  19. from plugins.ita_plugin import (
  20. CalendarQueryCheckpoint, ItaCalendarPreparationRequiredError,
  21. ItaCalendarTransientError, ItaPlugin,
  22. )
  23. from test.test_debug_ita_production_query_gate import (
  24. FakeProductionPage, gate_env, make_plugin,
  25. )
  26. from vs_types import (
  27. BizLogicError, PermissionDeniedError, RateLimiteddError,
  28. SessionExpiredOrInvalidError,
  29. )
  30. class RecoveryPage(FakeProductionPage):
  31. """Real Query and audit mechanics run against deterministic page responses."""
  32. def __init__(self, outcomes=("available",), *, restore_outcome="calendar"):
  33. plans = [[(2026, 8, [12])]] * 100
  34. super().__init__(plans)
  35. self.outcomes = list(outcomes)
  36. self.attempts = 0
  37. self.tab_id = "offline-tab"
  38. self.runtime_service = "offline-runtime-service"
  39. self.restore_outcome = restore_outcome
  40. self.get_calls = []
  41. def run_js(self, script, *args):
  42. if script == RUNTIME_SERVICE_SCRIPT:
  43. return {"path": "/BookingCalendar", "count": 1, "value": self.runtime_service}
  44. if script == "return 1;":
  45. if not self.driver.is_running:
  46. raise PageDisconnectedError("offline disconnected")
  47. return 1
  48. return super().run_js(script, *args)
  49. def get(self, url):
  50. self.get_calls.append(url)
  51. if self.restore_outcome == "form":
  52. self.url = "https://prenotami.esteri.it/Services/Booking/9001"
  53. elif self.restore_outcome == "expired":
  54. self.url = "https://iam.esteri.it/signin/"
  55. else:
  56. self.url = url
  57. if self.restore_outcome == "wrong-service":
  58. self.runtime_service = "different-runtime-service"
  59. def refresh(self):
  60. outcome = self.outcomes[min(self.attempts, len(self.outcomes) - 1)]
  61. self.attempts += 1
  62. if outcome == "disconnect":
  63. self.driver.is_running = False
  64. raise PageDisconnectedError("offline private@example.test secret-token")
  65. if outcome == "cancel":
  66. raise KeyboardInterrupt
  67. if outcome in {"expired", "timeout"}:
  68. if outcome == "expired":
  69. self.url = "https://iam.esteri.it/signin/"
  70. self.packet = None
  71. return
  72. if outcome == "none":
  73. self.plans[self.refresh_calls] = [(2026, month, []) for month in (8, 9, 10)]
  74. super().refresh()
  75. if outcome in {"rate", "denied"}:
  76. self.packet.response.status = 429 if outcome == "rate" else 403
  77. if outcome == "malformed":
  78. self.packet.response.raw_body = "private@example.test secret-token"
  79. if outcome == "unexpected-otp":
  80. self.emit("/BookingCalendar/GenerateOTP?ServiceID=9001", "GET")
  81. self.emit("/BookingCalendar/RetrieveTimeSlots") # native auto-load, not a Query fanout
  82. def prepared_plugin(page=None):
  83. page = page or RecoveryPage()
  84. plugin, route = make_plugin(page)
  85. plugin.free_config["query_mode"] = "calendar"
  86. plugin.is_healthy = True
  87. plugin.session_create_time = time.time()
  88. plugin._persistent_profile_path = Path("/offline/query-profile")
  89. plugin.user_data_path = str(plugin._persistent_profile_path)
  90. plugin._query_stop_reason = None
  91. plugin._reset_calendar_book_state()
  92. return plugin, route
  93. def availability_packet(status=200, *, response_url=None):
  94. response = SimpleNamespace(status=status, raw_body=json.dumps(json.dumps([])))
  95. if response_url is not None:
  96. response.url = response_url
  97. return SimpleNamespace(
  98. url="https://prenotami.esteri.it" + AVAILABILITY_PATH, method="POST",
  99. request=SimpleNamespace(postData={"_Servizio": "test-runtime", "selectedDay": "01/08/2026"}),
  100. response=response,
  101. )
  102. class CalendarFailureClassificationTests(unittest.TestCase):
  103. def test_distinguishes_http_failure_categories(self):
  104. cases = {
  105. 401: CalendarAuthenticationExpiredError, 403: CalendarAccessDeniedError,
  106. 429: CalendarRateLimitedError, 408: CalendarTransientError,
  107. 502: CalendarTransientError, 503: CalendarTransientError,
  108. 504: CalendarTransientError, 418: CalendarGateError,
  109. }
  110. for status, expected in cases.items():
  111. with self.subTest(status=status):
  112. packet = availability_packet(status)
  113. packet.response.raw_body = "private@example.test secret-token"
  114. with self.assertRaises(expected) as caught:
  115. parse_availability_packet(packet)
  116. self.assertNotIn("private@example.test", str(caught.exception))
  117. self.assertNotIn("secret-token", str(caught.exception))
  118. def test_followed_login_redirect_is_not_schema_or_no_inventory(self):
  119. with self.assertRaises(CalendarAuthenticationExpiredError):
  120. parse_availability_packet(availability_packet(response_url="https://iam.esteri.it/signin/"))
  121. def test_direct_login_redirect_is_classified_without_exposing_oauth_values(self):
  122. packet = availability_packet(302)
  123. packet.response.headers = {"Location": "https://iam.esteri.it/signin/?state=secret-token"}
  124. with self.assertRaises(CalendarAuthenticationExpiredError) as caught:
  125. parse_availability_packet(packet)
  126. self.assertNotIn("secret-token", str(caught.exception))
  127. def test_invalid_http_status_is_not_a_retryable_network_failure(self):
  128. for value in (False, "200", None):
  129. with self.subTest(value=value), self.assertRaises(CalendarGateError) as caught:
  130. parse_availability_packet(availability_packet(value))
  131. self.assertNotIsInstance(caught.exception, CalendarTransientError)
  132. def test_foreign_or_unexpected_response_never_becomes_inventory(self):
  133. for url in ("https://foreign.example" + AVAILABILITY_PATH,
  134. "https://prenotami.esteri.it/Services"):
  135. with self.subTest(url=url), self.assertRaises(CalendarGateError):
  136. parse_availability_packet(availability_packet(response_url=url))
  137. def test_auth_rate_and_permission_errors_invalidate_health_and_scan(self):
  138. for outcome, expected in (("expired", SessionExpiredOrInvalidError),
  139. ("rate", RateLimiteddError), ("denied", PermissionDeniedError)):
  140. with self.subTest(outcome=outcome):
  141. plugin, route = prepared_plugin(RecoveryPage([outcome]))
  142. plugin.last_calendar_query_scan = object()
  143. with self.assertRaises(expected):
  144. plugin.query(route.appointment_type())
  145. self.assertFalse(plugin.is_healthy)
  146. self.assertFalse(plugin.health_check())
  147. self.assertIsNone(plugin.last_calendar_query_scan)
  148. self.assertIsNone(plugin._calendar_query_route_key)
  149. with self.assertRaises(SessionExpiredOrInvalidError):
  150. plugin.query(route.appointment_type())
  151. self.assertEqual(plugin.page.attempts, 1)
  152. def test_transient_error_keeps_valid_session_without_old_scan(self):
  153. plugin, route = prepared_plugin(RecoveryPage(["timeout", "available"]))
  154. plugin.last_calendar_query_scan = object()
  155. with self.assertRaises(ItaCalendarTransientError):
  156. plugin.query(route.appointment_type())
  157. self.assertTrue(plugin.is_healthy)
  158. self.assertIsNone(plugin.last_calendar_query_scan)
  159. self.assertTrue(plugin.query(route.appointment_type()).success)
  160. def test_browser_disconnection_invalidates_health_without_hiding_type(self):
  161. plugin, route = prepared_plugin(RecoveryPage(["disconnect"]))
  162. with self.assertRaises(PageDisconnectedError):
  163. plugin.query(route.appointment_type())
  164. self.assertFalse(plugin.health_check())
  165. self.assertIsNone(plugin._calendar_query_route_key)
  166. def test_configured_lifetime_and_health_probe_clear_preparation(self):
  167. plugin, route = prepared_plugin()
  168. plugin.capture_calendar_query_checkpoint(route.appointment_type())
  169. plugin.session_create_time = 1
  170. self.assertFalse(plugin.health_check())
  171. self.assertFalse(plugin.is_healthy)
  172. self.assertIsNone(plugin._calendar_query_checkpoint)
  173. def test_health_probe_does_not_turn_operator_cancel_into_disconnection(self):
  174. plugin, _ = prepared_plugin()
  175. plugin.page.run_js = Mock(side_effect=KeyboardInterrupt)
  176. with self.assertRaises(KeyboardInterrupt):
  177. plugin.health_check()
  178. self.assertTrue(plugin.is_healthy)
  179. class CalendarReadOnlyRestoreTests(unittest.TestCase):
  180. def test_restore_rebinds_new_page_then_runs_real_fresh_query(self):
  181. old, route = prepared_plugin()
  182. old_result = old.query(route.appointment_type())
  183. old_scan = old.last_calendar_query_scan
  184. checkpoint = old.capture_calendar_query_checkpoint(route.appointment_type())
  185. new, _ = prepared_plugin()
  186. new._clear_calendar_query_state()
  187. new.page.url = "https://prenotami.esteri.it/UserArea"
  188. new.restore_calendar_query(route.appointment_type(), checkpoint)
  189. self.assertEqual(new.page.get_calls, [checkpoint.calendar_url])
  190. self.assertIsNone(new.last_calendar_query_scan)
  191. result = new.query(route.appointment_type())
  192. self.assertIsNot(new.last_calendar_query_scan, old_scan)
  193. self.assertIsNot(result, old_result)
  194. self.assertTrue(result.success)
  195. self.assertEqual(new._calendar_query_route_key, route.routing_key)
  196. self.assertEqual(new.page.refresh_calls, 1)
  197. def test_checkpoint_contains_no_dates_credentials_or_slot_state(self):
  198. plugin, route = prepared_plugin()
  199. checkpoint = plugin.capture_calendar_query_checkpoint(route.appointment_type())
  200. self.assertEqual({item.name for item in fields(checkpoint)},
  201. {"route", "username", "profile_path", "calendar_url", "runtime_service"})
  202. self.assertNotIn(plugin.config.account.username, repr(checkpoint))
  203. self.assertNotIn(plugin.config.account.password, repr(checkpoint))
  204. def test_mismatched_owner_or_profile_or_config_is_rejected_before_navigation(self):
  205. old, route = prepared_plugin()
  206. checkpoint = old.capture_calendar_query_checkpoint(route.appointment_type())
  207. for mutation in ("account", "profile", "route", "book", "query-mode"):
  208. with self.subTest(mutation=mutation):
  209. plugin, _ = prepared_plugin()
  210. if mutation == "account":
  211. plugin.config.account.username = "other@example.test"
  212. elif mutation == "profile":
  213. plugin._persistent_profile_path = Path("/offline/other")
  214. elif mutation == "route":
  215. plugin.free_config["apt_configs"][route.routing_key]["service_id"] = 9002
  216. elif mutation == "book":
  217. plugin._calendar_book_prepared_context = ("book",)
  218. else:
  219. plugin.free_config["query_mode"] = "services_probe"
  220. with self.assertRaises(BizLogicError):
  221. plugin.restore_calendar_query(route.appointment_type(), checkpoint)
  222. self.assertEqual(plugin.page.get_calls, [])
  223. def test_form_redirect_stops_at_pending_otp_todo_without_preparation(self):
  224. old, route = prepared_plugin()
  225. checkpoint = old.capture_calendar_query_checkpoint(route.appointment_type())
  226. plugin, _ = prepared_plugin(RecoveryPage(restore_outcome="form"))
  227. with self.assertRaisesRegex(ItaCalendarPreparationRequiredError, "ITA-OTP-01"):
  228. plugin.restore_calendar_query(route.appointment_type(), checkpoint)
  229. self.assertEqual(len(plugin.page.get_calls), 1)
  230. self.assertEqual(plugin.page.refresh_calls, 0)
  231. self.assertIsNone(plugin._calendar_query_route_key)
  232. self.assertIsNone(plugin._calendar_query_checkpoint)
  233. def test_wrong_runtime_service_cannot_reuse_old_proof(self):
  234. old, route = prepared_plugin()
  235. checkpoint = old.capture_calendar_query_checkpoint(route.appointment_type())
  236. plugin, _ = prepared_plugin(RecoveryPage(restore_outcome="wrong-service"))
  237. with self.assertRaisesRegex(BizLogicError, "service does not match"):
  238. plugin.restore_calendar_query(route.appointment_type(), checkpoint)
  239. self.assertIsNone(plugin._calendar_query_checkpoint)
  240. def test_changed_runtime_tab_or_generation_rejects_next_query(self):
  241. for mutation in ("service", "tab", "generation"):
  242. with self.subTest(mutation=mutation):
  243. plugin, route = prepared_plugin()
  244. plugin.capture_calendar_query_checkpoint(route.appointment_type())
  245. if mutation == "service":
  246. plugin.page.runtime_service = "changed"
  247. elif mutation == "tab":
  248. plugin.page.tab_id = "changed"
  249. else:
  250. plugin.session_create_time += 1
  251. with self.assertRaises(BizLogicError):
  252. plugin.query(route.appointment_type())
  253. self.assertEqual(plugin.page.refresh_calls, 0)
  254. def test_checkpoint_cannot_redirect_to_external_origin(self):
  255. plugin, route = prepared_plugin()
  256. checkpoint = plugin.capture_calendar_query_checkpoint(route.appointment_type())
  257. malicious = replace(checkpoint, calendar_url="https://foreign.example/BookingCalendar")
  258. with self.assertRaises(CalendarGateError):
  259. plugin.restore_calendar_query(route.appointment_type(), malicious)
  260. self.assertEqual(plugin.page.get_calls, [])
  261. def test_checkpoint_with_unobserved_query_parameters_is_rejected_before_navigation(self):
  262. plugin, route = prepared_plugin()
  263. checkpoint = plugin.capture_calendar_query_checkpoint(route.appointment_type())
  264. unknown = replace(checkpoint, calendar_url=checkpoint.calendar_url + "?unknown=1")
  265. with self.assertRaises(BizLogicError):
  266. plugin.restore_calendar_query(route.appointment_type(), unknown)
  267. self.assertEqual(plugin.page.get_calls, [])
  268. def test_real_cleanup_reports_failure_instead_of_allowing_silent_recreation(self):
  269. plugin, _route = prepared_plugin()
  270. plugin.__dict__.pop("cleanup")
  271. plugin.root_workspace = "/nonexistent/ita-offline-cleanup-test"
  272. plugin.tunnel = SimpleNamespace(stop=Mock())
  273. plugin._profile_lock_handle = None
  274. page = plugin.page
  275. page.quit = Mock(side_effect=RuntimeError("private@example.test secret-token"))
  276. with self.assertRaises(BizLogicError) as caught:
  277. plugin.cleanup()
  278. self.assertNotIn("secret-token", str(caught.exception))
  279. self.assertIsNone(plugin.page)
  280. self.assertFalse(plugin.is_healthy)
  281. plugin.__del__()
  282. page.quit.assert_called_once()
  283. class ExistingSentinelLifecycleTests(unittest.TestCase):
  284. def test_real_monitor_retires_failed_italy_session_once_without_publishing(self):
  285. # Import from the same already-stubbed dependency environment as other
  286. # repository tests. Neither __init__ nor live API/Redis is invoked.
  287. from test.test_ita_plugin import GroupConfig, Task
  288. try:
  289. from sentinel import SentinelGCO
  290. except ModuleNotFoundError as exc:
  291. if exc.name != "redis":
  292. raise
  293. redis_stub = types.ModuleType("redis")
  294. redis_stub.Redis = Mock
  295. with patch.dict(sys.modules, {"redis": redis_stub}):
  296. from sentinel import SentinelGCO
  297. from vs_types import QueryWaitConfig, QueryWaitMode
  298. plugin, route = prepared_plugin(RecoveryPage(["expired"]))
  299. config = GroupConfig(identifier="offline-italy")
  300. config.appointment_types = [route.appointment_type()]
  301. config.appointment_types[0].weight = 1
  302. config.sentinel.query_wait = QueryWaitConfig(mode=QueryWaitMode.Fixed, fixed_wait=60)
  303. sentinel = SentinelGCO.__new__(SentinelGCO)
  304. sentinel.m_cfg = config
  305. sentinel.m_logger = Mock()
  306. sentinel.redis_client = Mock()
  307. sentinel.m_lock = threading.RLock()
  308. sentinel.m_tasks = [Task(instance=plugin, qw_cfg=config.sentinel.query_wait, next_run=0)]
  309. sentinel.m_stop_event = Mock()
  310. sentinel.m_stop_event.is_set.side_effect = [False, False, True]
  311. # Patch the loaded method's dependencies directly. patch.dict(sys.modules)
  312. # removes temporary modules on exit, so string-based patch would import
  313. # Sentinel again and require an installed/live Redis dependency.
  314. isolated = {
  315. "time": SimpleNamespace(sleep=lambda _delay: None, time=lambda: 1000),
  316. "VSCloudApi": SimpleNamespace(Instance=Mock(return_value=Mock())),
  317. }
  318. with patch.dict(SentinelGCO._monitor_loop.__globals__, isolated):
  319. sentinel._monitor_loop()
  320. self.assertEqual(sentinel.m_tasks, [])
  321. plugin.cleanup.assert_called_once()
  322. sentinel.redis_client.setex.assert_not_called()
  323. self.assertEqual(plugin.page.attempts, 1)
  324. if __name__ == "__main__":
  325. unittest.main()