-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: validate PermissionScope model (DEV-2784) (#31)
- Loading branch information
Showing
9 changed files
with
206 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from dsp_permissions_scripts.models.groups import BuiltinGroup | ||
|
||
|
||
def dereference_prefix( | ||
identifier: str, | ||
context: dict[str, str], | ||
) -> str: | ||
prefix, actual_id = identifier.split(":") | ||
return context[prefix] + actual_id | ||
|
||
|
||
def _get_sort_pos_of_custom_group(group: str) -> int: | ||
alphabet = list("abcdefghijklmnopqrstuvwxyz") | ||
relevant_letter = group.replace("http://www.knora.org/ontology/knora-admin#", "")[0] | ||
return alphabet.index(relevant_letter.lower()) + 99 # must be higher than the highest index of the builtin groups | ||
|
||
|
||
def sort_groups(groups_original: list[str]) -> list[str]: | ||
"""Sorts groups, first according to their power (most powerful first), then alphabetically.""" | ||
sort_key = list(reversed([x.value for x in BuiltinGroup])) | ||
groups = groups_original.copy() | ||
groups.sort(key=lambda x: sort_key.index(x) if x in sort_key else _get_sort_pos_of_custom_group(x)) | ||
return groups |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import unittest | ||
|
||
from dsp_permissions_scripts.models.groups import BuiltinGroup | ||
from dsp_permissions_scripts.utils.helpers import sort_groups | ||
|
||
|
||
class TestHelpers(unittest.TestCase): | ||
|
||
def test_sort_groups(self) -> None: | ||
groups_original = [ | ||
"http://www.knora.org/ontology/knora-admin#C_CustomGroup", | ||
BuiltinGroup.UNKNOWN_USER.value, | ||
BuiltinGroup.PROJECT_ADMIN.value, | ||
BuiltinGroup.PROJECT_MEMBER.value, | ||
BuiltinGroup.CREATOR.value, | ||
"http://www.knora.org/ontology/knora-admin#A_CustomGroup", | ||
"http://www.knora.org/ontology/knora-admin#B_CustomGroup", | ||
BuiltinGroup.KNOWN_USER.value, | ||
BuiltinGroup.SYSTEM_ADMIN.value, | ||
] | ||
groups_expected = [ | ||
BuiltinGroup.SYSTEM_ADMIN.value, | ||
BuiltinGroup.CREATOR.value, | ||
BuiltinGroup.PROJECT_ADMIN.value, | ||
BuiltinGroup.PROJECT_MEMBER.value, | ||
BuiltinGroup.KNOWN_USER.value, | ||
BuiltinGroup.UNKNOWN_USER.value, | ||
"http://www.knora.org/ontology/knora-admin#A_CustomGroup", | ||
"http://www.knora.org/ontology/knora-admin#B_CustomGroup", | ||
"http://www.knora.org/ontology/knora-admin#C_CustomGroup", | ||
] | ||
groups_returned = sort_groups(groups_original=groups_original) | ||
self.assertEqual(groups_returned, groups_expected) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import unittest | ||
|
||
from dsp_permissions_scripts.models.groups import BuiltinGroup | ||
from dsp_permissions_scripts.models.scope import PermissionScope | ||
|
||
|
||
class TestScope(unittest.TestCase): | ||
|
||
def test_scope_validation_on_creation(self) -> None: | ||
with self.assertRaisesRegex(ValueError, "must not occur in more than one field"): | ||
PermissionScope.create( | ||
CR={BuiltinGroup.PROJECT_ADMIN}, | ||
D={BuiltinGroup.PROJECT_ADMIN}, | ||
V={BuiltinGroup.UNKNOWN_USER, BuiltinGroup.KNOWN_USER}, | ||
) | ||
|
||
def test_scope_validation_on_add_to_same_permission(self) -> None: | ||
scope = PermissionScope.create( | ||
CR={BuiltinGroup.PROJECT_ADMIN}, | ||
V={BuiltinGroup.UNKNOWN_USER, BuiltinGroup.KNOWN_USER}, | ||
) | ||
with self.assertRaisesRegex( | ||
ValueError, | ||
"Group 'http://www.knora.org/ontology/knora-admin#ProjectAdmin' is already in permission 'CR'" | ||
): | ||
_ = scope.add("CR", BuiltinGroup.PROJECT_ADMIN) | ||
|
||
def test_scope_validation_on_add_to_different_permission(self) -> None: | ||
scope = PermissionScope.create( | ||
CR={BuiltinGroup.PROJECT_ADMIN}, | ||
V={BuiltinGroup.UNKNOWN_USER, BuiltinGroup.KNOWN_USER}, | ||
) | ||
with self.assertRaisesRegex(ValueError, "must not occur in more than one field"): | ||
_ = scope.add("RV", BuiltinGroup.PROJECT_ADMIN) | ||
|
||
def test_add_to_scope(self) -> None: | ||
scope = PermissionScope.create( | ||
D={BuiltinGroup.SYSTEM_ADMIN}, | ||
M={BuiltinGroup.PROJECT_MEMBER, BuiltinGroup.KNOWN_USER}, | ||
) | ||
scope_added = scope.add("CR", BuiltinGroup.PROJECT_ADMIN) | ||
self.assertEqual( | ||
scope_added.model_dump_json(), | ||
PermissionScope.create( | ||
CR={BuiltinGroup.PROJECT_ADMIN}, | ||
D={BuiltinGroup.SYSTEM_ADMIN}, | ||
M={BuiltinGroup.PROJECT_MEMBER, BuiltinGroup.KNOWN_USER}, | ||
).model_dump_json(), | ||
) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters