Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 22, 2024
1 parent 2fc07d6 commit f717641
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 102 deletions.
102 changes: 0 additions & 102 deletions tests/test_group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from unittest.mock import Mock
from unittest.mock import patch

import pytest

Expand All @@ -16,11 +15,6 @@
from dsp_permissions_scripts.models.group import CustomGroup
from dsp_permissions_scripts.models.group import group_builder
from dsp_permissions_scripts.models.group import is_prefixed_group_iri
from dsp_permissions_scripts.models.group_utils import _get_full_iri_from_builtin_group
from dsp_permissions_scripts.models.group_utils import _get_full_iri_from_custom_group
from dsp_permissions_scripts.models.group_utils import get_full_iri_from_prefixed_iri
from dsp_permissions_scripts.models.group_utils import get_prefixed_iri_from_full_iri
from dsp_permissions_scripts.models.group_utils import sort_groups
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE

Expand Down Expand Up @@ -54,75 +48,6 @@ def test_is_prefixed_iri() -> None:
is_prefixed_group_iri("ProjectAdmin")


@pytest.mark.parametrize("group_name", NAMES_OF_BUILTIN_GROUPS)
def test_get_prefixed_iri_from_full_iri_builtin(group_name: str) -> None:
returned = get_prefixed_iri_from_full_iri(f"{KNORA_ADMIN_ONTO_NAMESPACE}{group_name}", DspClient("foo"))
assert returned == f"knora-admin:{group_name}"


def test_get_prefixed_iri_from_full_iri_custom(dsp_client_with_2_groups: DspClient) -> None:
new_group_iri = "http://rdfh.ch/groups/083A/_zbeAt-lQDSUNItmBBafvw"
old_group_iri = "http://rdfh.ch/groups/0001/thing-searcher"
new_group_iri_returned = get_prefixed_iri_from_full_iri(new_group_iri, dsp_client_with_2_groups)
old_group_iri_returned = get_prefixed_iri_from_full_iri(old_group_iri, dsp_client_with_2_groups)
assert new_group_iri_returned == "btt:btt-editors"
assert old_group_iri_returned == "anything:Thing searcher"


@pytest.mark.parametrize(
"iri",
["http://www.knora.org/ontology/knora-admin#Thing searcher", "http://www.knora.org/ontology/knora-base#Value"],
)
def test_get_prefixed_iri_from_full_iri_invalid_iri(iri: str) -> None:
with pytest.raises(InvalidIRIError):
get_prefixed_iri_from_full_iri(iri, DspClient("foo"))


def test_get_prefixed_iri_from_full_iri_invalid_group(dsp_client_with_2_groups: DspClient) -> None:
with pytest.raises(InvalidGroupError):
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"))


@patch("dsp_permissions_scripts.models.group_utils._get_full_iri_from_builtin_group")
def test_get_full_iri_from_prefixed_iri_with_builtin_group(patched_func: Mock) -> None:
get_full_iri_from_prefixed_iri("knora-admin:ProjectAdmin", DspClient("foo"))
patched_func.assert_called_once_with("knora-admin", "ProjectAdmin")


@patch("dsp_permissions_scripts.models.group_utils._get_full_iri_from_custom_group")
def test_get_full_iri_from_prefixed_iri_with_custom_group(patched_func: Mock) -> None:
dsp_client = DspClient("foo")
get_full_iri_from_prefixed_iri("limc:groupname", dsp_client)
patched_func.assert_called_once_with("limc", "groupname", dsp_client)


@pytest.mark.parametrize("group_name", NAMES_OF_BUILTIN_GROUPS)
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 Expand Up @@ -180,30 +105,3 @@ def test_group_builder() -> None:
def test_group_builder_invalid(inv: str) -> None:
with pytest.raises(InvalidGroupError):
group_builder(inv)


