import inspect import json import shutil import subprocess import sys import threading import types import unittest from datetime import date from pathlib import Path from tempfile import TemporaryDirectory from unittest.mock import Mock, patch from plugins.ita_plugin import ( BOOKING_FORM_DISCOVERY_STOP_LABEL, BOOKING_RECHECK_NONE_AVAILABLE_LABEL, PERSISTENT_BROWSER_PROFILE_KEY, SERVICE_LINK_WAIT_SECONDS, SERVICE_LIST_DIAGNOSTIC_SCRIPT, ItaAuthenticationChallengeError, ItaPlugin, ItaTargetServiceLinkMissingError, ) try: from booker import OrderBookerGCO except ModuleNotFoundError as exc: if exc.name != "redis": raise redis_stub = types.ModuleType("redis") redis_stub.Redis = Mock with patch.dict(sys.modules, {"redis": redis_stub}): from booker import OrderBookerGCO from vs_types import ( AppointmentType, AvailabilityStatus, BizLogicError, GroupConfig, SessionExpiredOrInvalidError, Task, VSAccount, VSPlgConfig, VSQueryResult, ) class FakeChromiumOptions: def set_local_port(self, _port): return self def set_user_data_path(self, _path): return self def set_paths(self, **_kwargs): return self def set_argument(self, _argument): return self def headless(self, _enabled): return self class FakeWait: def doc_loaded(self): return None def load_start(self): return None class FakeRect: viewport_size = (1920, 1080) class FakeElement: def __init__(self, page, name): self.page = page self.name = name def input(self, value, clear=False): self.page.input_values[self.name] = { "value": value, "clear": clear, } class FakePage: URLS = { "landing": "https://prenotami.esteri.it/", "pingid": ( "https://iam.esteri.it/signin/?realm=/external" "&state=sensitive-state" "&code_challenge=sensitive-challenge#/" ), "dashboard": "https://prenotami.esteri.it/UserArea", "services": "https://prenotami.esteri.it/Services", "radware": ( "https://validate.perfdrive.com/" "8ab9454422ad153e7d4ce3a5b41556a7/" ), } def __init__( self, *, submit_succeeds=True, has_services_link=True, login_target="pingid", ): self.state = "landing" self.submit_succeeds = submit_succeeds self.has_services_link = has_services_link self.login_target = login_target self.wait = FakeWait() self.rect = FakeRect() self.clicked = [] self.input_values = {} self.refresh_calls = 0 self.quit_called = False self.quit_force = None @property def url(self): return self.URLS[self.state] @property def title(self): return { "landing": "Login", "pingid": "Sign in", "dashboard": "Home", "services": "Services", "radware": "Access Denied", }[self.state] @property def html(self): return "" def get(self, _url): self.state = "landing" def refresh(self): self.refresh_calls += 1 def quit(self, force=False): self.quit_called = True self.quit_force = force def ele(self, selector, timeout=None): del timeout if selector == "tag:a@@href=/Language/ChangeLanguage?lang=2": return FakeElement(self, "language") if self.state == "landing" else None if selector == "#pingid-button" and self.state == "landing": return FakeElement(self, "login") if selector == "@name=callback_1" and self.state == "pingid": return FakeElement(self, "username") if selector == "@name=callback_2" and self.state == "pingid": return FakeElement(self, "password") if selector == "tag:button@type=submit" and self.state == "pingid": return FakeElement(self, "submit") if selector == ".app-menu" and self.state == "dashboard": return FakeElement(self, "app-menu") if ( selector == "@href=/Services" and self.state == "dashboard" and self.has_services_link ): return FakeElement(self, "book") return None class FakeMouse: def __init__(self, page, debug=False): self.page = page self.debug = debug def move(self, _x, _y): return None def human_click_ele(self, element): if element is None: raise AssertionError("The state machine attempted to click a missing element") self.page.clicked.append(element.name) if element.name == "login": self.page.state = self.page.login_target elif element.name == "submit" and self.page.submit_succeeds: self.page.state = "dashboard" elif element.name == "book": self.page.state = "services" class FakeKeyboard: def __init__(self, page): self.page = page class FakeQueryWait: def __init__(self): self.load_start_calls = 0 self.doc_loaded_calls = 0 def load_start(self): self.load_start_calls += 1 def doc_loaded(self): self.doc_loaded_calls += 1 class FakeQueryElement: def __init__( self, page, name, attributes=None, *, text="", property_value=None, ): self.page = page self.name = name self.attributes = attributes or {} self.text = text self.value = property_value def attr(self, name): return self.attributes.get(name) class FakeQueryPage: URLS = { "services": "https://prenotami.esteri.it/Services", "user_area": "https://prenotami.esteri.it/UserArea", "no_slots": "https://prenotami.esteri.it/Services", "property_no_slots": "https://prenotami.esteri.it/Services", "visible_no_slots": "https://prenotami.esteri.it/Services", "unknown_popup": "https://prenotami.esteri.it/Services", "empty_warning": "https://prenotami.esteri.it/Services", "login": "https://iam.esteri.it/login/oauth2/authorize", "calendar": "https://prenotami.esteri.it/BookingCalendar", "unknown": "https://prenotami.esteri.it/Unexpected", } def __init__( self, outcome="no_slots", *, has_link=True, initial_state="services", service_id=1321, already_booked_notice_rows=0, ): self.outcome = outcome self.has_link = has_link self.state = initial_state self.service_id = service_id self.already_booked_notice_rows = already_booked_notice_rows self.wait = FakeQueryWait() self.clicked = [] self.get_calls = [] self.looked_up = [] self.run_js_calls = [] @property def url(self): if self.state in {"booking_form", "booking_path_without_form"}: return ( "https://prenotami.esteri.it/Services/Booking/" f"{self.service_id}" ) return self.URLS[self.state] def get(self, url): self.get_calls.append(url) if url != "https://prenotami.esteri.it/Services": raise AssertionError(f"Unexpected query navigation: {url}") self.state = "services" def run_js(self, script): self.run_js_calls.append(script) if "coordinate:ita-service-list-structure" in script: return { "path": "/Services", "ready_state": "complete", "table_count": 1, "row_count": 0, "booking_link_count": 0, "target_link_count": 0, "already_booked_notice_row_count": self.already_booked_notice_rows, } if self.state != "booking_form": return None return { "path": f"/Services/Booking/{self.service_id}", "form": { "method": "POST", "action_path": f"/Services/Booking/{self.service_id}", "untrusted": "must-not-survive", }, "field_count": 2, "fields_truncated": False, "fields": [ { "tag": "input", "type": "hidden", "id": "RequestToken", "name": "__RequestVerificationToken", "required": False, "value": "must-not-survive", }, { "tag": "select", "type": None, "id": "BookingType", "name": "BookingType", "required": True, }, ], "button_count": 1, "buttons_truncated": False, "buttons": [ { "tag": "button", "type": "submit", "id": "btnAvanti", "name": None, "text": "Avanti", "value": "must-not-survive", } ], "untrusted": "must-not-survive", } def ele(self, selector, timeout=None): self.looked_up.append((selector, timeout)) if selector == "#pingid-button" and self.state == "login": return FakeQueryElement(self, "login") if ( selector == f"tag:a@@href=/Services/Booking/{self.service_id}" and self.state == "services" and self.has_link ): return FakeQueryElement(self, f"booking-{self.service_id}") if selector == "#WNotAvailable" and self.state == "no_slots": return FakeQueryElement( self, "not-available", {"value": "No appointments are currently available."}, ) if selector == "#WNotAvailable" and self.state == "property_no_slots": return FakeQueryElement( self, "property-not-available", property_value="No appointments are currently available.", ) if selector == "#WNotAvailable" and self.state == "empty_warning": return FakeQueryElement(self, "empty-warning", {"value": " "}) if selector == ".jconfirm-content" and self.state == "visible_no_slots": return FakeQueryElement( self, "visible-not-available", text=( " STANTE L’ELEVATA RICHIESTA\n" "i posti disponibili per il servizio scelto sono esauriti. " ), ) if selector == ".jconfirm-content" and self.state == "unknown_popup": return FakeQueryElement( self, "unknown-popup", text="Il servizio richiede una nuova verifica.", ) if selector == "#bookingForm" and self.state == "booking_form": return FakeQueryElement(self, "booking-form") return None class FakeQueryMouse: def __init__(self, page): self.page = page def human_click_ele(self, element): if element is None: raise AssertionError("Query attempted to click a missing element") self.page.clicked.append(element.name) self.page.state = self.page.outcome class ItaPluginRandomSleepTests(unittest.TestCase): def test_random_sleep_uses_generated_duration_within_requested_range(self): plugin = ItaPlugin.__new__(ItaPlugin) plugin.group_id = "test-group" plugin.logger = Mock() plugin.page = None plugin.root_workspace = "/nonexistent/ita-plugin-unit-test" plugin.tunnel = None with ( patch("plugins.ita_plugin.random.uniform", return_value=4.25) as uniform, patch("plugins.ita_plugin.time.sleep") as sleep, ): plugin._random_sleep(3, 5) uniform.assert_called_once_with(3, 5) sleep.assert_called_once_with(4.25) plugin.logger.assert_called_once_with( "[ItaPlugin] [test-group] " "Anti-Rate-Limit: Sleeping for 4.25 seconds..." ) class ItaPluginCreateSessionStateMachineTests(unittest.TestCase): @staticmethod def _config(): return VSPlgConfig( account=VSAccount( id=0, username="offline-test@example.test", password="offline-password", ) ) def test_stops_at_authenticated_user_area_without_opening_services(self): fake_page = FakePage() config = self._config() logger = Mock() with TemporaryDirectory() as workspace: with patch( "plugins.ita_plugin.os.path.abspath", return_value=str(Path(workspace)), ): plugin = ItaPlugin("offline-state-machine") plugin.set_log(logger) plugin.set_config(config) with ( patch("plugins.ita_plugin.ChromiumOptions", FakeChromiumOptions), patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page), patch("plugins.ita_plugin.HumanMouse", FakeMouse), patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard), patch("plugins.ita_plugin.time.sleep"), ): try: plugin.create_session() finally: plugin.cleanup() self.assertEqual(fake_page.state, "dashboard") self.assertGreater(plugin.session_create_time, 0) self.assertEqual( fake_page.clicked, ["login", "username", "password", "submit"], ) self.assertEqual( fake_page.input_values, { "username": { "value": "offline-test@example.test", "clear": True, }, "password": { "value": "offline-password", "clear": True, }, }, ) self.assertEqual(fake_page.refresh_calls, 0) self.assertTrue(fake_page.quit_called) log_output = "\n".join( call.args[0] for call in logger.call_args_list if call.args ) self.assertIn("https://iam.esteri.it/signin/", log_output) self.assertNotIn("sensitive-state", log_output) self.assertNotIn("sensitive-challenge", log_output) def test_never_resubmits_credentials_or_refreshes_after_login_submission(self): fake_page = FakePage(submit_succeeds=False) config = self._config() with TemporaryDirectory() as workspace: with patch( "plugins.ita_plugin.os.path.abspath", return_value=str(Path(workspace)), ): plugin = ItaPlugin("offline-stalled-login") plugin.set_log(Mock()) plugin.set_config(config) with ( patch("plugins.ita_plugin.ChromiumOptions", FakeChromiumOptions), patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page), patch("plugins.ita_plugin.HumanMouse", FakeMouse), patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard), patch("plugins.ita_plugin.time.sleep"), ): try: with self.assertRaisesRegex( BizLogicError, "Failed to create authenticated /UserArea session", ): plugin.create_session() finally: plugin.cleanup() self.assertEqual(fake_page.clicked.count("login"), 1) self.assertEqual(fake_page.clicked.count("username"), 1) self.assertEqual(fake_page.clicked.count("password"), 1) self.assertEqual(fake_page.clicked.count("submit"), 1) self.assertEqual(fake_page.refresh_calls, 0) def test_requires_user_area_services_link_as_authentication_marker(self): fake_page = FakePage(has_services_link=False) config = self._config() with TemporaryDirectory() as workspace: with patch( "plugins.ita_plugin.os.path.abspath", return_value=str(Path(workspace)), ): plugin = ItaPlugin("offline-incomplete-dashboard") plugin.set_log(Mock()) plugin.set_config(config) with ( patch("plugins.ita_plugin.ChromiumOptions", FakeChromiumOptions), patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page), patch("plugins.ita_plugin.HumanMouse", FakeMouse), patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard), patch("plugins.ita_plugin.time.sleep"), ): try: with self.assertRaisesRegex( BizLogicError, "Failed to create authenticated /UserArea session", ): plugin.create_session() finally: plugin.cleanup() self.assertEqual(fake_page.state, "dashboard") self.assertEqual(fake_page.clicked.count("submit"), 1) def test_create_session_source_has_no_language_or_services_navigation(self): create_session_source = inspect.getsource(ItaPlugin.create_session) self.assertNotIn("ChangeLanguage", create_session_source) self.assertNotIn("book_btn", create_session_source) def test_radware_validation_is_classified_before_credentials_or_refresh(self): fake_page = FakePage(login_target="radware") config = self._config() logger = Mock() with TemporaryDirectory() as workspace: with patch( "plugins.ita_plugin.os.path.abspath", return_value=str(Path(workspace)), ): plugin = ItaPlugin("offline-radware") plugin.set_log(logger) plugin.set_config(config) with ( patch("plugins.ita_plugin.ChromiumOptions", FakeChromiumOptions), patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page), patch("plugins.ita_plugin.HumanMouse", FakeMouse), patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard), patch("plugins.ita_plugin.time.sleep"), ): try: with self.assertRaisesRegex( ItaAuthenticationChallengeError, "Radware browser validation", ): plugin.create_session() finally: plugin.cleanup() self.assertEqual(fake_page.clicked, ["login"]) self.assertEqual(fake_page.input_values, {}) self.assertEqual(fake_page.refresh_calls, 0) log_output = "\n".join( call.args[0] for call in logger.call_args_list if call.args ) self.assertIn( "https://validate.perfdrive.com/", log_output, ) self.assertNotIn("8ab9454422ad153e7d4ce3a5b41556a7", log_output) class ItaPluginPersistentProfileTests(unittest.TestCase): @staticmethod def _new_plugin(*, transient_root: Path, profile_path: Path) -> ItaPlugin: with patch( "plugins.ita_plugin.os.path.abspath", return_value=str(transient_root), ): plugin = ItaPlugin("offline-persistent-profile") plugin.set_config( VSPlgConfig( account=VSAccount( id=0, username="offline-test@example.test", password="offline-password", ), free_config={ PERSISTENT_BROWSER_PROFILE_KEY: str(profile_path), }, ) ) return plugin def test_cleanup_preserves_explicit_persistent_profile(self): with TemporaryDirectory() as temp_dir: base = Path(temp_dir) profile_path = base / "persistent" / "profile-id" profile_path.mkdir(parents=True, mode=0o700) profile_path.chmod(0o700) marker = profile_path / "cookie-state" marker.write_text("offline", encoding="utf-8") transient_root = base / "transient" plugin = self._new_plugin( transient_root=transient_root, profile_path=profile_path, ) plugin.cleanup() self.assertEqual(plugin.user_data_path, str(profile_path.resolve())) self.assertTrue(marker.exists()) self.assertFalse(transient_root.exists()) def test_default_cleanup_still_deletes_transient_profile(self): with TemporaryDirectory() as temp_dir: transient_root = Path(temp_dir) / "transient" with patch( "plugins.ita_plugin.os.path.abspath", return_value=str(transient_root), ): plugin = ItaPlugin("offline-transient-profile") marker = Path(plugin.user_data_path) / "cookie-state" marker.parent.mkdir(parents=True) marker.write_text("offline", encoding="utf-8") plugin.cleanup() self.assertFalse(transient_root.exists()) def test_persistent_profile_is_exclusively_locked_until_cleanup(self): with TemporaryDirectory() as temp_dir: base = Path(temp_dir) profile_path = base / "persistent" / "profile-id" profile_path.mkdir(parents=True, mode=0o700) profile_path.chmod(0o700) first = self._new_plugin( transient_root=base / "transient-one", profile_path=profile_path, ) second = self._new_plugin( transient_root=base / "transient-two", profile_path=profile_path, ) try: first._acquire_persistent_profile_lock() with self.assertRaisesRegex(BizLogicError, "already in use"): second._acquire_persistent_profile_lock() finally: first.cleanup() try: second._acquire_persistent_profile_lock() self.assertIsNotNone(second._profile_lock_handle) finally: second.cleanup() def test_rejects_persistent_profile_inside_temporary_workspace(self): with TemporaryDirectory() as temp_dir: transient_root = Path(temp_dir) / "transient" profile_path = transient_root / "user_data" profile_path.mkdir(parents=True, mode=0o700) profile_path.chmod(0o700) with self.assertRaisesRegex( BizLogicError, "outside the temporary workspace", ): self._new_plugin( transient_root=transient_root, profile_path=profile_path, ) @unittest.skipUnless(shutil.which("node"), "Node is required for the DOM-script fixture") class ItaServiceNoticeScriptTests(unittest.TestCase): def _inspect(self, rows): # Execute the production JS against a synthetic DOM, never a live tab. harness = r""" const payload = JSON.parse(require('fs').readFileSync(0, 'utf8')); const rows = payload.rows.map((cells) => ({cells: cells.map((cell) => ({ innerText: cell.text, getClientRects: () => cell.noSize ? [] : [{}], style: {visibility: cell.visibility || 'visible'}, }))})); const table = {querySelectorAll: (selector) => { if (selector !== 'tbody tr') throw Error('unexpected table selector'); return rows; }}; const document = {readyState: 'complete', querySelectorAll: (selector) => { if (selector === 'table') return [table]; if (selector === 'a[href^="/Services/Booking/"]') return []; throw Error('unexpected document selector'); }}; const window = {location: {pathname: '/Services'}, getComputedStyle: (cell) => cell.style}; process.stdout.write(JSON.stringify(new Function('document', 'window', payload.script)(document, window))); """ result = subprocess.run( [shutil.which("node"), "-e", harness], input=json.dumps({"rows": rows, "script": SERVICE_LIST_DIAGNOSTIC_SCRIPT}), text=True, capture_output=True, check=True, timeout=10, ) return json.loads(result.stdout) def test_only_exact_rendered_notice_matches(self): notice = "Prenotazione già effettuata per questo servizio" for cell, expected in ( ({"text": notice}, 1), ({"text": " PRENOTAZIONE GIÀ\nEFFETTUATA PER QUESTO\u00a0SERVIZIO. "}, 1), ({"text": notice, "noSize": True}, 0), ({"text": notice, "visibility": "hidden"}, 0), ({"text": "Non " + notice}, 0), ({"text": "PRENOTA"}, 0), ({"text": "Annullata"}, 0), ({"text": "Unknown notice"}, 0), ): with self.subTest(cell=cell): self.assertEqual(self._inspect([[cell]])["already_booked_notice_row_count"], expected) def test_counts_rows_not_cells_and_does_not_export_row_content(self): notice = {"text": "Prenotazione già effettuata per questo servizio"} result = self._inspect([[notice, notice, {"text": "synthetic-private-row-value"}]]) self.assertEqual(result["already_booked_notice_row_count"], 1) self.assertEqual(result["row_count"], 1) self.assertEqual(result["target_link_count"], 0) self.assertNotIn("synthetic-private-row-value", json.dumps(result)) self.assertNotIn("Prenotazione", json.dumps(result)) class ItaPluginQuerySafetyTests(unittest.TestCase): @staticmethod def _appointment_type( routing_key="slot.dub.it.tourist", *, city="Dublin", ): return AppointmentType( routing_key=routing_key, city=city, country="Italy", visa_type="Tourist", ) @staticmethod def _plugin( page, *, service_id=1321, routing_key="slot.dub.it.tourist", query_mode="services_probe", capability="services_probe_only", lookahead_months=3, ): plugin = ItaPlugin.__new__(ItaPlugin) plugin.group_id = "offline-query" plugin.logger = Mock() plugin.page = page plugin.mouse = FakeQueryMouse(page) route = { "service_id": service_id, "capability": capability, } if capability == "single_schengen_tourism": route.update( { "calendar_scan": { "lookahead_months": lookahead_months, }, "form": { "booking_type_control_id": "typeofbookingddl", "booking_type_value": "1", "booking_type_labels": ["Single booking"], "purpose_control_id": "ddls_0", "purpose_value": "42", "purpose_labels": ["Tourism"], }, } ) plugin.free_config = { "query_mode": query_mode, "final_submit_enabled": False, "apt_configs": { routing_key: route, }, } plugin.ita_url = "https://prenotami.esteri.it" plugin.root_workspace = "/nonexistent/ita-plugin-query-test" plugin.tunnel = None plugin._calendar_query_route_key = ( routing_key if query_mode == "calendar" else None ) return plugin def test_returns_none_available_only_for_non_empty_w_not_available(self): page = FakeQueryPage(outcome="no_slots") plugin = self._plugin(page) apt_type = self._appointment_type() result = plugin.query(apt_type) self.assertFalse(result.success) self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable) self.assertEqual(result.apt_type, apt_type) self.assertEqual(page.clicked, ["booking-1321"]) self.assertEqual(page.wait.load_start_calls, 1) self.assertEqual(page.wait.doc_loaded_calls, 2) def test_returns_none_available_for_property_only_w_not_available(self): page = FakeQueryPage(outcome="property_no_slots") plugin = self._plugin(page) result = plugin.query(self._appointment_type()) self.assertFalse(result.success) self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable) self.assertEqual(page.clicked, ["booking-1321"]) def test_returns_none_available_for_verified_visible_italian_notice(self): page = FakeQueryPage(outcome="visible_no_slots") plugin = self._plugin(page) result = plugin.query(self._appointment_type()) self.assertFalse(result.success) self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable) self.assertEqual(page.clicked, ["booking-1321"]) def test_unknown_visible_notice_fails_closed(self): page = FakeQueryPage(outcome="unknown_popup") plugin = self._plugin(page) with self.assertRaisesRegex(BizLogicError, "unrecognized visible"): plugin.query(self._appointment_type()) self.assertEqual(page.clicked, ["booking-1321"]) def test_changed_notice_is_not_guessed_as_no_availability(self): plugin = self._plugin(FakeQueryPage()) status = plugin._classify_service_notice( "I posti disponibili per il servizio scelto non sono esauriti." ) self.assertIsNone(status) def test_reloads_services_before_each_no_slot_probe(self): page = FakeQueryPage(outcome="no_slots") plugin = self._plugin(page) apt_type = self._appointment_type() first_result = plugin.query(apt_type) second_result = plugin.query(apt_type) self.assertFalse(first_result.success) self.assertFalse(second_result.success) self.assertEqual( page.get_calls, [ "https://prenotami.esteri.it/Services", "https://prenotami.esteri.it/Services", ], ) self.assertEqual(page.clicked, ["booking-1321", "booking-1321"]) self.assertEqual(page.wait.load_start_calls, 2) self.assertEqual(page.wait.doc_loaded_calls, 4) def test_empty_w_not_available_is_not_treated_as_no_slots(self): page = FakeQueryPage(outcome="empty_warning") plugin = self._plugin(page) with self.assertRaisesRegex(BizLogicError, "non-empty"): plugin.query(self._appointment_type()) self.assertEqual(page.clicked, ["booking-1321"]) def test_booking_form_branch_returns_bls_style_weak_signal(self): page = FakeQueryPage(outcome="booking_form") plugin = self._plugin(page) result = plugin.query(self._appointment_type()) self.assertTrue(result.success) self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable) self.assertIsNone(result.earliest_date) self.assertEqual(result.availability, []) self.assertEqual(page.clicked, ["booking-1321"]) def test_booking_form_state_can_be_reprobed_on_the_next_query_cycle(self): page = FakeQueryPage(outcome="booking_form") plugin = self._plugin(page) apt_type = self._appointment_type() first_result = plugin.query(apt_type) second_result = plugin.query(apt_type) self.assertTrue(first_result.success) self.assertTrue(second_result.success) self.assertEqual( page.get_calls, [ "https://prenotami.esteri.it/Services", "https://prenotami.esteri.it/Services", ], ) self.assertEqual(page.clicked, ["booking-1321", "booking-1321"]) def test_booking_path_without_form_fails_closed(self): page = FakeQueryPage(outcome="booking_path_without_form") plugin = self._plugin(page) with self.assertRaisesRegex(BizLogicError, "without #bookingForm"): plugin.query(self._appointment_type()) self.assertEqual(page.clicked, ["booking-1321"]) def test_missing_target_link_is_a_structure_error_not_no_slots(self): page = FakeQueryPage(has_link=False) plugin = self._plugin(page) with self.assertRaisesRegex( ItaTargetServiceLinkMissingError, "target service link is missing", ): plugin.query(self._appointment_type()) self.assertEqual(page.clicked, []) self.assertIn( ("tag:a@@href=/Services/Booking/1321", SERVICE_LINK_WAIT_SECONDS), page.looked_up, ) self.assertEqual( plugin.last_service_list_structure, { "path": "/Services", "ready_state": "complete", "table_count": 1, "row_count": 0, "booking_link_count": 0, "target_link_count": 0, "already_booked_notice_row_count": 0, }, ) def test_missing_link_with_booked_notice_reports_context_without_attribution(self): page = FakeQueryPage(has_link=False, already_booked_notice_rows=1) plugin = self._plugin(page) with self.assertRaises(ItaTargetServiceLinkMissingError) as raised: plugin.query(self._appointment_type()) self.assertIn("Services contains an already-booked notice", str(raised.exception)) self.assertIn("not bound to the configured service ID", str(raised.exception)) self.assertEqual(plugin.last_service_list_structure["already_booked_notice_row_count"], 1) self.assertEqual(page.clicked, []) self.assertEqual(page.get_calls, ["https://prenotami.esteri.it/Services"]) def test_existing_other_service_booking_does_not_block_present_target_link(self): page = FakeQueryPage(outcome="booking_form", already_booked_notice_rows=1) plugin = self._plugin(page) result = plugin.query(self._appointment_type()) self.assertTrue(result.success) self.assertEqual(page.clicked, ["booking-1321"]) self.assertFalse(any("service-list-structure" in script for script in page.run_js_calls)) def test_invalid_notice_counts_do_not_claim_an_existing_booking(self): for raw_count in (None, True, "1", -1, {}, []): with self.subTest(raw_count=raw_count): page = FakeQueryPage(has_link=False, already_booked_notice_rows=raw_count) plugin = self._plugin(page) with self.assertRaises(ItaTargetServiceLinkMissingError) as raised: plugin.query(self._appointment_type()) self.assertNotIn("already-booked notice", str(raised.exception)) self.assertEqual(page.clicked, []) def test_book_propagates_existing_notice_without_creating_a_success(self): page = FakeQueryPage(has_link=False, already_booked_notice_rows=1) plugin = self._plugin(page) trigger = VSQueryResult(apt_type=self._appointment_type()) with self.assertRaisesRegex(ItaTargetServiceLinkMissingError, "already-booked notice"): plugin.book(trigger) self.assertIsNone(plugin.last_booking_precommit) self.assertEqual(page.clicked, []) def test_invalid_route_service_id_is_a_configuration_error(self): page = FakeQueryPage() plugin = self._plugin(page, service_id=0) with self.assertRaisesRegex( BizLogicError, "route configuration rejected.*positive_integer_required", ): plugin.query(self._appointment_type()) self.assertEqual(page.clicked, []) def test_second_sede_uses_the_same_query_path_without_a_city_branch(self): routing_key = "slot.hkg.it.tourist" page = FakeQueryPage( outcome="booking_form", service_id=4937, ) plugin = self._plugin( page, service_id=4937, routing_key=routing_key, ) result = plugin.query( self._appointment_type(routing_key, city="Hong Kong") ) self.assertTrue(result.success) self.assertEqual(page.clicked, ["booking-4937"]) def test_calendar_mode_returns_vfs_style_earliest_date_only(self): routing_key = "slot.sample.it.tourist" page = FakeQueryPage(initial_state="calendar", service_id=9001) plugin = self._plugin( page, service_id=9001, routing_key=routing_key, query_mode="calendar", capability="single_schengen_tourism", lookahead_months=12, ) apt_type = self._appointment_type( routing_key, city="Sample City", ) scan = types.SimpleNamespace( earliest_available_date=date(2026, 10, 12) ) with patch( "plugins.ita_plugin.scan_until_first_available_month", return_value=scan, ) as scanner: result = plugin.query(apt_type) scanner.assert_called_once_with( page, lookahead_months=12, log=plugin._log, ) self.assertTrue(result.success) self.assertEqual( result.availability_status, AvailabilityStatus.Available, ) self.assertEqual( result.earliest_date.strftime("%Y-%m-%d"), "2026-10-12", ) self.assertEqual(len(result.availability), 1) self.assertEqual( result.availability[0].date.strftime("%Y-%m-%d"), "2026-10-12", ) self.assertEqual(result.availability[0].times, []) self.assertEqual(page.clicked, []) self.assertIs(plugin.last_calendar_query_scan, scan) def test_calendar_diagnostic_does_not_survive_a_failed_query(self): routing_key = "slot.sample.it.tourist" page = FakeQueryPage(initial_state="calendar", service_id=9001) plugin = self._plugin( page, service_id=9001, routing_key=routing_key, query_mode="calendar", capability="single_schengen_tourism", ) plugin.last_calendar_query_scan = object() plugin._calendar_query_route_key = None with self.assertRaisesRegex(BizLogicError, "route continuity"): plugin.query(self._appointment_type(routing_key, city="Sample City")) self.assertIsNone(plugin.last_calendar_query_scan) def test_calendar_mode_returns_none_only_after_scanner_exhaustion(self): routing_key = "slot.sample.it.tourist" page = FakeQueryPage(initial_state="calendar", service_id=9001) plugin = self._plugin( page, service_id=9001, routing_key=routing_key, query_mode="calendar", capability="single_schengen_tourism", ) apt_type = self._appointment_type( routing_key, city="Sample City", ) with patch( "plugins.ita_plugin.scan_until_first_available_month", return_value=types.SimpleNamespace( earliest_available_date=None ), ): result = plugin.query(apt_type) self.assertFalse(result.success) self.assertEqual( result.availability_status, AvailabilityStatus.NoneAvailable, ) self.assertIsNone(result.earliest_date) self.assertEqual(result.availability, []) def test_calendar_mode_requires_a_route_confirmed_calendar_session(self): routing_key = "slot.sample.it.tourist" page = FakeQueryPage(initial_state="user_area", service_id=9001) plugin = self._plugin( page, service_id=9001, routing_key=routing_key, query_mode="calendar", capability="single_schengen_tourism", ) plugin._calendar_query_route_key = None with patch( "plugins.ita_plugin.scan_until_first_available_month" ) as scanner: with self.assertRaisesRegex( BizLogicError, "preparation is not complete", ): plugin.query( self._appointment_type( routing_key, city="Sample City", ) ) scanner.assert_not_called() def test_calendar_mode_rejects_a_different_prepared_route(self): routing_key = "slot.sample.it.tourist" page = FakeQueryPage(initial_state="calendar", service_id=9001) plugin = self._plugin( page, service_id=9001, routing_key=routing_key, query_mode="calendar", capability="single_schengen_tourism", ) plugin._calendar_query_route_key = "slot.other.it.tourist" with patch( "plugins.ita_plugin.scan_until_first_available_month" ) as scanner: with self.assertRaisesRegex( BizLogicError, "route continuity", ): plugin.query( self._appointment_type( routing_key, city="Sample City", ) ) scanner.assert_not_called() def test_calendar_mode_rejects_a_probe_only_route(self): page = FakeQueryPage(initial_state="calendar") plugin = self._plugin(page, query_mode="calendar") with self.assertRaisesRegex( BizLogicError, "single_schengen_tourism", ): plugin.query(self._appointment_type()) def test_login_redirect_is_reported_as_expired_session(self): page = FakeQueryPage(initial_state="login") plugin = self._plugin(page) with self.assertRaises(SessionExpiredOrInvalidError): plugin.query(self._appointment_type()) self.assertEqual(page.clicked, []) def test_query_source_contains_no_otp_or_submission_path(self): query_source = inspect.getsource(ItaPlugin.query) forbidden_fragments = ( "otp-send", "otp-input", "123456", "PrivacyCheck", "btnAvanti", "InsertNewBooking", "_perform_request", ) for fragment in forbidden_fragments: with self.subTest(fragment=fragment): self.assertNotIn(fragment, query_source) class ItaPluginBookDiscoveryTests(unittest.TestCase): @staticmethod def _appointment_type(): return AppointmentType( routing_key="slot.dub.it.tourist", city="Dublin", country="Italy", visa_type="Tourist", ) @classmethod def _weak_signal(cls): return VSQueryResult( success=False, apt_type=cls._appointment_type(), availability_status=AvailabilityStatus.NoneAvailable, ) @staticmethod def _plugin(page): plugin = ItaPlugin.__new__(ItaPlugin) plugin.group_id = "offline-book-discovery" plugin.logger = Mock() plugin.page = page plugin.mouse = FakeQueryMouse(page) plugin.free_config = { "query_mode": "services_probe", "final_submit_enabled": False, "apt_configs": { "slot.dub.it.tourist": { "service_id": 1321, "capability": "services_probe_only", } }, } plugin.ita_url = "https://prenotami.esteri.it" plugin.root_workspace = "/nonexistent/ita-plugin-book-test" plugin.tunnel = None return plugin def test_book_rechecks_once_and_returns_false_when_user_has_no_slot(self): page = FakeQueryPage(outcome="no_slots", initial_state="user_area") plugin = self._plugin(page) result = plugin.book(self._weak_signal(), user_inputs={}) self.assertFalse(result.success) self.assertEqual(result.label, BOOKING_RECHECK_NONE_AVAILABLE_LABEL) self.assertEqual(page.clicked, ["booking-1321"]) self.assertEqual(page.run_js_calls, []) def test_book_reads_allowlisted_structure_and_stops_before_avanti(self): page = FakeQueryPage(outcome="booking_form", initial_state="user_area") plugin = self._plugin(page) result = plugin.book(self._weak_signal(), user_inputs={}) self.assertFalse(result.success) self.assertEqual(result.label, BOOKING_FORM_DISCOVERY_STOP_LABEL) self.assertEqual(page.clicked, ["booking-1321"]) self.assertEqual(len(page.run_js_calls), 1) structure = plugin.last_booking_form_structure self.assertEqual(structure["path"], "/Services/Booking/1321") self.assertEqual(structure["form"]["method"], "POST") self.assertEqual(structure["field_count"], 2) self.assertNotIn("value", structure["fields"][0]) self.assertNotIn("untrusted", str(structure)) def test_book_fails_closed_when_form_metadata_has_an_unexpected_path(self): page = FakeQueryPage(outcome="booking_form", initial_state="user_area") page.state = "booking_form" structure = page.run_js("fixture") page.state = "user_area" structure["path"] = "/Unexpected" page.run_js_calls.clear() page.run_js = Mock(return_value=structure) plugin = self._plugin(page) with self.assertRaisesRegex(BizLogicError, "unexpected path"): plugin.book(self._weak_signal(), user_inputs={}) self.assertEqual(page.clicked, ["booking-1321"]) self.assertEqual(page.run_js.call_count, 1) def test_book_source_contains_no_form_submission_calendar_or_otp_path(self): book_source = inspect.getsource(ItaPlugin.book) forbidden_fragments = ( "GenerateOTP", "get_email_verify_code", "fetch_mail_content", "PrivacyCheck", "btnAvanti", "BookingCalendar", "InsertNewBooking", "_perform_request", ".submit(", ".click(", ) for fragment in forbidden_fragments: with self.subTest(fragment=fragment): self.assertNotIn(fragment, book_source) class FakeRetainedSignalRedis: def __init__(self, payload, clock, *, ttl): self.payload = json.dumps(payload) self.clock = clock self.ttl = ttl def get(self, _name): if self.clock["now"] <= self.ttl: return self.payload return None def delete(self, *_names): return 0 class CountingItaOrderBooker(OrderBookerGCO): def _execute_book_job(self, task, query_result): del task, query_result self.book_calls += 1 class ItaSignalReuseTests(unittest.TestCase): @staticmethod def _appointment_type(): return AppointmentType( routing_key="slot.dub.it.tourist", city="Dublin", country="Italy", visa_type="Tourist", ) def test_retained_thirty_second_signal_triggers_one_book_call(self): apt_type = self._appointment_type() weak_result = VSQueryResult( success=True, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable, ) payload = { "apt_type": apt_type.model_dump(), "query_result": weak_result.to_snapshot_payload(), } clock = {"now": 0.0} task = Task( instance=Mock(), next_run=0.0, task_ref=1, acceptable_routing_keys=[apt_type.routing_key], source_queue=apt_type.routing_key, book_allowed=True, ) config = GroupConfig( identifier="pernotami.ie.it", appointment_types=[apt_type], booker={ "account_source": "order", "target_instances": 1, "booking_cooldown": 45, }, ) booker = CountingItaOrderBooker.__new__(CountingItaOrderBooker) booker.m_cfg = config booker.m_logger = Mock() booker.m_tasks = [task] booker.m_lock = threading.RLock() booker.m_stop_event = threading.Event() booker.m_task_data_cache = {} booker.redis_client = FakeRetainedSignalRedis(payload, clock, ttl=30) booker.book_calls = 0 def fake_sleep(seconds): clock["now"] += seconds if clock["now"] > 31: booker.m_stop_event.set() booker_time = OrderBookerGCO._booking_trigger_loop.__globals__["time"] with ( patch.object(booker_time, "sleep", side_effect=fake_sleep), patch.object(booker_time, "time", side_effect=lambda: clock["now"]), ): booker._booking_trigger_loop() self.assertEqual(booker.book_calls, 1) self.assertGreater(task.next_run, 30) def test_italy_config_keeps_existing_single_worker_cooldown_contract(self): repo_root = Path(__file__).resolve().parents[1] booker_groups = json.loads( (repo_root / "config" / "config_booker.json").read_text() ) sentinel_groups = json.loads( (repo_root / "config" / "config_sentinel.json").read_text() ) booker_group = next( group for group in booker_groups if group["identifier"] == "pernotami.ie.it" ) sentinel_group = next( group for group in sentinel_groups if group["identifier"] == "pernotami.ie.it" ) self.assertEqual(booker_group["booker"]["target_instances"], 1) self.assertEqual(sentinel_group["sentinel"]["target_instances"], 1) self.assertGreater( booker_group["booker"]["booking_cooldown"], sentinel_group["sentinel"]["signal_ttl"], ) self.assertGreater( sentinel_group["sentinel"]["query_wait"]["random_min"], booker_group["booker"]["booking_cooldown"], ) if __name__ == "__main__": unittest.main()