51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
import unittest
|
|
|
|
import _bootstrap # noqa: F401
|
|
|
|
from netbox_plugin_store.access import has_store_access
|
|
from netbox_plugin_store.navigation import menu
|
|
|
|
|
|
PERMISSION = "netbox_plugin_store.manage_plugin"
|
|
|
|
|
|
class NetBoxUser:
|
|
"""NetBox 4.6 users deliberately have no is_staff attribute."""
|
|
|
|
def __init__(self, *, authenticated=True, superuser=False, permissions=()):
|
|
self.is_authenticated = authenticated
|
|
self.is_superuser = superuser
|
|
self.permissions = set(permissions)
|
|
|
|
def has_perm(self, permission_name):
|
|
return permission_name in self.permissions
|
|
|
|
|
|
class AccessCompatibilityTests(unittest.TestCase):
|
|
def test_permission_user_without_is_staff_is_authorized(self):
|
|
user = NetBoxUser(permissions=(PERMISSION,))
|
|
self.assertTrue(has_store_access(user, PERMISSION))
|
|
|
|
def test_unauthenticated_and_unprivileged_users_are_rejected(self):
|
|
self.assertFalse(
|
|
has_store_access(NetBoxUser(authenticated=False, permissions=(PERMISSION,)), PERMISSION)
|
|
)
|
|
self.assertFalse(has_store_access(NetBoxUser(), PERMISSION))
|
|
|
|
def test_superuser_is_authorized_without_explicit_permission(self):
|
|
self.assertTrue(has_store_access(NetBoxUser(superuser=True), PERMISSION))
|
|
|
|
|
|
class NavigationCompatibilityTests(unittest.TestCase):
|
|
def test_menu_uses_permission_not_staff_only(self):
|
|
items = [item for _label, group_items in menu.groups for item in group_items]
|
|
self.assertEqual(len(items), 3)
|
|
for item in items:
|
|
self.assertTrue(item.auth_required)
|
|
self.assertFalse(item.staff_only)
|
|
self.assertEqual(item.permissions, [PERMISSION])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|