From 0c18cf80e591b3497e5e1103a9d2af41c3e7c0a0 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 31 Jan 2024 17:16:20 +0100 Subject: [PATCH] Read admin assets version from configured folder (#3376) --- docs/kinto-admin.rst | 1 + kinto/plugins/admin/__init__.py | 16 +++++++++------- tests/plugins/test_admin.py | 22 ++++++++++++++-------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/kinto-admin.rst b/docs/kinto-admin.rst index 0db0b383c..0786b5272 100644 --- a/docs/kinto-admin.rst +++ b/docs/kinto-admin.rst @@ -11,6 +11,7 @@ configuration, a Web admin UI is available at ``/v1/admin/``. +=========================+==========+=================================================+ | kinto.admin_assets_path | None | Absolute path to the Admin UI assets files. | | | | The folder must contain an ``index.html`` file. | +| | | and a ``VERSION`` file. | +-------------------------+----------+-------------------------------------------------+ diff --git a/kinto/plugins/admin/__init__.py b/kinto/plugins/admin/__init__.py index 9a36d3766..664821027 100644 --- a/kinto/plugins/admin/__init__.py +++ b/kinto/plugins/admin/__init__.py @@ -6,11 +6,16 @@ from .views import admin_home_view -VERSION_FILE_PATH = Path(__file__).parent / "VERSION" - - def includeme(config): - admin_version = VERSION_FILE_PATH.read_text().strip() + admin_assets_path = config.registry.settings["admin_assets_path"] + if not admin_assets_path: + # Use bundled admin. + admin_assets_path = "kinto.plugins.admin:build" + version_file_parent = Path(__file__).parent + else: + version_file_parent = Path(admin_assets_path) + + admin_version = (version_file_parent / "VERSION").read_text().strip() # Expose capability. config.add_api_capability( @@ -23,9 +28,6 @@ def includeme(config): config.add_route("admin_home", "/admin/") config.add_view(admin_home_view, route_name="admin_home") - admin_assets_path = ( - config.registry.settings["admin_assets_path"] or "kinto.plugins.admin:build" - ) build_dir = static_view(admin_assets_path, use_subpath=True) config.add_route("catchall_static", "/admin/*subpath") config.add_view(build_dir, route_name="catchall_static") diff --git a/tests/plugins/test_admin.py b/tests/plugins/test_admin.py index 2c9e1daf2..ec7f02134 100644 --- a/tests/plugins/test_admin.py +++ b/tests/plugins/test_admin.py @@ -74,6 +74,17 @@ def test_admin_has_csp_header(self): class OverriddenAdminViewTest(BaseWebTest, unittest.TestCase): + @classmethod + def make_app(cls, *args, **kwargs): + cls.tmp_dir = tempfile.TemporaryDirectory() + with open(os.path.join(cls.tmp_dir.name, "VERSION"), "w") as f: + f.write("42.0.0") + with open(os.path.join(cls.tmp_dir.name, "index.html"), "w") as f: + f.write("mine!") + with open(os.path.join(cls.tmp_dir.name, "script.js"), "w") as f: + f.write("kiddy") + return super().make_app(*args, **kwargs) + @classmethod def tearDownClass(cls): super().tearDownClass() @@ -81,19 +92,14 @@ def tearDownClass(cls): @classmethod def get_app_settings(cls, extras=None): - cls.tmp_dir = tempfile.TemporaryDirectory() - settings = super().get_app_settings(extras) settings["includes"] = "kinto.plugins.admin" settings["admin_assets_path"] = cls.tmp_dir.name return settings - def setUp(self) -> None: - super().setUp() - with open(os.path.join(self.tmp_dir.name, "index.html"), "w") as f: - f.write("mine!") - with open(os.path.join(self.tmp_dir.name, "script.js"), "w") as f: - f.write("kiddy") + def test_admin_capability_reads_version_from_configured_folder(self): + resp = self.app.get("/") + self.assertEqual(resp.json["capabilities"]["admin"]["version"], "42.0.0") def test_admin_ui_is_served_from_configured_folder(self): resp = self.app.get("/admin/")