test_ita_local_handoff.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 Mock, 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.query_calls = 0
  78. self.book_calls = 0
  79. self.cleanup_calls = 0
  80. self.last_trigger = None
  81. _FlowBookPlugin.last_instance = self
  82. def set_log(self, logger):
  83. self.logger = logger
  84. def set_config(self, config):
  85. self.config = config
  86. def create_session(self):
  87. return None
  88. def book(self, trigger, user_inputs=None):
  89. del user_inputs
  90. self.last_trigger = trigger
  91. self.book_calls += 1
  92. return VSBookResult(
  93. success=False,
  94. label=BOOKING_FORM_DISCOVERY_STOP_LABEL,
  95. )
  96. def cleanup(self):
  97. self.cleanup_calls += 1
  98. class ItaLocalHandoffStorageTests(unittest.TestCase):
  99. def test_signal_is_private_metadata_only_and_consumed_once(self):
  100. with tempfile.TemporaryDirectory() as temp_dir:
  101. root = Path(temp_dir) / "handoff"
  102. signal_path = publish_local_weak_signal(
  103. service_id=SERVICE_ID,
  104. routing_key=ROUTING_KEY,
  105. issued_at=100.0,
  106. handoff_root=root,
  107. )
  108. body = signal_path.read_text(encoding="utf-8")
  109. payload = json.loads(body)
  110. self.assertEqual(payload["service_id"], SERVICE_ID)
  111. self.assertEqual(payload["routing_key"], ROUTING_KEY)
  112. self.assertEqual(payload["evidence"], "booking-form")
  113. self.assertNotIn("username", body.lower())
  114. self.assertNotIn("password", body.lower())
  115. self.assertNotIn("cookie", body.lower())
  116. if os.name == "posix":
  117. self.assertEqual(stat.S_IMODE(root.stat().st_mode), 0o700)
  118. self.assertEqual(stat.S_IMODE(signal_path.stat().st_mode), 0o600)
  119. claimed = claim_local_weak_signal(
  120. expected_service_id=SERVICE_ID,
  121. expected_routing_key=ROUTING_KEY,
  122. now=110.0,
  123. handoff_root=root,
  124. )
  125. claimed_again = claim_local_weak_signal(
  126. expected_service_id=SERVICE_ID,
  127. expected_routing_key=ROUTING_KEY,
  128. now=110.0,
  129. handoff_root=root,
  130. )
  131. self.assertIsNotNone(claimed)
  132. self.assertEqual(claimed.service_id, SERVICE_ID)
  133. self.assertIsNone(claimed_again)
  134. def test_stale_signal_is_rejected_and_removed(self):
  135. with tempfile.TemporaryDirectory() as temp_dir:
  136. root = Path(temp_dir) / "handoff"
  137. publish_local_weak_signal(
  138. service_id=SERVICE_ID,
  139. routing_key=ROUTING_KEY,
  140. issued_at=100.0,
  141. handoff_root=root,
  142. )
  143. with self.assertRaises(ItaLocalSignalStaleError):
  144. claim_local_weak_signal(
  145. expected_service_id=SERVICE_ID,
  146. expected_routing_key=ROUTING_KEY,
  147. now=131.0,
  148. handoff_root=root,
  149. )
  150. self.assertIsNone(
  151. claim_local_weak_signal(
  152. expected_service_id=SERVICE_ID,
  153. expected_routing_key=ROUTING_KEY,
  154. now=131.0,
  155. handoff_root=root,
  156. )
  157. )
  158. def test_two_consumers_can_claim_the_signal_only_once(self):
  159. with tempfile.TemporaryDirectory() as temp_dir:
  160. root = Path(temp_dir) / "handoff"
  161. publish_local_weak_signal(
  162. service_id=SERVICE_ID,
  163. routing_key=ROUTING_KEY,
  164. issued_at=100.0,
  165. handoff_root=root,
  166. )
  167. def claim():
  168. return claim_local_weak_signal(
  169. expected_service_id=SERVICE_ID,
  170. expected_routing_key=ROUTING_KEY,
  171. now=110.0,
  172. handoff_root=root,
  173. )
  174. with ThreadPoolExecutor(max_workers=2) as pool:
  175. results = list(pool.map(lambda _index: claim(), range(2)))
  176. self.assertEqual(sum(item is not None for item in results), 1)
  177. def test_wrong_target_is_consumed_and_never_available_for_book(self):
  178. with tempfile.TemporaryDirectory() as temp_dir:
  179. root = Path(temp_dir) / "handoff"
  180. publish_local_weak_signal(
  181. service_id=9999,
  182. routing_key=ROUTING_KEY,
  183. issued_at=100.0,
  184. handoff_root=root,
  185. )
  186. with self.assertRaisesRegex(
  187. ItaLocalHandoffError,
  188. "service_id does not match",
  189. ):
  190. claim_local_weak_signal(
  191. expected_service_id=SERVICE_ID,
  192. expected_routing_key=ROUTING_KEY,
  193. now=110.0,
  194. handoff_root=root,
  195. )
  196. self.assertIsNone(
  197. claim_local_weak_signal(
  198. expected_service_id=SERVICE_ID,
  199. expected_routing_key=ROUTING_KEY,
  200. now=110.0,
  201. handoff_root=root,
  202. )
  203. )
  204. class ItaLocalHandoffFlowTests(unittest.TestCase):
  205. def test_one_query_weak_signal_triggers_one_independent_book_recheck(self):
  206. query_stdout = io.StringIO()
  207. book_stdout = io.StringIO()
  208. stderr = io.StringIO()
  209. with tempfile.TemporaryDirectory() as temp_dir:
  210. root = Path(temp_dir) / "handoff"
  211. def publish():
  212. publish_local_weak_signal(
  213. service_id=SERVICE_ID,
  214. routing_key=ROUTING_KEY,
  215. issued_at=100.0,
  216. handoff_root=root,
  217. )
  218. with (
  219. patch(
  220. "tools.debug_ita_monitor.resolve_chrome_path",
  221. return_value=Path("/test/chrome"),
  222. ),
  223. patch(
  224. "tools.debug_ita_monitor._get_query_credentials",
  225. return_value=("query@example.test", "query-password"),
  226. ),
  227. patch(
  228. "tools.debug_ita_monitor._prepare_query_profile",
  229. return_value=Path("/offline/query-profile"),
  230. ),
  231. patch(
  232. "tools.debug_ita_monitor._bind_query_profile",
  233. return_value=Mock(),
  234. ),
  235. patch("tools.debug_ita_monitor.ItaPlugin", _FlowQueryPlugin),
  236. patch.dict("tools.debug_ita_monitor.os.environ", {}, clear=False),
  237. redirect_stdout(query_stdout),
  238. redirect_stderr(stderr),
  239. ):
  240. monitor_exit = run_monitor(
  241. {},
  242. apply=True,
  243. non_interactive=True,
  244. min_wait=60,
  245. max_wait=60,
  246. max_probes=1,
  247. publish_local_signal=True,
  248. signal_publisher=publish,
  249. )
  250. def claim():
  251. return claim_local_weak_signal(
  252. expected_service_id=SERVICE_ID,
  253. expected_routing_key=ROUTING_KEY,
  254. now=110.0,
  255. handoff_root=root,
  256. )
  257. with (
  258. patch(
  259. "tools.debug_ita_book.resolve_chrome_path",
  260. return_value=Path("/test/chrome"),
  261. ),
  262. patch(
  263. "tools.debug_ita_book._get_credentials",
  264. return_value=("user@example.test", "user-password"),
  265. ),
  266. patch(
  267. "tools.debug_ita_book._assert_distinct_local_accounts"
  268. ),
  269. patch("tools.debug_ita_book.ItaPlugin", _FlowBookPlugin),
  270. patch.dict("tools.debug_ita_book.os.environ", {}, clear=False),
  271. redirect_stdout(book_stdout),
  272. redirect_stderr(stderr),
  273. ):
  274. book_exit = run_book_recheck_once(
  275. {},
  276. non_interactive=True,
  277. wait_for_local_signal=True,
  278. signal_timeout=1,
  279. signal_claimer=claim,
  280. monotonic=lambda: 0.0,
  281. sleep=lambda _seconds: None,
  282. )
  283. query_plugin = _FlowQueryPlugin.last_instance
  284. book_plugin = _FlowBookPlugin.last_instance
  285. self.assertEqual(monitor_exit, 4)
  286. self.assertEqual(book_exit, 4)
  287. self.assertIsNot(query_plugin, book_plugin)
  288. self.assertEqual(query_plugin.query_calls, 1)
  289. self.assertEqual(query_plugin.book_calls, 0)
  290. self.assertEqual(book_plugin.query_calls, 0)
  291. self.assertEqual(book_plugin.book_calls, 1)
  292. self.assertTrue(book_plugin.last_trigger.success)
  293. self.assertEqual(
  294. book_plugin.last_trigger.availability_status,
  295. AvailabilityStatus.NoneAvailable,
  296. )
  297. self.assertEqual(book_plugin.cleanup_calls, 1)
  298. self.assertIn(
  299. "ITA_MONITOR_HANDOFF_PUBLISHED",
  300. query_stdout.getvalue(),
  301. )
  302. self.assertIn("ITA_BOOK_SIGNAL_CLAIMED", book_stdout.getvalue())
  303. self.assertIn(
  304. "stage=before-avanti submit_attempts=0",
  305. book_stdout.getvalue(),
  306. )
  307. combined_output = query_stdout.getvalue() + book_stdout.getvalue()
  308. for secret in (
  309. "query@example.test",
  310. "query-password",
  311. "user@example.test",
  312. "user-password",
  313. ):
  314. self.assertNotIn(secret, combined_output)
  315. self.assertIsNone(claim())
  316. self.assertEqual(stderr.getvalue(), "")
  317. if __name__ == "__main__":
  318. unittest.main()