import ast import io import json import os import stat import tempfile import unittest from contextlib import redirect_stderr, redirect_stdout from pathlib import Path from unittest.mock import patch from tools.debug_ita_query import ( _BOOKING_FORM_DISCOVERY_SCRIPT, DEFAULT_ITA_ROUTING_KEY, DEFAULT_ITA_SERVICE_ID, _get_query_credentials, _prepare_query_profile, _sanitize_booking_form_structure, build_query_config, resolve_debug_route, run_query_once, ) from tools.debug_ita_session import DebugConfigurationError from utils.ita_local_profile import ItaLocalProfileError, prepare_ita_query_profile from vs_types import ( AvailabilityStatus, BizLogicError, VSQueryResult, ) class FakeQueryDebugElement: def __init__(self, text): self.text = text class FakeQueryDebugPage: def __init__(self): self.url = ( "https://prenotami.esteri.it/Services" "?state=sensitive-state#sensitive-fragment" ) self.popup_text = "" self.form_structure = None def ele(self, selector, timeout=None): del timeout if selector == ".jconfirm-content" and self.popup_text: return FakeQueryDebugElement(self.popup_text) return None def run_js(self, script): self.last_script = script return self.form_structure class FakeQueryDebugPlugin: behavior = "none-available" last_instance = None def __init__(self, group_id): self.group_id = group_id self.page = FakeQueryDebugPage() self.create_session_calls = 0 self.query_calls = 0 self.cleanup_called = False FakeQueryDebugPlugin.last_instance = self def set_log(self, logger): self.logger = logger def set_config(self, config): self.config = config def create_session(self): self.create_session_calls += 1 def query(self, apt_type): self.query_calls += 1 if self.behavior == "weak-entry-signal": self.page.form_structure = { "path": "/Services/Booking/1321", "form": { "method": "POST", "action_path": "/Services/Booking/1321", "forbidden": "must-not-appear", }, "field_count": 1, "fields_truncated": False, "fields": [ { "tag": "input", "type": "hidden", "id": "RequestToken", "name": "__RequestVerificationToken", "required": False, "value": "private@example.test", } ], "button_count": 1, "buttons_truncated": False, "buttons": [ { "tag": "button", "type": "submit", "id": "btnAvanti", "name": None, "text": "Avanti", "value": "server-secret", } ], "unknown": "must-not-appear", } return VSQueryResult( success=True, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable, ) if self.behavior == "unexpected-error": self.page.popup_text = ( "Messaggio per private@example.test, riferimento 12345678 " "token abcdefghijklmnopqrstuvwxyz123456." ) raise RuntimeError( "failed at https://iam.esteri.it/signin/" "?state=sensitive-state&code_challenge=sensitive-challenge" ) if self.behavior == "unexpected-result": return VSQueryResult( success=True, apt_type=apt_type, availability_status=AvailabilityStatus.Available, ) return VSQueryResult( success=False, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable, ) def cleanup(self): self.cleanup_called = True class DebugItaQueryConfigTests(unittest.TestCase): def test_default_query_config_uses_the_safe_compatibility_route(self): config = build_query_config( {}, username="offline-test@example.test", password="offline-password", ) self.assertEqual( config.free_config, { "query_mode": "services_probe", "final_submit_enabled": False, "apt_configs": { DEFAULT_ITA_ROUTING_KEY: { "service_id": DEFAULT_ITA_SERVICE_ID, "capability": "services_probe_only", } }, }, ) def test_query_config_adds_only_an_explicit_persistent_profile(self): profile_path = Path("/private/local/ita-query-profile") config = build_query_config( {}, username="offline-test@example.test", password="offline-password", persistent_profile_path=profile_path, ) self.assertEqual( config.free_config, { "query_mode": "services_probe", "final_submit_enabled": False, "apt_configs": { DEFAULT_ITA_ROUTING_KEY: { "service_id": DEFAULT_ITA_SERVICE_ID, "capability": "services_probe_only", } }, "persistent_browser_profile_path": str(profile_path), }, ) def test_prepared_profile_is_private_deterministic_and_hides_username(self): username = "Private.Query.Account@example.test" with tempfile.TemporaryDirectory() as temp_dir: profile_root = Path(temp_dir) / "profiles" first = prepare_ita_query_profile( username, profile_root=profile_root, ) second = prepare_ita_query_profile( username.lower(), profile_root=profile_root, ) self.assertEqual(first, second) self.assertNotIn("private.query.account", str(first).lower()) if os.name == "posix": self.assertEqual( stat.S_IMODE(profile_root.stat().st_mode), 0o700, ) self.assertEqual(stat.S_IMODE(first.stat().st_mode), 0o700) def test_prepare_query_profile_wraps_errors_without_exposing_username(self): private_username = "private-query@example.test" with patch( "tools.debug_ita_query.prepare_ita_query_profile", side_effect=ItaLocalProfileError(private_username), ): with self.assertRaises(DebugConfigurationError) as raised: _prepare_query_profile(private_username) self.assertNotIn(private_username, str(raised.exception)) def test_accepts_an_explicit_non_default_route(self): env = { "ITA_ROUTING_KEY": "slot.sample.it.tourist", "ITA_SERVICE_ID": "9999", "ITA_CITY": "Sample City", "ITA_VISA_TYPE": "Short-stay tourism", } route = resolve_debug_route(env) config = build_query_config( env, username="offline-test@example.test", password="offline-password", route=route, ) self.assertEqual(route.service_id, 9999) self.assertEqual(route.city, "Sample City") self.assertEqual( config.free_config["apt_configs"], { route.routing_key: { "service_id": 9999, "capability": "services_probe_only", } }, ) def test_local_query_account_takes_priority_over_environment(self): local_values = { "username": "local-query@example.test", "password": "local-password", "account_upload_token": "", } repo_root = Path(__file__).resolve().parents[1] with tempfile.TemporaryDirectory(dir=repo_root) as temp_dir: config_path = Path(temp_dir) / "ita_query_account.local.json" config_path.write_text(json.dumps(local_values), encoding="utf-8") config_path.chmod(0o600) with patch( "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE", config_path, ): credentials = _get_query_credentials( { "ITA_USERNAME": "environment@example.test", "ITA_PASSWORD": "environment-password", }, non_interactive=True, ) self.assertEqual( credentials, (local_values["username"], local_values["password"]), ) def test_invalid_existing_local_file_fails_without_environment_fallback(self): local_values = { "username": "private-local@example.test", "password": "", "account_upload_token": "", } repo_root = Path(__file__).resolve().parents[1] with tempfile.TemporaryDirectory(dir=repo_root) as temp_dir: config_path = Path(temp_dir) / "ita_query_account.local.json" config_path.write_text(json.dumps(local_values), encoding="utf-8") config_path.chmod(0o600) with ( patch( "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE", config_path, ), self.assertRaises(DebugConfigurationError) as raised, ): _get_query_credentials( { "ITA_USERNAME": "fallback@example.test", "ITA_PASSWORD": "fallback-password", }, non_interactive=True, ) self.assertIn("password is required", str(raised.exception)) self.assertNotIn("private-local", str(raised.exception)) self.assertNotIn("fallback-password", str(raised.exception)) def test_missing_local_file_preserves_original_environment_fallback(self): with tempfile.TemporaryDirectory() as temp_dir: missing_path = Path(temp_dir) / "missing.json" with patch( "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE", missing_path, ): credentials = _get_query_credentials( { "ITA_USERNAME": "fallback@example.test", "ITA_PASSWORD": "fallback-password", }, non_interactive=True, ) self.assertEqual( credentials, ("fallback@example.test", "fallback-password"), ) class DebugItaQuerySafetyTests(unittest.TestCase): def setUp(self): FakeQueryDebugPlugin.behavior = "none-available" FakeQueryDebugPlugin.last_instance = None @staticmethod def _run(): stdout = io.StringIO() stderr = io.StringIO() env = { "ITA_USERNAME": "offline-test@example.test", "ITA_PASSWORD": "offline-password", } with ( patch( "tools.debug_ita_query.resolve_chrome_path", return_value=Path("/test/chrome"), ), patch("tools.debug_ita_query.ItaPlugin", FakeQueryDebugPlugin), patch( "tools.debug_ita_query.LOCAL_QUERY_ACCOUNT_FILE", Path("/definitely-missing/ita_query_account.local.json"), ), patch( "tools.debug_ita_query._prepare_query_profile", return_value=Path("/offline/ita-query-profile"), ), patch.dict("tools.debug_ita_query.os.environ", {}, clear=False), redirect_stdout(stdout), redirect_stderr(stderr), ): exit_code = run_query_once(env, non_interactive=True) return exit_code, stdout.getvalue(), stderr.getvalue() def test_no_slot_result_is_reported_after_exactly_one_query(self): exit_code, stdout, stderr = self._run() plugin = FakeQueryDebugPlugin.last_instance self.assertEqual(exit_code, 0) self.assertEqual(plugin.create_session_calls, 1) self.assertEqual(plugin.query_calls, 1) self.assertTrue(plugin.cleanup_called) self.assertIn("query_attempt=1", stdout) self.assertIn( "ITA_QUERY_NONE_AVAILABLE service_id=1321 " "routing_key=slot.dub.it.tourist status=NoAvailable", stdout, ) self.assertNotIn("ITA_QUERY_MESSAGE_ZH", stdout) self.assertIn("ITA_QUERY_CLEANUP_OK", stdout) self.assertEqual(stderr, "") def test_weak_entry_signal_is_reported_safely_without_retry(self): FakeQueryDebugPlugin.behavior = "weak-entry-signal" exit_code, stdout, stderr = self._run() plugin = FakeQueryDebugPlugin.last_instance self.assertEqual(exit_code, 4) self.assertEqual(plugin.query_calls, 1) self.assertTrue(plugin.cleanup_called) self.assertIn( "ITA_QUERY_WEAK_SIGNAL service_id=1321 " "routing_key=slot.dub.it.tourist " "status=NoAvailable evidence=booking-form", stdout, ) self.assertIn("ITA_BOOKING_FORM_STRUCTURE=", stdout) self.assertIn('"action_path": "/Services/Booking/1321"', stdout) self.assertIn('"name": "__RequestVerificationToken"', stdout) self.assertNotIn("private@example.test", stdout) self.assertNotIn("server-secret", stdout) self.assertNotIn("must-not-appear", stdout) self.assertNotIn("ITA_QUERY_FAILED", stdout) self.assertEqual(stderr, "") def test_unexpected_available_result_also_stops_safely(self): FakeQueryDebugPlugin.behavior = "unexpected-result" exit_code, stdout, _stderr = self._run() plugin = FakeQueryDebugPlugin.last_instance self.assertEqual(exit_code, 4) self.assertEqual(plugin.query_calls, 1) self.assertTrue(plugin.cleanup_called) self.assertIn("reason=non-empty-availability-result", stdout) def test_unexpected_error_is_sanitized_and_not_retried(self): FakeQueryDebugPlugin.behavior = "unexpected-error" exit_code, stdout, stderr = self._run() plugin = FakeQueryDebugPlugin.last_instance self.assertEqual(exit_code, 1) self.assertEqual(plugin.query_calls, 1) self.assertTrue(plugin.cleanup_called) self.assertIn("ITA_QUERY_FAILED type=RuntimeError", stdout) self.assertIn( "diagnostic_url=https://prenotami.esteri.it/Services", stdout, ) self.assertNotIn("sensitive-state", stdout) self.assertNotIn("sensitive-challenge", stdout) self.assertNotIn("sensitive-fragment", stdout) self.assertIn("diagnostic_popup_text=Messaggio per ", stdout) self.assertNotIn("private@example.test", stdout) self.assertNotIn("12345678", stdout) self.assertNotIn("abcdefghijklmnopqrstuvwxyz123456", stdout) self.assertEqual(stderr, "") def test_entrypoint_has_one_query_call_and_no_book_call(self): source_path = ( Path(__file__).resolve().parents[1] / "tools" / "debug_ita_query.py" ) tree = ast.parse(source_path.read_text(encoding="utf-8")) query_calls = [] book_calls = [] for node in ast.walk(tree): if not isinstance(node, ast.Call): continue if not isinstance(node.func, ast.Attribute): continue if node.func.attr == "query": query_calls.append(node) if node.func.attr == "book": book_calls.append(node) self.assertEqual(len(query_calls), 1) self.assertEqual(book_calls, []) def test_book_debugger_never_uses_the_query_browser_profile(self): source_path = ( Path(__file__).resolve().parents[1] / "tools" / "debug_ita_book.py" ) source = source_path.read_text(encoding="utf-8") self.assertNotIn("prepare_ita_query_profile", source) self.assertNotIn("persistent_browser_profile_path", source) def test_booking_form_discovery_script_never_reads_values_or_submits(self): forbidden_fragments = ( ".value", "getAttribute('value')", 'getAttribute("value")', "outerHTML", "innerHTML", "document.cookie", "localStorage", "sessionStorage", ".click(", ".submit(", ) for fragment in forbidden_fragments: with self.subTest(fragment=fragment): self.assertNotIn(fragment, _BOOKING_FORM_DISCOVERY_SCRIPT) def test_booking_form_structure_uses_an_explicit_allowlist(self): sanitized = _sanitize_booking_form_structure( { "path": "/Services/Booking/1321", "form": { "method": "POST", "action_path": "/Services/Booking/1321", "csrf": "server-secret", }, "fields": [ { "tag": "input", "type": "text", "id": "Email", "name": "Email", "required": True, "value": "private@example.test", } ], "buttons": [], "raw_html": "must-not-appear", } ) serialized = str(sanitized) self.assertIn("/Services/Booking/1321", serialized) self.assertNotIn("private@example.test", serialized) self.assertNotIn("server-secret", serialized) self.assertNotIn("must-not-appear", serialized) self.assertNotIn("value", sanitized["fields"][0]) if __name__ == "__main__": unittest.main()