| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- """Offline regression for Italy form readiness; never contact the real site."""
- import io
- import json
- import shutil
- import subprocess
- import unittest
- from contextlib import redirect_stdout
- from dataclasses import asdict
- 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.ita.test_debug_ita_calendar_gate import booking_preflight_snapshot
- from test.ita.test_ita_protocol import live_route
- 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, enabled, expected in (
- (False, True, 'turnstile_token_pending'),
- (True, False, 'avanti_disabled'),
- (False, False, '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(
- turnstileReady=token, avantiEnabled=enabled))
- self.assertIn(expected, str(raised.exception))
- def test_production_wait_does_not_emit_private_snapshots_without_a_diagnostic(self):
- raw = booking_preflight_snapshot(turnstileReady=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)
- self.assertNotIn('ITA_BOOK_PREFLIGHT_DIAGNOSTIC=', output.getvalue())
- 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(formReady=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(turnstileReady=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 ('turnstileReady', 'avantiEnabled'):
- 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_pending_validation_does_not_rewrite_snapshot_or_relax_other_fields(self):
- raw = booking_preflight_snapshot(turnstileReady=False, avantiEnabled=False)
- original = dict(raw)
- session.validate_booking_preflight(raw, allow_pending=True)
- self.assertEqual(raw, original)
- with self.assertRaises(session.CalendarGateError):
- session.validate_booking_preflight(raw)
- for key, value in (("formReady", False), ("otpValuePresent", True),
- ("otpSendEnabled", False)):
- with self.subTest(key=key), self.assertRaisesRegex(session.CalendarGateError, key):
- session.validate_booking_preflight({**raw, key: value}, allow_pending=True)
- def test_pending_policy_requires_a_real_boolean(self):
- for value in (None, 1, "true"):
- with self.subTest(value=value), self.assertRaisesRegex(session.CalendarGateError, "policy"):
- session.validate_booking_preflight(booking_preflight_snapshot(), allow_pending=value)
- 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(turnstileReady=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['turnstileReady'])
- self.assertEqual(page.run_js.call_count, 2)
- action.assert_called_once()
- def test_contract_fields_are_plain_booleans(self):
- for field in ('formReady', 'turnstileReady', 'otpValuePresent',
- 'otpSendEnabled', 'avantiEnabled'):
- for value in (1, 0, 1.0, 0.0, 'true', None):
- with self.subTest(field=field, value=value), redirect_stdout(io.StringIO()), \
- self.assertRaisesRegex(session.CalendarGateError, field):
- self.run_wait(booking_preflight_snapshot(**{field: value}))
- FORM_JS_HARNESS = r"""
- const vm = require('node:vm');
- const input = JSON.parse(require('node:fs').readFileSync(0, 'utf8'));
- const cfg = input.config, mode = input.mode;
- const calls = [];
- const fields = {
- hiddenTipoPrenotazione: {value: '0'}, numAccSelected: {value: '0'},
- hiddenSelezionato_0: {value: '0'},
- PrivacyCheck: {checked: false, click() {this.checked = true; calls.push('privacy');}},
- 'otp-input': {value: ''}, 'otp-send': {disabled: false}, btnAvanti: {disabled: false},
- };
- function control(spec, role) {
- fields[spec.control_id] = {
- value: '0', options: [{value: spec.value, textContent: mode === role + '_wrong_label'
- ? 'Unrelated service' : ' ' + spec.labels[0].toUpperCase() + ' '}],
- dispatchEvent(event) {
- if (event.type !== 'change' || event.bubbles !== true) throw Error('Wrong change event');
- calls.push(role);
- if (role === 'booking' && mode !== 'hidden_unsynced') fields.hiddenTipoPrenotazione.value = this.value;
- if (role === 'booking' && mode === 'navigated_during_change') location.pathname = '/Services';
- },
- };
- if (mode === role + '_missing') delete fields[spec.control_id];
- }
- control(cfg.booking_type, 'booking');
- control(cfg.purpose, 'purpose');
- if (mode === 'companions') fields.numAccSelected.value = '1';
- if (mode === 'purpose_hidden') fields.hiddenSelezionato_0.value = '999';
- const token = {value: mode === 'pending' ? '' : 'synthetic-widget-token'};
- const location = {
- origin: mode === 'wrong_origin' ? 'https://other.example.test' : 'https://prenotami.esteri.it',
- pathname: mode === 'wrong_path' ? '/Services' : '/Services/Booking/' + cfg.service_id,
- };
- location.href = location.origin + location.pathname;
- const form = {
- action: mode === 'wrong_action' ? 'https://other.example.test/submit' : location.href,
- method: mode === 'wrong_method' ? 'GET' : 'POST',
- enctype: mode === 'wrong_enctype' ? 'application/json' : 'multipart/form-data',
- querySelectorAll(selector) {
- if (selector === 'input[type="file"]') return mode === 'upload' ? [{}] : [];
- if (selector === '[required]') return mode === 'unknown_required' ? [{id: 'new-mandatory-field'}] : [];
- throw Error('Unexpected form selector: ' + selector);
- },
- };
- const document = {
- visibilityState: 'visible',
- getElementById: id => fields[id] || null,
- querySelector(selector) {
- if (selector === '#bookingForm') return form;
- if (selector.startsWith('script[')) return {};
- throw Error('Unexpected selector: ' + selector);
- },
- querySelectorAll(selector) {
- if (selector === '#bookingForm') return mode === 'duplicate_form' ? [form, form] : [form];
- if (selector === 'input[name="cf-turnstile-response"]') return mode === 'duplicate_token' ? [token, token] : [token];
- if (selector === '#otp-input' && mode === 'duplicate_otp') return [fields['otp-input'], fields['otp-input']];
- if (selector === '#otp-send' && mode === 'duplicate_send') return [fields['otp-send'], fields['otp-send']];
- if (selector === '#btnAvanti' && mode === 'duplicate_avanti') return [fields.btnAvanti, fields.btnAvanti];
- const field = fields[selector.slice(1)];
- if (field) return [field];
- throw Error('Unexpected selector: ' + selector);
- },
- };
- const context = {document, location, window: {location, turnstile: {}}, URL,
- Event: class {constructor(type, options) {this.type = type; this.bubbles = options.bubbles;}}};
- const run = () => vm.runInNewContext('(function(){' + input.source + '\n})()', context, {timeout: 1000});
- const before = run(), second = run();
- fields['otp-input'].value = '001234';
- fields['otp-send'].disabled = true;
- const after = run();
- process.stdout.write(JSON.stringify({before, second, after, calls}));
- """
- @unittest.skipUnless(shutil.which('node'), 'Node required for actual form JavaScript execution')
- class FormJavaScriptTests(TestCase):
- def run_case(self, mode='ready', *, alternate=False, source_factory=None):
- config = live_route()
- if alternate:
- config['service_id'] = 9012
- config['form'].update(booking_type_control_id='other-booking', booking_type_value='31',
- booking_type_labels=['Individual'], purpose_control_id='other-purpose',
- purpose_value='67', purpose_labels=['Tourism'])
- route = session.resolve_route_config({'sample': config}, 'sample')
- source = (source_factory or session.guarded_preflight_script)(
- route, f'/Services/Booking/{route.service_id}')
- completed = subprocess.run([shutil.which('node'), '-e', FORM_JS_HARNESS],
- input=json.dumps({'source': source, 'mode': mode, 'config': asdict(route)}),
- capture_output=True, text=True, timeout=10)
- self.assertEqual(completed.returncode, 0, completed.stderr)
- return json.loads(completed.stdout)
- def test_two_routes_configure_controls_once_and_preserve_post_otp_cooldown(self):
- for alternate in (False, True):
- with self.subTest(alternate=alternate):
- result = self.run_case(alternate=alternate)
- session.validate_booking_preflight(result['before'])
- session.validate_booking_preflight(result['second'])
- session.validate_booking_preflight(result['after'], after_otp=True)
- self.assertEqual(result['calls'], ['booking', 'purpose', 'privacy'])
- self.assertNotIn('001234', json.dumps(result))
- self.assertNotIn('synthetic-widget-token', json.dumps(result))
- def test_boundary_rejects_before_any_form_changes(self):
- for mode in ('wrong_origin', 'wrong_path', 'wrong_action', 'wrong_method',
- 'wrong_enctype', 'upload', 'duplicate_form'):
- with self.subTest(mode=mode):
- result = self.run_case(mode)
- self.assertEqual(result['calls'], [])
- with self.assertRaises(session.CalendarGateError):
- session.validate_booking_preflight(result['before'])
- def test_unknown_controls_unsynced_values_and_duplicate_otp_remain_rejected(self):
- for mode in ('booking_wrong_label', 'purpose_wrong_label', 'booking_missing',
- 'purpose_missing', 'hidden_unsynced', 'companions', 'purpose_hidden',
- 'unknown_required', 'duplicate_otp', 'pending'):
- with self.subTest(mode=mode):
- result = self.run_case(mode)
- with self.assertRaises(session.CalendarGateError):
- session.validate_booking_preflight(result['before'])
- if mode.startswith('booking_'):
- self.assertNotIn('booking', result['calls'])
- if mode.startswith('purpose_') and mode != 'purpose_hidden':
- self.assertNotIn('purpose', result['calls'])
- def test_existing_calendar_debug_entry_uses_the_same_guarded_form(self):
- from tools import debug_ita_calendar_gate as gate
- for alternate in (False, True):
- for mode in ('ready', 'wrong_origin', 'wrong_action', 'upload'):
- with self.subTest(alternate=alternate, mode=mode):
- result = self.run_case(mode, alternate=alternate,
- source_factory=gate.guarded_preflight_script)
- if mode == 'ready':
- session.validate_booking_preflight(result['before'])
- session.validate_booking_preflight(result['after'],
- after_otp=True)
- self.assertEqual(result['calls'], ['booking', 'purpose', 'privacy'])
- else:
- self.assertEqual(result['calls'], [])
- with self.assertRaises(session.CalendarGateError):
- session.validate_booking_preflight(result['before'])
- def test_duplicate_business_controls_cannot_pass_either_otp_stage(self):
- for mode in ('duplicate_send', 'duplicate_avanti', 'duplicate_token'):
- with self.subTest(mode=mode):
- result = self.run_case(mode)
- for snapshot, after_otp in (('before', False), ('after', True)):
- self.assertFalse(result[snapshot]['formReady'])
- with self.assertRaises(session.CalendarGateError):
- session.validate_booking_preflight(result[snapshot], after_otp=after_otp)
- def test_native_change_navigation_is_not_reported_as_a_ready_form(self):
- result = self.run_case('navigated_during_change')
- self.assertFalse(result['before']['formReady'])
- with self.assertRaisesRegex(session.CalendarGateError, 'formReady'):
- session.validate_booking_preflight(result['before'])
- self.assertEqual(result['second'], {
- 'formReady': False, 'turnstileReady': False, 'otpValuePresent': False,
- 'otpSendEnabled': False, 'avantiEnabled': False,
- })
- class TurnstileRecoveryTests(TestCase):
- def setUp(self):
- self.page = SimpleNamespace(title='Booking - Prenot@Mi', refresh=Mock(), run_js=Mock())
- self.guard = Mock()
- self.recovery = turnstile.turnstile_recovery(self.page, 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, after_otp=False):
- self.recovery(raw if raw is not None else booking_preflight_snapshot(turnstileReady=False),
- after_otp=after_otp,
- 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(avantiEnabled=False))
- 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.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, after_otp=True)
- self.assertEqual(self.helper.click_verification_button.call_count, 3)
- def test_wrong_form_or_missing_field_cannot_interact(self):
- for changes in ({'formReady': False}, {'turnstileReady': None}):
- with self.subTest(changes=changes), self.assertRaises(session.CalendarGateError):
- raw = booking_preflight_snapshot(turnstileReady=False)
- raw.update(changes)
- self.attempt(raw=raw)
- 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()
- self.assertNotIn('PRIVATE_BROWSER_DATA', output.getvalue())
- self.assertNotIn('ITA_BOOK_TURNSTILE_INTERACTION=', output.getvalue())
- self.assertEqual(self.helper.click_verification_button.call_count, 1)
- def test_interaction_success_alone_cannot_release_preflight(self):
- page = SimpleNamespace(run_js=Mock(return_value=booking_preflight_snapshot(turnstileReady=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()
|