test_ita_single_file.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. """Offline single-artifact install, factory loading and hot-reload contracts."""
  2. import ast
  3. import builtins
  4. from contextlib import ExitStack, redirect_stdout
  5. import hashlib
  6. from io import StringIO
  7. import os
  8. from pathlib import Path
  9. import sys
  10. from tempfile import TemporaryDirectory
  11. from types import SimpleNamespace
  12. import unittest
  13. from unittest.mock import Mock, patch
  14. from plugins import ita_plugin # Preload shared host dependencies, not a loader substitute.
  15. from test.ita.test_ita_calendar_book import FakePage
  16. from test.ita.test_ita_protocol import live_route
  17. from tools import ita_query_book_local as local
  18. from utils import plugin_updater
  19. from vs_plg_factory import VSPlgFactory
  20. from vs_plg import IVSPlg
  21. from vs_types import AppointmentType, VSBookResult, VSQueryResult
  22. ROOT = Path(__file__).resolve().parents[2]
  23. class SingleFileSourceContractTests(unittest.TestCase):
  24. def test_runtime_compiles_and_implements_the_host_interface(self):
  25. source = (ROOT / "plugins/ita_plugin.py").read_text()
  26. compile(source, "ita_plugin.py", "exec")
  27. tree = ast.parse(source)
  28. plugins = [node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == "ItaPlugin"]
  29. self.assertEqual(len(plugins), 1)
  30. methods = {node.name for node in plugins[0].body if isinstance(node, ast.FunctionDef)}
  31. self.assertTrue(IVSPlg.__abstractmethods__ <= methods)
  32. self.assertTrue(issubclass(ita_plugin.ItaPlugin, IVSPlg))
  33. def test_browser_lifecycle_reuses_the_installed_host_helpers(self):
  34. from utils import plugin_utils
  35. for name in ("init_chromium_options", "cleanup_browser_workspace", "filter_dates_in_range"):
  36. self.assertIs(getattr(ita_plugin, name), getattr(plugin_utils, name))
  37. def test_runtime_has_no_private_italy_or_tool_imports_or_dynamic_packaging(self):
  38. tree = ast.parse((ROOT / "plugins/ita_plugin.py").read_text())
  39. for node in ast.walk(tree):
  40. if isinstance(node, (ast.Import, ast.ImportFrom)):
  41. names = [node.module or ""] if isinstance(node, ast.ImportFrom) else [a.name for a in node.names]
  42. self.assertFalse(any(name == "plugins" or name.startswith(("plugins.", "tools.")) for name in names), names)
  43. if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
  44. self.assertNotIn(node.func.id, {"exec", "eval", "__import__"})
  45. self.assertEqual(sorted(p.name for p in (ROOT / "plugins").glob("ita_*.py")), ["ita_plugin.py"])
  46. class SingleFileInstallationTests(unittest.TestCase):
  47. def setUp(self):
  48. self.stack = ExitStack()
  49. self.addCleanup(self.stack.close)
  50. self.directory = Path(self.stack.enter_context(TemporaryDirectory()))
  51. self.source = (ROOT / "plugins/ita_plugin.py").read_text()
  52. self.path = self.directory / "ita_plugin.py"
  53. old_cwd = Path.cwd()
  54. os.chdir(self.directory)
  55. self.stack.callback(os.chdir, old_cwd)
  56. self.stack.enter_context(redirect_stdout(StringIO()))
  57. old_module = sys.modules.get("ita_plugin")
  58. def restore_module():
  59. sys.modules.pop("ita_plugin", None)
  60. if old_module is not None:
  61. sys.modules["ita_plugin"] = old_module
  62. self.stack.callback(restore_module)
  63. original_import = builtins.__import__
  64. def guarded_import(name, *args, **kwargs):
  65. if name == "plugins" or name.startswith(("plugins.", "tools.")):
  66. raise AssertionError("Single artifact attempted a private runtime import: " + name)
  67. return original_import(name, *args, **kwargs)
  68. self.stack.enter_context(patch("builtins.__import__", side_effect=guarded_import))
  69. for target in ("socket.socket.connect", "socket.create_connection", "requests.sessions.Session.request",
  70. "subprocess.Popen"):
  71. self.stack.enter_context(patch(target, side_effect=AssertionError("Unexpected external I/O")))
  72. self.stack.enter_context(patch.object(sys, "dont_write_bytecode", True))
  73. self.factory = VSPlgFactory()
  74. self.factory.register_plugin("ita_plugin", str(self.path), "ItaPlugin")
  75. self.install(self.source, "1.0.1")
  76. def install(self, source, version):
  77. self.assertTrue(plugin_updater.download_and_install_plugin(
  78. "ita_plugin", version, code_content=source, plugins_dir=str(self.directory),
  79. expected_sha256=hashlib.sha256(source.encode()).hexdigest()))
  80. def create(self):
  81. plugin = self.factory.create("offline-single-file", "ita_plugin")
  82. self.stack.callback(plugin.cleanup)
  83. return plugin
  84. def test_only_one_uploaded_file_loads_all_runtime_sections(self):
  85. self.assertEqual([p.name for p in self.directory.glob("*.py")], ["ita_plugin.py"])
  86. plugin = self.create()
  87. runtime = type(plugin).book.__globals__
  88. for name in ("ItaRouteConfig", "_submit_final_once",
  89. "parse_otp_body", "prepare_calendar", "recheck_calendar_before_final", "submit_final"):
  90. self.assertEqual(runtime[name].__module__, "ita_plugin", name)
  91. self.assertEqual(runtime["parse_otp_body"]("OTP Code:001234"), "001234")
  92. self.assertEqual(plugin_updater.get_installed_plugins(str(self.directory))["ita_plugin"], "1.0.1")
  93. self.assertIsNot(runtime["ItaCapability"], ita_plugin.ItaCapability)
  94. def test_public_query_and_book_bind_the_loaded_file_not_canonical_import(self):
  95. plugin = self.create()
  96. plugin.page = SimpleNamespace(url="https://prenotami.esteri.it/UserArea", quit=Mock())
  97. plugin.free_config = {"otp_mode": "cloud", "query_mode": "calendar"}
  98. negative = VSQueryResult(success=False)
  99. runtime = type(plugin).book.__globals__
  100. with patch.dict(runtime, prepare_query_if_needed=Mock(return_value=negative)):
  101. self.assertIs(plugin.query(AppointmentType(routing_key="fixture")), negative)
  102. runtime["prepare_query_if_needed"].assert_called_once()
  103. key = "slot.fixture.it.tourist"
  104. plugin.free_config = {"otp_mode": "cloud", "query_mode": "services_probe",
  105. "final_submit_enabled": False, "apt_configs": {key: live_route()}}
  106. plugin.config = SimpleNamespace(account=SimpleNamespace(username="fixture@example.test"))
  107. plugin.session_create_time = 1
  108. plugin.page = FakePage()
  109. plugin.page.tab_id = "fixture-tab"
  110. plugin.page.quit = Mock()
  111. route = runtime["resolve_route_config"](plugin.free_config["apt_configs"], key)
  112. plugin._record_calendar_prepared_route(route, role="book")
  113. trigger = VSQueryResult(success=True, apt_type=AppointmentType(routing_key=key))
  114. with patch.dict(runtime, recheck_calendar_before_final=Mock(return_value=None)), \
  115. patch.object(ita_plugin, "recheck_calendar_before_final", side_effect=AssertionError("canonical runtime used")):
  116. result = plugin.book(trigger, {"username": "fixture@example.test"})
  117. self.assertFalse(result.success)
  118. self.assertEqual(result.label, runtime["BOOKING_RECHECK_NONE_AVAILABLE_LABEL"])
  119. runtime["recheck_calendar_before_final"].assert_called_once()
  120. def test_loaded_final_adapter_uses_the_instance_runtime_after_cache_replacement(self):
  121. plugin = self.create()
  122. runtime = type(plugin).book.__globals__
  123. key = "slot.fixture.it.tourist"
  124. config = {key: live_route()}
  125. route = runtime["resolve_route_config"](config, key)
  126. configured_route = ita_plugin.resolve_route_config(config, key)
  127. self.assertNotEqual(route, configured_route) # Distinct loaded dataclass types.
  128. self.assertEqual(route.routing_key, configured_route.routing_key)
  129. expected = VSBookResult(success=False, label="synthetic")
  130. adapter = Mock(return_value=expected)
  131. self.create() # The host overwrites sys.modules['ita_plugin'] for Query.
  132. self.assertIsNot(sys.modules["ita_plugin"].ItaPlugin, type(plugin))
  133. approval = object()
  134. with patch.dict(runtime, _submit_final_once=adapter):
  135. self.assertIs(runtime["submit_final"](plugin, route, approval), expected)
  136. adapter.assert_called_once_with(plugin, route, approval)
  137. def test_local_observer_preserves_standard_false_book_result(self):
  138. plugin = self.create()
  139. observer = local.ObservedPlugin(plugin, "book")
  140. result = VSBookResult(success=False, label="retryable")
  141. plugin.book = Mock(return_value=result)
  142. self.assertIs(observer.book(VSQueryResult(), {}), result)
  143. self.assertIsNone(observer.error)
  144. def test_real_reload_replaces_code_and_preserves_process_wide_otp_lock(self):
  145. first = self.create()
  146. old = type(first).book.__globals__
  147. updated = self.source.replace('version = "1.0.1"', 'version = "1.0.200"')
  148. self.install(updated, "1.0.200")
  149. owner = self
  150. events = []
  151. cfg = SimpleNamespace(plugin_config=SimpleNamespace(plugin_name="ita_plugin"))
  152. previous = SimpleNamespace(m_cfg=cfg, stop=lambda: events.append("stop"))
  153. class Wrapper:
  154. def __init__(self, cls, config, redis):
  155. self.m_cfg = config
  156. def load(self):
  157. events.append("load")
  158. self.plugin = owner.create()
  159. def start(self):
  160. events.append("start")
  161. # Execute the unmodified host function without importing/starting main.
  162. node = next(n for n in ast.parse((ROOT / "main.py").read_text()).body
  163. if isinstance(n, ast.FunctionDef) and n.name == "reload_wrappers_for_plugin")
  164. namespace = {"Dict": dict, "GCOWrapper": Wrapper, "sys": sys, "REDIS_CFG": {}}
  165. exec(compile(ast.Module(body=[node], type_ignores=[]), str(ROOT / "main.py"), "exec"), namespace)
  166. wrappers = {"fixture": previous}
  167. namespace["reload_wrappers_for_plugin"]("ita_plugin", wrappers, {"fixture": object}, Mock())
  168. new = type(wrappers["fixture"].plugin).book.__globals__
  169. self.assertEqual(events, ["stop", "load", "start"])
  170. self.assertEqual(wrappers["fixture"].plugin.version, "1.0.200")
  171. for name in ("ItaPlugin", "ItaCapability", "OtpMailConfig", "_submit_final_once",
  172. "fetch_otp_from_cloud", "prepare_calendar", "submit_final"):
  173. self.assertIsNot(new[name], old[name], name)
  174. self.assertIsNot(new["ItaPlugin"].book, old["ItaPlugin"].book)
  175. self.assertIs(new["_OTP_INPUT_LOCK"], old["_OTP_INPUT_LOCK"])
  176. self.assertIs(new["_OTP_INPUT_LOCK"], ita_plugin._OTP_INPUT_LOCK)
  177. with old["_OTP_INPUT_LOCK"]:
  178. self.assertFalse(new["_OTP_INPUT_LOCK"].acquire(blocking=False))
  179. self.assertIs(type(first).book.__globals__, old)
  180. if __name__ == "__main__":
  181. unittest.main()