From eeefa70c7456167b04dc2e37f3711acb1f0c86a5 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Thu, 28 Mar 2024 09:30:08 +0100 Subject: [PATCH] fix tests --- dsp_permissions_scripts/models/scope.py | 4 ++++ .../utils/scope_serialization.py | 13 ++++++++----- tests/test_scope.py | 2 +- tests/test_scope_serialization.py | 4 ++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/dsp_permissions_scripts/models/scope.py b/dsp_permissions_scripts/models/scope.py index d6bdde8d..fa803c94 100644 --- a/dsp_permissions_scripts/models/scope.py +++ b/dsp_permissions_scripts/models/scope.py @@ -34,6 +34,10 @@ def create( V=frozenset(V), RV=frozenset(RV), ) + + @staticmethod + def from_dict(d: dict[str, list[str]]) -> PermissionScope: + return PermissionScope.model_validate({k: [Group(val=v) for v in vs] for k, vs in d.items()}) @model_validator(mode="after") def check_group_occurs_only_once(self) -> PermissionScope: diff --git a/dsp_permissions_scripts/utils/scope_serialization.py b/dsp_permissions_scripts/utils/scope_serialization.py index 91d39298..9c011e50 100644 --- a/dsp_permissions_scripts/utils/scope_serialization.py +++ b/dsp_permissions_scripts/utils/scope_serialization.py @@ -1,5 +1,6 @@ -from typing import Any +from typing import Any, cast +from dsp_permissions_scripts.models.group import Group from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.utils.helpers import sort_groups @@ -8,8 +9,9 @@ def create_string_from_scope(perm_scope: PermissionScope) -> str: """Serializes a permission scope to a permissions string as used by /v2 routes.""" as_dict = {} for perm_letter, groups in perm_scope.model_dump(mode="json").items(): + groups = cast(list[dict[str, str]], groups) if groups: - as_dict[perm_letter] = sort_groups(groups) + as_dict[perm_letter] = sort_groups([Group(val=g) for _dict in groups for g in _dict.values()]) strs = [f"{k} {','.join([x.val for x in l])}" for k, l in as_dict.items()] return "|".join(strs) @@ -23,7 +25,7 @@ def create_scope_from_string(permission_string: str) -> PermissionScope: groups = groups_as_str.split(",") groups = [g.replace("knora-admin:", "http://www.knora.org/ontology/knora-admin#") for g in groups] kwargs[perm_letter] = groups - return PermissionScope(**kwargs) # type: ignore[arg-type] + return PermissionScope.from_dict(kwargs) def create_scope_from_admin_route_object(admin_route_object: list[dict[str, Any]]) -> PermissionScope: @@ -37,17 +39,18 @@ def create_scope_from_admin_route_object(admin_route_object: list[dict[str, Any] kwargs[attr_name].append(group) else: kwargs[attr_name] = [group] - return PermissionScope(**kwargs) # type: ignore[arg-type] + return PermissionScope.from_dict(kwargs) def create_admin_route_object_from_scope(perm_scope: PermissionScope) -> list[dict[str, str | None]]: """Serializes a permission scope to an object that can be used for requests to /admin/permissions routes.""" scope_elements: list[dict[str, str | None]] = [] for perm_letter, groups in perm_scope.model_dump(mode="json").items(): + groups = cast(list[dict[str, str]], groups) for group in groups: scope_elements.append( { - "additionalInformation": group, + "additionalInformation": group["val"], "name": perm_letter, "permissionCode": None, } diff --git a/tests/test_scope.py b/tests/test_scope.py index c5e69c03..270776c2 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -20,7 +20,7 @@ def test_scope_validation_on_add_to_same_permission(self) -> None: V={group.UNKNOWN_USER, group.KNOWN_USER}, ) with self.assertRaisesRegex( - ValueError, "Group 'http://www.knora.org/ontology/knora-admin#ProjectAdmin' is already in permission 'CR'" + ValueError, "Group 'val='http://www.knora.org/ontology/knora-admin#ProjectAdmin'' is already in permission 'CR'" ): _ = scope.add("CR", group.PROJECT_ADMIN) diff --git a/tests/test_scope_serialization.py b/tests/test_scope_serialization.py index 3bb01704..6a688b33 100644 --- a/tests/test_scope_serialization.py +++ b/tests/test_scope_serialization.py @@ -17,9 +17,9 @@ def compare_scopes( msg: str | None = None, ) -> None: scope1_dict = scope1.model_dump(mode="json") - scope1_dict = {k: sorted(v) for k, v in scope1_dict.items()} + scope1_dict = {k: sorted(v, key=lambda x: x["val"]) for k, v in scope1_dict.items()} scope2_dict = scope2.model_dump(mode="json") - scope2_dict = {k: sorted(v) for k, v in scope2_dict.items()} + scope2_dict = {k: sorted(v, key=lambda x: x["val"]) for k, v in scope2_dict.items()} unittest.TestCase().assertDictEqual(scope1_dict, scope2_dict, msg=msg)