"""Offline checks for the Italy profile consumed by the existing public tester.""" import copy import json from pathlib import Path from types import SimpleNamespace import unittest from unittest.mock import Mock, patch from plugins.ita_plugin import ItaCapability, resolve_route_config from sentinel import SentinelGCO from tools.plugin_tester import PluginTester from vs_types import GroupConfig def hong_kong_group(): groups = json.loads((Path(__file__).resolve().parents[2] / "config/config_sentinel.json").read_text()) matches = [group for group in groups if group["identifier"] == "ita.hong_kong"] if len(matches) != 1: raise AssertionError("Expected exactly one Hong Kong Sentinel profile") return GroupConfig.from_json(matches[0]) class ItalyPublicTesterConfigTests(unittest.TestCase): def setUp(self): # Exercise the actual JSON loader and registration, never create a plugin, # open a browser, or use the configured credentials in an assertion. with patch("vs_plg_factory.VSC_INFO"), patch( "tools.plugin_tester.VSPlgFactory.create", side_effect=AssertionError("Offline configuration check must not create a plugin"), ): self.tester = PluginTester.from_json_config( "ita_plugin", str(Path(__file__).resolve().parents[2] / "config/test_conf.json"), user_override="query@example.test", password_override="synthetic-not-a-secret", proxy_override="http://127.0.0.1:9999", ) def test_existing_loader_resolves_one_hong_kong_appointment(self): self.assertIsNone(self.tester.plugin) self.assertEqual(self.tester.group_id, "ita.hong_kong") self.assertEqual(len(self.tester.appointment_types), 1) appointment = self.tester.appointment_types[0] self.assertEqual(appointment.routing_key, "slot.hkg.it.tourist") self.assertEqual(appointment.city, "Hong Kong") self.assertEqual(appointment.country, "Italy") self.assertEqual(appointment.visa_type, "Tourist") def test_query_profile_uses_cloud_preparation_and_disables_final_submission(self): config = self.tester.config self.assertIs(config.debug, False) self.assertEqual(config.free_config["query_mode"], "calendar") self.assertEqual(config.free_config["otp_mode"], "cloud") self.assertIs(config.free_config["final_submit_enabled"], False) def test_existing_protocol_parser_accepts_evidenced_form_and_scan_settings(self): route = resolve_route_config( self.tester.config.free_config["apt_configs"], self.tester.appointment_types[0].routing_key, ) self.assertEqual(route.service_id, 4937) self.assertIs(route.capability, ItaCapability.SINGLE_SCHENGEN_TOURISM) self.assertEqual(route.lookahead_months, 12) self.assertEqual(route.booking_type.control_id, "typeofbookingddl") self.assertEqual(route.booking_type.value, "1") self.assertEqual(route.booking_type.labels, ("Prenotazione Singola", "Single booking")) self.assertEqual(route.purpose.control_id, "ddls_0") self.assertEqual(route.purpose.value, "42") self.assertEqual(route.purpose.labels, ("Turismo", "Tourism")) self.assertIsNotNone(route.mail) self.assertEqual(route.mail.authorization_source, "account") self.assertIsNone(route.mail.authorization_email) self.assertEqual(route.mail.mailbox_for("query@example.test"), "query@example.test") def test_public_tester_and_sentinel_share_route_and_otp_contract(self): group = hong_kong_group() config = self.tester.config.free_config self.assertEqual(group.appointment_types, self.tester.appointment_types) for key in ("website", "query_mode", "otp_mode", "final_submit_enabled", "apt_configs"): self.assertEqual(group.free_config[key], config[key]) def test_original_configuration_path_keeps_its_account_binding(self): path = Path(__file__).resolve().parents[2] / "config/test_conf.json" data = json.loads(path.read_text()) profile = data.get("plugins", data)["ita_plugin"] with patch("vs_plg_factory.VSC_INFO"): tester = PluginTester.from_json_config("ita_plugin", str(path)) for field in ("username", "password"): # Assert the binding without including credentials in failure output. self.assertTrue(getattr(tester.config.account, field) == profile["account"][field], f"Italy {field} must come from the original configuration") self.assertNotIn("capsolver_key", profile["free_config"]) class ItalySingleAccountPoolConfigTests(unittest.TestCase): def setUp(self): self.group = hong_kong_group() # Do not create browsers, read account secrets or call a live backend. for target in ("socket.socket.connect", "socket.socket.connect_ex", "socket.create_connection", "requests.sessions.Session.request", "subprocess.Popen"): self.enterContext(patch(target, side_effect=AssertionError("Offline test attempted external I/O"))) self.cloud = Mock() self.cloud.get_next_account.return_value = { "id": 71, "pool_name": "hkg.it.sentinel", "username": "pooled@example.test", "password": "synthetic-pool-password", "status": "active", } self.cloud.get_next_proxy.return_value = {"id": 72, "ip": "127.0.0.1", "port": 9999} self.redis = Mock() self.enterContext(patch.dict(SentinelGCO.__init__.__globals__, { "SafeRedisClient": Mock(return_value=self.redis), "VSCloudApi": SimpleNamespace(Instance=lambda: self.cloud), })) self.sentinel = SentinelGCO(self.group, {}, Mock()) self.plugin = Mock() self.sentinel.m_factory.create = Mock(return_value=self.plugin) self.addCleanup(self.sentinel.stop) def test_profile_is_disabled_single_instance_cloud_query_without_local_credentials(self): group = self.group self.assertFalse(group.enable) self.assertFalse(group.debug) self.assertTrue(group.need_account) self.assertEqual(group.sentinel.account_source, "built-in") self.assertEqual(group.sentinel.account_pool_id, "hkg.it.sentinel") self.assertEqual(group.sentinel.target_instances, 1) self.assertEqual(group.sentinel.account_cd, group.session_max_life) self.assertEqual(group.sentinel.account_cd, 1800) self.assertEqual(group.free_config["otp_mode"], "cloud") self.assertIs(group.free_config["final_submit_enabled"], False) for key in ("account", "password", "local_account_file", "authorization_code"): self.assertNotIn(key, group.free_config) self.sentinel.start() self.sentinel.m_factory.create.assert_not_called() self.cloud.get_next_account.assert_not_called() def test_existing_sentinel_spawner_binds_pool_account_and_mail_configuration(self): before = copy.deepcopy(self.group) self.sentinel._spawn_sentinel_worker() self.cloud.get_next_account.assert_called_once_with("hkg.it.sentinel", 1800) self.cloud.get_next_proxy.assert_called_once_with(self.group.proxy_pool, self.group.proxy_cd) self.plugin.set_config.assert_called_once() config = self.plugin.set_config.call_args.args[0] self.assertEqual(config.account.id, 71) self.assertEqual(config.account.username, "pooled@example.test") self.assertEqual(config.account.password, "synthetic-pool-password") self.assertEqual(config.free_config, self.group.free_config) self.assertEqual(self.group, before) self.plugin.create_session.assert_called_once() self.assertEqual(len(self.sentinel.m_tasks), 1) self.assertFalse(self.sentinel.m_tasks[0].book_allowed) self.plugin.book.assert_not_called() self.redis.setex.assert_not_called() def test_empty_or_cooling_pool_does_not_fall_back_to_a_local_account(self): self.cloud.get_next_account.side_effect = RuntimeError("synthetic unavailable pool") self.sentinel._spawn_sentinel_worker() self.cloud.get_next_account.assert_called_once_with("hkg.it.sentinel", 1800) self.sentinel.m_factory.create.assert_not_called() self.plugin.create_session.assert_not_called() self.assertEqual(self.sentinel.m_tasks, []) def test_another_pool_account_uses_its_own_authorized_mailbox(self): self.cloud.get_next_account.return_value["username"] = "another@example.test" self.sentinel._spawn_sentinel_worker() config = self.plugin.set_config.call_args.args[0] route = resolve_route_config(config.free_config["apt_configs"], self.group.appointment_types[0].routing_key) self.assertEqual(config.account.username, "another@example.test") self.assertEqual(route.mail.mailbox_for(config.account.username), "another@example.test") self.plugin.book.assert_not_called() if __name__ == "__main__": unittest.main()