From 59bf35eb4ffa87b120aef253653e27a9e7a27bbc Mon Sep 17 00:00:00 2001 From: Lukas Vinclav Date: Sat, 23 Sep 2023 08:01:56 +0200 Subject: [PATCH] feat: navigations tests (#130) --- src/unfold/sites.py | 5 +- tests/settings.py | 9 +++ tests/test_navigations.py | 115 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 tests/test_navigations.py diff --git a/src/unfold/sites.py b/src/unfold/sites.py index 1ec3ea26..4eee6c47 100644 --- a/src/unfold/sites.py +++ b/src/unfold/sites.py @@ -268,7 +268,9 @@ def get_tabs_list(self, request: HttpRequest) -> List[Dict[str, Any]]: return tabs - def _call_permission_callback(self, callback: Union[str, Callable, None], request: HttpRequest): + def _call_permission_callback( + self, callback: Union[str, Callable, None], request: HttpRequest + ): if callback is None: return True @@ -276,6 +278,7 @@ def _call_permission_callback(self, callback: Union[str, Callable, None], reques callback = import_string(callback) return callback(request) + def _get_value( self, instance: Union[str, Callable, None], *args: Any ) -> Optional[str]: diff --git a/tests/settings.py b/tests/settings.py index 12f8a7ab..9a03ba4f 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,3 +1,12 @@ +from django.core.management.utils import get_random_secret_key + +SECRET_KEY = get_random_secret_key() + DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3"}} +INSTALLED_APPS = [ + "django.contrib.auth", + "django.contrib.contenttypes", +] + USE_TZ = False diff --git a/tests/test_navigations.py b/tests/test_navigations.py new file mode 100644 index 00000000..f787faa0 --- /dev/null +++ b/tests/test_navigations.py @@ -0,0 +1,115 @@ +from django.test import TestCase +from django.test.client import RequestFactory +from django.test.utils import override_settings +from unfold.settings import CONFIG_DEFAULTS, get_config +from unfold.sites import UnfoldAdminSite + + +def deny_permission(request): + return False + + +def allow_permission(request): + return True + + +class NavigationTestCase(TestCase): + @override_settings( + UNFOLD={ + **CONFIG_DEFAULTS, + **{ + "TABS": [ + { + "items": [ + { + "title": "Example Title", + "link": "https://example.com", + "permission": lambda request: False, + } + ], + } + ] + }, + } + ) + def test_check_tab_lambda_deny_permission(self): + admin_site = UnfoldAdminSite() + request = RequestFactory().get("/rand") + tabs = admin_site.get_tabs_list(request) + self.assertEqual(len(tabs[0]["items"]), 0) + get_config.cache_clear() + + @override_settings( + UNFOLD={ + **CONFIG_DEFAULTS, + **{ + "TABS": [ + { + "items": [ + { + "title": "Example Title", + "link": "https://example.com", + "permission": lambda request: True, + } + ] + } + ] + }, + } + ) + def test_check_tab_lambda_allow_permission(self): + admin_site = UnfoldAdminSite() + request = RequestFactory().get("/rand") + tabs = admin_site.get_tabs_list(request) + self.assertEqual(len(tabs[0]["items"]), 1) + get_config.cache_clear() + + @override_settings( + UNFOLD={ + **CONFIG_DEFAULTS, + **{ + "TABS": [ + { + "items": [ + { + "title": "Example Title", + "link": "https://example.com", + "permission": "tests.test_navigations.deny_permission", + } + ], + } + ] + }, + } + ) + def test_check_tab_path_deny_permission(self): + admin_site = UnfoldAdminSite() + request = RequestFactory().get("/rand") + tabs = admin_site.get_tabs_list(request) + self.assertEqual(len(tabs[0]["items"]), 0) + get_config.cache_clear() + + @override_settings( + UNFOLD={ + **CONFIG_DEFAULTS, + **{ + "TABS": [ + { + "items": [ + { + "title": "Example Title", + "link": "https://example.com", + "permission": "tests.test_navigations.allow_permission", + } + ], + } + ] + }, + } + ) + def test_check_tab_path_allow_permission(self): + admin_site = UnfoldAdminSite() + request = RequestFactory().get("/rand") + tabs = admin_site.get_tabs_list(request) + self.assertEqual(len(tabs[0]["items"]), 1) + get_config.cache_clear()