|
@@ -0,0 +1,257 @@
|
|
|
|
|
+"""Offline regressions for notification, booking and traffic controls."""
|
|
|
|
|
+import json
|
|
|
|
|
+import os
|
|
|
|
|
+import tempfile
|
|
|
|
|
+import threading
|
|
|
|
|
+import types
|
|
|
|
|
+import unittest
|
|
|
|
|
+from datetime import datetime
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+from unittest.mock import MagicMock, patch
|
|
|
|
|
+
|
|
|
|
|
+from booker import BuiltinBookerGCO
|
|
|
|
|
+from core_types import (AppointmentType, AvailabilityStatus, BookResult, BookerStandaloneConfig,
|
|
|
|
|
+ DateAvailability, GroupConfig, PluginConfig, QueryResult, Task, BizLogicError)
|
|
|
|
|
+from plugins.grc_plugin import GrcPlugin
|
|
|
|
|
+from plugins.usa_plugin import UsaPlugin
|
|
|
|
|
+from plugins.vfs_plugin import VfsPlugin
|
|
|
|
|
+from sentinel import SentinelGCO
|
|
|
|
|
+from standalone import StandaloneGCO
|
|
|
|
|
+from toolkit.notifier import notify_book_result, notify_date_changes
|
|
|
|
|
+from utils.booking_outbox import BookingOutbox
|
|
|
|
|
+from toolkit import notifier
|
|
|
|
|
+from collections import OrderedDict
|
|
|
|
|
+from plugin_factory import PluginFactory
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class BookingReliabilityTests(unittest.TestCase):
|
|
|
|
|
+ def setUp(self):
|
|
|
|
|
+ self.tmp = tempfile.TemporaryDirectory()
|
|
|
|
|
+ self.addCleanup(self.tmp.cleanup)
|
|
|
|
|
+ env = patch.dict(os.environ, {'BOOKING_OUTBOX_PATH': str(Path(self.tmp.name) / 'outbox.sqlite3')})
|
|
|
|
|
+ env.start()
|
|
|
|
|
+ self.addCleanup(env.stop)
|
|
|
|
|
+ network = patch('socket.socket.connect', side_effect=AssertionError('Network forbidden in tests'))
|
|
|
|
|
+ network.start()
|
|
|
|
|
+ self.addCleanup(network.stop)
|
|
|
|
|
+ for name, value in (('_pending', {}), ('_sent', OrderedDict()), ('_slot_states', {})):
|
|
|
|
|
+ state = patch.object(notifier, name, value)
|
|
|
|
|
+ state.start()
|
|
|
|
|
+ self.addCleanup(state.stop)
|
|
|
|
|
+ self.cloud = MagicMock()
|
|
|
|
|
+ cloud = patch('toolkit.cloud_api.CloudApi.Instance', return_value=self.cloud)
|
|
|
|
|
+ cloud.start()
|
|
|
|
|
+ self.addCleanup(cloud.stop)
|
|
|
|
|
+ self.apt = AppointmentType(routing_key='audit.slot')
|
|
|
|
|
+ self.available = QueryResult(success=True, apt_type=self.apt,
|
|
|
|
|
+ availability_status=AvailabilityStatus.Available,
|
|
|
|
|
+ earliest_date=datetime(2026, 11, 1),
|
|
|
|
|
+ availability=[DateAvailability(date=datetime(2026, 11, 1))])
|
|
|
|
|
+
|
|
|
|
|
+ def pending(self):
|
|
|
|
|
+ return len(notifier._pending)
|
|
|
|
|
+
|
|
|
|
|
+ def test_notifications_do_not_use_telemetry_executor(self):
|
|
|
|
|
+ with patch('toolkit.task_executor.submit_background', return_value=False):
|
|
|
|
|
+ self.assertTrue(notify_date_changes(self.available))
|
|
|
|
|
+ self.assertTrue(notify_book_result(BookResult(success=True, urn='confirmed')))
|
|
|
|
|
+ self.assertEqual(self.pending(), 2)
|
|
|
|
|
+ notifier._flush_pending(self.cloud, lambda _: None)
|
|
|
|
|
+ self.assertEqual(self.cloud.push_weixin_text.call_count, 2)
|
|
|
|
|
+ self.assertEqual(self.pending(), 0)
|
|
|
|
|
+
|
|
|
|
|
+ def test_full_memory_queue_does_not_advance_slot_cache(self):
|
|
|
|
|
+ cache = {}
|
|
|
|
|
+ with patch.object(notifier, '_MAX_PENDING', 0):
|
|
|
|
|
+ self.assertFalse(notify_date_changes(self.available, cache, logger=lambda _: None))
|
|
|
|
|
+ self.assertEqual(cache, {})
|
|
|
|
|
+ self.assertTrue(notify_date_changes(self.available, cache))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def test_plugin_preferences_mutation_does_not_change_configuration(self):
|
|
|
|
|
+ preferences = {'email': 'audit@example.invalid'}
|
|
|
|
|
+ g = BuiltinBookerGCO(GroupConfig(identifier='audit', enable=True), {}, logger=lambda _: None)
|
|
|
|
|
+ self.addCleanup(g.redis_client.close)
|
|
|
|
|
+ g.redis_client = MagicMock()
|
|
|
|
|
+ self.cloud.get_vas_task_pop.return_value = {'id': 42, 'user_inputs': preferences}
|
|
|
|
|
+ def book(_, inputs):
|
|
|
|
|
+ inputs['alias_email'] = 'generated@example.invalid'
|
|
|
|
|
+ return BookResult(success=False)
|
|
|
|
|
+ plugin = MagicMock(config=PluginConfig())
|
|
|
|
|
+ plugin.book.side_effect = book
|
|
|
|
|
+ g._execute_book_job(Task(instance=plugin), self.available)
|
|
|
|
|
+ plugin.book.assert_called_once()
|
|
|
|
|
+ self.assertNotIn('alias_email', preferences)
|
|
|
|
|
+
|
|
|
|
|
+ def test_failed_delivery_retries_without_duplicate_enqueue(self):
|
|
|
|
|
+ self.cloud.push_weixin_text.side_effect = RuntimeError('offline')
|
|
|
|
|
+ notify_date_changes(self.available)
|
|
|
|
|
+ notifier._flush_pending(self.cloud, lambda _: None)
|
|
|
|
|
+ self.assertFalse(notify_date_changes(self.available))
|
|
|
|
|
+ self.assertEqual(self.pending(), 1)
|
|
|
|
|
+ self.cloud.push_weixin_text.side_effect = None
|
|
|
|
|
+ with patch('toolkit.notifier.time.time', return_value=9999999999):
|
|
|
|
|
+ notifier._flush_pending(self.cloud, lambda _: None)
|
|
|
|
|
+ self.assertEqual(self.pending(), 0)
|
|
|
|
|
+
|
|
|
|
|
+ def test_reopened_same_date_and_new_time_and_waitlist_notify(self):
|
|
|
|
|
+ notify_date_changes(self.available)
|
|
|
|
|
+ notify_date_changes(QueryResult(apt_type=self.apt))
|
|
|
|
|
+ notify_date_changes(self.available)
|
|
|
|
|
+ from core_types import TimeSlot
|
|
|
|
|
+ modified = self.available.model_copy(deep=True)
|
|
|
|
|
+ modified.availability[0].times.append(TimeSlot(time='10:00'))
|
|
|
|
|
+ notify_date_changes(modified)
|
|
|
|
|
+ notify_date_changes(QueryResult(success=True, apt_type=self.apt,
|
|
|
|
|
+ availability_status=AvailabilityStatus.Waitlist))
|
|
|
|
|
+ self.assertEqual(self.pending(), 4)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def sentinel(self):
|
|
|
|
|
+ g = SentinelGCO(GroupConfig(identifier='audit', enable=True), {}, logger=lambda _: None)
|
|
|
|
|
+ self.addCleanup(g.redis_client.close)
|
|
|
|
|
+ g.redis_client = MagicMock()
|
|
|
|
|
+ g.redis_client.get.return_value = None
|
|
|
|
|
+ plugin = MagicMock(config=PluginConfig())
|
|
|
|
|
+ return g, plugin, Task(instance=plugin, next_health_check=float('inf'))
|
|
|
|
|
+
|
|
|
|
|
+ def test_unavailable_result_replaces_positive_signal(self):
|
|
|
|
|
+ g, plugin, task = self.sentinel()
|
|
|
|
|
+ plugin.query.side_effect = [self.available, QueryResult()]
|
|
|
|
|
+ with patch('sentinel.notify_date_changes'):
|
|
|
|
|
+ g._monitor_task(task, self.apt)
|
|
|
|
|
+ g._monitor_task(task, self.apt)
|
|
|
|
|
+ self.assertEqual(g.redis_client.setex.call_count, 2)
|
|
|
|
|
+ last = json.loads(g.redis_client.setex.call_args.args[2])
|
|
|
|
|
+ self.assertFalse(last['query_result']['success'])
|
|
|
|
|
+ self.assertEqual(last['query_result']['availability_status'], AvailabilityStatus.NoneAvailable.value)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def test_vfs_window_has_hard_six_month_budget(self):
|
|
|
|
|
+ self.assertEqual(len(VfsPlugin._get_filtered_covered_months(None, '2000-01-01', '2100-01-01', '2026-10-25')), 6)
|
|
|
|
|
+
|
|
|
|
|
+ def test_grc_does_not_replay_post_on_read_timeout(self):
|
|
|
|
|
+ session = MagicMock()
|
|
|
|
|
+ session.request.side_effect = TimeoutError('response lost after commit')
|
|
|
|
|
+ plugin = types.SimpleNamespace(session=session, config=PluginConfig(), _log=lambda _: None,
|
|
|
|
|
+ _init_session=lambda **_: session)
|
|
|
|
|
+ plugin._perform_request = types.MethodType(GrcPlugin._perform_request, plugin)
|
|
|
|
|
+ with self.assertRaises(TimeoutError), patch('plugins.grc_plugin.time.sleep'):
|
|
|
|
|
+ plugin._perform_request('POST', 'https://example.invalid/reservation')
|
|
|
|
|
+ session.request.assert_called_once()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def test_booking_outbox_queues_success_notification(self):
|
|
|
|
|
+ outbox = BookingOutbox()
|
|
|
|
|
+ outbox.put(42, 'tracker', {'status': 'grabbed', 'grabbed_history': {'urn': 'receipt'}})
|
|
|
|
|
+ BookingOutbox(outbox.path).flush(self.cloud, MagicMock(), lambda _: None)
|
|
|
|
|
+ self.assertEqual(self.pending(), 1)
|
|
|
|
|
+ self.assertIsNone(outbox.get(42))
|
|
|
|
|
+
|
|
|
|
|
+ def test_standalone_success_stops_further_booking_in_current_process(self):
|
|
|
|
|
+ cfg = BookerStandaloneConfig(identifier='audit', appointment_types=[self.apt])
|
|
|
|
|
+ g = StandaloneGCO(cfg, {}, logger=lambda _: None)
|
|
|
|
|
+ self.addCleanup(g.redis_client.close)
|
|
|
|
|
+ plugin = MagicMock()
|
|
|
|
|
+ plugin.query.return_value = self.available
|
|
|
|
|
+ plugin.book.return_value = BookResult(success=True, urn='receipt')
|
|
|
|
|
+ g.m_instance = plugin
|
|
|
|
|
+ stopped = threading.Event()
|
|
|
|
|
+ g.m_stop_event = MagicMock()
|
|
|
|
|
+ g.m_stop_event.is_set.side_effect = stopped.is_set
|
|
|
|
|
+ waits = []
|
|
|
|
|
+ def wait(*_):
|
|
|
|
|
+ waits.append(1)
|
|
|
|
|
+ if len(waits) >= 2:
|
|
|
|
|
+ stopped.set()
|
|
|
|
|
+ return stopped.is_set()
|
|
|
|
|
+ g.m_stop_event.wait.side_effect = wait
|
|
|
|
|
+ with patch.object(g, '_get_wait_interval', return_value=0):
|
|
|
|
|
+ g._worker_loop()
|
|
|
|
|
+ plugin.book.assert_called_once()
|
|
|
|
|
+ self.assertEqual(g._booking_outcome[1].urn, 'receipt')
|
|
|
|
|
+
|
|
|
|
|
+ def test_usa_rejects_click_as_success_and_accepts_confirmation(self):
|
|
|
|
|
+ element = MagicMock()
|
|
|
|
|
+ element.parent.return_value.text = '09:00'
|
|
|
|
|
+ element.text = ''
|
|
|
|
|
+ page = MagicMock()
|
|
|
|
|
+ page.url = 'https://example.invalid/schedule'
|
|
|
|
|
+ page.ele.return_value = element
|
|
|
|
|
+ plugin = types.SimpleNamespace(page=page, mouse=MagicMock(), config=PluginConfig(),
|
|
|
|
|
+ _random_sleep=lambda *_: None, _log=lambda _: None)
|
|
|
|
|
+ with patch('plugins.usa_plugin.time.sleep'), self.assertRaises(BizLogicError):
|
|
|
|
|
+ UsaPlugin.book(plugin, self.available, {})
|
|
|
|
|
+ page.url = 'https://example.invalid/appointment-confirmation/'
|
|
|
|
|
+ element.text = 'Confirmed: 2026-11-01'
|
|
|
|
|
+ self.assertTrue(UsaPlugin.book(plugin, self.available, {}).success)
|
|
|
|
|
+
|
|
|
|
|
+ def test_vfs_payment_resolution_error_preserves_confirmed_booking(self):
|
|
|
|
|
+ route = {'subcategory_code': 'sub'}
|
|
|
|
|
+ plugin = types.SimpleNamespace(
|
|
|
|
|
+ _log=lambda _: None, config=PluginConfig(), free_config={'apt_configs': {self.apt.routing_key: route}},
|
|
|
|
|
+ subcategory_conf={'sub': {}}, booking_wait_applied=True,
|
|
|
|
|
+ _fetch_configurations=MagicMock(), _add_primary_applicant=MagicMock(return_value='receipt'),
|
|
|
|
|
+ _get_filtered_covered_months=MagicMock(return_value=['2026-11-01']),
|
|
|
|
|
+ _query_slot_calendar=MagicMock(return_value=['2026-11-01']),
|
|
|
|
|
+ _saveuseractionaudit=MagicMock(return_value=True),
|
|
|
|
|
+ _query_slot_time=MagicMock(return_value=[{'allocationId': '1', 'slot': '09:00'}]),
|
|
|
|
|
+ _submit_no_addition_service=MagicMock(), _query_fee=MagicMock(return_value=(1, 'EUR')),
|
|
|
|
|
+ _schedule=MagicMock(return_value={'IsAppointmentBooked': True, 'isPaymentRequired': True}),
|
|
|
|
|
+ _pay_request=MagicMock(side_effect=TimeoutError('payment redirect lost')))
|
|
|
|
|
+ plugin.book = types.MethodType(VfsPlugin.book, plugin)
|
|
|
|
|
+ receipt = plugin.book(self.available,
|
|
|
|
|
+ {'email': 'audit@example.invalid', 'expected_start_date': '2026-11-01',
|
|
|
|
|
+ 'expected_end_date': '2026-12-01'})
|
|
|
|
|
+ self.assertTrue(receipt.success)
|
|
|
|
|
+ self.assertEqual(receipt.urn, 'receipt')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def test_unsupported_adapters_fail_before_browser_or_order_consumption(self):
|
|
|
|
|
+ root = Path(__file__).resolve().parent.parent
|
|
|
|
|
+ for name in ('bel', 'pol', 'prt'):
|
|
|
|
|
+ factory = PluginFactory()
|
|
|
|
|
+ factory.register_plugin(name + '_plugin', str(root / 'plugins' / (name + '_plugin.py')),
|
|
|
|
|
+ name.title() + 'Plugin')
|
|
|
|
|
+ with self.assertRaises(NotImplementedError):
|
|
|
|
|
+ factory.ensure_booking_supported(name + '_plugin')
|
|
|
|
|
+
|
|
|
|
|
+ def test_proxy_host_ports_and_native_listeners_default_to_loopback(self):
|
|
|
|
|
+ import yaml
|
|
|
|
|
+ from start_proxy import secure_listener_config
|
|
|
|
|
+ root = Path(__file__).resolve().parent.parent
|
|
|
|
|
+ compose = yaml.safe_load((root / 'docker-compose.yml').read_text(encoding='utf-8'))
|
|
|
|
|
+ self.assertTrue(all(str(port).startswith('127.0.0.1:')
|
|
|
|
|
+ for port in compose['services']['proxy']['ports']))
|
|
|
|
|
+ cfg = secure_listener_config({'allow-lan': True, 'listeners': [{'listen': '0.0.0.0', 'port': 16001}]})
|
|
|
|
|
+ self.assertFalse(cfg['allow-lan'])
|
|
|
|
|
+ self.assertEqual(cfg['bind-address'], '127.0.0.1')
|
|
|
|
|
+ self.assertEqual(cfg['listeners'][0]['listen'], '127.0.0.1')
|
|
|
|
|
+
|
|
|
|
|
+ def test_expired_and_unavailable_signals_never_start_booking(self):
|
|
|
|
|
+ g = BuiltinBookerGCO(GroupConfig(identifier='audit', enable=True,
|
|
|
|
|
+ appointment_types=[self.apt]), {}, logger=lambda _: None)
|
|
|
|
|
+ self.addCleanup(g.redis_client.close)
|
|
|
|
|
+ g.redis_client = MagicMock()
|
|
|
|
|
+ g.redis_client.get.return_value = None
|
|
|
|
|
+ g.m_tasks = [Task(instance=MagicMock(), acceptable_routing_keys=[self.apt.routing_key])]
|
|
|
|
|
+ for payload in [
|
|
|
|
|
+ {'apt_type': self.apt.model_dump(), 'query_result': self.available.to_snapshot_payload(), 'expires_at': 1},
|
|
|
|
|
+ {'apt_type': self.apt.model_dump(), 'query_result': QueryResult().to_snapshot_payload()},
|
|
|
|
|
+ ]:
|
|
|
|
|
+ g.redis_client.mget.return_value = [json.dumps(payload)]
|
|
|
|
|
+ with patch.object(g._executor, 'submit') as submit:
|
|
|
|
|
+ g._poll_booking_signals()
|
|
|
|
|
+ submit.assert_not_called()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def test_confirmed_success_supersedes_pending_pause(self):
|
|
|
|
|
+ outbox = BookingOutbox()
|
|
|
|
|
+ outbox.put(42, 'tracker', {'status': 'pause'})
|
|
|
|
|
+ outbox.put(42, 'tracker', {'status': 'grabbed', 'grabbed_history': {'urn': 'receipt'}})
|
|
|
|
|
+ outbox.put(42, 'tracker', {'status': 'pause'})
|
|
|
|
|
+ self.assertEqual(outbox.get(42)['status'], 'grabbed')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
|
+ unittest.main()
|