diff --git a/dsp_permissions_scripts/utils/permissions.py b/dsp_permissions_scripts/utils/permissions.py index 07d46117..ca6d79df 100644 --- a/dsp_permissions_scripts/utils/permissions.py +++ b/dsp_permissions_scripts/utils/permissions.py @@ -77,7 +77,7 @@ def set_doaps_of_groups( print("Old DOAP:\n=========") print(d.model_dump_json(indent=2)) new_doap = update_doap_scope( - permission_iri=d.iri, + doap_iri=d.iri, scope=scope, host=host, token=token, @@ -214,7 +214,7 @@ def get_permissions_for_project( def update_doap_scope( - permission_iri: str, + doap_iri: str, scope: PermissionScope, host: str, token: str, @@ -222,7 +222,7 @@ def update_doap_scope( """ Updates the scope of the given DOAP. """ - iri = quote_plus(permission_iri, safe="") + iri = quote_plus(doap_iri, safe="") headers = {"Authorization": f"Bearer {token}"} protocol = get_protocol(host) url = f"{protocol}://{host}/admin/permissions/{iri}/hasPermissions" diff --git a/dsp_permissions_scripts/utils/scope_serialization.py b/dsp_permissions_scripts/utils/scope_serialization.py index 8eca0588..4ed92f5e 100644 --- a/dsp_permissions_scripts/utils/scope_serialization.py +++ b/dsp_permissions_scripts/utils/scope_serialization.py @@ -10,9 +10,7 @@ def create_string_from_scope(perm_scope: PermissionScope) -> str: for perm_letter, groups in perm_scope.model_dump().items(): if groups: groups_as_str = [g.value if isinstance(g, BuiltinGroup) else g for g in groups] - as_dict[perm_letter] = [ - g.replace("http://www.knora.org/ontology/knora-admin#", "knora-admin:") for g in groups_as_str - ] + as_dict[perm_letter] = groups_as_str strs = [f"{k} {','.join(l)}" for k, l in as_dict.items()] return "|".join(strs) @@ -47,9 +45,6 @@ def create_admin_route_object_from_scope(perm_scope: PermissionScope) -> list[di for perm_letter, groups in perm_scope.model_dump().items(): if groups: groups_as_str = [g.value if isinstance(g, BuiltinGroup) else g for g in groups] - groups_as_str = [ - g.replace("http://www.knora.org/ontology/knora-admin#", "knora-admin:") for g in groups_as_str - ] for group in groups_as_str: scope_elements.append( { diff --git a/tests/test_scope_serialization.py b/tests/test_scope_serialization.py index bcc99305..920949aa 100644 --- a/tests/test_scope_serialization.py +++ b/tests/test_scope_serialization.py @@ -1,3 +1,4 @@ +from typing import Any import unittest from dsp_permissions_scripts.models.groups import BuiltinGroup @@ -78,20 +79,31 @@ def test_create_scope_from_admin_route_object(self) -> None: def test_create_string_from_scope(self) -> None: for perm_string, scope in zip(self.perm_strings, self.scopes): + perm_string_full = self._resolve_prefixes_of_perm_string(perm_string) self.assertEqual( create_string_from_scope(scope), - perm_string, + perm_string_full, msg=f"Failed with permission string '{perm_string}'", ) def test_create_admin_route_object_from_scope(self) -> None: for admin_route_object, scope, index in zip(self.admin_route_objects, self.scopes, range(len(self.scopes))): + admin_route_object_full = self._resolve_prefixes_of_admin_route_object(admin_route_object) self.assertEqual( create_admin_route_object_from_scope(scope), - admin_route_object, + admin_route_object_full, msg=f"Failed with admin group object no. {index}", ) + def _resolve_prefixes_of_admin_route_object(self, admin_route_object: list[dict[str, Any]]) -> list[dict[str, Any]]: + for obj in admin_route_object: + obj["additionalInformation"] = obj["additionalInformation"].replace( + "knora-admin:", "http://www.knora.org/ontology/knora-admin#" + ) + return admin_route_object + + def _resolve_prefixes_of_perm_string(self, perm_string: str) -> str: + return perm_string.replace("knora-admin:", "http://www.knora.org/ontology/knora-admin#") if __name__ == "__main__": unittest.main()