test_ita_preflight.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. """Offline regression for Italy form readiness; never contact the real site."""
  2. import io
  3. import json
  4. import shutil
  5. import subprocess
  6. import unittest
  7. from contextlib import redirect_stdout
  8. from dataclasses import asdict
  9. from types import SimpleNamespace
  10. from unittest import TestCase
  11. from unittest.mock import Mock, patch
  12. from plugins import ita_plugin as session
  13. from plugins import ita_plugin as turnstile
  14. from test.ita.test_debug_ita_calendar_gate import booking_preflight_snapshot
  15. from test.ita.test_ita_protocol import live_route
  16. class PreflightWaitTests(TestCase):
  17. def run_wait(self, raw, **kwargs):
  18. page = SimpleNamespace(run_js=Mock(return_value=raw))
  19. with patch.object(session.time, 'monotonic', side_effect=[0, 0, 121]), \
  20. patch.object(session.time, 'sleep'):
  21. return session.wait_for_booking_preflight(page, script='offline', **kwargs)
  22. def test_timeout_distinguishes_token_and_button(self):
  23. for token, enabled, expected in (
  24. (False, True, 'turnstile_token_pending'),
  25. (True, False, 'avanti_disabled'),
  26. (False, False, 'turnstile_and_avanti_pending'),
  27. ):
  28. with self.subTest(expected=expected), redirect_stdout(io.StringIO()):
  29. with self.assertRaises(session.CalendarGateError) as raised:
  30. self.run_wait(booking_preflight_snapshot(
  31. turnstileReady=token, avantiEnabled=enabled))
  32. self.assertIn(expected, str(raised.exception))
  33. def test_production_wait_does_not_emit_private_snapshots_without_a_diagnostic(self):
  34. raw = booking_preflight_snapshot(turnstileReady=False)
  35. raw.update(token='PRIVATE-TOKEN', email='private@example.test',
  36. turnstileApiPresent=True, documentVisible=False)
  37. output = io.StringIO()
  38. with redirect_stdout(output), self.assertRaises(session.CalendarGateError):
  39. self.run_wait(raw)
  40. self.assertNotIn('ITA_BOOK_PREFLIGHT_DIAGNOSTIC=', output.getvalue())
  41. self.assertNotIn('PRIVATE-TOKEN', output.getvalue())
  42. self.assertNotIn('private@example.test', output.getvalue())
  43. def test_ready_never_invokes_recovery(self):
  44. action = Mock()
  45. with redirect_stdout(io.StringIO()):
  46. self.run_wait(booking_preflight_snapshot(), wait_action=action)
  47. action.assert_not_called()
  48. def test_bad_structure_never_invokes_recovery(self):
  49. action = Mock()
  50. with redirect_stdout(io.StringIO()), self.assertRaises(session.CalendarGateError):
  51. self.run_wait(booking_preflight_snapshot(formReady=False), wait_action=action)
  52. action.assert_not_called()
  53. def test_session_guard_runs_before_dom_read(self):
  54. current = Mock(side_effect=session.CalendarGateError('session changed'))
  55. page = SimpleNamespace(run_js=Mock())
  56. with self.assertRaises(session.CalendarGateError):
  57. session.wait_for_booking_preflight(page, script='offline', check_current=current)
  58. page.run_js.assert_not_called()
  59. def test_wait_action_does_not_substitute_for_a_new_ready_snapshot(self):
  60. raw = booking_preflight_snapshot(turnstileReady=False)
  61. action = Mock(return_value=True)
  62. with redirect_stdout(io.StringIO()), self.assertRaises(session.CalendarGateError):
  63. self.run_wait(raw, wait_action=action)
  64. action.assert_called_once()
  65. def test_bad_dynamic_boolean_is_not_hidden_by_wait_validation(self):
  66. for field in ('turnstileReady', 'avantiEnabled'):
  67. with self.subTest(field=field), redirect_stdout(io.StringIO()), \
  68. self.assertRaisesRegex(session.CalendarGateError, field):
  69. self.run_wait(booking_preflight_snapshot(**{field: 'private-string'}))
  70. def test_pending_validation_does_not_rewrite_snapshot_or_relax_other_fields(self):
  71. raw = booking_preflight_snapshot(turnstileReady=False, avantiEnabled=False)
  72. original = dict(raw)
  73. session.validate_booking_preflight(raw, allow_pending=True)
  74. self.assertEqual(raw, original)
  75. with self.assertRaises(session.CalendarGateError):
  76. session.validate_booking_preflight(raw)
  77. for key, value in (("formReady", False), ("otpValuePresent", True),
  78. ("otpSendEnabled", False)):
  79. with self.subTest(key=key), self.assertRaisesRegex(session.CalendarGateError, key):
  80. session.validate_booking_preflight({**raw, key: value}, allow_pending=True)
  81. def test_pending_policy_requires_a_real_boolean(self):
  82. for value in (None, 1, "true"):
  83. with self.subTest(value=value), self.assertRaisesRegex(session.CalendarGateError, "policy"):
  84. session.validate_booking_preflight(booking_preflight_snapshot(), allow_pending=value)
  85. def test_dom_exception_is_redacted(self):
  86. page = SimpleNamespace(run_js=Mock(side_effect=ValueError('PRIVATE_JS_VALUE')))
  87. output = io.StringIO()
  88. with redirect_stdout(output), self.assertRaisesRegex(session.CalendarGateError, 'read_failed') as raised:
  89. session.wait_for_booking_preflight(page, script='offline')
  90. self.assertNotIn('PRIVATE_JS_VALUE', str(raised.exception) + output.getvalue())
  91. def test_wait_can_observe_readiness_after_recovery(self):
  92. page = SimpleNamespace(run_js=Mock(side_effect=[
  93. booking_preflight_snapshot(turnstileReady=False), booking_preflight_snapshot()]))
  94. action = Mock()
  95. with patch.object(session.time, 'monotonic', side_effect=[0, 0, 1]), \
  96. patch.object(session.time, 'sleep'), redirect_stdout(io.StringIO()):
  97. result = session.wait_for_booking_preflight(page, script='offline', wait_action=action)
  98. self.assertTrue(result['turnstileReady'])
  99. self.assertEqual(page.run_js.call_count, 2)
  100. action.assert_called_once()
  101. def test_contract_fields_are_plain_booleans(self):
  102. for field in ('formReady', 'turnstileReady', 'otpValuePresent',
  103. 'otpSendEnabled', 'avantiEnabled'):
  104. for value in (1, 0, 1.0, 0.0, 'true', None):
  105. with self.subTest(field=field, value=value), redirect_stdout(io.StringIO()), \
  106. self.assertRaisesRegex(session.CalendarGateError, field):
  107. self.run_wait(booking_preflight_snapshot(**{field: value}))
  108. FORM_JS_HARNESS = r"""
  109. const vm = require('node:vm');
  110. const input = JSON.parse(require('node:fs').readFileSync(0, 'utf8'));
  111. const cfg = input.config, mode = input.mode;
  112. const calls = [];
  113. const fields = {
  114. hiddenTipoPrenotazione: {value: '0'}, numAccSelected: {value: '0'},
  115. hiddenSelezionato_0: {value: '0'},
  116. PrivacyCheck: {checked: false, click() {this.checked = true; calls.push('privacy');}},
  117. 'otp-input': {value: ''}, 'otp-send': {disabled: false}, btnAvanti: {disabled: false},
  118. };
  119. function control(spec, role) {
  120. fields[spec.control_id] = {
  121. value: '0', options: [{value: spec.value, textContent: mode === role + '_wrong_label'
  122. ? 'Unrelated service' : ' ' + spec.labels[0].toUpperCase() + ' '}],
  123. dispatchEvent(event) {
  124. if (event.type !== 'change' || event.bubbles !== true) throw Error('Wrong change event');
  125. calls.push(role);
  126. if (role === 'booking' && mode !== 'hidden_unsynced') fields.hiddenTipoPrenotazione.value = this.value;
  127. if (role === 'booking' && mode === 'navigated_during_change') location.pathname = '/Services';
  128. },
  129. };
  130. if (mode === role + '_missing') delete fields[spec.control_id];
  131. }
  132. control(cfg.booking_type, 'booking');
  133. control(cfg.purpose, 'purpose');
  134. if (mode === 'companions') fields.numAccSelected.value = '1';
  135. if (mode === 'purpose_hidden') fields.hiddenSelezionato_0.value = '999';
  136. const token = {value: mode === 'pending' ? '' : 'synthetic-widget-token'};
  137. const location = {
  138. origin: mode === 'wrong_origin' ? 'https://other.example.test' : 'https://prenotami.esteri.it',
  139. pathname: mode === 'wrong_path' ? '/Services' : '/Services/Booking/' + cfg.service_id,
  140. };
  141. location.href = location.origin + location.pathname;
  142. const form = {
  143. action: mode === 'wrong_action' ? 'https://other.example.test/submit' : location.href,
  144. method: mode === 'wrong_method' ? 'GET' : 'POST',
  145. enctype: mode === 'wrong_enctype' ? 'application/json' : 'multipart/form-data',
  146. querySelectorAll(selector) {
  147. if (selector === 'input[type="file"]') return mode === 'upload' ? [{}] : [];
  148. if (selector === '[required]') return mode === 'unknown_required' ? [{id: 'new-mandatory-field'}] : [];
  149. throw Error('Unexpected form selector: ' + selector);
  150. },
  151. };
  152. const document = {
  153. visibilityState: 'visible',
  154. getElementById: id => fields[id] || null,
  155. querySelector(selector) {
  156. if (selector === '#bookingForm') return form;
  157. if (selector.startsWith('script[')) return {};
  158. throw Error('Unexpected selector: ' + selector);
  159. },
  160. querySelectorAll(selector) {
  161. if (selector === '#bookingForm') return mode === 'duplicate_form' ? [form, form] : [form];
  162. if (selector === 'input[name="cf-turnstile-response"]') return mode === 'duplicate_token' ? [token, token] : [token];
  163. if (selector === '#otp-input' && mode === 'duplicate_otp') return [fields['otp-input'], fields['otp-input']];
  164. if (selector === '#otp-send' && mode === 'duplicate_send') return [fields['otp-send'], fields['otp-send']];
  165. if (selector === '#btnAvanti' && mode === 'duplicate_avanti') return [fields.btnAvanti, fields.btnAvanti];
  166. const field = fields[selector.slice(1)];
  167. if (field) return [field];
  168. throw Error('Unexpected selector: ' + selector);
  169. },
  170. };
  171. const context = {document, location, window: {location, turnstile: {}}, URL,
  172. Event: class {constructor(type, options) {this.type = type; this.bubbles = options.bubbles;}}};
  173. const run = () => vm.runInNewContext('(function(){' + input.source + '\n})()', context, {timeout: 1000});
  174. const before = run(), second = run();
  175. fields['otp-input'].value = '001234';
  176. fields['otp-send'].disabled = true;
  177. const after = run();
  178. process.stdout.write(JSON.stringify({before, second, after, calls}));
  179. """
  180. @unittest.skipUnless(shutil.which('node'), 'Node required for actual form JavaScript execution')
  181. class FormJavaScriptTests(TestCase):
  182. def run_case(self, mode='ready', *, alternate=False, source_factory=None):
  183. config = live_route()
  184. if alternate:
  185. config['service_id'] = 9012
  186. config['form'].update(booking_type_control_id='other-booking', booking_type_value='31',
  187. booking_type_labels=['Individual'], purpose_control_id='other-purpose',
  188. purpose_value='67', purpose_labels=['Tourism'])
  189. route = session.resolve_route_config({'sample': config}, 'sample')
  190. source = (source_factory or session.guarded_preflight_script)(
  191. route, f'/Services/Booking/{route.service_id}')
  192. completed = subprocess.run([shutil.which('node'), '-e', FORM_JS_HARNESS],
  193. input=json.dumps({'source': source, 'mode': mode, 'config': asdict(route)}),
  194. capture_output=True, text=True, timeout=10)
  195. self.assertEqual(completed.returncode, 0, completed.stderr)
  196. return json.loads(completed.stdout)
  197. def test_two_routes_configure_controls_once_and_preserve_post_otp_cooldown(self):
  198. for alternate in (False, True):
  199. with self.subTest(alternate=alternate):
  200. result = self.run_case(alternate=alternate)
  201. session.validate_booking_preflight(result['before'])
  202. session.validate_booking_preflight(result['second'])
  203. session.validate_booking_preflight(result['after'], after_otp=True)
  204. self.assertEqual(result['calls'], ['booking', 'purpose', 'privacy'])
  205. self.assertNotIn('001234', json.dumps(result))
  206. self.assertNotIn('synthetic-widget-token', json.dumps(result))
  207. def test_boundary_rejects_before_any_form_changes(self):
  208. for mode in ('wrong_origin', 'wrong_path', 'wrong_action', 'wrong_method',
  209. 'wrong_enctype', 'upload', 'duplicate_form'):
  210. with self.subTest(mode=mode):
  211. result = self.run_case(mode)
  212. self.assertEqual(result['calls'], [])
  213. with self.assertRaises(session.CalendarGateError):
  214. session.validate_booking_preflight(result['before'])
  215. def test_unknown_controls_unsynced_values_and_duplicate_otp_remain_rejected(self):
  216. for mode in ('booking_wrong_label', 'purpose_wrong_label', 'booking_missing',
  217. 'purpose_missing', 'hidden_unsynced', 'companions', 'purpose_hidden',
  218. 'unknown_required', 'duplicate_otp', 'pending'):
  219. with self.subTest(mode=mode):
  220. result = self.run_case(mode)
  221. with self.assertRaises(session.CalendarGateError):
  222. session.validate_booking_preflight(result['before'])
  223. if mode.startswith('booking_'):
  224. self.assertNotIn('booking', result['calls'])
  225. if mode.startswith('purpose_') and mode != 'purpose_hidden':
  226. self.assertNotIn('purpose', result['calls'])
  227. def test_existing_calendar_debug_entry_uses_the_same_guarded_form(self):
  228. from tools import debug_ita_calendar_gate as gate
  229. for alternate in (False, True):
  230. for mode in ('ready', 'wrong_origin', 'wrong_action', 'upload'):
  231. with self.subTest(alternate=alternate, mode=mode):
  232. result = self.run_case(mode, alternate=alternate,
  233. source_factory=gate.guarded_preflight_script)
  234. if mode == 'ready':
  235. session.validate_booking_preflight(result['before'])
  236. session.validate_booking_preflight(result['after'],
  237. after_otp=True)
  238. self.assertEqual(result['calls'], ['booking', 'purpose', 'privacy'])
  239. else:
  240. self.assertEqual(result['calls'], [])
  241. with self.assertRaises(session.CalendarGateError):
  242. session.validate_booking_preflight(result['before'])
  243. def test_duplicate_business_controls_cannot_pass_either_otp_stage(self):
  244. for mode in ('duplicate_send', 'duplicate_avanti', 'duplicate_token'):
  245. with self.subTest(mode=mode):
  246. result = self.run_case(mode)
  247. for snapshot, after_otp in (('before', False), ('after', True)):
  248. self.assertFalse(result[snapshot]['formReady'])
  249. with self.assertRaises(session.CalendarGateError):
  250. session.validate_booking_preflight(result[snapshot], after_otp=after_otp)
  251. def test_native_change_navigation_is_not_reported_as_a_ready_form(self):
  252. result = self.run_case('navigated_during_change')
  253. self.assertFalse(result['before']['formReady'])
  254. with self.assertRaisesRegex(session.CalendarGateError, 'formReady'):
  255. session.validate_booking_preflight(result['before'])
  256. self.assertEqual(result['second'], {
  257. 'formReady': False, 'turnstileReady': False, 'otpValuePresent': False,
  258. 'otpSendEnabled': False, 'avantiEnabled': False,
  259. })
  260. class TurnstileRecoveryTests(TestCase):
  261. def setUp(self):
  262. self.page = SimpleNamespace(title='Booking - Prenot@Mi', refresh=Mock(), run_js=Mock())
  263. self.guard = Mock()
  264. self.recovery = turnstile.turnstile_recovery(self.page, self.guard)
  265. self.helper = Mock()
  266. self.helper.click_verification_button.return_value = True
  267. self.factory = patch.object(turnstile, 'CloudflareBypasser', return_value=self.helper)
  268. self.factory_mock = self.factory.start()
  269. self.addCleanup(self.factory.stop)
  270. def attempt(self, elapsed=5, remaining=100, raw=None, after_otp=False):
  271. self.recovery(raw if raw is not None else booking_preflight_snapshot(turnstileReady=False),
  272. after_otp=after_otp,
  273. elapsed=elapsed, remaining=remaining)
  274. def test_reuses_vfs_widget_method_not_title_based_bypass(self):
  275. with redirect_stdout(io.StringIO()):
  276. self.attempt()
  277. self.factory_mock.assert_called_once_with(self.page, log=False)
  278. self.helper.click_verification_button.assert_called_once_with(is_dfs=False)
  279. self.helper.bypass.assert_not_called()
  280. self.page.refresh.assert_not_called()
  281. self.page.run_js.assert_not_called()
  282. self.assertEqual(self.guard.call_count, 2)
  283. def test_waits_initially_and_leaves_successful_widget_untouched(self):
  284. self.attempt(elapsed=4)
  285. self.attempt(elapsed=5, remaining=1)
  286. self.attempt(raw=booking_preflight_snapshot(avantiEnabled=False))
  287. self.attempt(raw=booking_preflight_snapshot())
  288. self.factory_mock.assert_not_called()
  289. def test_attempt_budget_and_interval_apply_even_if_control_cannot_be_clicked(self):
  290. self.helper.click_verification_button.return_value = False
  291. with redirect_stdout(io.StringIO()):
  292. for seconds in (5, 5.5, 10, 15, 16, 25, 35, 55, 100):
  293. self.attempt(elapsed=seconds)
  294. self.assertEqual(self.helper.click_verification_button.call_count, 3)
  295. def test_stage_change_does_not_reset_total_attempt_budget(self):
  296. with redirect_stdout(io.StringIO()):
  297. for seconds in (5, 15, 25):
  298. self.attempt(elapsed=seconds)
  299. self.attempt(elapsed=5, after_otp=True)
  300. self.assertEqual(self.helper.click_verification_button.call_count, 3)
  301. def test_wrong_form_or_missing_field_cannot_interact(self):
  302. for changes in ({'formReady': False}, {'turnstileReady': None}):
  303. with self.subTest(changes=changes), self.assertRaises(session.CalendarGateError):
  304. raw = booking_preflight_snapshot(turnstileReady=False)
  305. raw.update(changes)
  306. self.attempt(raw=raw)
  307. self.factory_mock.assert_not_called()
  308. def test_session_change_prevents_interaction(self):
  309. self.guard.side_effect = session.CalendarGateError('session changed')
  310. with self.assertRaises(session.CalendarGateError):
  311. self.attempt()
  312. self.factory_mock.assert_not_called()
  313. def test_helper_errors_are_bounded_and_do_not_leak_browser_data(self):
  314. self.helper.click_verification_button.side_effect = RuntimeError('PRIVATE_BROWSER_DATA')
  315. output = io.StringIO()
  316. with redirect_stdout(output):
  317. self.attempt()
  318. self.assertNotIn('PRIVATE_BROWSER_DATA', output.getvalue())
  319. self.assertNotIn('ITA_BOOK_TURNSTILE_INTERACTION=', output.getvalue())
  320. self.assertEqual(self.helper.click_verification_button.call_count, 1)
  321. def test_interaction_success_alone_cannot_release_preflight(self):
  322. page = SimpleNamespace(run_js=Mock(return_value=booking_preflight_snapshot(turnstileReady=False)))
  323. with patch.object(session.time, 'monotonic', side_effect=[0, 5, 121]), \
  324. patch.object(session.time, 'sleep'), redirect_stdout(io.StringIO()):
  325. with self.assertRaisesRegex(session.CalendarGateError, 'turnstile_token_pending'):
  326. session.wait_for_booking_preflight(page, script='offline', wait_action=self.recovery)
  327. self.helper.click_verification_button.assert_called_once()