From 99c948229f2f5f31936928a9361f480ed07ac481 Mon Sep 17 00:00:00 2001 From: Halit Celik Date: Fri, 1 Nov 2024 16:37:33 +0100 Subject: [PATCH] adds a raise block for showing proper message on missing name --- cms/apphook_pool.py | 7 ++++++- cms/tests/test_apphooks.py | 22 ++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cms/apphook_pool.py b/cms/apphook_pool.py index c3c8f4aefac..5cda7f4f87d 100644 --- a/cms/apphook_pool.py +++ b/cms/apphook_pool.py @@ -43,7 +43,7 @@ def register(self, app=None, discovering_apps=False): warnings.warn("You define a 'menu' attribute on CMS application " "%r, but the 'menus' attribute is empty, " "did you make a typo?" % app.__name__) - + self.apps[app.__name__] = app() return app @@ -73,6 +73,11 @@ def get_apphooks(self): app = self.apps[app_name] if app.get_urls(): + if app.name is None: + raise ImproperlyConfigured( + 'CMS application must define name ' + 'but %r does not have one' % app_name + ) hooks.append((app_name, app.name)) # Unfortunately, we lose the ordering since we now have a list of diff --git a/cms/tests/test_apphooks.py b/cms/tests/test_apphooks.py index 4b3a1af317e..80da76fd920 100644 --- a/cms/tests/test_apphooks.py +++ b/cms/tests/test_apphooks.py @@ -9,6 +9,7 @@ from django.core import checks from django.core.cache import cache from django.core.checks.urls import check_url_config +from django.core.exceptions import ImproperlyConfigured from django.test.utils import override_settings from django.urls import NoReverseMatch, clear_url_caches, resolve, reverse from django.utils.timezone import now @@ -31,7 +32,6 @@ from cms.utils.urlutils import admin_reverse from menus.menu_pool import menu_pool from menus.utils import DefaultLanguageChanger - APP_NAME = 'SampleApp' NS_APP_NAME = 'NamespacedApp' APP_MODULE = "cms.test_utils.project.sampleapp.cms_apps" @@ -149,7 +149,25 @@ def test_explicit_apphooks(self): self.assertEqual(len(hooks), 1) self.assertEqual(app_names, [APP_NAME]) self.apphook_clear() - + + @override_settings(CMS_APPHOOKS=[f'{APP_MODULE}.{APP_NAME}']) + def test_apphook_without_name_raises_improperly_configured(self): + """ + Test that an apphook without a name raises an ImproperlyConfigured exception. + """ + class AppWithoutName(CMSApp): + def get_urls(self, page=None, language=None, **kwargs): + return ["sampleapp.urls"] + + apphook_pool.register(AppWithoutName) + + with self.assertRaises(ImproperlyConfigured) as cm: + apphook_pool.get_apphooks() + + self.assertIn( + 'CMS application must define name but AppWithoutName does not have one', + str(cm.exception) + ) @override_settings( INSTALLED_APPS=['cms.test_utils.project.sampleapp'], ROOT_URLCONF='cms.test_utils.project.urls_for_apphook_tests',