Skip to content

Commit

Permalink
[Fixes #12713] Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Nov 11, 2024
1 parent 5738b29 commit bf17472
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions geonode/security/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class BasePermissionsHandler(ABC):
(example advanced workflow)
"""

def __str__(self):
return f"{self.__module__}.{self.__class__.__name__}"

def __repr__(self):
return self.__str__()

@staticmethod
def fixup_perms(instance, perms_payload, *args, **kwargs):
return perms_payload
Expand Down
39 changes: 39 additions & 0 deletions geonode/security/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from geonode.layers.models import Dataset
from geonode.documents.models import Document
from geonode.compat import ensure_string
from geonode.security.handlers import BasePermissionsHandler
from geonode.upload.models import ResourceHandlerInfo
from geonode.utils import check_ogc_backend
from geonode.tests.utils import check_dataset
Expand All @@ -56,6 +57,7 @@
from geonode.groups.models import Group, GroupMember, GroupProfile
from geonode.layers.populate_datasets_data import create_dataset_data
from geonode.base.auth import create_auth_token, get_or_create_token
from geonode.security.registry import permissions_registry

from geonode.base.models import Configuration, UserGeoLimit, GroupGeoLimit
from geonode.base.populate_test_data import (
Expand Down Expand Up @@ -2662,3 +2664,40 @@ def test_user_can_publish(self):
# setting back the owner to admin
self.dataset.owner = self.admin
self.dataset.save()


class DummyPermissionsHandler(BasePermissionsHandler):
@staticmethod
def fixup_perms(instance, perms_payload, *args, **kwargs):
return {"perms": ["this", "is", "fake"]}


@override_settings(PERMISSIONS_HANDLERS=["geonode.security.handlers.AdvancedWorkflowPermissionsHandler"])
class TestPermissionsRegistry(GeoNodeBaseTestSupport):
"""
Test to verify the permissions registry
"""

def test_registry_is_correctly_initiated(self):
"""
The permissions registry should initiated correctly
"""
permissions_registry.init_registry()
self.assertIsNotNone(permissions_registry.get_registry())

def test_new_handler_is_registered(self):
permissions_registry.add("geonode.security.tests.DummyPermissionsHandler")
reg = permissions_registry.get_registry()
self.assertTrue("geonode.security.tests.DummyPermissionsHandler" in (str(r) for r in reg))

def test_should_raise_exception_if_is_not_subclass(self):
with self.assertRaises(Exception):
permissions_registry.add(int)

def test_handler_should_handle_the_perms_payload(self):
# create resource
instance = create_single_dataset("fake_dataset")
# adding the dummy at the end, means will win over the other handler
permissions_registry.add("geonode.security.tests.DummyPermissionsHandler")
perms = permissions_registry.fixup_perms(instance, instance.get_all_level_info())
self.assertDictEqual({"perms": ["this", "is", "fake"]}, perms)

0 comments on commit bf17472

Please sign in to comment.