test_ita_booker_integration.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Italy Book integration with the existing shared OrderBooker."""
  2. import json
  3. import threading
  4. import unittest
  5. from types import SimpleNamespace
  6. from unittest.mock import Mock, patch
  7. from plugins.ita_plugin import FINAL_PATH, NATIVE_FINAL_SCRIPT
  8. from test.ita import test_ita_calendar_book_flow as flow
  9. from test.ita.test_ita_plugin import OrderBookerGCO
  10. class FinalPage(flow.FlowPage):
  11. def __init__(self):
  12. super().__init__([flow.month("2026-09", {1: 0}), flow.month("2026-10", {13: 2})])
  13. self.url += "?selectedService=Visti%20Nazionali%20e%20Schengen"
  14. self.final_count = 0
  15. self.response_error = ""
  16. self.wrong_request_day = False
  17. def run_js(self, script, *args):
  18. if script != NATIVE_FINAL_SCRIPT:
  19. return super().run_js(script, *args)
  20. _service, day, daily_id, time_label, capacity = args
  21. self.final_count += 1
  22. self.listen.queue.append(SimpleNamespace(
  23. url="https://prenotami.esteri.it" + FINAL_PATH,
  24. method="POST",
  25. tab_id=self.tab_id,
  26. is_failed=False,
  27. request=SimpleNamespace(postData={
  28. "idCalendarioGiornaliero": daily_id,
  29. "selectedDay": "2026-10-14" if self.wrong_request_day else day,
  30. "selectedHour": f"{time_label}\n({capacity})",
  31. }),
  32. response=SimpleNamespace(
  33. status=200,
  34. url="https://prenotami.esteri.it" + FINAL_PATH,
  35. raw_body=json.dumps({
  36. "errorMessage": self.response_error,
  37. "url": "/Reservation" if not self.response_error else "/BookingCalendar",
  38. }),
  39. ),
  40. ))
  41. return {"clicked": True}
  42. class BookerIntegrationTests(unittest.TestCase):
  43. def setUp(self):
  44. self.page = FinalPage()
  45. self.plugin, self.route, self.trigger = flow.prepared_plugin(self.page)
  46. self.plugin.free_config.update(otp_mode="manual", final_submit_enabled=True)
  47. self.plugin.is_healthy = True
  48. self.plugin._log = Mock()
  49. self.inputs = {
  50. "username": self.plugin.config.account.username,
  51. "password": "synthetic-not-for-reporting",
  52. "expected_start_date": "2026-09-01",
  53. "expected_end_date": "2026-10-31",
  54. }
  55. self.task = SimpleNamespace(task_ref=991, book_allowed=True, instance=self.plugin)
  56. self.booker = OrderBookerGCO.__new__(OrderBookerGCO)
  57. self.booker.m_lock = threading.RLock()
  58. self.booker.m_tasks = [self.task]
  59. self.booker.m_task_data_cache = {"991": {
  60. "status": "processing", "order_id": "SYNTHETIC-ORDER", "user_inputs": self.inputs,
  61. }}
  62. self.booker.m_tracker_key = "synthetic-tracker"
  63. self.booker.redis_client = Mock()
  64. self.booker._log = Mock()
  65. self.booker._cleanup_task = Mock()
  66. self.booker._on_task_removed = Mock()
  67. self.cloud = Mock()
  68. self.queue = []
  69. self.executor = Mock()
  70. self.executor.enqueue.side_effect = self.queue.append
  71. self.enterContext(patch.dict(
  72. self.booker._execute_book_job.__func__.__globals__,
  73. VSCloudApi=SimpleNamespace(Instance=lambda: self.cloud),
  74. ThreadPool=SimpleNamespace(getInstance=lambda: self.executor),
  75. ))
  76. self.enterContext(flow.browser_navigation(self.page))
  77. def run_booker(self):
  78. self.booker._execute_book_job(self.task, self.trigger)
  79. def drain_reports(self):
  80. while self.queue:
  81. self.queue.pop(0)()
  82. def test_explicit_site_success_uses_shared_success_reporting(self):
  83. self.run_booker()
  84. self.assertEqual(self.page.final_count, 1)
  85. self.assertEqual(self.booker.m_tasks, [])
  86. self.booker._cleanup_task.assert_called_once_with(self.task, "booking success")
  87. self.assertEqual(len(self.queue), 1)
  88. self.drain_reports()
  89. task_id, payload = self.cloud.update_vas_task.call_args.args
  90. self.assertEqual(task_id, "991")
  91. self.assertEqual(payload["status"], "grabbed")
  92. history = payload["grabbed_history"]
  93. self.assertEqual(history["urn"], "")
  94. self.assertEqual(history["account"], self.inputs["username"].lower())
  95. self.assertEqual(history["slot_date"], "2026-10-13")
  96. self.assertEqual(history["slot_time"], "10:05 - 11:05")
  97. self.assertNotIn(self.inputs["password"], json.dumps(payload))
  98. def test_site_rejection_stays_in_shared_retry_on_next_signal_branch(self):
  99. self.page.response_error = "Synthetic slot no longer available"
  100. self.run_booker()
  101. self.assertEqual(self.page.final_count, 1)
  102. self.assertEqual(self.booker.m_tasks, [self.task])
  103. self.assertTrue(self.task.book_allowed)
  104. self.assertIsNone(self.plugin._prepared_calendar)
  105. self.cloud.update_vas_task.assert_not_called()
  106. self.executor.enqueue.assert_not_called()
  107. def test_later_signal_can_retry_after_fresh_calendar_preparation(self):
  108. self.page.response_error = "Synthetic rejection"
  109. self.run_booker()
  110. self.plugin._record_calendar_prepared_route(self.route, role="book")
  111. self.page.response_error = ""
  112. self.run_booker()
  113. self.assertEqual(self.page.final_count, 2)
  114. self.assertEqual(self.booker.m_tasks, [])
  115. self.assertTrue(any("Will retry on next signal" in str(call) for call in self.booker._log.call_args_list))
  116. def test_unmatched_post_is_failure_not_pause(self):
  117. self.page.wrong_request_day = True
  118. self.run_booker()
  119. self.assertEqual(self.page.final_count, 1)
  120. self.assertEqual(self.booker.m_tasks, [self.task])
  121. self.assertTrue(self.task.book_allowed)
  122. self.cloud.update_vas_task.assert_not_called()
  123. def test_prefinal_mode_never_calls_final_or_reports_success(self):
  124. self.plugin.free_config["final_submit_enabled"] = False
  125. self.run_booker()
  126. self.assertEqual(self.page.final_count, 0)
  127. self.assertEqual(self.booker.m_tasks, [self.task])
  128. self.executor.enqueue.assert_not_called()
  129. self.cloud.update_vas_task.assert_not_called()
  130. if __name__ == "__main__":
  131. unittest.main()