Skip to content

Commit

Permalink
Merge branch 'main' into wip/scenario-tanner
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Oct 2, 2023
2 parents fb5d7f1 + 295f06c commit 62706cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions dsp_permissions_scripts/utils/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -214,15 +214,15 @@ def get_permissions_for_project(


def update_doap_scope(
permission_iri: str,
doap_iri: str,
scope: PermissionScope,
host: str,
token: str,
) -> Doap:
"""
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"
Expand Down
7 changes: 1 addition & 6 deletions dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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(
{
Expand Down
16 changes: 14 additions & 2 deletions tests/test_scope_serialization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
import unittest

from dsp_permissions_scripts.models.groups import BuiltinGroup
Expand Down Expand Up @@ -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()

0 comments on commit 62706cb

Please sign in to comment.