From 13342835b0cffe7334db7106a7db330926b887b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Thu, 2 Dec 2021 22:38:12 +0100 Subject: [PATCH] feat: theme-agnostic view to fetch theme assets (#29461) It is possible to set custom logos in microfrontends, for instance with the LOGO_URL setting. Ideally, we would like that MFEs share the same logos as the LMS. But this is made difficult when comprehensive theming is enabled, and the logo is overridden by a custom theme. In that scenario, the logo url becomes /static/mytheme/images/logo.png. But the MFEs do no know that the "mytheme" theme is enabled. To resolve this issue, we introduce here a view, at the "/theming/asset/" url, that redirects to the corresponding asset in the theme that is currently enabled. Thus, MFEs only have to set `LOGO_URL=http://lmshost/theming/asset/images/logo.png` to point to the themed logo. Related issue: https://github.com/overhangio/tutor-mfe/issues/25 --- .../djangoapps/theming/tests/test_views.py | 51 ++++++++++--------- openedx/core/djangoapps/theming/urls.py | 27 ++++++---- openedx/core/djangoapps/theming/views.py | 16 ++++++ 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/openedx/core/djangoapps/theming/tests/test_views.py b/openedx/core/djangoapps/theming/tests/test_views.py index 08b75e23d296..3eefd7bed890 100644 --- a/openedx/core/djangoapps/theming/tests/test_views.py +++ b/openedx/core/djangoapps/theming/tests/test_views.py @@ -1,16 +1,15 @@ """ Tests for comprehensive them """ - +from unittest.mock import patch from django.conf import settings -from django.contrib.messages.middleware import MessageMiddleware from django.contrib.sites.models import Site -from django.test import TestCase +from django.test import TestCase, override_settings +from django.urls import reverse -from common.djangoapps.student.tests.factories import GlobalStaffFactory -from openedx.core.djangoapps.theming.middleware import CurrentSiteThemeMiddleware -from common.djangoapps.student.tests.factories import UserFactory +from common.djangoapps.student.tests.factories import GlobalStaffFactory, UserFactory +from openedx.core.djangoapps.theming.models import SiteTheme THEMING_ADMIN_URL = '/theming/admin' TEST_THEME_NAME = 'test-theme' @@ -21,23 +20,6 @@ class TestThemingViews(TestCase): """ Test theming views. """ - def setUp(self): - """ - Initialize middleware and related objects - """ - super().setUp() - - self.site_theme_middleware = CurrentSiteThemeMiddleware() - self.user = UserFactory.create() - - def initialize_mock_request(self, request): - """ - Initialize a test request. - """ - request.user = self.user - request.site, __ = Site.objects.get_or_create(domain='test', name='test') - request.session = {} - MessageMiddleware().process_request(request) def test_preview_theme_access(self): """ @@ -57,7 +39,8 @@ def test_preview_theme_access(self): ) # Logged in non-global staff get a 404 - self.client.login(username=self.user.username, password=TEST_PASSWORD) + non_global_staff_user = UserFactory.create() + self.client.login(username=non_global_staff_user.username, password=TEST_PASSWORD) response = self.client.get(THEMING_ADMIN_URL) assert response.status_code == 404 @@ -108,3 +91,23 @@ def test_preview_theme(self): response, f'