test_ita_route_wiring.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import ast
  2. import json
  3. import unittest
  4. from pathlib import Path
  5. from plugins.ita_plugin import ItaPlugin
  6. from tools.debug_ita_query import (
  7. DEFAULT_ITA_ROUTING_KEY,
  8. DEFAULT_ITA_SERVICE_ID,
  9. build_query_config,
  10. resolve_debug_route,
  11. )
  12. from vs_types import AppointmentType, BizLogicError, VSPlgConfig
  13. REPO_ROOT = Path(__file__).resolve().parents[1]
  14. ITALY_GROUP_ID = "pernotami.ie.it"
  15. def appointment_type(routing_key, *, city):
  16. return AppointmentType(
  17. routing_key=routing_key,
  18. country="Italy",
  19. city=city,
  20. visa_type="Tourist",
  21. )
  22. def probe_route(service_id):
  23. return {
  24. "service_id": service_id,
  25. "capability": "services_probe_only",
  26. }
  27. class ItaPluginRouteWiringTests(unittest.TestCase):
  28. @staticmethod
  29. def _plugin(apt_configs):
  30. plugin = ItaPlugin.__new__(ItaPlugin)
  31. plugin.page = None
  32. plugin.root_workspace = "/nonexistent/ita-route-wiring"
  33. plugin.tunnel = None
  34. plugin._profile_lock_handle = None
  35. plugin.free_config = {
  36. "query_mode": "services_probe",
  37. "final_submit_enabled": False,
  38. "apt_configs": apt_configs,
  39. }
  40. return plugin
  41. def test_two_sede_fixtures_use_the_same_route_resolver(self):
  42. first_key = "slot.dub.it.tourist"
  43. second_key = "slot.hkg.it.tourist"
  44. plugin = self._plugin(
  45. {
  46. first_key: probe_route(1321),
  47. second_key: probe_route(4937),
  48. }
  49. )
  50. first = plugin._resolve_route(
  51. appointment_type(first_key, city="Dublin")
  52. )
  53. second = plugin._resolve_route(
  54. appointment_type(second_key, city="Hong Kong")
  55. )
  56. self.assertEqual(first.routing_key, first_key)
  57. self.assertEqual(first.service_id, 1321)
  58. self.assertEqual(second.routing_key, second_key)
  59. self.assertEqual(second.service_id, 4937)
  60. def test_top_level_service_id_is_not_a_runtime_fallback(self):
  61. plugin = self._plugin({})
  62. plugin.free_config["service_id"] = 1321
  63. with self.assertRaisesRegex(
  64. BizLogicError,
  65. "route configuration rejected.*route_missing",
  66. ):
  67. plugin._resolve_route(
  68. appointment_type("slot.sample.it.tourist", city="Sample")
  69. )
  70. def test_unknown_capability_fails_closed(self):
  71. routing_key = "slot.sample.it.tourist"
  72. plugin = self._plugin(
  73. {
  74. routing_key: {
  75. "service_id": 100,
  76. "capability": "guessed_live",
  77. }
  78. }
  79. )
  80. with self.assertRaisesRegex(
  81. BizLogicError,
  82. "route configuration rejected.*capability_unsupported",
  83. ):
  84. plugin._resolve_route(
  85. appointment_type(routing_key, city="Sample")
  86. )
  87. def test_cs3_accepts_calendar_mode_but_keeps_final_disabled(self):
  88. plugin = ItaPlugin.__new__(ItaPlugin)
  89. plugin.page = None
  90. plugin._transient_user_data_path = "/nonexistent/transient"
  91. plugin.root_workspace = "/nonexistent/root"
  92. plugin.tunnel = None
  93. plugin._profile_lock_handle = None
  94. plugin.set_config(
  95. VSPlgConfig(free_config={"query_mode": "calendar"})
  96. )
  97. self.assertEqual(plugin.free_config["query_mode"], "calendar")
  98. for free_config, expected in (
  99. ({"query_mode": "unknown"}, "query_mode"),
  100. ({"final_submit_enabled": True}, "final submission requires explicit OTP mode on an applicant Book configuration"),
  101. ):
  102. with self.subTest(free_config=free_config):
  103. with self.assertRaisesRegex(BizLogicError, expected):
  104. plugin.set_config(VSPlgConfig(free_config=free_config))
  105. class ItaDebugRouteTests(unittest.TestCase):
  106. def test_default_route_is_only_a_backward_compatible_default(self):
  107. route = resolve_debug_route({})
  108. self.assertEqual(route.routing_key, DEFAULT_ITA_ROUTING_KEY)
  109. self.assertEqual(route.service_id, DEFAULT_ITA_SERVICE_ID)
  110. def test_explicit_route_changes_key_service_and_display_metadata_together(self):
  111. route = resolve_debug_route(
  112. {
  113. "ITA_ROUTING_KEY": "slot.sample.it.tourist",
  114. "ITA_SERVICE_ID": "9001",
  115. "ITA_CITY": "Sample City",
  116. "ITA_VISA_TYPE": "Short-stay tourism",
  117. }
  118. )
  119. config = build_query_config(
  120. {
  121. "ITA_ROUTING_KEY": route.routing_key,
  122. "ITA_SERVICE_ID": str(route.service_id),
  123. "ITA_CITY": route.city,
  124. "ITA_VISA_TYPE": route.visa_type,
  125. },
  126. username="offline-test@example.test",
  127. password="offline-password",
  128. )
  129. apt_type = route.appointment_type()
  130. self.assertEqual(apt_type.routing_key, route.routing_key)
  131. self.assertEqual(apt_type.city, route.city)
  132. self.assertEqual(
  133. config.free_config["apt_configs"][route.routing_key],
  134. probe_route(route.service_id),
  135. )
  136. self.assertNotIn("service_id", config.free_config)
  137. self.assertEqual(config.free_config["query_mode"], "services_probe")
  138. self.assertIs(config.free_config["final_submit_enabled"], False)
  139. def test_partial_route_override_fails_closed(self):
  140. for env in (
  141. {"ITA_SERVICE_ID": "4937"},
  142. {"ITA_ROUTING_KEY": "slot.hkg.it.tourist"},
  143. {
  144. "ITA_ROUTING_KEY": "slot.hkg.it.tourist",
  145. "ITA_SERVICE_ID": "4937",
  146. "ITA_CITY": "Hong Kong",
  147. },
  148. ):
  149. with self.subTest(env=env):
  150. with self.assertRaisesRegex(
  151. ValueError,
  152. "route override must set",
  153. ):
  154. resolve_debug_route(env)
  155. def test_invalid_explicit_service_id_is_rejected(self):
  156. for value in ("0", "-1", "not-an-integer"):
  157. with self.subTest(value=value):
  158. with self.assertRaisesRegex(ValueError, "ITA_SERVICE_ID"):
  159. resolve_debug_route(
  160. {
  161. "ITA_ROUTING_KEY": "slot.sample.it.tourist",
  162. "ITA_SERVICE_ID": value,
  163. "ITA_CITY": "Sample City",
  164. "ITA_VISA_TYPE": "Short-stay tourism",
  165. }
  166. )
  167. class ItaProductionConfigParityTests(unittest.TestCase):
  168. @staticmethod
  169. def _italy_group(path):
  170. groups = json.loads(path.read_text(encoding="utf-8"))
  171. return next(
  172. group for group in groups if group["identifier"] == ITALY_GROUP_ID
  173. )
  174. def test_sentinel_and_booker_share_the_same_safe_route_contract(self):
  175. sentinel = self._italy_group(
  176. REPO_ROOT / "config" / "config_sentinel.json"
  177. )
  178. booker = self._italy_group(
  179. REPO_ROOT / "config" / "config_booker.json"
  180. )
  181. for group in (sentinel, booker):
  182. free_config = group["free_config"]
  183. self.assertNotIn("service_id", free_config)
  184. self.assertEqual(free_config["query_mode"], "services_probe")
  185. self.assertIs(free_config["final_submit_enabled"], False)
  186. self.assertEqual(
  187. sentinel["free_config"]["apt_configs"],
  188. booker["free_config"]["apt_configs"],
  189. )
  190. routing_key = sentinel["appointment_types"][0]["routing_key"]
  191. self.assertIn(routing_key, sentinel["free_config"]["apt_configs"])
  192. def test_runtime_code_has_no_sede_or_service_id_control_branch(self):
  193. paths = [
  194. REPO_ROOT / "plugins" / "ita_plugin.py",
  195. REPO_ROOT / "tools" / "debug_ita_query.py",
  196. REPO_ROOT / "tools" / "debug_ita_book.py",
  197. REPO_ROOT / "tools" / "debug_ita_monitor.py",
  198. REPO_ROOT / "tools" / "debug_ita_soak.py",
  199. REPO_ROOT / "tools" / "debug_ita_calendar_gate.py",
  200. REPO_ROOT / "tools" / "ita_local_handoff.py",
  201. ]
  202. forbidden_constants = {1321, 4937, "Dublin", "Hong Kong", "hkg"}
  203. violations = []
  204. for path in paths:
  205. tree = ast.parse(path.read_text(encoding="utf-8"))
  206. for node in ast.walk(tree):
  207. if not isinstance(node, (ast.If, ast.IfExp)):
  208. continue
  209. for child in ast.walk(node.test):
  210. if (
  211. isinstance(child, ast.Constant)
  212. and child.value in forbidden_constants
  213. ):
  214. violations.append((path.name, node.lineno, child.value))
  215. self.assertEqual(violations, [])
  216. if __name__ == "__main__":
  217. unittest.main()