Skip to content

Commit

Permalink
Read admin assets version from configured folder (#3376)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Jan 31, 2024
1 parent fe3bdde commit 0c18cf8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/kinto-admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
+-------------------------+----------+-------------------------------------------------+


Expand Down
16 changes: 9 additions & 7 deletions kinto/plugins/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Expand Down
22 changes: 14 additions & 8 deletions tests/plugins/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,32 @@ 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()
cls.tmp_dir.cleanup()

@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/")
Expand Down

0 comments on commit 0c18cf8

Please sign in to comment.