| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- import io
- import json
- import os
- import stat
- import tempfile
- import unittest
- from concurrent.futures import ThreadPoolExecutor
- from contextlib import redirect_stderr, redirect_stdout
- from pathlib import Path
- from unittest.mock import patch
- from plugins.ita_plugin import BOOKING_FORM_DISCOVERY_STOP_LABEL
- from tools.debug_ita_book import run_book_recheck_once
- from tools.debug_ita_monitor import run_monitor
- from tools.ita_local_handoff import (
- ItaLocalHandoffError,
- ItaLocalSignalStaleError,
- claim_local_weak_signal,
- publish_local_weak_signal,
- )
- from vs_types import AvailabilityStatus, VSBookResult, VSQueryResult
- SERVICE_ID = 1321
- ROUTING_KEY = "slot.dub.it.tourist"
- class _FlowPage:
- url = "https://prenotami.esteri.it/Services/Booking/1321"
- def ele(self, _selector, timeout=None):
- del timeout
- return None
- def run_js(self, _script):
- return {
- "path": "/Services/Booking/1321",
- "form": {
- "method": "POST",
- "action_path": "/Services/Booking/1321",
- },
- "field_count": 0,
- "fields": [],
- "button_count": 1,
- "buttons": [
- {
- "tag": "button",
- "type": "submit",
- "id": "btnAvanti",
- "name": None,
- "text": "Avanti",
- }
- ],
- }
- class _FlowQueryPlugin:
- last_instance = None
- def __init__(self, group_id):
- self.group_id = group_id
- self.page = _FlowPage()
- self.query_calls = 0
- self.book_calls = 0
- self.cleanup_calls = 0
- _FlowQueryPlugin.last_instance = self
- def set_log(self, logger):
- self.logger = logger
- def set_config(self, config):
- self.config = config
- def create_session(self):
- return None
- def query(self, apt_type):
- self.query_calls += 1
- return VSQueryResult(
- success=True,
- apt_type=apt_type,
- availability_status=AvailabilityStatus.NoneAvailable,
- )
- def cleanup(self):
- self.cleanup_calls += 1
- class _FlowBookPlugin:
- last_instance = None
- def __init__(self, group_id):
- self.group_id = group_id
- self.page = _FlowPage()
- self.last_booking_form_structure = self.page.run_js("")
- self.query_calls = 0
- self.book_calls = 0
- self.cleanup_calls = 0
- self.last_trigger = None
- _FlowBookPlugin.last_instance = self
- def set_log(self, logger):
- self.logger = logger
- def set_config(self, config):
- self.config = config
- def create_session(self):
- return None
- def book(self, trigger, user_inputs=None):
- del user_inputs
- self.last_trigger = trigger
- self.book_calls += 1
- return VSBookResult(
- success=False,
- label=BOOKING_FORM_DISCOVERY_STOP_LABEL,
- )
- def cleanup(self):
- self.cleanup_calls += 1
- class ItaLocalHandoffStorageTests(unittest.TestCase):
- def test_signal_is_private_metadata_only_and_consumed_once(self):
- with tempfile.TemporaryDirectory() as temp_dir:
- root = Path(temp_dir) / "handoff"
- signal_path = publish_local_weak_signal(
- service_id=SERVICE_ID,
- routing_key=ROUTING_KEY,
- issued_at=100.0,
- handoff_root=root,
- )
- body = signal_path.read_text(encoding="utf-8")
- payload = json.loads(body)
- self.assertEqual(payload["service_id"], SERVICE_ID)
- self.assertEqual(payload["routing_key"], ROUTING_KEY)
- self.assertEqual(payload["evidence"], "booking-form")
- self.assertNotIn("username", body.lower())
- self.assertNotIn("password", body.lower())
- self.assertNotIn("cookie", body.lower())
- if os.name == "posix":
- self.assertEqual(stat.S_IMODE(root.stat().st_mode), 0o700)
- self.assertEqual(stat.S_IMODE(signal_path.stat().st_mode), 0o600)
- claimed = claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=110.0,
- handoff_root=root,
- )
- claimed_again = claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=110.0,
- handoff_root=root,
- )
- self.assertIsNotNone(claimed)
- self.assertEqual(claimed.service_id, SERVICE_ID)
- self.assertIsNone(claimed_again)
- def test_stale_signal_is_rejected_and_removed(self):
- with tempfile.TemporaryDirectory() as temp_dir:
- root = Path(temp_dir) / "handoff"
- publish_local_weak_signal(
- service_id=SERVICE_ID,
- routing_key=ROUTING_KEY,
- issued_at=100.0,
- handoff_root=root,
- )
- with self.assertRaises(ItaLocalSignalStaleError):
- claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=131.0,
- handoff_root=root,
- )
- self.assertIsNone(
- claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=131.0,
- handoff_root=root,
- )
- )
- def test_two_consumers_can_claim_the_signal_only_once(self):
- with tempfile.TemporaryDirectory() as temp_dir:
- root = Path(temp_dir) / "handoff"
- publish_local_weak_signal(
- service_id=SERVICE_ID,
- routing_key=ROUTING_KEY,
- issued_at=100.0,
- handoff_root=root,
- )
- def claim():
- return claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=110.0,
- handoff_root=root,
- )
- with ThreadPoolExecutor(max_workers=2) as pool:
- results = list(pool.map(lambda _index: claim(), range(2)))
- self.assertEqual(sum(item is not None for item in results), 1)
- def test_wrong_target_is_consumed_and_never_available_for_book(self):
- with tempfile.TemporaryDirectory() as temp_dir:
- root = Path(temp_dir) / "handoff"
- publish_local_weak_signal(
- service_id=9999,
- routing_key=ROUTING_KEY,
- issued_at=100.0,
- handoff_root=root,
- )
- with self.assertRaisesRegex(
- ItaLocalHandoffError,
- "service_id does not match",
- ):
- claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=110.0,
- handoff_root=root,
- )
- self.assertIsNone(
- claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=110.0,
- handoff_root=root,
- )
- )
- class ItaLocalHandoffFlowTests(unittest.TestCase):
- def test_one_query_weak_signal_triggers_one_independent_book_recheck(self):
- query_stdout = io.StringIO()
- book_stdout = io.StringIO()
- stderr = io.StringIO()
- with tempfile.TemporaryDirectory() as temp_dir:
- root = Path(temp_dir) / "handoff"
- def publish():
- publish_local_weak_signal(
- service_id=SERVICE_ID,
- routing_key=ROUTING_KEY,
- issued_at=100.0,
- handoff_root=root,
- )
- with (
- patch(
- "tools.debug_ita_monitor.resolve_chrome_path",
- return_value=Path("/test/chrome"),
- ),
- patch(
- "tools.debug_ita_monitor._get_query_credentials",
- return_value=("query@example.test", "query-password"),
- ),
- patch(
- "tools.debug_ita_monitor._prepare_query_profile",
- return_value=Path("/offline/query-profile"),
- ),
- patch("tools.debug_ita_monitor.ItaPlugin", _FlowQueryPlugin),
- patch.dict("tools.debug_ita_monitor.os.environ", {}, clear=False),
- redirect_stdout(query_stdout),
- redirect_stderr(stderr),
- ):
- monitor_exit = run_monitor(
- {},
- apply=True,
- non_interactive=True,
- min_wait=60,
- max_wait=60,
- max_probes=1,
- publish_local_signal=True,
- signal_publisher=publish,
- )
- def claim():
- return claim_local_weak_signal(
- expected_service_id=SERVICE_ID,
- expected_routing_key=ROUTING_KEY,
- now=110.0,
- handoff_root=root,
- )
- with (
- patch(
- "tools.debug_ita_book.resolve_chrome_path",
- return_value=Path("/test/chrome"),
- ),
- patch(
- "tools.debug_ita_book._get_credentials",
- return_value=("user@example.test", "user-password"),
- ),
- patch(
- "tools.debug_ita_book._assert_distinct_local_accounts"
- ),
- patch("tools.debug_ita_book.ItaPlugin", _FlowBookPlugin),
- patch.dict("tools.debug_ita_book.os.environ", {}, clear=False),
- redirect_stdout(book_stdout),
- redirect_stderr(stderr),
- ):
- book_exit = run_book_recheck_once(
- {},
- non_interactive=True,
- wait_for_local_signal=True,
- signal_timeout=1,
- signal_claimer=claim,
- monotonic=lambda: 0.0,
- sleep=lambda _seconds: None,
- )
- query_plugin = _FlowQueryPlugin.last_instance
- book_plugin = _FlowBookPlugin.last_instance
- self.assertEqual(monitor_exit, 4)
- self.assertEqual(book_exit, 4)
- self.assertIsNot(query_plugin, book_plugin)
- self.assertEqual(query_plugin.query_calls, 1)
- self.assertEqual(query_plugin.book_calls, 0)
- self.assertEqual(book_plugin.query_calls, 0)
- self.assertEqual(book_plugin.book_calls, 1)
- self.assertTrue(book_plugin.last_trigger.success)
- self.assertEqual(
- book_plugin.last_trigger.availability_status,
- AvailabilityStatus.NoneAvailable,
- )
- self.assertEqual(book_plugin.cleanup_calls, 1)
- self.assertIn(
- "ITA_MONITOR_HANDOFF_PUBLISHED",
- query_stdout.getvalue(),
- )
- self.assertIn("ITA_BOOK_SIGNAL_CLAIMED", book_stdout.getvalue())
- self.assertIn(
- "stage=before-avanti submit_attempts=0",
- book_stdout.getvalue(),
- )
- combined_output = query_stdout.getvalue() + book_stdout.getvalue()
- for secret in (
- "query@example.test",
- "query-password",
- "user@example.test",
- "user-password",
- ):
- self.assertNotIn(secret, combined_output)
- self.assertIsNone(claim())
- self.assertEqual(stderr.getvalue(), "")
- if __name__ == "__main__":
- unittest.main()
|