Skip to content

Commit

Permalink
write compare_scopes() function
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 11, 2023
1 parent 85792b6 commit 7ac675b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
17 changes: 9 additions & 8 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dsp_permissions_scripts.models.groups import BuiltinGroup
from dsp_permissions_scripts.models.scope import PermissionScope
from tests.test_scope_serialization import compare_scopes


class TestScope(unittest.TestCase):
Expand Down Expand Up @@ -39,13 +40,13 @@ def test_add_to_scope(self) -> None:
M={BuiltinGroup.PROJECT_MEMBER, BuiltinGroup.KNOWN_USER},
)
scope_added = scope.add("CR", BuiltinGroup.PROJECT_ADMIN)
self.assertEqual(
scope_added.model_dump_json(),
PermissionScope.create(
compare_scopes(
scope1=scope_added,
scope2=PermissionScope.create(
CR={BuiltinGroup.PROJECT_ADMIN},
D={BuiltinGroup.SYSTEM_ADMIN},
M={BuiltinGroup.PROJECT_MEMBER, BuiltinGroup.KNOWN_USER},
).model_dump_json(),
),
)

def test_remove_inexisting_group(self) -> None:
Expand All @@ -71,12 +72,12 @@ def test_remove_from_scope(self) -> None:
M={BuiltinGroup.PROJECT_MEMBER, BuiltinGroup.KNOWN_USER},
)
scope_removed = scope.remove("CR", BuiltinGroup.PROJECT_ADMIN)
self.assertEqual(
scope_removed.model_dump_json(),
PermissionScope.create(
compare_scopes(
scope1=scope_removed,
scope2=PermissionScope.create(
D={BuiltinGroup.SYSTEM_ADMIN},
M={BuiltinGroup.PROJECT_MEMBER, BuiltinGroup.KNOWN_USER},
).model_dump_json(),
),
)

if __name__ == "__main__":
Expand Down
32 changes: 18 additions & 14 deletions tests/test_scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
)


def compare_scopes(
scope1: PermissionScope,
scope2: PermissionScope,
msg: str | None = None,
) -> None:
scope1_dict = json.loads(scope1.model_dump_json())
scope1_dict = {k: sorted(v) for k, v in scope1_dict.items()}
scope2_dict = json.loads(scope2.model_dump_json())
scope2_dict = {k: sorted(v) for k, v in scope2_dict.items()}
unittest.TestCase().assertDictEqual(scope1_dict, scope2_dict, msg=msg)


class TestScopeSerialization(unittest.TestCase):
perm_strings = [
"CR knora-admin:SystemAdmin|V knora-admin:CustomGroup",
Expand Down Expand Up @@ -64,25 +76,17 @@ class TestScopeSerialization(unittest.TestCase):

def test_create_scope_from_string(self) -> None:
for perm_string, scope in zip(self.perm_strings, self.scopes):
returned = json.loads(create_scope_from_string(perm_string).model_dump_json())
returned = {k: sorted(v) for k, v in returned.items()}
expected = json.loads(scope.model_dump_json())
expected = {k: sorted(v) for k, v in expected.items()}
self.assertDictEqual(
returned,
expected,
compare_scopes(
scope1=create_scope_from_string(perm_string),
scope2=scope,
msg=f"Failed with permission string '{perm_string}'",
)

def test_create_scope_from_admin_route_object(self) -> None:
for admin_route_object, scope, index in zip(self.admin_route_objects, self.scopes, range(len(self.scopes))):
returned = json.loads(create_scope_from_admin_route_object(admin_route_object).model_dump_json())
returned = {k: sorted(v) for k, v in returned.items()}
expected = json.loads(scope.model_dump_json())
expected = {k: sorted(v) for k, v in expected.items()}
self.assertDictEqual(
returned,
expected,
compare_scopes(
scope1=create_scope_from_admin_route_object(admin_route_object),
scope2=scope,
msg=f"Failed with admin group object no. {index}",
)

Expand Down

0 comments on commit 7ac675b

Please sign in to comment.