test_ita_query_recovery.py 15 KB

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