test_debug_ita_query.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. import ast
  2. import io
  3. import json
  4. import os
  5. import stat
  6. import tempfile
  7. import unittest
  8. from contextlib import redirect_stderr, redirect_stdout
  9. from pathlib import Path
  10. from unittest.mock import patch
  11. from tools.debug_ita_query import (
  12. _BOOKING_FORM_DISCOVERY_SCRIPT,
  13. DEFAULT_ITA_ROUTING_KEY,
  14. DEFAULT_ITA_SERVICE_ID,
  15. _get_query_credentials,
  16. _prepare_query_profile,
  17. _sanitize_booking_form_structure,
  18. build_query_config,
  19. resolve_debug_route,
  20. run_query_once,
  21. )
  22. from tools.debug_ita_session import DebugConfigurationError
  23. from utils.ita_local_profile import ItaLocalProfileError, prepare_ita_query_profile
  24. from vs_types import (
  25. AvailabilityStatus,
  26. BizLogicError,
  27. VSQueryResult,
  28. )
  29. class FakeQueryDebugElement:
  30. def __init__(self, text):
  31. self.text = text
  32. class FakeQueryDebugPage:
  33. def __init__(self):
  34. self.url = (
  35. "https://prenotami.esteri.it/Services"
  36. "?state=sensitive-state#sensitive-fragment"
  37. )
  38. self.popup_text = ""
  39. self.form_structure = None
  40. def ele(self, selector, timeout=None):
  41. del timeout
  42. if selector == ".jconfirm-content" and self.popup_text:
  43. return FakeQueryDebugElement(self.popup_text)
  44. return None
  45. def run_js(self, script):
  46. self.last_script = script
  47. return self.form_structure
  48. class FakeQueryDebugPlugin:
  49. behavior = "none-available"
  50. last_instance = None
  51. def __init__(self, group_id):
  52. self.group_id = group_id
  53. self.page = FakeQueryDebugPage()
  54. self.create_session_calls = 0
  55. self.query_calls = 0
  56. self.cleanup_called = False
  57. FakeQueryDebugPlugin.last_instance = self
  58. def set_log(self, logger):
  59. self.logger = logger
  60. def set_config(self, config):
  61. self.config = config
  62. def create_session(self):
  63. self.create_session_calls += 1
  64. def query(self, apt_type):
  65. self.query_calls += 1
  66. if self.behavior == "weak-entry-signal":
  67. self.page.form_structure = {
  68. "path": "/Services/Booking/1321",
  69. "form": {
  70. "method": "POST",
  71. "action_path": "/Services/Booking/1321",
  72. "forbidden": "must-not-appear",
  73. },
  74. "field_count": 1,
  75. "fields_truncated": False,
  76. "fields": [
  77. {
  78. "tag": "input",
  79. "type": "hidden",
  80. "id": "RequestToken",
  81. "name": "__RequestVerificationToken",
  82. "required": False,
  83. "value": "private@example.test",
  84. }
  85. ],
  86. "button_count": 1,
  87. "buttons_truncated": False,
  88. "buttons": [
  89. {
  90. "tag": "button",
  91. "type": "submit",
  92. "id": "btnAvanti",
  93. "name": None,
  94. "text": "Avanti",
  95. "value": "server-secret",
  96. }
  97. ],
  98. "unknown": "must-not-appear",
  99. }
  100. return VSQueryResult(
  101. success=True,
  102. apt_type=apt_type,
  103. availability_status=AvailabilityStatus.NoneAvailable,
  104. )
  105. if self.behavior == "unexpected-error":
  106. self.page.popup_text = (
  107. "Messaggio per private@example.test, riferimento 12345678 "
  108. "token abcdefghijklmnopqrstuvwxyz123456."
  109. )
  110. raise RuntimeError(
  111. "failed at https://iam.esteri.it/signin/"
  112. "?state=sensitive-state&code_challenge=sensitive-challenge"
  113. )
  114. if self.behavior == "unexpected-result":
  115. return VSQueryResult(
  116. success=True,
  117. apt_type=apt_type,
  118. availability_status=AvailabilityStatus.Available,
  119. )
  120. return VSQueryResult(
  121. success=False,
  122. apt_type=apt_type,
  123. availability_status=AvailabilityStatus.NoneAvailable,
  124. )
  125. def cleanup(self):
  126. self.cleanup_called = True
  127. class DebugItaQueryConfigTests(unittest.TestCase):
  128. def test_default_query_config_uses_the_safe_compatibility_route(self):
  129. config = build_query_config(
  130. {},
  131. username="offline-test@example.test",
  132. password="offline-password",
  133. )
  134. self.assertEqual(
  135. config.free_config,
  136. {
  137. "query_mode": "services_probe",
  138. "final_submit_enabled": False,
  139. "apt_configs": {
  140. DEFAULT_ITA_ROUTING_KEY: {
  141. "service_id": DEFAULT_ITA_SERVICE_ID,
  142. "capability": "services_probe_only",
  143. }
  144. },
  145. },
  146. )
  147. def test_query_config_adds_only_an_explicit_persistent_profile(self):
  148. profile_path = Path("/private/local/ita-query-profile")
  149. config = build_query_config(
  150. {},
  151. username="offline-test@example.test",
  152. password="offline-password",
  153. persistent_profile_path=profile_path,
  154. )
  155. self.assertEqual(
  156. config.free_config,
  157. {
  158. "query_mode": "services_probe",
  159. "final_submit_enabled": False,
  160. "apt_configs": {
  161. DEFAULT_ITA_ROUTING_KEY: {
  162. "service_id": DEFAULT_ITA_SERVICE_ID,
  163. "capability": "services_probe_only",
  164. }
  165. },
  166. "persistent_browser_profile_path": str(profile_path),
  167. },
  168. )
  169. def test_prepared_profile_is_private_deterministic_and_hides_username(self):
  170. username = "Private.Query.Account@example.test"
  171. with tempfile.TemporaryDirectory() as temp_dir:
  172. profile_root = Path(temp_dir) / "profiles"
  173. first = prepare_ita_query_profile(
  174. username,
  175. profile_root=profile_root,
  176. )
  177. second = prepare_ita_query_profile(
  178. username.lower(),
  179. profile_root=profile_root,
  180. )
  181. self.assertEqual(first, second)
  182. self.assertNotIn("private.query.account", str(first).lower())
  183. if os.name == "posix":
  184. self.assertEqual(
  185. stat.S_IMODE(profile_root.stat().st_mode),
  186. 0o700,
  187. )
  188. self.assertEqual(stat.S_IMODE(first.stat().st_mode), 0o700)
  189. def test_prepare_query_profile_wraps_errors_without_exposing_username(self):
  190. private_username = "private-query@example.test"
  191. with patch(
  192. "tools.debug_ita_query.prepare_ita_query_profile",
  193. side_effect=ItaLocalProfileError(private_username),
  194. ):
  195. with self.assertRaises(DebugConfigurationError) as raised:
  196. _prepare_query_profile(private_username)
  197. self.assertNotIn(private_username, str(raised.exception))
  198. def test_accepts_an_explicit_non_default_route(self):
  199. env = {
  200. "ITA_ROUTING_KEY": "slot.sample.it.tourist",
  201. "ITA_SERVICE_ID": "9999",
  202. "ITA_CITY": "Sample City",
  203. "ITA_VISA_TYPE": "Short-stay tourism",
  204. }
  205. route = resolve_debug_route(env)
  206. config = build_query_config(
  207. env,
  208. username="offline-test@example.test",
  209. password="offline-password",
  210. route=route,
  211. )
  212. self.assertEqual(route.service_id, 9999)
  213. self.assertEqual(route.city, "Sample City")
  214. self.assertEqual(
  215. config.free_config["apt_configs"],
  216. {
  217. route.routing_key: {
  218. "service_id": 9999,
  219. "capability": "services_probe_only",
  220. }
  221. },
  222. )
  223. def test_local_query_account_takes_priority_over_environment(self):
  224. local_values = {
  225. "username": "local-query@example.test",
  226. "password": "local-password",
  227. "account_upload_token": "",
  228. }
  229. repo_root = Path(__file__).resolve().parents[1]
  230. with tempfile.TemporaryDirectory(dir=repo_root) as temp_dir:
  231. config_path = Path(temp_dir) / "ita_query_account.local.json"
  232. config_path.write_text(json.dumps(local_values), encoding="utf-8")
  233. config_path.chmod(0o600)
  234. with patch(
  235. "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE",
  236. config_path,
  237. ):
  238. credentials = _get_query_credentials(
  239. {
  240. "ITA_USERNAME": "environment@example.test",
  241. "ITA_PASSWORD": "environment-password",
  242. },
  243. non_interactive=True,
  244. )
  245. self.assertEqual(
  246. credentials,
  247. (local_values["username"], local_values["password"]),
  248. )
  249. def test_invalid_existing_local_file_fails_without_environment_fallback(self):
  250. local_values = {
  251. "username": "private-local@example.test",
  252. "password": "",
  253. "account_upload_token": "",
  254. }
  255. repo_root = Path(__file__).resolve().parents[1]
  256. with tempfile.TemporaryDirectory(dir=repo_root) as temp_dir:
  257. config_path = Path(temp_dir) / "ita_query_account.local.json"
  258. config_path.write_text(json.dumps(local_values), encoding="utf-8")
  259. config_path.chmod(0o600)
  260. with (
  261. patch(
  262. "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE",
  263. config_path,
  264. ),
  265. self.assertRaises(DebugConfigurationError) as raised,
  266. ):
  267. _get_query_credentials(
  268. {
  269. "ITA_USERNAME": "fallback@example.test",
  270. "ITA_PASSWORD": "fallback-password",
  271. },
  272. non_interactive=True,
  273. )
  274. self.assertIn("password is required", str(raised.exception))
  275. self.assertNotIn("private-local", str(raised.exception))
  276. self.assertNotIn("fallback-password", str(raised.exception))
  277. def test_missing_local_file_preserves_original_environment_fallback(self):
  278. with tempfile.TemporaryDirectory() as temp_dir:
  279. missing_path = Path(temp_dir) / "missing.json"
  280. with patch(
  281. "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE",
  282. missing_path,
  283. ):
  284. credentials = _get_query_credentials(
  285. {
  286. "ITA_USERNAME": "fallback@example.test",
  287. "ITA_PASSWORD": "fallback-password",
  288. },
  289. non_interactive=True,
  290. )
  291. self.assertEqual(
  292. credentials,
  293. ("fallback@example.test", "fallback-password"),
  294. )
  295. class DebugItaQuerySafetyTests(unittest.TestCase):
  296. def setUp(self):
  297. FakeQueryDebugPlugin.behavior = "none-available"
  298. FakeQueryDebugPlugin.last_instance = None
  299. @staticmethod
  300. def _run():
  301. stdout = io.StringIO()
  302. stderr = io.StringIO()
  303. env = {
  304. "ITA_USERNAME": "offline-test@example.test",
  305. "ITA_PASSWORD": "offline-password",
  306. }
  307. with (
  308. patch(
  309. "tools.debug_ita_query.resolve_chrome_path",
  310. return_value=Path("/test/chrome"),
  311. ),
  312. patch("tools.debug_ita_query.ItaPlugin", FakeQueryDebugPlugin),
  313. patch(
  314. "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE",
  315. Path("/definitely-missing/ita_query_account.local.json"),
  316. ),
  317. patch(
  318. "tools.debug_ita_query._prepare_query_profile",
  319. return_value=Path("/offline/ita-query-profile"),
  320. ),
  321. patch.dict("tools.debug_ita_query.os.environ", {}, clear=False),
  322. redirect_stdout(stdout),
  323. redirect_stderr(stderr),
  324. ):
  325. exit_code = run_query_once(env, non_interactive=True)
  326. return exit_code, stdout.getvalue(), stderr.getvalue()
  327. def test_no_slot_result_is_reported_after_exactly_one_query(self):
  328. exit_code, stdout, stderr = self._run()
  329. plugin = FakeQueryDebugPlugin.last_instance
  330. self.assertEqual(exit_code, 0)
  331. self.assertEqual(plugin.create_session_calls, 1)
  332. self.assertEqual(plugin.query_calls, 1)
  333. self.assertTrue(plugin.cleanup_called)
  334. self.assertIn("query_attempt=1", stdout)
  335. self.assertIn(
  336. "ITA_QUERY_NONE_AVAILABLE service_id=1321 "
  337. "routing_key=slot.dub.it.tourist status=NoAvailable",
  338. stdout,
  339. )
  340. self.assertNotIn("ITA_QUERY_MESSAGE_ZH", stdout)
  341. self.assertIn("ITA_QUERY_CLEANUP_OK", stdout)
  342. self.assertEqual(stderr, "")
  343. def test_weak_entry_signal_is_reported_safely_without_retry(self):
  344. FakeQueryDebugPlugin.behavior = "weak-entry-signal"
  345. exit_code, stdout, stderr = self._run()
  346. plugin = FakeQueryDebugPlugin.last_instance
  347. self.assertEqual(exit_code, 4)
  348. self.assertEqual(plugin.query_calls, 1)
  349. self.assertTrue(plugin.cleanup_called)
  350. self.assertIn(
  351. "ITA_QUERY_WEAK_SIGNAL service_id=1321 "
  352. "routing_key=slot.dub.it.tourist "
  353. "status=NoAvailable evidence=booking-form",
  354. stdout,
  355. )
  356. self.assertIn("ITA_BOOKING_FORM_STRUCTURE=", stdout)
  357. self.assertIn('"action_path": "/Services/Booking/1321"', stdout)
  358. self.assertIn('"name": "__RequestVerificationToken"', stdout)
  359. self.assertNotIn("private@example.test", stdout)
  360. self.assertNotIn("server-secret", stdout)
  361. self.assertNotIn("must-not-appear", stdout)
  362. self.assertNotIn("ITA_QUERY_FAILED", stdout)
  363. self.assertEqual(stderr, "")
  364. def test_unexpected_available_result_also_stops_safely(self):
  365. FakeQueryDebugPlugin.behavior = "unexpected-result"
  366. exit_code, stdout, _stderr = self._run()
  367. plugin = FakeQueryDebugPlugin.last_instance
  368. self.assertEqual(exit_code, 4)
  369. self.assertEqual(plugin.query_calls, 1)
  370. self.assertTrue(plugin.cleanup_called)
  371. self.assertIn("reason=non-empty-availability-result", stdout)
  372. def test_unexpected_error_is_sanitized_and_not_retried(self):
  373. FakeQueryDebugPlugin.behavior = "unexpected-error"
  374. exit_code, stdout, stderr = self._run()
  375. plugin = FakeQueryDebugPlugin.last_instance
  376. self.assertEqual(exit_code, 1)
  377. self.assertEqual(plugin.query_calls, 1)
  378. self.assertTrue(plugin.cleanup_called)
  379. self.assertIn("ITA_QUERY_FAILED type=RuntimeError", stdout)
  380. self.assertIn(
  381. "diagnostic_url=https://prenotami.esteri.it/Services",
  382. stdout,
  383. )
  384. self.assertNotIn("sensitive-state", stdout)
  385. self.assertNotIn("sensitive-challenge", stdout)
  386. self.assertNotIn("sensitive-fragment", stdout)
  387. self.assertIn("diagnostic_popup_text=Messaggio per <redacted-email>", stdout)
  388. self.assertNotIn("private@example.test", stdout)
  389. self.assertNotIn("12345678", stdout)
  390. self.assertNotIn("abcdefghijklmnopqrstuvwxyz123456", stdout)
  391. self.assertEqual(stderr, "")
  392. def test_entrypoint_has_one_query_call_and_no_book_call(self):
  393. source_path = (
  394. Path(__file__).resolve().parents[1] / "tools" / "debug_ita_query.py"
  395. )
  396. tree = ast.parse(source_path.read_text(encoding="utf-8"))
  397. query_calls = []
  398. book_calls = []
  399. for node in ast.walk(tree):
  400. if not isinstance(node, ast.Call):
  401. continue
  402. if not isinstance(node.func, ast.Attribute):
  403. continue
  404. if node.func.attr == "query":
  405. query_calls.append(node)
  406. if node.func.attr == "book":
  407. book_calls.append(node)
  408. self.assertEqual(len(query_calls), 1)
  409. self.assertEqual(book_calls, [])
  410. def test_book_debugger_never_uses_the_query_browser_profile(self):
  411. source_path = (
  412. Path(__file__).resolve().parents[1] / "tools" / "debug_ita_book.py"
  413. )
  414. source = source_path.read_text(encoding="utf-8")
  415. self.assertNotIn("prepare_ita_query_profile", source)
  416. self.assertNotIn("persistent_browser_profile_path", source)
  417. def test_booking_form_discovery_script_never_reads_values_or_submits(self):
  418. forbidden_fragments = (
  419. ".value",
  420. "getAttribute('value')",
  421. 'getAttribute("value")',
  422. "outerHTML",
  423. "innerHTML",
  424. "document.cookie",
  425. "localStorage",
  426. "sessionStorage",
  427. ".click(",
  428. ".submit(",
  429. )
  430. for fragment in forbidden_fragments:
  431. with self.subTest(fragment=fragment):
  432. self.assertNotIn(fragment, _BOOKING_FORM_DISCOVERY_SCRIPT)
  433. def test_booking_form_structure_uses_an_explicit_allowlist(self):
  434. sanitized = _sanitize_booking_form_structure(
  435. {
  436. "path": "/Services/Booking/1321",
  437. "form": {
  438. "method": "POST",
  439. "action_path": "/Services/Booking/1321",
  440. "csrf": "server-secret",
  441. },
  442. "fields": [
  443. {
  444. "tag": "input",
  445. "type": "text",
  446. "id": "Email",
  447. "name": "Email",
  448. "required": True,
  449. "value": "private@example.test",
  450. }
  451. ],
  452. "buttons": [],
  453. "raw_html": "must-not-appear",
  454. }
  455. )
  456. serialized = str(sanitized)
  457. self.assertIn("/Services/Booking/1321", serialized)
  458. self.assertNotIn("private@example.test", serialized)
  459. self.assertNotIn("server-secret", serialized)
  460. self.assertNotIn("must-not-appear", serialized)
  461. self.assertNotIn("value", sanitized["fields"][0])
  462. if __name__ == "__main__":
  463. unittest.main()