| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- """Offline regression for Italy form readiness; never contact the real site."""
- import io
- import json
- from contextlib import redirect_stdout
- from types import SimpleNamespace
- from unittest import TestCase
- from unittest.mock import Mock, patch
- from plugins import ita_plugin as session
- from plugins import ita_plugin as turnstile
- from test.test_debug_ita_calendar_gate import booking_preflight_snapshot
- class PreflightWaitTests(TestCase):
- def run_wait(self, raw, **kwargs):
- page = SimpleNamespace(run_js=Mock(return_value=raw))
- with patch.object(session.time, 'monotonic', side_effect=[0, 0, 121]), \
- patch.object(session.time, 'sleep'):
- return session.wait_for_booking_preflight(page, script='offline', **kwargs)
- def test_timeout_distinguishes_token_and_button(self):
- for token, disabled, expected in (
- (False, False, 'turnstile_token_pending'),
- (True, True, 'avanti_disabled'),
- (False, True, 'turnstile_and_avanti_pending'),
- ):
- with self.subTest(expected=expected), redirect_stdout(io.StringIO()):
- with self.assertRaises(session.CalendarGateError) as raised:
- self.run_wait(booking_preflight_snapshot(
- turnstileTokenPresent=token, avantiDisabled=disabled))
- self.assertIn(expected, str(raised.exception))
- def test_production_default_diagnostic_survives_timeout_without_private_values(self):
- raw = booking_preflight_snapshot(turnstileTokenPresent=False)
- raw.update(token='PRIVATE-TOKEN', email='private@example.test',
- turnstileApiPresent=True, documentVisible=False)
- output = io.StringIO()
- with redirect_stdout(output), self.assertRaises(session.CalendarGateError):
- self.run_wait(raw)
- lines = [line.split('=', 1)[1] for line in output.getvalue().splitlines()
- if line.startswith('ITA_BOOK_PREFLIGHT_DIAGNOSTIC=')]
- self.assertTrue(lines)
- payload = json.loads(lines[-1])
- self.assertEqual(payload['outcome'], 'timeout')
- self.assertFalse(payload['turnstileTokenPresent'])
- self.assertFalse(payload['avantiDisabled'])
- self.assertTrue(payload['turnstileApiPresent'])
- self.assertNotIn('PRIVATE-TOKEN', output.getvalue())
- self.assertNotIn('private@example.test', output.getvalue())
- def test_ready_never_invokes_recovery(self):
- action = Mock()
- with redirect_stdout(io.StringIO()):
- self.run_wait(booking_preflight_snapshot(), wait_action=action)
- action.assert_not_called()
- def test_bad_structure_never_invokes_recovery(self):
- action = Mock()
- with redirect_stdout(io.StringIO()), self.assertRaises(session.CalendarGateError):
- self.run_wait(booking_preflight_snapshot(formPresent=False), wait_action=action)
- action.assert_not_called()
- def test_session_guard_runs_before_dom_read(self):
- current = Mock(side_effect=session.CalendarGateError('session changed'))
- page = SimpleNamespace(run_js=Mock())
- with self.assertRaises(session.CalendarGateError):
- session.wait_for_booking_preflight(page, script='offline', check_current=current)
- page.run_js.assert_not_called()
- def test_wait_action_does_not_substitute_for_a_new_ready_snapshot(self):
- raw = booking_preflight_snapshot(turnstileTokenPresent=False)
- action = Mock(return_value=True)
- with redirect_stdout(io.StringIO()), self.assertRaises(session.CalendarGateError):
- self.run_wait(raw, wait_action=action)
- action.assert_called_once()
- def test_bad_dynamic_boolean_is_not_hidden_by_wait_validation(self):
- for field in ('turnstileTokenPresent', 'avantiDisabled'):
- with self.subTest(field=field), redirect_stdout(io.StringIO()), \
- self.assertRaisesRegex(session.CalendarGateError, field):
- self.run_wait(booking_preflight_snapshot(**{field: 'private-string'}))
- def test_dom_exception_is_redacted(self):
- page = SimpleNamespace(run_js=Mock(side_effect=ValueError('PRIVATE_JS_VALUE')))
- output = io.StringIO()
- with redirect_stdout(output), self.assertRaisesRegex(session.CalendarGateError, 'read_failed') as raised:
- session.wait_for_booking_preflight(page, script='offline')
- self.assertNotIn('PRIVATE_JS_VALUE', str(raised.exception) + output.getvalue())
- def test_wait_can_observe_readiness_after_recovery(self):
- page = SimpleNamespace(run_js=Mock(side_effect=[
- booking_preflight_snapshot(turnstileTokenPresent=False), booking_preflight_snapshot()]))
- action = Mock()
- with patch.object(session.time, 'monotonic', side_effect=[0, 0, 1]), \
- patch.object(session.time, 'sleep'), redirect_stdout(io.StringIO()):
- result = session.wait_for_booking_preflight(page, script='offline', wait_action=action)
- self.assertTrue(result['turnstileTokenPresent'])
- self.assertEqual(page.run_js.call_count, 2)
- action.assert_called_once()
- class TurnstileRecoveryTests(TestCase):
- def setUp(self):
- self.page = SimpleNamespace(title='Booking - Prenot@Mi', refresh=Mock(), run_js=Mock())
- self.guard = Mock()
- self.recovery = turnstile.ItalyTurnstileRecovery(self.page, check_current=self.guard)
- self.helper = Mock()
- self.helper.click_verification_button.return_value = True
- self.factory = patch.object(turnstile, 'CloudflareBypasser', return_value=self.helper)
- self.factory_mock = self.factory.start()
- self.addCleanup(self.factory.stop)
- def attempt(self, elapsed=5, remaining=100, raw=None, stage=None):
- self.recovery(raw if raw is not None else booking_preflight_snapshot(turnstileTokenPresent=False),
- stage=stage or session.BookingPreflightStage.BEFORE_OTP_SEND,
- elapsed=elapsed, remaining=remaining)
- def test_reuses_vfs_widget_method_not_title_based_bypass(self):
- with redirect_stdout(io.StringIO()):
- self.attempt()
- self.factory_mock.assert_called_once_with(self.page, log=False)
- self.helper.click_verification_button.assert_called_once_with(is_dfs=False)
- self.helper.bypass.assert_not_called()
- self.page.refresh.assert_not_called()
- self.page.run_js.assert_not_called()
- self.assertEqual(self.guard.call_count, 2)
- def test_waits_initially_and_leaves_successful_widget_untouched(self):
- self.attempt(elapsed=4)
- self.attempt(elapsed=5, remaining=1)
- self.attempt(raw=booking_preflight_snapshot(avantiDisabled=True))
- self.attempt(raw=booking_preflight_snapshot())
- self.factory_mock.assert_not_called()
- def test_attempt_budget_and_interval_apply_even_if_control_cannot_be_clicked(self):
- self.helper.click_verification_button.return_value = False
- with redirect_stdout(io.StringIO()):
- for seconds in (5, 5.5, 10, 15, 16, 25, 35, 55, 100):
- self.attempt(elapsed=seconds)
- self.assertEqual(self.recovery.attempts, 3)
- self.assertEqual(self.helper.click_verification_button.call_count, 3)
- def test_stage_change_does_not_reset_total_attempt_budget(self):
- with redirect_stdout(io.StringIO()):
- for seconds in (5, 15, 25):
- self.attempt(elapsed=seconds)
- self.attempt(elapsed=5, stage=session.BookingPreflightStage.AFTER_OTP_SEND)
- self.assertEqual(self.helper.click_verification_button.call_count, 3)
- def test_wrong_form_or_missing_field_cannot_interact(self):
- for changes in ({'formPresent': False}, {'pathMatches': False}, {'turnstileCount': 2}):
- with self.subTest(changes=changes), self.assertRaises(session.CalendarGateError):
- self.attempt(raw=booking_preflight_snapshot(turnstileTokenPresent=False, **changes))
- self.factory_mock.assert_not_called()
- def test_session_change_prevents_interaction(self):
- self.guard.side_effect = session.CalendarGateError('session changed')
- with self.assertRaises(session.CalendarGateError):
- self.attempt()
- self.factory_mock.assert_not_called()
- def test_helper_errors_are_bounded_and_do_not_leak_browser_data(self):
- self.helper.click_verification_button.side_effect = RuntimeError('PRIVATE_BROWSER_DATA')
- output = io.StringIO()
- with redirect_stdout(output):
- self.attempt()
- payload = json.loads(output.getvalue().split('=', 1)[1])
- self.assertTrue(payload['helperFailed'])
- self.assertFalse(payload['clicked'])
- self.assertNotIn('PRIVATE_BROWSER_DATA', output.getvalue())
- self.assertEqual(self.recovery.attempts, 1)
- def test_interaction_success_alone_cannot_release_preflight(self):
- page = SimpleNamespace(run_js=Mock(return_value=booking_preflight_snapshot(turnstileTokenPresent=False)))
- with patch.object(session.time, 'monotonic', side_effect=[0, 5, 121]), \
- patch.object(session.time, 'sleep'), redirect_stdout(io.StringIO()):
- with self.assertRaisesRegex(session.CalendarGateError, 'turnstile_token_pending'):
- session.wait_for_booking_preflight(page, script='offline', wait_action=self.recovery)
- self.helper.click_verification_button.assert_called_once()
|