Skip to content

Commit

Permalink
feat: navigations tests (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav authored Sep 23, 2023
1 parent 0aa696d commit 59bf35e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/unfold/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,17 @@ 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

if isinstance(callback, str):
callback = import_string(callback)

return callback(request)

def _get_value(
self, instance: Union[str, Callable, None], *args: Any
) -> Optional[str]:
Expand Down
9 changes: 9 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -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
115 changes: 115 additions & 0 deletions tests/test_navigations.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 59bf35e

Please sign in to comment.