| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292 |
- import inspect
- import json
- import sys
- import threading
- import types
- import unittest
- from dataclasses import replace
- 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,
- SERVICE_LINK_WAIT_SECONDS,
- 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 = []
- self.tab_id = "query-fixture-tab"
- @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-book-runtime-service" in script:
- return {"path": "/BookingCalendar", "count": 1, "value": str(self.service_id)}
- 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("utils.plugin_utils.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("Entering username", log_output)
- self.assertIn("Successfully reached authenticated /UserArea", 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("utils.plugin_utils.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("utils.plugin_utils.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("utils.plugin_utils.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)
- self.assertTrue(any("@radware" in call.args[0] for call in logger.call_args_list))
- 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.is_healthy = True
- plugin.session_create_time = 111.0
- plugin.config = VSPlgConfig(account=VSAccount(username="query@example.test"), free_config=plugin.free_config)
- plugin._reset_calendar_state()
- if (page is not None and query_mode == "calendar" and capability == "single_schengen_tourism"
- and page.url.startswith("https://prenotami.esteri.it/BookingCalendar")):
- plugin._record_calendar_prepared_route(
- plugin._resolve_route(AppointmentType(routing_key=routing_key)), role="query")
- 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._enter_service(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._enter_service(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._enter_service(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._enter_service(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._enter_service(apt_type)
- second_result = plugin._enter_service(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._enter_service(self._appointment_type())
- self.assertEqual(page.clicked, ["booking-1321"])
- def test_internal_entry_reports_form_without_inventory(self):
- page = FakeQueryPage(outcome="booking_form")
- plugin = self._plugin(page)
- result = plugin._enter_service(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_internal_entry_can_be_reprobed_without_publishing_inventory(self):
- page = FakeQueryPage(outcome="booking_form")
- plugin = self._plugin(page)
- apt_type = self._appointment_type()
- first_result = plugin._enter_service(apt_type)
- second_result = plugin._enter_service(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._enter_service(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._enter_service(self._appointment_type())
- self.assertEqual(page.clicked, [])
- self.assertIn(
- ("tag:a@@href=/Services/Booking/1321", SERVICE_LINK_WAIT_SECONDS),
- page.looked_up,
- )
- self.assertFalse(any("service-list-structure" in script for script in page.run_js_calls))
- def test_missing_link_with_page_notice_is_not_attributed_to_the_target(self):
- page = FakeQueryPage(has_link=False, already_booked_notice_rows=1)
- plugin = self._plugin(page)
- with self.assertRaises(ItaTargetServiceLinkMissingError) as raised:
- plugin._enter_service(self._appointment_type())
- self.assertIn("target service link is missing", str(raised.exception))
- self.assertNotIn("already-booked notice", str(raised.exception))
- self.assertFalse(any("service-list-structure" in script for script in page.run_js_calls))
- 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._enter_service(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._enter_service(self._appointment_type())
- self.assertNotIn("already-booked notice", str(raised.exception))
- self.assertEqual(page.clicked, [])
- def test_book_propagates_missing_link_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, "target service link is missing"):
- plugin.book(trigger)
- 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._enter_service(self._appointment_type())
- self.assertEqual(page.clicked, [])
- def test_second_sede_uses_same_entry_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._enter_service(
- self._appointment_type(routing_key, city="Hong Kong")
- )
- self.assertTrue(result.success)
- self.assertEqual(page.clicked, ["booking-4937"])
- def test_public_query_rejects_legacy_probe_before_browser_actions(self):
- for mode in (None, "services_probe"):
- for capability in ("services_probe_only", "single_schengen_tourism"):
- with self.subTest(mode=mode, capability=capability):
- page = FakeQueryPage(outcome="booking_form")
- plugin = self._plugin(page, capability=capability)
- if mode is None:
- plugin.free_config.pop("query_mode")
- with self.assertRaisesRegex(BizLogicError, "diagnostic-only"):
- plugin.query(self._appointment_type())
- self.assertEqual(page.get_calls, [])
- self.assertEqual(page.clicked, [])
- self.assertEqual(page.run_js_calls, [])
- 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(
- available_days=(types.SimpleNamespace(day=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.assertFalse(hasattr(plugin, "last_calendar_query_scan"))
- def test_failed_query_does_not_create_diagnostic_state(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._prepared_calendar = None
- with self.assertRaisesRegex(BizLogicError, "identity or route"):
- plugin.query(self._appointment_type(routing_key, city="Sample City"))
- self.assertFalse(hasattr(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(available_days=()),),
- ):
- 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._prepared_calendar = 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._prepared_calendar = replace(plugin._prepared_calendar,
- route=replace(plugin._prepared_calendar.route, routing_key="slot.other.it.tourist"))
- with patch(
- "plugins.ita_plugin.scan_until_first_available_month"
- ) as scanner:
- with self.assertRaisesRegex(
- BizLogicError,
- "identity or route",
- ):
- 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, query_mode="calendar", capability="single_schengen_tourism")
- 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
- plugin._reset_calendar_state()
- plugin.query = Mock(side_effect=AssertionError("Book must not call public Query"))
- 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, [])
- plugin.query.assert_not_called()
- def test_book_diagnostic_mode_stops_before_avanti_without_collecting_metadata(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(page.run_js_calls, [])
- plugin.query.assert_not_called()
- def test_book_rejects_a_form_on_an_unexpected_actual_page(self):
- page = FakeQueryPage(outcome="unknown", initial_state="user_area")
- original_ele = page.ele
- page.ele = lambda selector, timeout=None: (
- FakeQueryElement(page, "wrong-form")
- if selector == "#bookingForm" and page.state == "unknown"
- else original_ele(selector, timeout))
- 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_calls, [])
- plugin.query.assert_not_called()
- 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[2]
- 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()
|