Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Mar 28, 2024
1 parent ed18904 commit eeefa70
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 4 additions & 0 deletions dsp_permissions_scripts/models/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 8 additions & 5 deletions dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

Expand All @@ -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:
Expand All @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit eeefa70

Please sign in to comment.