Skip to content

Commit

Permalink
split up get_full_iri_from_prefixed_iri() + write tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 22, 2024
1 parent 414170c commit 5808717
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
29 changes: 22 additions & 7 deletions dsp_permissions_scripts/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@


def is_prefixed_group_iri(iri: str) -> bool:
if iri.startswith((KNORA_ADMIN_ONTO_NAMESPACE, "http://rdfh.ch/groups/")):
if iri.startswith((KNORA_ADMIN_ONTO_NAMESPACE, "http://rdfh.ch/groups/", "knora-base:", "knora-api:")):
return False
elif iri.startswith("knora-admin:") and not iri.endswith(tuple(NAMES_OF_BUILTIN_GROUPS)):
return False
elif re.search(PREFIXED_IRI_REGEX, iri):
return True
Expand All @@ -48,15 +50,28 @@ def get_prefixed_iri_from_full_iri(full_iri: str, dsp_client: DspClient) -> str:


def get_full_iri_from_prefixed_iri(prefixed_iri: str, dsp_client: DspClient) -> str:
shortname, groupname = prefixed_iri.split(":")
if shortname == "knora-admin":
return prefixed_iri.replace("knora-admin:", KNORA_ADMIN_ONTO_NAMESPACE)
if not is_prefixed_group_iri(prefixed_iri):
raise InvalidIRIError(f"{prefixed_iri} is not a valid prefixed group IRI")
prefix, groupname = prefixed_iri.split(":")
if prefix == "knora-admin":
return _get_full_iri_from_builtin_group(prefix, groupname)
else:
return _get_full_iri_from_custom_group(prefix, groupname, dsp_client)


def _get_full_iri_from_builtin_group(prefix: str, groupname: str) -> str:
if groupname not in NAMES_OF_BUILTIN_GROUPS:
raise InvalidGroupError(f"{prefix}:{groupname} is not a valid builtin group")
return f"{KNORA_ADMIN_ONTO_NAMESPACE}{groupname}"


def _get_full_iri_from_custom_group(prefix: str, groupname: str, dsp_client: DspClient) -> str:
all_groups = dsp_client.get("/admin/groups")["groups"]
proj_groups = [grp for grp in all_groups if grp["project"]["shortname"] == shortname]
proj_groups = [grp for grp in all_groups if grp["project"]["shortname"] == prefix]
if not (group := [grp for grp in proj_groups if grp["name"] == groupname]):
raise InvalidGroupError(
f"{prefixed_iri} is not a valid group. "
f"Available groups: {', '.join([grp['name'] for grp in proj_groups])}"
f"{prefix}:{groupname} is not a valid group. "
f"Available groups for the project {prefix}: {', '.join([grp['name'] for grp in proj_groups])}"
)
full_iri: str = group[0]["id"]
return full_iri
Expand Down
52 changes: 44 additions & 8 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import pytest

import dsp_permissions_scripts
import dsp_permissions_scripts.models
from dsp_permissions_scripts.models.errors import InvalidGroupError
from dsp_permissions_scripts.models.errors import InvalidIRIError
from dsp_permissions_scripts.models.group import CREATOR
Expand All @@ -14,6 +16,8 @@
from dsp_permissions_scripts.models.group import UNKNOWN_USER
from dsp_permissions_scripts.models.group import BuiltinGroup
from dsp_permissions_scripts.models.group import CustomGroup
from dsp_permissions_scripts.models.group import _get_full_iri_from_builtin_group
from dsp_permissions_scripts.models.group import _get_full_iri_from_custom_group
from dsp_permissions_scripts.models.group import get_full_iri_from_prefixed_iri
from dsp_permissions_scripts.models.group import get_prefixed_iri_from_full_iri
from dsp_permissions_scripts.models.group import group_builder
Expand All @@ -36,12 +40,8 @@ def old_custom_group_iri() -> str:
def dsp_client_with_2_groups(new_custom_group_iri: str, old_custom_group_iri: str) -> DspClient:
get_response = {
"groups": [
{"id": new_custom_group_iri, "name": "btt-editors", "project": {"shortname": "btt", "shortcode": "083A"}},
{
"id": old_custom_group_iri,
"name": "Thing searcher",
"project": {"shortname": "anything", "shortcode": "0001"},
},
{"id": new_custom_group_iri, "name": "btt-editors", "project": {"shortname": "btt"}},
{"id": old_custom_group_iri, "name": "Thing searcher", "project": {"shortname": "anything"}},
]
}
dsp_client = Mock(spec=DspClient)
Expand Down Expand Up @@ -85,12 +85,48 @@ def test_get_prefixed_iri_from_full_iri_invalid_group(dsp_client_with_2_groups:
get_prefixed_iri_from_full_iri("http://rdfh.ch/groups/0123/8-5f7B639_79ef6a043baW", dsp_client_with_2_groups)


@pytest.mark.parametrize("iri", ["knora-base:Value", f"{KNORA_ADMIN_ONTO_NAMESPACE}groupname"])
def test_get_full_iri_from_prefixed_iri_invalid(iri: str) -> None:
with pytest.raises(InvalidIRIError):
get_full_iri_from_prefixed_iri(iri, DspClient("foo"))


def test_get_full_iri_from_prefixed_iri_with_builtin_group() -> None:
mock = Mock()
dsp_permissions_scripts.models.group._get_full_iri_from_builtin_group = mock
get_full_iri_from_prefixed_iri("knora-admin:ProjectAdmin", DspClient("foo"))
mock.assert_called_once_with("knora-admin", "ProjectAdmin")


def test_get_full_iri_from_prefixed_iri_with_custom_group() -> None:
mock = Mock()
dsp_client = DspClient("foo")
dsp_permissions_scripts.models.group._get_full_iri_from_custom_group = mock
get_full_iri_from_prefixed_iri("limc:groupname", dsp_client)
mock.assert_called_once_with("limc", "groupname", dsp_client)


@pytest.mark.parametrize("group_name", NAMES_OF_BUILTIN_GROUPS)
def test_get_full_iri_from_prefixed_iri(group_name: str) -> None:
res = get_full_iri_from_prefixed_iri(f"knora-admin:{group_name}", DspClient("foo"))
def test_get_full_iri_from_builtin_group(group_name: str) -> None:
res = _get_full_iri_from_builtin_group("knora-admin", group_name)
assert res == f"{KNORA_ADMIN_ONTO_NAMESPACE}{group_name}"


def test_get_full_iri_from_builtin_group_invalid() -> None:
with pytest.raises(InvalidGroupError):
_get_full_iri_from_builtin_group("knora-admin", "NonExistent")


def test_get_full_iri_from_custom_group(dsp_client_with_2_groups: DspClient, new_custom_group_iri: str) -> None:
res = _get_full_iri_from_custom_group("btt", "btt-editors", dsp_client_with_2_groups)
assert res == new_custom_group_iri


def test_get_full_iri_from_custom_group_invalid(dsp_client_with_2_groups: DspClient) -> None:
with pytest.raises(InvalidGroupError):
_get_full_iri_from_custom_group("limc", "limc-editors", dsp_client_with_2_groups)


@pytest.mark.parametrize("group_name", NAMES_OF_BUILTIN_GROUPS)
def test_builtin_group_from_prefixed_iri(group_name: str) -> None:
builtin_group = BuiltinGroup(prefixed_iri=f"knora-admin:{group_name}")
Expand Down

0 comments on commit 5808717

Please sign in to comment.