test_ita_plugin.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. import inspect
  2. import json
  3. import sys
  4. import threading
  5. import types
  6. import unittest
  7. from dataclasses import replace
  8. from datetime import date
  9. from pathlib import Path
  10. from tempfile import TemporaryDirectory
  11. from unittest.mock import Mock, patch
  12. from plugins.ita_plugin import (
  13. BOOKING_FORM_DISCOVERY_STOP_LABEL,
  14. BOOKING_RECHECK_NONE_AVAILABLE_LABEL,
  15. SERVICE_LINK_WAIT_SECONDS,
  16. ItaAuthenticationChallengeError,
  17. ItaPlugin,
  18. ItaTargetServiceLinkMissingError,
  19. )
  20. try:
  21. from booker import OrderBookerGCO
  22. except ModuleNotFoundError as exc:
  23. if exc.name != "redis":
  24. raise
  25. redis_stub = types.ModuleType("redis")
  26. redis_stub.Redis = Mock
  27. with patch.dict(sys.modules, {"redis": redis_stub}):
  28. from booker import OrderBookerGCO
  29. from vs_types import (
  30. AppointmentType,
  31. AvailabilityStatus,
  32. BizLogicError,
  33. GroupConfig,
  34. SessionExpiredOrInvalidError,
  35. Task,
  36. VSAccount,
  37. VSPlgConfig,
  38. VSQueryResult,
  39. )
  40. class FakeChromiumOptions:
  41. def set_local_port(self, _port):
  42. return self
  43. def set_user_data_path(self, _path):
  44. return self
  45. def set_paths(self, **_kwargs):
  46. return self
  47. def set_argument(self, _argument):
  48. return self
  49. def headless(self, _enabled):
  50. return self
  51. class FakeWait:
  52. def doc_loaded(self):
  53. return None
  54. def load_start(self):
  55. return None
  56. class FakeRect:
  57. viewport_size = (1920, 1080)
  58. class FakeElement:
  59. def __init__(self, page, name):
  60. self.page = page
  61. self.name = name
  62. def input(self, value, clear=False):
  63. self.page.input_values[self.name] = {
  64. "value": value,
  65. "clear": clear,
  66. }
  67. class FakePage:
  68. URLS = {
  69. "landing": "https://prenotami.esteri.it/",
  70. "pingid": (
  71. "https://iam.esteri.it/signin/?realm=/external"
  72. "&state=sensitive-state"
  73. "&code_challenge=sensitive-challenge#/"
  74. ),
  75. "dashboard": "https://prenotami.esteri.it/UserArea",
  76. "services": "https://prenotami.esteri.it/Services",
  77. "radware": (
  78. "https://validate.perfdrive.com/"
  79. "8ab9454422ad153e7d4ce3a5b41556a7/"
  80. ),
  81. }
  82. def __init__(
  83. self,
  84. *,
  85. submit_succeeds=True,
  86. has_services_link=True,
  87. login_target="pingid",
  88. ):
  89. self.state = "landing"
  90. self.submit_succeeds = submit_succeeds
  91. self.has_services_link = has_services_link
  92. self.login_target = login_target
  93. self.wait = FakeWait()
  94. self.rect = FakeRect()
  95. self.clicked = []
  96. self.input_values = {}
  97. self.refresh_calls = 0
  98. self.quit_called = False
  99. self.quit_force = None
  100. @property
  101. def url(self):
  102. return self.URLS[self.state]
  103. @property
  104. def title(self):
  105. return {
  106. "landing": "Login",
  107. "pingid": "Sign in",
  108. "dashboard": "Home",
  109. "services": "Services",
  110. "radware": "Access Denied",
  111. }[self.state]
  112. @property
  113. def html(self):
  114. return ""
  115. def get(self, _url):
  116. self.state = "landing"
  117. def refresh(self):
  118. self.refresh_calls += 1
  119. def quit(self, force=False):
  120. self.quit_called = True
  121. self.quit_force = force
  122. def ele(self, selector, timeout=None):
  123. del timeout
  124. if selector == "tag:a@@href=/Language/ChangeLanguage?lang=2":
  125. return FakeElement(self, "language") if self.state == "landing" else None
  126. if selector == "#pingid-button" and self.state == "landing":
  127. return FakeElement(self, "login")
  128. if selector == "@name=callback_1" and self.state == "pingid":
  129. return FakeElement(self, "username")
  130. if selector == "@name=callback_2" and self.state == "pingid":
  131. return FakeElement(self, "password")
  132. if selector == "tag:button@type=submit" and self.state == "pingid":
  133. return FakeElement(self, "submit")
  134. if selector == ".app-menu" and self.state == "dashboard":
  135. return FakeElement(self, "app-menu")
  136. if (
  137. selector == "@href=/Services"
  138. and self.state == "dashboard"
  139. and self.has_services_link
  140. ):
  141. return FakeElement(self, "book")
  142. return None
  143. class FakeMouse:
  144. def __init__(self, page, debug=False):
  145. self.page = page
  146. self.debug = debug
  147. def move(self, _x, _y):
  148. return None
  149. def human_click_ele(self, element):
  150. if element is None:
  151. raise AssertionError("The state machine attempted to click a missing element")
  152. self.page.clicked.append(element.name)
  153. if element.name == "login":
  154. self.page.state = self.page.login_target
  155. elif element.name == "submit" and self.page.submit_succeeds:
  156. self.page.state = "dashboard"
  157. elif element.name == "book":
  158. self.page.state = "services"
  159. class FakeKeyboard:
  160. def __init__(self, page):
  161. self.page = page
  162. class FakeQueryWait:
  163. def __init__(self):
  164. self.load_start_calls = 0
  165. self.doc_loaded_calls = 0
  166. def load_start(self):
  167. self.load_start_calls += 1
  168. def doc_loaded(self):
  169. self.doc_loaded_calls += 1
  170. class FakeQueryElement:
  171. def __init__(
  172. self,
  173. page,
  174. name,
  175. attributes=None,
  176. *,
  177. text="",
  178. property_value=None,
  179. ):
  180. self.page = page
  181. self.name = name
  182. self.attributes = attributes or {}
  183. self.text = text
  184. self.value = property_value
  185. def attr(self, name):
  186. return self.attributes.get(name)
  187. class FakeQueryPage:
  188. URLS = {
  189. "services": "https://prenotami.esteri.it/Services",
  190. "user_area": "https://prenotami.esteri.it/UserArea",
  191. "no_slots": "https://prenotami.esteri.it/Services",
  192. "property_no_slots": "https://prenotami.esteri.it/Services",
  193. "visible_no_slots": "https://prenotami.esteri.it/Services",
  194. "unknown_popup": "https://prenotami.esteri.it/Services",
  195. "empty_warning": "https://prenotami.esteri.it/Services",
  196. "login": "https://iam.esteri.it/login/oauth2/authorize",
  197. "calendar": "https://prenotami.esteri.it/BookingCalendar",
  198. "unknown": "https://prenotami.esteri.it/Unexpected",
  199. }
  200. def __init__(
  201. self,
  202. outcome="no_slots",
  203. *,
  204. has_link=True,
  205. initial_state="services",
  206. service_id=1321,
  207. already_booked_notice_rows=0,
  208. ):
  209. self.outcome = outcome
  210. self.has_link = has_link
  211. self.state = initial_state
  212. self.service_id = service_id
  213. self.already_booked_notice_rows = already_booked_notice_rows
  214. self.wait = FakeQueryWait()
  215. self.clicked = []
  216. self.get_calls = []
  217. self.looked_up = []
  218. self.run_js_calls = []
  219. self.tab_id = "query-fixture-tab"
  220. @property
  221. def url(self):
  222. if self.state in {"booking_form", "booking_path_without_form"}:
  223. return (
  224. "https://prenotami.esteri.it/Services/Booking/"
  225. f"{self.service_id}"
  226. )
  227. return self.URLS[self.state]
  228. def get(self, url):
  229. self.get_calls.append(url)
  230. if url != "https://prenotami.esteri.it/Services":
  231. raise AssertionError(f"Unexpected query navigation: {url}")
  232. self.state = "services"
  233. def run_js(self, script):
  234. self.run_js_calls.append(script)
  235. if "coordinate:ita-book-runtime-service" in script:
  236. return {"path": "/BookingCalendar", "count": 1, "value": str(self.service_id)}
  237. if "coordinate:ita-service-list-structure" in script:
  238. return {
  239. "path": "/Services",
  240. "ready_state": "complete",
  241. "table_count": 1,
  242. "row_count": 0,
  243. "booking_link_count": 0,
  244. "target_link_count": 0,
  245. "already_booked_notice_row_count": self.already_booked_notice_rows,
  246. }
  247. if self.state != "booking_form":
  248. return None
  249. return {
  250. "path": f"/Services/Booking/{self.service_id}",
  251. "form": {
  252. "method": "POST",
  253. "action_path": f"/Services/Booking/{self.service_id}",
  254. "untrusted": "must-not-survive",
  255. },
  256. "field_count": 2,
  257. "fields_truncated": False,
  258. "fields": [
  259. {
  260. "tag": "input",
  261. "type": "hidden",
  262. "id": "RequestToken",
  263. "name": "__RequestVerificationToken",
  264. "required": False,
  265. "value": "must-not-survive",
  266. },
  267. {
  268. "tag": "select",
  269. "type": None,
  270. "id": "BookingType",
  271. "name": "BookingType",
  272. "required": True,
  273. },
  274. ],
  275. "button_count": 1,
  276. "buttons_truncated": False,
  277. "buttons": [
  278. {
  279. "tag": "button",
  280. "type": "submit",
  281. "id": "btnAvanti",
  282. "name": None,
  283. "text": "Avanti",
  284. "value": "must-not-survive",
  285. }
  286. ],
  287. "untrusted": "must-not-survive",
  288. }
  289. def ele(self, selector, timeout=None):
  290. self.looked_up.append((selector, timeout))
  291. if selector == "#pingid-button" and self.state == "login":
  292. return FakeQueryElement(self, "login")
  293. if (
  294. selector
  295. == f"tag:a@@href=/Services/Booking/{self.service_id}"
  296. and self.state == "services"
  297. and self.has_link
  298. ):
  299. return FakeQueryElement(self, f"booking-{self.service_id}")
  300. if selector == "#WNotAvailable" and self.state == "no_slots":
  301. return FakeQueryElement(
  302. self,
  303. "not-available",
  304. {"value": "No appointments are currently available."},
  305. )
  306. if selector == "#WNotAvailable" and self.state == "property_no_slots":
  307. return FakeQueryElement(
  308. self,
  309. "property-not-available",
  310. property_value="No appointments are currently available.",
  311. )
  312. if selector == "#WNotAvailable" and self.state == "empty_warning":
  313. return FakeQueryElement(self, "empty-warning", {"value": " "})
  314. if selector == ".jconfirm-content" and self.state == "visible_no_slots":
  315. return FakeQueryElement(
  316. self,
  317. "visible-not-available",
  318. text=(
  319. " STANTE L’ELEVATA RICHIESTA\n"
  320. "i posti disponibili per il servizio scelto sono esauriti. "
  321. ),
  322. )
  323. if selector == ".jconfirm-content" and self.state == "unknown_popup":
  324. return FakeQueryElement(
  325. self,
  326. "unknown-popup",
  327. text="Il servizio richiede una nuova verifica.",
  328. )
  329. if selector == "#bookingForm" and self.state == "booking_form":
  330. return FakeQueryElement(self, "booking-form")
  331. return None
  332. class FakeQueryMouse:
  333. def __init__(self, page):
  334. self.page = page
  335. def human_click_ele(self, element):
  336. if element is None:
  337. raise AssertionError("Query attempted to click a missing element")
  338. self.page.clicked.append(element.name)
  339. self.page.state = self.page.outcome
  340. class ItaPluginRandomSleepTests(unittest.TestCase):
  341. def test_random_sleep_uses_generated_duration_within_requested_range(self):
  342. plugin = ItaPlugin.__new__(ItaPlugin)
  343. plugin.group_id = "test-group"
  344. plugin.logger = Mock()
  345. plugin.page = None
  346. plugin.root_workspace = "/nonexistent/ita-plugin-unit-test"
  347. plugin.tunnel = None
  348. with (
  349. patch("plugins.ita_plugin.random.uniform", return_value=4.25) as uniform,
  350. patch("plugins.ita_plugin.time.sleep") as sleep,
  351. ):
  352. plugin._random_sleep(3, 5)
  353. uniform.assert_called_once_with(3, 5)
  354. sleep.assert_called_once_with(4.25)
  355. plugin.logger.assert_called_once_with(
  356. "[ItaPlugin] [test-group] "
  357. "Anti-Rate-Limit: Sleeping for 4.25 seconds..."
  358. )
  359. class ItaPluginCreateSessionStateMachineTests(unittest.TestCase):
  360. @staticmethod
  361. def _config():
  362. return VSPlgConfig(
  363. account=VSAccount(
  364. id=0,
  365. username="offline-test@example.test",
  366. password="offline-password",
  367. )
  368. )
  369. def test_stops_at_authenticated_user_area_without_opening_services(self):
  370. fake_page = FakePage()
  371. config = self._config()
  372. logger = Mock()
  373. with TemporaryDirectory() as workspace:
  374. with patch(
  375. "plugins.ita_plugin.os.path.abspath",
  376. return_value=str(Path(workspace)),
  377. ):
  378. plugin = ItaPlugin("offline-state-machine")
  379. plugin.set_log(logger)
  380. plugin.set_config(config)
  381. with (
  382. patch("utils.plugin_utils.ChromiumOptions", FakeChromiumOptions),
  383. patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page),
  384. patch("plugins.ita_plugin.HumanMouse", FakeMouse),
  385. patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard),
  386. patch("plugins.ita_plugin.time.sleep"),
  387. ):
  388. try:
  389. plugin.create_session()
  390. finally:
  391. plugin.cleanup()
  392. self.assertEqual(fake_page.state, "dashboard")
  393. self.assertGreater(plugin.session_create_time, 0)
  394. self.assertEqual(
  395. fake_page.clicked,
  396. ["login", "username", "password", "submit"],
  397. )
  398. self.assertEqual(
  399. fake_page.input_values,
  400. {
  401. "username": {
  402. "value": "offline-test@example.test",
  403. "clear": True,
  404. },
  405. "password": {
  406. "value": "offline-password",
  407. "clear": True,
  408. },
  409. },
  410. )
  411. self.assertEqual(fake_page.refresh_calls, 0)
  412. self.assertTrue(fake_page.quit_called)
  413. log_output = "\n".join(
  414. call.args[0] for call in logger.call_args_list if call.args
  415. )
  416. self.assertIn("Entering username", log_output)
  417. self.assertIn("Successfully reached authenticated /UserArea", log_output)
  418. def test_never_resubmits_credentials_or_refreshes_after_login_submission(self):
  419. fake_page = FakePage(submit_succeeds=False)
  420. config = self._config()
  421. with TemporaryDirectory() as workspace:
  422. with patch(
  423. "plugins.ita_plugin.os.path.abspath",
  424. return_value=str(Path(workspace)),
  425. ):
  426. plugin = ItaPlugin("offline-stalled-login")
  427. plugin.set_log(Mock())
  428. plugin.set_config(config)
  429. with (
  430. patch("utils.plugin_utils.ChromiumOptions", FakeChromiumOptions),
  431. patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page),
  432. patch("plugins.ita_plugin.HumanMouse", FakeMouse),
  433. patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard),
  434. patch("plugins.ita_plugin.time.sleep"),
  435. ):
  436. try:
  437. with self.assertRaisesRegex(
  438. BizLogicError,
  439. "Failed to create authenticated /UserArea session",
  440. ):
  441. plugin.create_session()
  442. finally:
  443. plugin.cleanup()
  444. self.assertEqual(fake_page.clicked.count("login"), 1)
  445. self.assertEqual(fake_page.clicked.count("username"), 1)
  446. self.assertEqual(fake_page.clicked.count("password"), 1)
  447. self.assertEqual(fake_page.clicked.count("submit"), 1)
  448. self.assertEqual(fake_page.refresh_calls, 0)
  449. def test_requires_user_area_services_link_as_authentication_marker(self):
  450. fake_page = FakePage(has_services_link=False)
  451. config = self._config()
  452. with TemporaryDirectory() as workspace:
  453. with patch(
  454. "plugins.ita_plugin.os.path.abspath",
  455. return_value=str(Path(workspace)),
  456. ):
  457. plugin = ItaPlugin("offline-incomplete-dashboard")
  458. plugin.set_log(Mock())
  459. plugin.set_config(config)
  460. with (
  461. patch("utils.plugin_utils.ChromiumOptions", FakeChromiumOptions),
  462. patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page),
  463. patch("plugins.ita_plugin.HumanMouse", FakeMouse),
  464. patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard),
  465. patch("plugins.ita_plugin.time.sleep"),
  466. ):
  467. try:
  468. with self.assertRaisesRegex(
  469. BizLogicError,
  470. "Failed to create authenticated /UserArea session",
  471. ):
  472. plugin.create_session()
  473. finally:
  474. plugin.cleanup()
  475. self.assertEqual(fake_page.state, "dashboard")
  476. self.assertEqual(fake_page.clicked.count("submit"), 1)
  477. def test_create_session_source_has_no_language_or_services_navigation(self):
  478. create_session_source = inspect.getsource(ItaPlugin.create_session)
  479. self.assertNotIn("ChangeLanguage", create_session_source)
  480. self.assertNotIn("book_btn", create_session_source)
  481. def test_radware_validation_is_classified_before_credentials_or_refresh(self):
  482. fake_page = FakePage(login_target="radware")
  483. config = self._config()
  484. logger = Mock()
  485. with TemporaryDirectory() as workspace:
  486. with patch(
  487. "plugins.ita_plugin.os.path.abspath",
  488. return_value=str(Path(workspace)),
  489. ):
  490. plugin = ItaPlugin("offline-radware")
  491. plugin.set_log(logger)
  492. plugin.set_config(config)
  493. with (
  494. patch("utils.plugin_utils.ChromiumOptions", FakeChromiumOptions),
  495. patch("plugins.ita_plugin.ChromiumPage", return_value=fake_page),
  496. patch("plugins.ita_plugin.HumanMouse", FakeMouse),
  497. patch("plugins.ita_plugin.HumanKeyboard", FakeKeyboard),
  498. patch("plugins.ita_plugin.time.sleep"),
  499. ):
  500. try:
  501. with self.assertRaisesRegex(
  502. ItaAuthenticationChallengeError,
  503. "Radware browser validation",
  504. ):
  505. plugin.create_session()
  506. finally:
  507. plugin.cleanup()
  508. self.assertEqual(fake_page.clicked, ["login"])
  509. self.assertEqual(fake_page.input_values, {})
  510. self.assertEqual(fake_page.refresh_calls, 0)
  511. self.assertTrue(any("@radware" in call.args[0] for call in logger.call_args_list))
  512. class ItaPluginQuerySafetyTests(unittest.TestCase):
  513. @staticmethod
  514. def _appointment_type(
  515. routing_key="slot.dub.it.tourist",
  516. *,
  517. city="Dublin",
  518. ):
  519. return AppointmentType(
  520. routing_key=routing_key,
  521. city=city,
  522. country="Italy",
  523. visa_type="Tourist",
  524. )
  525. @staticmethod
  526. def _plugin(
  527. page,
  528. *,
  529. service_id=1321,
  530. routing_key="slot.dub.it.tourist",
  531. query_mode="services_probe",
  532. capability="services_probe_only",
  533. lookahead_months=3,
  534. ):
  535. plugin = ItaPlugin.__new__(ItaPlugin)
  536. plugin.group_id = "offline-query"
  537. plugin.logger = Mock()
  538. plugin.page = page
  539. plugin.mouse = FakeQueryMouse(page)
  540. route = {
  541. "service_id": service_id,
  542. "capability": capability,
  543. }
  544. if capability == "single_schengen_tourism":
  545. route.update(
  546. {
  547. "calendar_scan": {
  548. "lookahead_months": lookahead_months,
  549. },
  550. "form": {
  551. "booking_type_control_id": "typeofbookingddl",
  552. "booking_type_value": "1",
  553. "booking_type_labels": ["Single booking"],
  554. "purpose_control_id": "ddls_0",
  555. "purpose_value": "42",
  556. "purpose_labels": ["Tourism"],
  557. },
  558. }
  559. )
  560. plugin.free_config = {
  561. "query_mode": query_mode,
  562. "final_submit_enabled": False,
  563. "apt_configs": {
  564. routing_key: route,
  565. },
  566. }
  567. plugin.ita_url = "https://prenotami.esteri.it"
  568. plugin.root_workspace = "/nonexistent/ita-plugin-query-test"
  569. plugin.tunnel = None
  570. plugin.is_healthy = True
  571. plugin.session_create_time = 111.0
  572. plugin.config = VSPlgConfig(account=VSAccount(username="query@example.test"), free_config=plugin.free_config)
  573. plugin._reset_calendar_state()
  574. if (page is not None and query_mode == "calendar" and capability == "single_schengen_tourism"
  575. and page.url.startswith("https://prenotami.esteri.it/BookingCalendar")):
  576. plugin._record_calendar_prepared_route(
  577. plugin._resolve_route(AppointmentType(routing_key=routing_key)), role="query")
  578. return plugin
  579. def test_returns_none_available_only_for_non_empty_w_not_available(self):
  580. page = FakeQueryPage(outcome="no_slots")
  581. plugin = self._plugin(page)
  582. apt_type = self._appointment_type()
  583. result = plugin._enter_service(apt_type)
  584. self.assertFalse(result.success)
  585. self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable)
  586. self.assertEqual(result.apt_type, apt_type)
  587. self.assertEqual(page.clicked, ["booking-1321"])
  588. self.assertEqual(page.wait.load_start_calls, 1)
  589. self.assertEqual(page.wait.doc_loaded_calls, 2)
  590. def test_returns_none_available_for_property_only_w_not_available(self):
  591. page = FakeQueryPage(outcome="property_no_slots")
  592. plugin = self._plugin(page)
  593. result = plugin._enter_service(self._appointment_type())
  594. self.assertFalse(result.success)
  595. self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable)
  596. self.assertEqual(page.clicked, ["booking-1321"])
  597. def test_returns_none_available_for_verified_visible_italian_notice(self):
  598. page = FakeQueryPage(outcome="visible_no_slots")
  599. plugin = self._plugin(page)
  600. result = plugin._enter_service(self._appointment_type())
  601. self.assertFalse(result.success)
  602. self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable)
  603. self.assertEqual(page.clicked, ["booking-1321"])
  604. def test_unknown_visible_notice_fails_closed(self):
  605. page = FakeQueryPage(outcome="unknown_popup")
  606. plugin = self._plugin(page)
  607. with self.assertRaisesRegex(BizLogicError, "unrecognized visible"):
  608. plugin._enter_service(self._appointment_type())
  609. self.assertEqual(page.clicked, ["booking-1321"])
  610. def test_changed_notice_is_not_guessed_as_no_availability(self):
  611. plugin = self._plugin(FakeQueryPage())
  612. status = plugin._classify_service_notice(
  613. "I posti disponibili per il servizio scelto non sono esauriti."
  614. )
  615. self.assertIsNone(status)
  616. def test_reloads_services_before_each_no_slot_probe(self):
  617. page = FakeQueryPage(outcome="no_slots")
  618. plugin = self._plugin(page)
  619. apt_type = self._appointment_type()
  620. first_result = plugin._enter_service(apt_type)
  621. second_result = plugin._enter_service(apt_type)
  622. self.assertFalse(first_result.success)
  623. self.assertFalse(second_result.success)
  624. self.assertEqual(
  625. page.get_calls,
  626. [
  627. "https://prenotami.esteri.it/Services",
  628. "https://prenotami.esteri.it/Services",
  629. ],
  630. )
  631. self.assertEqual(page.clicked, ["booking-1321", "booking-1321"])
  632. self.assertEqual(page.wait.load_start_calls, 2)
  633. self.assertEqual(page.wait.doc_loaded_calls, 4)
  634. def test_empty_w_not_available_is_not_treated_as_no_slots(self):
  635. page = FakeQueryPage(outcome="empty_warning")
  636. plugin = self._plugin(page)
  637. with self.assertRaisesRegex(BizLogicError, "non-empty"):
  638. plugin._enter_service(self._appointment_type())
  639. self.assertEqual(page.clicked, ["booking-1321"])
  640. def test_internal_entry_reports_form_without_inventory(self):
  641. page = FakeQueryPage(outcome="booking_form")
  642. plugin = self._plugin(page)
  643. result = plugin._enter_service(self._appointment_type())
  644. self.assertTrue(result.success)
  645. self.assertEqual(result.availability_status, AvailabilityStatus.NoneAvailable)
  646. self.assertIsNone(result.earliest_date)
  647. self.assertEqual(result.availability, [])
  648. self.assertEqual(page.clicked, ["booking-1321"])
  649. def test_internal_entry_can_be_reprobed_without_publishing_inventory(self):
  650. page = FakeQueryPage(outcome="booking_form")
  651. plugin = self._plugin(page)
  652. apt_type = self._appointment_type()
  653. first_result = plugin._enter_service(apt_type)
  654. second_result = plugin._enter_service(apt_type)
  655. self.assertTrue(first_result.success)
  656. self.assertTrue(second_result.success)
  657. self.assertEqual(
  658. page.get_calls,
  659. [
  660. "https://prenotami.esteri.it/Services",
  661. "https://prenotami.esteri.it/Services",
  662. ],
  663. )
  664. self.assertEqual(page.clicked, ["booking-1321", "booking-1321"])
  665. def test_booking_path_without_form_fails_closed(self):
  666. page = FakeQueryPage(outcome="booking_path_without_form")
  667. plugin = self._plugin(page)
  668. with self.assertRaisesRegex(BizLogicError, "without #bookingForm"):
  669. plugin._enter_service(self._appointment_type())
  670. self.assertEqual(page.clicked, ["booking-1321"])
  671. def test_missing_target_link_is_a_structure_error_not_no_slots(self):
  672. page = FakeQueryPage(has_link=False)
  673. plugin = self._plugin(page)
  674. with self.assertRaisesRegex(
  675. ItaTargetServiceLinkMissingError,
  676. "target service link is missing",
  677. ):
  678. plugin._enter_service(self._appointment_type())
  679. self.assertEqual(page.clicked, [])
  680. self.assertIn(
  681. ("tag:a@@href=/Services/Booking/1321", SERVICE_LINK_WAIT_SECONDS),
  682. page.looked_up,
  683. )
  684. self.assertFalse(any("service-list-structure" in script for script in page.run_js_calls))
  685. def test_missing_link_with_page_notice_is_not_attributed_to_the_target(self):
  686. page = FakeQueryPage(has_link=False, already_booked_notice_rows=1)
  687. plugin = self._plugin(page)
  688. with self.assertRaises(ItaTargetServiceLinkMissingError) as raised:
  689. plugin._enter_service(self._appointment_type())
  690. self.assertIn("target service link is missing", str(raised.exception))
  691. self.assertNotIn("already-booked notice", str(raised.exception))
  692. self.assertFalse(any("service-list-structure" in script for script in page.run_js_calls))
  693. self.assertEqual(page.clicked, [])
  694. self.assertEqual(page.get_calls, ["https://prenotami.esteri.it/Services"])
  695. def test_existing_other_service_booking_does_not_block_present_target_link(self):
  696. page = FakeQueryPage(outcome="booking_form", already_booked_notice_rows=1)
  697. plugin = self._plugin(page)
  698. result = plugin._enter_service(self._appointment_type())
  699. self.assertTrue(result.success)
  700. self.assertEqual(page.clicked, ["booking-1321"])
  701. self.assertFalse(any("service-list-structure" in script for script in page.run_js_calls))
  702. def test_invalid_notice_counts_do_not_claim_an_existing_booking(self):
  703. for raw_count in (None, True, "1", -1, {}, []):
  704. with self.subTest(raw_count=raw_count):
  705. page = FakeQueryPage(has_link=False, already_booked_notice_rows=raw_count)
  706. plugin = self._plugin(page)
  707. with self.assertRaises(ItaTargetServiceLinkMissingError) as raised:
  708. plugin._enter_service(self._appointment_type())
  709. self.assertNotIn("already-booked notice", str(raised.exception))
  710. self.assertEqual(page.clicked, [])
  711. def test_book_propagates_missing_link_without_creating_a_success(self):
  712. page = FakeQueryPage(has_link=False, already_booked_notice_rows=1)
  713. plugin = self._plugin(page)
  714. trigger = VSQueryResult(apt_type=self._appointment_type())
  715. with self.assertRaisesRegex(ItaTargetServiceLinkMissingError, "target service link is missing"):
  716. plugin.book(trigger)
  717. self.assertEqual(page.clicked, [])
  718. def test_invalid_route_service_id_is_a_configuration_error(self):
  719. page = FakeQueryPage()
  720. plugin = self._plugin(page, service_id=0)
  721. with self.assertRaisesRegex(
  722. BizLogicError,
  723. "route configuration rejected.*positive_integer_required",
  724. ):
  725. plugin._enter_service(self._appointment_type())
  726. self.assertEqual(page.clicked, [])
  727. def test_second_sede_uses_same_entry_without_a_city_branch(self):
  728. routing_key = "slot.hkg.it.tourist"
  729. page = FakeQueryPage(
  730. outcome="booking_form",
  731. service_id=4937,
  732. )
  733. plugin = self._plugin(
  734. page,
  735. service_id=4937,
  736. routing_key=routing_key,
  737. )
  738. result = plugin._enter_service(
  739. self._appointment_type(routing_key, city="Hong Kong")
  740. )
  741. self.assertTrue(result.success)
  742. self.assertEqual(page.clicked, ["booking-4937"])
  743. def test_public_query_rejects_legacy_probe_before_browser_actions(self):
  744. for mode in (None, "services_probe"):
  745. for capability in ("services_probe_only", "single_schengen_tourism"):
  746. with self.subTest(mode=mode, capability=capability):
  747. page = FakeQueryPage(outcome="booking_form")
  748. plugin = self._plugin(page, capability=capability)
  749. if mode is None:
  750. plugin.free_config.pop("query_mode")
  751. with self.assertRaisesRegex(BizLogicError, "diagnostic-only"):
  752. plugin.query(self._appointment_type())
  753. self.assertEqual(page.get_calls, [])
  754. self.assertEqual(page.clicked, [])
  755. self.assertEqual(page.run_js_calls, [])
  756. def test_calendar_mode_returns_vfs_style_earliest_date_only(self):
  757. routing_key = "slot.sample.it.tourist"
  758. page = FakeQueryPage(initial_state="calendar", service_id=9001)
  759. plugin = self._plugin(
  760. page,
  761. service_id=9001,
  762. routing_key=routing_key,
  763. query_mode="calendar",
  764. capability="single_schengen_tourism",
  765. lookahead_months=12,
  766. )
  767. apt_type = self._appointment_type(
  768. routing_key,
  769. city="Sample City",
  770. )
  771. scan = (types.SimpleNamespace(
  772. available_days=(types.SimpleNamespace(day=date(2026, 10, 12)),)
  773. ),)
  774. with patch(
  775. "plugins.ita_plugin.scan_until_first_available_month",
  776. return_value=scan,
  777. ) as scanner:
  778. result = plugin.query(apt_type)
  779. scanner.assert_called_once_with(
  780. page,
  781. lookahead_months=12,
  782. log=plugin._log,
  783. )
  784. self.assertTrue(result.success)
  785. self.assertEqual(
  786. result.availability_status,
  787. AvailabilityStatus.Available,
  788. )
  789. self.assertEqual(
  790. result.earliest_date.strftime("%Y-%m-%d"),
  791. "2026-10-12",
  792. )
  793. self.assertEqual(len(result.availability), 1)
  794. self.assertEqual(
  795. result.availability[0].date.strftime("%Y-%m-%d"),
  796. "2026-10-12",
  797. )
  798. self.assertEqual(result.availability[0].times, [])
  799. self.assertEqual(page.clicked, [])
  800. self.assertFalse(hasattr(plugin, "last_calendar_query_scan"))
  801. def test_failed_query_does_not_create_diagnostic_state(self):
  802. routing_key = "slot.sample.it.tourist"
  803. page = FakeQueryPage(initial_state="calendar", service_id=9001)
  804. plugin = self._plugin(
  805. page,
  806. service_id=9001,
  807. routing_key=routing_key,
  808. query_mode="calendar",
  809. capability="single_schengen_tourism",
  810. )
  811. plugin._prepared_calendar = None
  812. with self.assertRaisesRegex(BizLogicError, "identity or route"):
  813. plugin.query(self._appointment_type(routing_key, city="Sample City"))
  814. self.assertFalse(hasattr(plugin, "last_calendar_query_scan"))
  815. def test_calendar_mode_returns_none_only_after_scanner_exhaustion(self):
  816. routing_key = "slot.sample.it.tourist"
  817. page = FakeQueryPage(initial_state="calendar", service_id=9001)
  818. plugin = self._plugin(
  819. page,
  820. service_id=9001,
  821. routing_key=routing_key,
  822. query_mode="calendar",
  823. capability="single_schengen_tourism",
  824. )
  825. apt_type = self._appointment_type(
  826. routing_key,
  827. city="Sample City",
  828. )
  829. with patch(
  830. "plugins.ita_plugin.scan_until_first_available_month",
  831. return_value=(types.SimpleNamespace(available_days=()),),
  832. ):
  833. result = plugin.query(apt_type)
  834. self.assertFalse(result.success)
  835. self.assertEqual(
  836. result.availability_status,
  837. AvailabilityStatus.NoneAvailable,
  838. )
  839. self.assertIsNone(result.earliest_date)
  840. self.assertEqual(result.availability, [])
  841. def test_calendar_mode_requires_a_route_confirmed_calendar_session(self):
  842. routing_key = "slot.sample.it.tourist"
  843. page = FakeQueryPage(initial_state="user_area", service_id=9001)
  844. plugin = self._plugin(
  845. page,
  846. service_id=9001,
  847. routing_key=routing_key,
  848. query_mode="calendar",
  849. capability="single_schengen_tourism",
  850. )
  851. plugin._prepared_calendar = None
  852. with patch(
  853. "plugins.ita_plugin.scan_until_first_available_month"
  854. ) as scanner:
  855. with self.assertRaisesRegex(
  856. BizLogicError,
  857. "preparation is not complete",
  858. ):
  859. plugin.query(
  860. self._appointment_type(
  861. routing_key,
  862. city="Sample City",
  863. )
  864. )
  865. scanner.assert_not_called()
  866. def test_calendar_mode_rejects_a_different_prepared_route(self):
  867. routing_key = "slot.sample.it.tourist"
  868. page = FakeQueryPage(initial_state="calendar", service_id=9001)
  869. plugin = self._plugin(
  870. page,
  871. service_id=9001,
  872. routing_key=routing_key,
  873. query_mode="calendar",
  874. capability="single_schengen_tourism",
  875. )
  876. plugin._prepared_calendar = replace(plugin._prepared_calendar,
  877. route=replace(plugin._prepared_calendar.route, routing_key="slot.other.it.tourist"))
  878. with patch(
  879. "plugins.ita_plugin.scan_until_first_available_month"
  880. ) as scanner:
  881. with self.assertRaisesRegex(
  882. BizLogicError,
  883. "identity or route",
  884. ):
  885. plugin.query(
  886. self._appointment_type(
  887. routing_key,
  888. city="Sample City",
  889. )
  890. )
  891. scanner.assert_not_called()
  892. def test_calendar_mode_rejects_a_probe_only_route(self):
  893. page = FakeQueryPage(initial_state="calendar")
  894. plugin = self._plugin(page, query_mode="calendar")
  895. with self.assertRaisesRegex(
  896. BizLogicError,
  897. "single_schengen_tourism",
  898. ):
  899. plugin.query(self._appointment_type())
  900. def test_login_redirect_is_reported_as_expired_session(self):
  901. page = FakeQueryPage(initial_state="login")
  902. plugin = self._plugin(page, query_mode="calendar", capability="single_schengen_tourism")
  903. with self.assertRaises(SessionExpiredOrInvalidError):
  904. plugin.query(self._appointment_type())
  905. self.assertEqual(page.clicked, [])
  906. def test_query_source_contains_no_otp_or_submission_path(self):
  907. query_source = inspect.getsource(ItaPlugin.query)
  908. forbidden_fragments = (
  909. "otp-send",
  910. "otp-input",
  911. "123456",
  912. "PrivacyCheck",
  913. "btnAvanti",
  914. "InsertNewBooking",
  915. "_perform_request",
  916. )
  917. for fragment in forbidden_fragments:
  918. with self.subTest(fragment=fragment):
  919. self.assertNotIn(fragment, query_source)
  920. class ItaPluginBookDiscoveryTests(unittest.TestCase):
  921. @staticmethod
  922. def _appointment_type():
  923. return AppointmentType(
  924. routing_key="slot.dub.it.tourist",
  925. city="Dublin",
  926. country="Italy",
  927. visa_type="Tourist",
  928. )
  929. @classmethod
  930. def _weak_signal(cls):
  931. return VSQueryResult(
  932. success=False,
  933. apt_type=cls._appointment_type(),
  934. availability_status=AvailabilityStatus.NoneAvailable,
  935. )
  936. @staticmethod
  937. def _plugin(page):
  938. plugin = ItaPlugin.__new__(ItaPlugin)
  939. plugin.group_id = "offline-book-discovery"
  940. plugin.logger = Mock()
  941. plugin.page = page
  942. plugin.mouse = FakeQueryMouse(page)
  943. plugin.free_config = {
  944. "query_mode": "services_probe",
  945. "final_submit_enabled": False,
  946. "apt_configs": {
  947. "slot.dub.it.tourist": {
  948. "service_id": 1321,
  949. "capability": "services_probe_only",
  950. }
  951. },
  952. }
  953. plugin.ita_url = "https://prenotami.esteri.it"
  954. plugin.root_workspace = "/nonexistent/ita-plugin-book-test"
  955. plugin.tunnel = None
  956. plugin._reset_calendar_state()
  957. plugin.query = Mock(side_effect=AssertionError("Book must not call public Query"))
  958. return plugin
  959. def test_book_rechecks_once_and_returns_false_when_user_has_no_slot(self):
  960. page = FakeQueryPage(outcome="no_slots", initial_state="user_area")
  961. plugin = self._plugin(page)
  962. result = plugin.book(self._weak_signal(), user_inputs={})
  963. self.assertFalse(result.success)
  964. self.assertEqual(result.label, BOOKING_RECHECK_NONE_AVAILABLE_LABEL)
  965. self.assertEqual(page.clicked, ["booking-1321"])
  966. self.assertEqual(page.run_js_calls, [])
  967. plugin.query.assert_not_called()
  968. def test_book_diagnostic_mode_stops_before_avanti_without_collecting_metadata(self):
  969. page = FakeQueryPage(outcome="booking_form", initial_state="user_area")
  970. plugin = self._plugin(page)
  971. result = plugin.book(self._weak_signal(), user_inputs={})
  972. self.assertFalse(result.success)
  973. self.assertEqual(result.label, BOOKING_FORM_DISCOVERY_STOP_LABEL)
  974. self.assertEqual(page.clicked, ["booking-1321"])
  975. self.assertEqual(page.run_js_calls, [])
  976. plugin.query.assert_not_called()
  977. def test_book_rejects_a_form_on_an_unexpected_actual_page(self):
  978. page = FakeQueryPage(outcome="unknown", initial_state="user_area")
  979. original_ele = page.ele
  980. page.ele = lambda selector, timeout=None: (
  981. FakeQueryElement(page, "wrong-form")
  982. if selector == "#bookingForm" and page.state == "unknown"
  983. else original_ele(selector, timeout))
  984. plugin = self._plugin(page)
  985. with self.assertRaisesRegex(BizLogicError, "unexpected path"):
  986. plugin.book(self._weak_signal(), user_inputs={})
  987. self.assertEqual(page.clicked, ["booking-1321"])
  988. self.assertEqual(page.run_js_calls, [])
  989. plugin.query.assert_not_called()
  990. def test_book_source_contains_no_form_submission_calendar_or_otp_path(self):
  991. book_source = inspect.getsource(ItaPlugin.book)
  992. forbidden_fragments = (
  993. "GenerateOTP",
  994. "get_email_verify_code",
  995. "fetch_mail_content",
  996. "PrivacyCheck",
  997. "btnAvanti",
  998. "BookingCalendar",
  999. "InsertNewBooking",
  1000. "_perform_request",
  1001. ".submit(",
  1002. ".click(",
  1003. )
  1004. for fragment in forbidden_fragments:
  1005. with self.subTest(fragment=fragment):
  1006. self.assertNotIn(fragment, book_source)
  1007. class FakeRetainedSignalRedis:
  1008. def __init__(self, payload, clock, *, ttl):
  1009. self.payload = json.dumps(payload)
  1010. self.clock = clock
  1011. self.ttl = ttl
  1012. def get(self, _name):
  1013. if self.clock["now"] <= self.ttl:
  1014. return self.payload
  1015. return None
  1016. def delete(self, *_names):
  1017. return 0
  1018. class CountingItaOrderBooker(OrderBookerGCO):
  1019. def _execute_book_job(self, task, query_result):
  1020. del task, query_result
  1021. self.book_calls += 1
  1022. class ItaSignalReuseTests(unittest.TestCase):
  1023. @staticmethod
  1024. def _appointment_type():
  1025. return AppointmentType(
  1026. routing_key="slot.dub.it.tourist",
  1027. city="Dublin",
  1028. country="Italy",
  1029. visa_type="Tourist",
  1030. )
  1031. def test_retained_thirty_second_signal_triggers_one_book_call(self):
  1032. apt_type = self._appointment_type()
  1033. weak_result = VSQueryResult(
  1034. success=True,
  1035. apt_type=apt_type,
  1036. availability_status=AvailabilityStatus.NoneAvailable,
  1037. )
  1038. payload = {
  1039. "apt_type": apt_type.model_dump(),
  1040. "query_result": weak_result.to_snapshot_payload(),
  1041. }
  1042. clock = {"now": 0.0}
  1043. task = Task(
  1044. instance=Mock(),
  1045. next_run=0.0,
  1046. task_ref=1,
  1047. acceptable_routing_keys=[apt_type.routing_key],
  1048. source_queue=apt_type.routing_key,
  1049. book_allowed=True,
  1050. )
  1051. config = GroupConfig(
  1052. identifier="pernotami.ie.it",
  1053. appointment_types=[apt_type],
  1054. booker={
  1055. "account_source": "order",
  1056. "target_instances": 1,
  1057. "booking_cooldown": 45,
  1058. },
  1059. )
  1060. booker = CountingItaOrderBooker.__new__(CountingItaOrderBooker)
  1061. booker.m_cfg = config
  1062. booker.m_logger = Mock()
  1063. booker.m_tasks = [task]
  1064. booker.m_lock = threading.RLock()
  1065. booker.m_stop_event = threading.Event()
  1066. booker.m_task_data_cache = {}
  1067. booker.redis_client = FakeRetainedSignalRedis(payload, clock, ttl=30)
  1068. booker.book_calls = 0
  1069. def fake_sleep(seconds):
  1070. clock["now"] += seconds
  1071. if clock["now"] > 31:
  1072. booker.m_stop_event.set()
  1073. booker_time = OrderBookerGCO._booking_trigger_loop.__globals__["time"]
  1074. with (
  1075. patch.object(booker_time, "sleep", side_effect=fake_sleep),
  1076. patch.object(booker_time, "time", side_effect=lambda: clock["now"]),
  1077. ):
  1078. booker._booking_trigger_loop()
  1079. self.assertEqual(booker.book_calls, 1)
  1080. self.assertGreater(task.next_run, 30)
  1081. def test_italy_config_keeps_existing_single_worker_cooldown_contract(self):
  1082. repo_root = Path(__file__).resolve().parents[2]
  1083. booker_groups = json.loads(
  1084. (repo_root / "config" / "config_booker.json").read_text()
  1085. )
  1086. sentinel_groups = json.loads(
  1087. (repo_root / "config" / "config_sentinel.json").read_text()
  1088. )
  1089. booker_group = next(
  1090. group
  1091. for group in booker_groups
  1092. if group["identifier"] == "pernotami.ie.it"
  1093. )
  1094. sentinel_group = next(
  1095. group
  1096. for group in sentinel_groups
  1097. if group["identifier"] == "pernotami.ie.it"
  1098. )
  1099. self.assertEqual(booker_group["booker"]["target_instances"], 1)
  1100. self.assertEqual(sentinel_group["sentinel"]["target_instances"], 1)
  1101. self.assertGreater(
  1102. booker_group["booker"]["booking_cooldown"],
  1103. sentinel_group["sentinel"]["signal_ttl"],
  1104. )
  1105. self.assertGreater(
  1106. sentinel_group["sentinel"]["query_wait"]["random_min"],
  1107. booker_group["booker"]["booking_cooldown"],
  1108. )
  1109. if __name__ == "__main__":
  1110. unittest.main()