From 37713bed12fea6cb1a65f6870faa80ca7040ca48 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum <39048939+jnussbaum@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:25:32 +0200 Subject: [PATCH] pave the way for custom group, 3rd round (#150) --- dsp_permissions_scripts/models/group_utils.py | 4 +- tests/test_group_utils.py | 44 +++++++++++-------- tests/test_scope_serialization.py | 25 ++++++----- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/dsp_permissions_scripts/models/group_utils.py b/dsp_permissions_scripts/models/group_utils.py index c203b9e1..7364bc4a 100644 --- a/dsp_permissions_scripts/models/group_utils.py +++ b/dsp_permissions_scripts/models/group_utils.py @@ -26,9 +26,9 @@ def sort_groups(groups_original: Iterable[Group]) -> list[Group]: return groups -def _get_sort_pos_of_custom_group(group: str) -> int: +def _get_sort_pos_of_custom_group(prefixed_iri: str) -> int: alphabet = list("abcdefghijklmnopqrstuvwxyz") - relevant_letter = group.replace("knora-admin:", "")[0] + relevant_letter = prefixed_iri.split(":")[-1][0] return alphabet.index(relevant_letter.lower()) + 99 # must be higher than the highest index of the builtin groups diff --git a/tests/test_group_utils.py b/tests/test_group_utils.py index 9de9a1df..64f57346 100644 --- a/tests/test_group_utils.py +++ b/tests/test_group_utils.py @@ -1,31 +1,37 @@ import pytest -from dsp_permissions_scripts.models import group +from dsp_permissions_scripts.models.group import CREATOR +from dsp_permissions_scripts.models.group import KNOWN_USER +from dsp_permissions_scripts.models.group import PROJECT_ADMIN +from dsp_permissions_scripts.models.group import PROJECT_MEMBER +from dsp_permissions_scripts.models.group import SYSTEM_ADMIN +from dsp_permissions_scripts.models.group import UNKNOWN_USER +from dsp_permissions_scripts.models.group import Group from dsp_permissions_scripts.models.group_utils import sort_groups def test_sort_groups() -> None: groups_original = [ - group.Group(prefixed_iri="knora-admin:C_CustomGroup"), - group.UNKNOWN_USER, - group.PROJECT_ADMIN, - group.PROJECT_MEMBER, - group.CREATOR, - group.Group(prefixed_iri="knora-admin:A_CustomGroup"), - group.Group(prefixed_iri="knora-admin:B_CustomGroup"), - group.KNOWN_USER, - group.SYSTEM_ADMIN, + Group(prefixed_iri="knora-admin:C_CustomGroup"), + UNKNOWN_USER, + PROJECT_ADMIN, + PROJECT_MEMBER, + CREATOR, + Group(prefixed_iri="knora-admin:A_CustomGroup"), + Group(prefixed_iri="knora-admin:B_CustomGroup"), + KNOWN_USER, + SYSTEM_ADMIN, ] groups_expected = [ - group.SYSTEM_ADMIN, - group.CREATOR, - group.PROJECT_ADMIN, - group.PROJECT_MEMBER, - group.KNOWN_USER, - group.UNKNOWN_USER, - group.Group(prefixed_iri="knora-admin:A_CustomGroup"), - group.Group(prefixed_iri="knora-admin:B_CustomGroup"), - group.Group(prefixed_iri="knora-admin:C_CustomGroup"), + SYSTEM_ADMIN, + CREATOR, + PROJECT_ADMIN, + PROJECT_MEMBER, + KNOWN_USER, + UNKNOWN_USER, + Group(prefixed_iri="knora-admin:A_CustomGroup"), + Group(prefixed_iri="knora-admin:B_CustomGroup"), + Group(prefixed_iri="knora-admin:C_CustomGroup"), ] groups_returned = sort_groups(groups_original) assert groups_returned == groups_expected diff --git a/tests/test_scope_serialization.py b/tests/test_scope_serialization.py index 2d236426..a190e26f 100644 --- a/tests/test_scope_serialization.py +++ b/tests/test_scope_serialization.py @@ -1,7 +1,12 @@ import pytest from pytest_unordered import unordered -from dsp_permissions_scripts.models import group +from dsp_permissions_scripts.models.group import CREATOR +from dsp_permissions_scripts.models.group import KNOWN_USER +from dsp_permissions_scripts.models.group import PROJECT_ADMIN +from dsp_permissions_scripts.models.group import PROJECT_MEMBER +from dsp_permissions_scripts.models.group import SYSTEM_ADMIN +from dsp_permissions_scripts.models.group import UNKNOWN_USER from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.utils.scope_serialization import create_admin_route_object_from_scope @@ -48,21 +53,21 @@ class TestScopeSerialization: ) scopes = ( PermissionScope.create( - CR=[group.SYSTEM_ADMIN], + CR=[SYSTEM_ADMIN], ), PermissionScope.create( - D=[group.PROJECT_ADMIN], - RV=[group.PROJECT_MEMBER], + D=[PROJECT_ADMIN], + RV=[PROJECT_MEMBER], ), PermissionScope.create( - M=[group.PROJECT_ADMIN], - V=[group.CREATOR, group.KNOWN_USER], - RV=[group.UNKNOWN_USER], + M=[PROJECT_ADMIN], + V=[CREATOR, KNOWN_USER], + RV=[UNKNOWN_USER], ), PermissionScope.create( - CR=[group.SYSTEM_ADMIN, group.PROJECT_ADMIN], - D=[group.CREATOR], - RV=[group.UNKNOWN_USER], + CR=[SYSTEM_ADMIN, PROJECT_ADMIN], + D=[CREATOR], + RV=[UNKNOWN_USER], ), )