Skip to content

Commit

Permalink
test_group_builder
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 22, 2024
1 parent 5808717 commit 14d6c18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CustomGroup(BaseModel):

@model_validator(mode="after")
def _check_regex(self) -> Self:
if not re.search(PREFIXED_IRI_REGEX, self.prefixed_iri):
if not is_prefixed_group_iri(self.prefixed_iri):
raise InvalidGroupError(f"{self.prefixed_iri} is not a valid group IRI")
if self.prefixed_iri.startswith(("knora-admin:", "knora-base:", "knora-api:")):
raise InvalidGroupError(f"{self.prefixed_iri} is not a custom group")
Expand Down
27 changes: 18 additions & 9 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,40 @@ def test_custom_group_invalid(group_name: str) -> None:


def test_group_builder() -> None:
pass
assert group_builder("knora-admin:UnknownUser") == UNKNOWN_USER
assert group_builder("limc:groupname") == CustomGroup(prefixed_iri="limc:groupname")


@pytest.mark.parametrize(
"inv", ["knora-api:foo", "knora-base:foo", "knora-admin:foo", f"{KNORA_ADMIN_ONTO_NAMESPACE}foo"]
)
def test_group_builder_invalid(inv: str) -> None:
with pytest.raises(InvalidGroupError):
group_builder(inv)


def test_sort_groups() -> None:
groups_original = [
group_builder("shortname:C_CustomGroup"),
groups_original: list[BuiltinGroup | CustomGroup] = [
CustomGroup(prefixed_iri="shortname:C_CustomGroup"),
UNKNOWN_USER,
PROJECT_ADMIN,
PROJECT_MEMBER,
CREATOR,
group_builder("shortname:A_CustomGroup"),
group_builder("shortname:B_CustomGroup"),
CustomGroup(prefixed_iri="shortname:A_CustomGroup"),
CustomGroup(prefixed_iri="shortname:B_CustomGroup"),
KNOWN_USER,
SYSTEM_ADMIN,
]
groups_expected = [
groups_expected: list[BuiltinGroup | CustomGroup] = [
SYSTEM_ADMIN,
CREATOR,
PROJECT_ADMIN,
PROJECT_MEMBER,
KNOWN_USER,
UNKNOWN_USER,
group_builder("shortname:A_CustomGroup"),
group_builder("shortname:B_CustomGroup"),
group_builder("shortname:C_CustomGroup"),
CustomGroup(prefixed_iri="shortname:A_CustomGroup"),
CustomGroup(prefixed_iri="shortname:B_CustomGroup"),
CustomGroup(prefixed_iri="shortname:C_CustomGroup"),
]
groups_returned = sort_groups(groups_original)
assert groups_returned == groups_expected

0 comments on commit 14d6c18

Please sign in to comment.