def test_sort_groups() -> None:
groups_original: list[BuiltinGroup | CustomGroup] = [
CustomGroup(prefixed_iri="shortname:C_CustomGroup"),
UNKNOWN_USER,
PROJECT_ADMIN,
PROJECT_MEMBER,
CREATOR,
CustomGroup(prefixed_iri="shortname:A_CustomGroup"),
CustomGroup(prefixed_iri="shortname:B_CustomGroup"),
KNOWN_USER,
SYSTEM_ADMIN,
]
groups_expected: list[BuiltinGroup | CustomGroup] = [
SYSTEM_ADMIN,
CREATOR,
PROJECT_ADMIN,
PROJECT_MEMBER,
KNOWN_USER,
UNKNOWN_USER,
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
141 changes: 141 additions & 0 deletions tests/test_group_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
from unittest.mock import Mock
from unittest.mock import patch

import pytest

from dsp_permissions_scripts.models.errors import InvalidGroupError
from dsp_permissions_scripts.models.errors import InvalidIRIError
from dsp_permissions_scripts.models.group import CREATOR
from dsp_permissions_scripts.models.group import KNOWN_USER
from dsp_permissions_scripts.models.group import NAMES_OF_BUILTIN_GROUPS
from dsp_permissions_scripts.models.group import PROJECT_ADMIN
from dsp_permissions_scripts.models.group import PROJECT_MEMBER
from dsp_permissions_scripts.models.group import SYSTEM_ADMIN
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_utils import _get_full_iri_from_builtin_group
from dsp_permissions_scripts.models.group_utils import _get_full_iri_from_custom_group
from dsp_permissions_scripts.models.group_utils import get_full_iri_from_prefixed_iri
from dsp_permissions_scripts.models.group_utils import get_prefixed_iri_from_full_iri
from dsp_permissions_scripts.models.group_utils import sort_groups
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE


@pytest.fixture
def new_custom_group_iri() -> str:
return "http://rdfh.ch/groups/083A/_zbeAt-lQDSUNItmBBafvw"


@pytest.fixture
def old_custom_group_iri() -> str:
return "http://rdfh.ch/groups/0001/thing-searcher"


@pytest.fixture
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"}},
{"id": old_custom_group_iri, "name": "Thing searcher", "project": {"shortname": "anything"}},
]
}
dsp_client = Mock(spec=DspClient, get=Mock(return_value=get_response))
return dsp_client


@pytest.mark.parametrize("group_name", NAMES_OF_BUILTIN_GROUPS)
def test_get_prefixed_iri_from_full_iri_builtin(group_name: str) -> None:
returned = get_prefixed_iri_from_full_iri(f"{KNORA_ADMIN_ONTO_NAMESPACE}{group_name}", DspClient("foo"))
assert returned == f"knora-admin:{group_name}"


def test_get_prefixed_iri_from_full_iri_custom(dsp_client_with_2_groups: DspClient) -> None:
new_group_iri = "http://rdfh.ch/groups/083A/_zbeAt-lQDSUNItmBBafvw"
old_group_iri = "http://rdfh.ch/groups/0001/thing-searcher"
new_group_iri_returned = get_prefixed_iri_from_full_iri(new_group_iri, dsp_client_with_2_groups)
old_group_iri_returned = get_prefixed_iri_from_full_iri(old_group_iri, dsp_client_with_2_groups)
assert new_group_iri_returned == "btt:btt-editors"
assert old_group_iri_returned == "anything:Thing searcher"


@pytest.mark.parametrize(
"iri",
["http://www.knora.org/ontology/knora-admin#Thing searcher", "http://www.knora.org/ontology/knora-base#Value"],
)
def test_get_prefixed_iri_from_full_iri_invalid_iri(iri: str) -> None:
with pytest.raises(InvalidIRIError):
get_prefixed_iri_from_full_iri(iri, DspClient("foo"))


def test_get_prefixed_iri_from_full_iri_invalid_group(dsp_client_with_2_groups: DspClient) -> None:
with pytest.raises(InvalidGroupError):
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"))


@patch("dsp_permissions_scripts.models.group_utils._get_full_iri_from_builtin_group")
def test_get_full_iri_from_prefixed_iri_with_builtin_group(patched_func: Mock) -> None:
get_full_iri_from_prefixed_iri("knora-admin:ProjectAdmin", DspClient("foo"))
patched_func.assert_called_once_with("knora-admin", "ProjectAdmin")


@patch("dsp_permissions_scripts.models.group_utils._get_full_iri_from_custom_group")
def test_get_full_iri_from_prefixed_iri_with_custom_group(patched_func: Mock) -> None:
dsp_client = DspClient("foo")
get_full_iri_from_prefixed_iri("limc:groupname", dsp_client)
patched_func.assert_called_once_with("limc", "groupname", dsp_client)


@pytest.mark.parametrize("group_name", NAMES_OF_BUILTIN_GROUPS)
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)


def test_sort_groups() -> None:
groups_original: list[BuiltinGroup | CustomGroup] = [
CustomGroup(prefixed_iri="shortname:C_CustomGroup"),
UNKNOWN_USER,
PROJECT_ADMIN,
PROJECT_MEMBER,
CREATOR,
CustomGroup(prefixed_iri="shortname:A_CustomGroup"),
CustomGroup(prefixed_iri="shortname:B_CustomGroup"),
KNOWN_USER,
SYSTEM_ADMIN,
]
groups_expected: list[BuiltinGroup | CustomGroup] = [
SYSTEM_ADMIN,
CREATOR,
PROJECT_ADMIN,
PROJECT_MEMBER,
KNOWN_USER,
UNKNOWN_USER,
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 f717641

Please sign in to comment.