test_ita_local_handoff.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import io
  2. import json
  3. import os
  4. import stat
  5. import tempfile
  6. import unittest
  7. from concurrent.futures import ThreadPoolExecutor
  8. from contextlib import redirect_stderr, redirect_stdout
  9. from pathlib import Path
  10. from unittest.mock import patch
  11. from plugins.ita_plugin import BOOKING_FORM_DISCOVERY_STOP_LABEL
  12. from tools.debug_ita_book import run_book_recheck_once
  13. from tools.debug_ita_monitor import run_monitor
  14. from tools.ita_local_handoff import (
  15. ItaLocalHandoffError,
  16. ItaLocalSignalStaleError,
  17. claim_local_weak_signal,
  18. publish_local_weak_signal,
  19. )
  20. from vs_types import AvailabilityStatus, VSBookResult, VSQueryResult
  21. SERVICE_ID = 1321
  22. ROUTING_KEY = "slot.dub.it.tourist"
  23. class _FlowPage:
  24. url = "https://prenotami.esteri.it/Services/Booking/1321"
  25. def ele(self, _selector, timeout=None):
  26. del timeout
  27. return None
  28. def run_js(self, _script):
  29. return {
  30. "path": "/Services/Booking/1321",
  31. "form": {
  32. "method": "POST",
  33. "action_path": "/Services/Booking/1321",
  34. },
  35. "field_count": 0,
  36. "fields": [],
  37. "button_count": 1,
  38. "buttons": [
  39. {
  40. "tag": "button",
  41. "type": "submit",
  42. "id": "btnAvanti",
  43. "name": None,
  44. "text": "Avanti",
  45. }
  46. ],
  47. }
  48. class _FlowQueryPlugin:
  49. last_instance = None
  50. def __init__(self, group_id):
  51. self.group_id = group_id
  52. self.page = _FlowPage()
  53. self.query_calls = 0
  54. self.book_calls = 0
  55. self.cleanup_calls = 0
  56. _FlowQueryPlugin.last_instance = self
  57. def set_log(self, logger):
  58. self.logger = logger
  59. def set_config(self, config):
  60. self.config = config
  61. def create_session(self):
  62. return None
  63. def query(self, apt_type):
  64. self.query_calls += 1
  65. return VSQueryResult(
  66. success=True,
  67. apt_type=apt_type,
  68. availability_status=AvailabilityStatus.NoneAvailable,
  69. )
  70. def cleanup(self):
  71. self.cleanup_calls += 1
  72. class _FlowBookPlugin:
  73. last_instance = None
  74. def __init__(self, group_id):
  75. self.group_id = group_id
  76. self.page = _FlowPage()
  77. self.last_booking_form_structure = self.page.run_js("")
  78. self.query_calls = 0
  79. self.book_calls = 0
  80. self.cleanup_calls = 0
  81. self.last_trigger = None
  82. _FlowBookPlugin.last_instance = self
  83. def set_log(self, logger):
  84. self.logger = logger
  85. def set_config(self, config):
  86. self.config = config
  87. def create_session(self):
  88. return None
  89. def book(self, trigger, user_inputs=None):
  90. del user_inputs
  91. self.last_trigger = trigger
  92. self.book_calls += 1
  93. return VSBookResult(
  94. success=False,
  95. label=BOOKING_FORM_DISCOVERY_STOP_LABEL,
  96. )
  97. def cleanup(self):
  98. self.cleanup_calls += 1
  99. class ItaLocalHandoffStorageTests(unittest.TestCase):
  100. def test_signal_is_private_metadata_only_and_consumed_once(self):
  101. with tempfile.TemporaryDirectory() as temp_dir:
  102. root = Path(temp_dir) / "handoff"
  103. signal_path = publish_local_weak_signal(
  104. service_id=SERVICE_ID,
  105. routing_key=ROUTING_KEY,
  106. issued_at=100.0,
  107. handoff_root=root,
  108. )
  109. body = signal_path.read_text(encoding="utf-8")
  110. payload = json.loads(body)
  111. self.assertEqual(payload["service_id"], SERVICE_ID)
  112. self.assertEqual(payload["routing_key"], ROUTING_KEY)
  113. self.assertEqual(payload["evidence"], "booking-form")
  114. self.assertNotIn("username", body.lower())
  115. self.assertNotIn("password", body.lower())
  116. self.assertNotIn("cookie", body.lower())
  117. if os.name == "posix":
  118. self.assertEqual(stat.S_IMODE(root.stat().st_mode), 0o700)
  119. self.assertEqual(stat.S_IMODE(signal_path.stat().st_mode), 0o600)
  120. claimed = claim_local_weak_signal(
  121. expected_service_id=SERVICE_ID,
  122. expected_routing_key=ROUTING_KEY,
  123. now=110.0,
  124. handoff_root=root,
  125. )
  126. claimed_again = claim_local_weak_signal(
  127. expected_service_id=SERVICE_ID,
  128. expected_routing_key=ROUTING_KEY,
  129. now=110.0,
  130. handoff_root=root,
  131. )
  132. self.assertIsNotNone(claimed)
  133. self.assertEqual(claimed.service_id, SERVICE_ID)
  134. self.assertIsNone(claimed_again)
  135. def test_stale_signal_is_rejected_and_removed(self):
  136. with tempfile.TemporaryDirectory() as temp_dir:
  137. root = Path(temp_dir) / "handoff"
  138. publish_local_weak_signal(
  139. service_id=SERVICE_ID,
  140. routing_key=ROUTING_KEY,
  141. issued_at=100.0,
  142. handoff_root=root,
  143. )
  144. with self.assertRaises(ItaLocalSignalStaleError):
  145. claim_local_weak_signal(
  146. expected_service_id=SERVICE_ID,
  147. expected_routing_key=ROUTING_KEY,
  148. now=131.0,
  149. handoff_root=root,
  150. )
  151. self.assertIsNone(
  152. claim_local_weak_signal(
  153. expected_service_id=SERVICE_ID,
  154. expected_routing_key=ROUTING_KEY,
  155. now=131.0,
  156. handoff_root=root,
  157. )
  158. )
  159. def test_two_consumers_can_claim_the_signal_only_once(self):
  160. with tempfile.TemporaryDirectory() as temp_dir:
  161. root = Path(temp_dir) / "handoff"
  162. publish_local_weak_signal(
  163. service_id=SERVICE_ID,
  164. routing_key=ROUTING_KEY,
  165. issued_at=100.0,
  166. handoff_root=root,
  167. )
  168. def claim():
  169. return claim_local_weak_signal(
  170. expected_service_id=SERVICE_ID,
  171. expected_routing_key=ROUTING_KEY,
  172. now=110.0,
  173. handoff_root=root,
  174. )
  175. with ThreadPoolExecutor(max_workers=2) as pool:
  176. results = list(pool.map(lambda _index: claim(), range(2)))
  177. self.assertEqual(sum(item is not None for item in results), 1)
  178. def test_wrong_target_is_consumed_and_never_available_for_book(self):
  179. with tempfile.TemporaryDirectory() as temp_dir:
  180. root = Path(temp_dir) / "handoff"
  181. publish_local_weak_signal(
  182. service_id=9999,
  183. routing_key=ROUTING_KEY,
  184. issued_at=100.0,
  185. handoff_root=root,
  186. )
  187. with self.assertRaisesRegex(
  188. ItaLocalHandoffError,
  189. "service_id does not match",
  190. ):
  191. claim_local_weak_signal(
  192. expected_service_id=SERVICE_ID,
  193. expected_routing_key=ROUTING_KEY,
  194. now=110.0,
  195. handoff_root=root,
  196. )
  197. self.assertIsNone(
  198. claim_local_weak_signal(
  199. expected_service_id=SERVICE_ID,
  200. expected_routing_key=ROUTING_KEY,
  201. now=110.0,
  202. handoff_root=root,
  203. )
  204. )
  205. class ItaLocalHandoffFlowTests(unittest.TestCase):
  206. def test_one_query_weak_signal_triggers_one_independent_book_recheck(self):
  207. query_stdout = io.StringIO()
  208. book_stdout = io.StringIO()
  209. stderr = io.StringIO()
  210. with tempfile.TemporaryDirectory() as temp_dir:
  211. root = Path(temp_dir) / "handoff"
  212. def publish():
  213. publish_local_weak_signal(
  214. service_id=SERVICE_ID,
  215. routing_key=ROUTING_KEY,
  216. issued_at=100.0,
  217. handoff_root=root,
  218. )
  219. with (
  220. patch(
  221. "tools.debug_ita_monitor.resolve_chrome_path",
  222. return_value=Path("/test/chrome"),
  223. ),
  224. patch(
  225. "tools.debug_ita_monitor._get_query_credentials",
  226. return_value=("query@example.test", "query-password"),
  227. ),
  228. patch(
  229. "tools.debug_ita_monitor._prepare_query_profile",
  230. return_value=Path("/offline/query-profile"),
  231. ),
  232. patch("tools.debug_ita_monitor.ItaPlugin", _FlowQueryPlugin),
  233. patch.dict("tools.debug_ita_monitor.os.environ", {}, clear=False),
  234. redirect_stdout(query_stdout),
  235. redirect_stderr(stderr),
  236. ):
  237. monitor_exit = run_monitor(
  238. {},
  239. apply=True,
  240. non_interactive=True,
  241. min_wait=60,
  242. max_wait=60,
  243. max_probes=1,
  244. publish_local_signal=True,
  245. signal_publisher=publish,
  246. )
  247. def claim():
  248. return claim_local_weak_signal(
  249. expected_service_id=SERVICE_ID,
  250. expected_routing_key=ROUTING_KEY,
  251. now=110.0,
  252. handoff_root=root,
  253. )
  254. with (
  255. patch(
  256. "tools.debug_ita_book.resolve_chrome_path",
  257. return_value=Path("/test/chrome"),
  258. ),
  259. patch(
  260. "tools.debug_ita_book._get_credentials",
  261. return_value=("user@example.test", "user-password"),
  262. ),
  263. patch(
  264. "tools.debug_ita_book._assert_distinct_local_accounts"
  265. ),
  266. patch("tools.debug_ita_book.ItaPlugin", _FlowBookPlugin),
  267. patch.dict("tools.debug_ita_book.os.environ", {}, clear=False),
  268. redirect_stdout(book_stdout),
  269. redirect_stderr(stderr),
  270. ):
  271. book_exit = run_book_recheck_once(
  272. {},
  273. non_interactive=True,
  274. wait_for_local_signal=True,
  275. signal_timeout=1,
  276. signal_claimer=claim,
  277. monotonic=lambda: 0.0,
  278. sleep=lambda _seconds: None,
  279. )
  280. query_plugin = _FlowQueryPlugin.last_instance
  281. book_plugin = _FlowBookPlugin.last_instance
  282. self.assertEqual(monitor_exit, 4)
  283. self.assertEqual(book_exit, 4)
  284. self.assertIsNot(query_plugin, book_plugin)
  285. self.assertEqual(query_plugin.query_calls, 1)
  286. self.assertEqual(query_plugin.book_calls, 0)
  287. self.assertEqual(book_plugin.query_calls, 0)
  288. self.assertEqual(book_plugin.book_calls, 1)
  289. self.assertTrue(book_plugin.last_trigger.success)
  290. self.assertEqual(
  291. book_plugin.last_trigger.availability_status,
  292. AvailabilityStatus.NoneAvailable,
  293. )
  294. self.assertEqual(book_plugin.cleanup_calls, 1)
  295. self.assertIn(
  296. "ITA_MONITOR_HANDOFF_PUBLISHED",
  297. query_stdout.getvalue(),
  298. )
  299. self.assertIn("ITA_BOOK_SIGNAL_CLAIMED", book_stdout.getvalue())
  300. self.assertIn(
  301. "stage=before-avanti submit_attempts=0",
  302. book_stdout.getvalue(),
  303. )
  304. combined_output = query_stdout.getvalue() + book_stdout.getvalue()
  305. for secret in (
  306. "query@example.test",
  307. "query-password",
  308. "user@example.test",
  309. "user-password",
  310. ):
  311. self.assertNotIn(secret, combined_output)
  312. self.assertIsNone(claim())
  313. self.assertEqual(stderr.getvalue(), "")
  314. if __name__ == "__main__":
  315. unittest.main()