test_ita_booking_final.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """Final POST contract through the current adapter; no browser or network."""
  2. import json
  3. import unittest
  4. from datetime import date, time
  5. from types import SimpleNamespace
  6. from unittest.mock import Mock, patch
  7. from plugins import ita_plugin as ita
  8. from test.ita.test_ita_compact import Response, route_config
  9. from vs_types import VSAccount
  10. class FinalSubmissionTests(unittest.TestCase):
  11. def setUp(self):
  12. self.route = ita.resolve_route_config({"route": route_config()}, "route")
  13. self.slot = ita.SlotCandidate(date(2026, 10, 2), 1, 0,
  14. time(10, 5), time(11, 5), "12345678")
  15. self.page = SimpleNamespace(tab_id="applicant-tab",
  16. url="https://prenotami.esteri.it/BookingCalendar")
  17. self.plugin = SimpleNamespace(
  18. page=self.page,
  19. config=SimpleNamespace(account=VSAccount(username="applicant@example.test")),
  20. _require_calendar_context=Mock(return_value=SimpleNamespace(
  21. tab_id=self.page.tab_id, runtime_service="4937")),
  22. _reset_calendar_state=Mock(),
  23. )
  24. self.verify = self.enterContext(patch.object(ita, "verify_precommit_selection"))
  25. self.fetch = self.enterContext(patch.object(ita, "perform_browser_fetch"))
  26. self.respond()
  27. def respond(self, *, status=200, body=None, url=None):
  28. self.fetch.return_value = Response(
  29. status, body if body is not None else json.dumps({
  30. "errorMessage": "", "url": "/Reservation"}),
  31. url or "https://prenotami.esteri.it" + ita.FINAL_PATH)
  32. def submit(self):
  33. return ita._submit_final_once(self.plugin, self.route, self.slot)
  34. def test_one_form_post_uses_current_applicant_selection(self):
  35. result = self.submit()
  36. self.assertTrue(result.success)
  37. self.assertEqual(result.account, "applicant@example.test")
  38. self.assertEqual(result.book_date, "2026-10-02")
  39. self.assertEqual(result.book_time, "10:05 - 11:05")
  40. self.fetch.assert_called_once_with(self.page, "POST", ita.FINAL_PATH, timeout=30, data={
  41. "idCalendarioGiornaliero": "12345678",
  42. "selectedDay": "2026-10-02", "selectedHour": "10:05 - 11:05\n(1)",
  43. })
  44. self.verify.assert_called_once_with(self.page, runtime_service="4937", candidate=self.slot)
  45. self.plugin._require_calendar_context.assert_called_once_with(
  46. self.route, role="book", username="applicant@example.test")
  47. def test_site_rejection_returns_false_and_discards_selection(self):
  48. self.respond(body=json.dumps({"errorMessage": "No longer available", "url": "/BookingCalendar"}))
  49. self.assertFalse(self.submit().success)
  50. self.fetch.assert_called_once()
  51. self.plugin._reset_calendar_state.assert_called_once()
  52. def test_http_errors_keep_framework_categories_without_internal_retry(self):
  53. for status, error in ((401, ita.SessionExpiredOrInvalidError),
  54. (403, ita.PermissionDeniedError), (404, ita.NotFoundError),
  55. (429, ita.RateLimiteddError), (0, ita.BizLogicError),
  56. (408, ita.BizLogicError), (500, ita.BizLogicError),
  57. (502, ita.BizLogicError), (503, ita.BizLogicError),
  58. (504, ita.BizLogicError)):
  59. with self.subTest(status=status):
  60. self.fetch.reset_mock()
  61. self.plugin._reset_calendar_state.reset_mock()
  62. self.respond(status=status)
  63. with self.assertRaises(error):
  64. self.submit()
  65. self.fetch.assert_called_once()
  66. self.plugin._reset_calendar_state.assert_called_once()
  67. def test_followed_iam_redirect_is_session_expiry(self):
  68. self.respond(url="https://iam.esteri.it/signin/", body="<html>Login</html>")
  69. with self.assertRaises(ita.SessionExpiredOrInvalidError):
  70. self.submit()
  71. self.plugin._reset_calendar_state.assert_called_once()
  72. def test_network_exception_does_not_repost_or_keep_stale_ids(self):
  73. self.fetch.side_effect = TimeoutError("synthetic timeout")
  74. with self.assertRaises(ita.BizLogicError) as caught:
  75. self.submit()
  76. self.assertIsInstance(caught.exception.__cause__, TimeoutError)
  77. self.fetch.assert_called_once()
  78. self.plugin._reset_calendar_state.assert_called_once()
  79. def test_existing_framework_exception_is_not_wrapped(self):
  80. for error_type in (ita.SessionExpiredOrInvalidError, ita.PermissionDeniedError,
  81. ita.NotFoundError, ita.RateLimiteddError, ita.BizLogicError):
  82. with self.subTest(error_type=error_type):
  83. error = error_type("synthetic")
  84. self.fetch.side_effect = error
  85. with self.assertRaises(error_type) as caught:
  86. self.submit()
  87. self.assertIs(caught.exception, error)
  88. def test_malformed_or_incomplete_response_is_not_success(self):
  89. for body in ("not json", "<html>error</html>", "null", "[]", "{}",
  90. '{"url":"/Reservation"}', '{"errorMessage":null,"url":"/Reservation"}',
  91. '{"errorMessage":false,"url":"/Reservation"}',
  92. '{"errorMessage":""}', '{"errorMessage":"","url":false}'):
  93. with self.subTest(body=body):
  94. self.fetch.reset_mock()
  95. self.respond(body=body)
  96. with self.assertRaises(ita.BizLogicError):
  97. self.submit()
  98. self.fetch.assert_called_once()
  99. def test_error_or_auth_pages_are_not_receipts(self):
  100. for destination in ("", " ", "/", "/UserArea", "/Services", "/BookingCalendar",
  101. "/Error", "/ReservationDetail/Index/", "/Reservation/other",
  102. "https://attacker.test/Reservation", "//attacker.test/Reservation",
  103. "http://prenotami.esteri.it/Reservation",
  104. "https://prenotami.esteri.it:8443/Reservation",
  105. "https://user@prenotami.esteri.it/Reservation"):
  106. with self.subTest(destination=destination):
  107. self.respond(body=json.dumps({"errorMessage": "", "url": destination}))
  108. with self.assertRaises(ita.BizLogicError):
  109. self.submit()
  110. def test_known_receipt_paths_accept_relative_and_absolute_urls(self):
  111. # Repository contract fixtures, not evidence of a live successful booking.
  112. for destination in ("/Reservation", "/Reservation/",
  113. "https://prenotami.esteri.it/Reservation",
  114. "/ReservationDetail/Index/synthetic-receipt"):
  115. with self.subTest(destination=destination):
  116. self.respond(body=json.dumps({"errorMessage": "", "url": destination}))
  117. self.assertTrue(self.submit().success)
  118. def test_foreign_or_wrong_final_response_endpoint_is_rejected(self):
  119. for url in ("https://attacker.test" + ita.FINAL_PATH,
  120. "https://prenotami.esteri.it/Reservation"):
  121. with self.subTest(url=url):
  122. self.respond(url=url)
  123. with self.assertRaises(ita.BizLogicError):
  124. self.submit()
  125. def test_changed_session_stops_before_post(self):
  126. self.plugin._require_calendar_context.side_effect = ita.SessionExpiredOrInvalidError()
  127. with self.assertRaises(ita.SessionExpiredOrInvalidError):
  128. self.submit()
  129. self.fetch.assert_not_called()
  130. self.plugin._reset_calendar_state.assert_called_once()
  131. def test_changed_tab_or_slot_stops_before_post(self):
  132. self.page.tab_id = "different-tab"
  133. with self.assertRaises(ita.BizLogicError):
  134. self.submit()
  135. self.fetch.assert_not_called()
  136. self.page.tab_id = "applicant-tab"
  137. self.verify.side_effect = ita.BizLogicError("Selection changed")
  138. with self.assertRaises(ita.BizLogicError):
  139. self.submit()
  140. self.fetch.assert_not_called()
  141. if __name__ == "__main__":
  142. unittest.main()