From 01d97eb394f3cb2b15bc841380f9f7e21b66c10e Mon Sep 17 00:00:00 2001 From: David Barnes <barnesdavidj@gmail.com> Date: Wed, 7 Sep 2022 10:26:05 -0400 Subject: [PATCH] Update the warning message for unassigned perms to be clearer about the type of view that has the issue. --- adminlte2_pdq/middleware.py | 10 ++++++++-- tests/test_middleware.py | 21 ++++++++++----------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/adminlte2_pdq/middleware.py b/adminlte2_pdq/middleware.py index 89292c6..03a5ba7 100644 --- a/adminlte2_pdq/middleware.py +++ b/adminlte2_pdq/middleware.py @@ -133,11 +133,17 @@ class AuthMiddleware: permissions = getattr(view_class, 'permission_required', []) one_of_permissions = getattr(view_class, 'permission_required_one', []) login_required = getattr(view_class, 'login_required', False) + view_name = view_class.__qualname__ + view_type = 'class-based' + view_perm_type = 'attribute' else: # Get attributes permissions = getattr(view.func, 'permissions', []) one_of_permissions = getattr(view.func, 'one_of_permissions', []) login_required = getattr(view.func, 'login_required', False) + view_name = view.func.__qualname__ + view_type = 'function-based' + view_perm_type = 'decorator' # If there are permissions, or login_required if exempt or permissions or one_of_permissions or login_required: @@ -145,9 +151,9 @@ class AuthMiddleware: # Permissions or Login Required not set, add messages, warnings, and return False warning_message = ( - f"The view '{view.func.__qualname__}' does not have the" + f"The {view_type} view '{view_name}' does not have the" " permission_required, one_of_permission, or login_required" - " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is" + f" {view_perm_type} set and the option ADMINLTE2_USE_STRICT_POLICY is" " set to True. This means that this view is inaccessible until" " either permissions are set on the view or the url_name for the" " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting." diff --git a/tests/test_middleware.py b/tests/test_middleware.py index 00871fb..d8112f0 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -89,9 +89,9 @@ class MiddlewareTestCase(TestCase): """test_middleware_blocks_when_user_anonymous_login_off_strict_on_login_wl_off_strict_wl_off""" with warnings.catch_warnings(record=True) as wa: warning_message = ( - "The view 'demo_css' does not have the" + "The function-based view 'demo_css' does not have the" " permission_required, one_of_permission, or login_required" - " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is" + " decorator set and the option ADMINLTE2_USE_STRICT_POLICY is" " set to True. This means that this view is inaccessible until" " either permissions are set on the view or the url_name for the" " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting." @@ -137,9 +137,9 @@ class MiddlewareTestCase(TestCase): # Home is a new request that fails the login required being on and thus redirect to login page. with warnings.catch_warnings(record=True) as wa: warning_message = ( - "The view 'demo_css' does not have the" + "The function-based view 'demo_css' does not have the" " permission_required, one_of_permission, or login_required" - " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is" + " decorator set and the option ADMINLTE2_USE_STRICT_POLICY is" " set to True. This means that this view is inaccessible until" " either permissions are set on the view or the url_name for the" " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting." @@ -223,9 +223,9 @@ class MiddlewareTestCase(TestCase): with warnings.catch_warnings(record=True) as wa: self.client.force_login(self.test_user_w_perms) warning_message = ( - "The view 'demo_css' does not have the" + "The function-based view 'demo_css' does not have the" " permission_required, one_of_permission, or login_required" - " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is" + " decorator set and the option ADMINLTE2_USE_STRICT_POLICY is" " set to True. This means that this view is inaccessible until" " either permissions are set on the view or the url_name for the" " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting." @@ -259,9 +259,9 @@ class MiddlewareTestCase(TestCase): with warnings.catch_warnings(record=True) as wa: self.client.force_login(self.test_user_w_perms) warning_message = ( - "The view 'demo_css' does not have the" + "The function-based view 'demo_css' does not have the" " permission_required, one_of_permission, or login_required" - " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is" + " decorator set and the option ADMINLTE2_USE_STRICT_POLICY is" " set to True. This means that this view is inaccessible until" " either permissions are set on the view or the url_name for the" " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting." @@ -284,9 +284,9 @@ class MiddlewareTestCase(TestCase): with warnings.catch_warnings(record=True) as wa: self.client.force_login(self.test_user_w_perms) warning_message = ( - "The view 'demo_css' does not have the" + "The function-based view 'demo_css' does not have the" " permission_required, one_of_permission, or login_required" - " attribute set and the option ADMINLTE2_USE_STRICT_POLICY is" + " decorator set and the option ADMINLTE2_USE_STRICT_POLICY is" " set to True. This means that this view is inaccessible until" " either permissions are set on the view or the url_name for the" " view is added to the ADMINLTE2_STRICT_POLICY_WHITELIST setting." @@ -343,7 +343,6 @@ class MiddlewareTestCase(TestCase): reverse('admin:auth_user_changelist'), follow=True ) - print(response.content.decode()) self.assertEqual(response.status_code, 200) self.assertContains(response, "Select user to change") -- GitLab