Skip to content

Commit

Permalink
adds a raise block for showing proper message on missing name
Browse files Browse the repository at this point in the history
  • Loading branch information
halitcelik committed Nov 1, 2024
1 parent c3022a8 commit 99c9482
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cms/apphook_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions cms/tests/test_apphooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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(

Check failure on line 167 in cms/tests/test_apphooks.py

View workflow job for this annotation

GitHub Actions / codespell

assertIn ==> asserting, assert in, assertion
'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',
Expand Down

0 comments on commit 99c9482

Please sign in to comment.