"""Final POST contract through the current adapter; no browser or network.""" import json import unittest from datetime import date, time from types import SimpleNamespace from unittest.mock import Mock, patch from plugins import ita_plugin as ita from test.ita.test_ita_compact import Response, route_config from vs_types import VSAccount class FinalSubmissionTests(unittest.TestCase): def setUp(self): self.route = ita.resolve_route_config({"route": route_config()}, "route") self.slot = ita.SlotCandidate(date(2026, 10, 2), 1, 0, time(10, 5), time(11, 5), "12345678") self.page = SimpleNamespace(tab_id="applicant-tab", url="https://prenotami.esteri.it/BookingCalendar") self.plugin = SimpleNamespace( page=self.page, config=SimpleNamespace(account=VSAccount(username="applicant@example.test")), _require_calendar_context=Mock(return_value=SimpleNamespace( tab_id=self.page.tab_id, runtime_service="4937")), _reset_calendar_state=Mock(), ) self.verify = self.enterContext(patch.object(ita, "verify_precommit_selection")) self.fetch = self.enterContext(patch.object(ita, "perform_browser_fetch")) self.respond() def respond(self, *, status=200, body=None, url=None): self.fetch.return_value = Response( status, body if body is not None else json.dumps({ "errorMessage": "", "url": "/Reservation"}), url or "https://prenotami.esteri.it" + ita.FINAL_PATH) def submit(self): return ita._submit_final_once(self.plugin, self.route, self.slot) def test_one_form_post_uses_current_applicant_selection(self): result = self.submit() self.assertTrue(result.success) self.assertEqual(result.account, "applicant@example.test") self.assertEqual(result.book_date, "2026-10-02") self.assertEqual(result.book_time, "10:05 - 11:05") self.fetch.assert_called_once_with(self.page, "POST", ita.FINAL_PATH, timeout=30, data={ "idCalendarioGiornaliero": "12345678", "selectedDay": "2026-10-02", "selectedHour": "10:05 - 11:05\n(1)", }) self.verify.assert_called_once_with(self.page, runtime_service="4937", candidate=self.slot) self.plugin._require_calendar_context.assert_called_once_with( self.route, role="book", username="applicant@example.test") def test_site_rejection_returns_false_and_discards_selection(self): self.respond(body=json.dumps({"errorMessage": "No longer available", "url": "/BookingCalendar"})) self.assertFalse(self.submit().success) self.fetch.assert_called_once() self.plugin._reset_calendar_state.assert_called_once() def test_http_errors_keep_framework_categories_without_internal_retry(self): for status, error in ((401, ita.SessionExpiredOrInvalidError), (403, ita.PermissionDeniedError), (404, ita.NotFoundError), (429, ita.RateLimiteddError), (0, ita.BizLogicError), (408, ita.BizLogicError), (500, ita.BizLogicError), (502, ita.BizLogicError), (503, ita.BizLogicError), (504, ita.BizLogicError)): with self.subTest(status=status): self.fetch.reset_mock() self.plugin._reset_calendar_state.reset_mock() self.respond(status=status) with self.assertRaises(error): self.submit() self.fetch.assert_called_once() self.plugin._reset_calendar_state.assert_called_once() def test_followed_iam_redirect_is_session_expiry(self): self.respond(url="https://iam.esteri.it/signin/", body="Login") with self.assertRaises(ita.SessionExpiredOrInvalidError): self.submit() self.plugin._reset_calendar_state.assert_called_once() def test_network_exception_does_not_repost_or_keep_stale_ids(self): self.fetch.side_effect = TimeoutError("synthetic timeout") with self.assertRaises(ita.BizLogicError) as caught: self.submit() self.assertIsInstance(caught.exception.__cause__, TimeoutError) self.fetch.assert_called_once() self.plugin._reset_calendar_state.assert_called_once() def test_existing_framework_exception_is_not_wrapped(self): for error_type in (ita.SessionExpiredOrInvalidError, ita.PermissionDeniedError, ita.NotFoundError, ita.RateLimiteddError, ita.BizLogicError): with self.subTest(error_type=error_type): error = error_type("synthetic") self.fetch.side_effect = error with self.assertRaises(error_type) as caught: self.submit() self.assertIs(caught.exception, error) def test_malformed_or_incomplete_response_is_not_success(self): for body in ("not json", "error", "null", "[]", "{}", '{"url":"/Reservation"}', '{"errorMessage":null,"url":"/Reservation"}', '{"errorMessage":false,"url":"/Reservation"}', '{"errorMessage":""}', '{"errorMessage":"","url":false}'): with self.subTest(body=body): self.fetch.reset_mock() self.respond(body=body) with self.assertRaises(ita.BizLogicError): self.submit() self.fetch.assert_called_once() def test_error_or_auth_pages_are_not_receipts(self): for destination in ("", " ", "/", "/UserArea", "/Services", "/BookingCalendar", "/Error", "/ReservationDetail/Index/", "/Reservation/other", "https://attacker.test/Reservation", "//attacker.test/Reservation", "http://prenotami.esteri.it/Reservation", "https://prenotami.esteri.it:8443/Reservation", "https://user@prenotami.esteri.it/Reservation"): with self.subTest(destination=destination): self.respond(body=json.dumps({"errorMessage": "", "url": destination})) with self.assertRaises(ita.BizLogicError): self.submit() def test_known_receipt_paths_accept_relative_and_absolute_urls(self): # Repository contract fixtures, not evidence of a live successful booking. for destination in ("/Reservation", "/Reservation/", "https://prenotami.esteri.it/Reservation", "/ReservationDetail/Index/synthetic-receipt"): with self.subTest(destination=destination): self.respond(body=json.dumps({"errorMessage": "", "url": destination})) self.assertTrue(self.submit().success) def test_foreign_or_wrong_final_response_endpoint_is_rejected(self): for url in ("https://attacker.test" + ita.FINAL_PATH, "https://prenotami.esteri.it/Reservation"): with self.subTest(url=url): self.respond(url=url) with self.assertRaises(ita.BizLogicError): self.submit() def test_changed_session_stops_before_post(self): self.plugin._require_calendar_context.side_effect = ita.SessionExpiredOrInvalidError() with self.assertRaises(ita.SessionExpiredOrInvalidError): self.submit() self.fetch.assert_not_called() self.plugin._reset_calendar_state.assert_called_once() def test_changed_tab_or_slot_stops_before_post(self): self.page.tab_id = "different-tab" with self.assertRaises(ita.BizLogicError): self.submit() self.fetch.assert_not_called() self.page.tab_id = "applicant-tab" self.verify.side_effect = ita.BizLogicError("Selection changed") with self.assertRaises(ita.BizLogicError): self.submit() self.fetch.assert_not_called() if __name__ == "__main__": unittest.main()