test_ita_route_wiring.py 8.6 KB

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