Skip to content

Commit

Permalink
test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Aug 9, 2024
1 parent 787383d commit 5a4e668
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 107 deletions.
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/oap/oap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@type",
"@context",
"rdfs:label",
"knora-api:DeletedValue",
# "knora-api:DeletedValue",
"knora-api:lastModificationDate",
"knora-api:creationDate",
"knora-api:arkUrl",
Expand Down
114 changes: 8 additions & 106 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import copy

from dsp_permissions_scripts.ap.ap_delete import delete_ap_of_group_on_server
from dsp_permissions_scripts.ap.ap_get import get_aps_of_project
from dsp_permissions_scripts.ap.ap_model import Ap
from dsp_permissions_scripts.ap.ap_model import ApValue
from dsp_permissions_scripts.ap.ap_serialize import serialize_aps_of_project
from dsp_permissions_scripts.ap.ap_set import apply_updated_aps_on_server
from dsp_permissions_scripts.doap.doap_get import get_doaps_of_project
from dsp_permissions_scripts.doap.doap_model import Doap
from dsp_permissions_scripts.doap.doap_serialize import serialize_doaps_of_project
from dsp_permissions_scripts.doap.doap_set import apply_updated_doaps_on_server
from dsp_permissions_scripts.models import group
from dsp_permissions_scripts.models.host import Hosts
from dsp_permissions_scripts.models.scope import PUBLIC
from dsp_permissions_scripts.oap.oap_get import get_all_oaps_of_project
Expand All @@ -28,93 +17,18 @@
logger = get_logger(__name__)


def modify_aps(aps: list[Ap]) -> list[Ap]:
"""Adapt this sample to your needs."""
modified_aps = []
for ap in copy.deepcopy(aps):
if ap.forGroup == group.UNKNOWN_USER:
if ApValue.ProjectAdminGroupAllPermission not in ap.hasPermissions:
ap.add_permission(ApValue.ProjectAdminGroupAllPermission)
modified_aps.append(ap)
return modified_aps


def modify_doaps(doaps: list[Doap]) -> list[Doap]:
"""Adapt this sample to your needs."""
modified_doaps = []
for doap in copy.deepcopy(doaps):
if doap.target.group == group.UNKNOWN_USER:
doap.scope = PUBLIC
modified_doaps.append(doap)
return modified_doaps


def modify_oaps(oaps: list[Oap]) -> list[ResourceOap | ValueOap]:
"""Adapt this sample to your needs."""
modified_oaps: list[ResourceOap | ValueOap] = []
for oap in copy.deepcopy(oaps):
if group.SYSTEM_ADMIN not in oap.resource_oap.scope.CR:
oap.resource_oap.scope = oap.resource_oap.scope.add("CR", group.SYSTEM_ADMIN)
modified_oaps.append(oap.resource_oap)
oap.resource_oap.scope = PUBLIC
modified_oaps.append(oap.resource_oap)
for value_oap in oap.value_oaps:
if group.SYSTEM_ADMIN not in value_oap.scope.CR:
value_oap.scope = value_oap.scope.add("CR", group.SYSTEM_ADMIN)
modified_oaps.append(value_oap)
value_oap.scope = PUBLIC
modified_oaps.append(value_oap)
return modified_oaps


def update_aps(host: str, shortcode: str, dsp_client: DspClient) -> None:
"""Sample function to modify the Administrative Permissions of a project."""
project_aps = get_aps_of_project(shortcode, dsp_client)
serialize_aps_of_project(
project_aps=project_aps,
shortcode=shortcode,
mode="original",
host=host,
)
remaining_aps = delete_ap_of_group_on_server(
host=host,
existing_aps=project_aps,
forGroup=group.UNKNOWN_USER,
dsp_client=dsp_client,
)
modified_aps = modify_aps(remaining_aps)
if not modified_aps:
logger.info("There are no APs to update.")
return
apply_updated_aps_on_server(modified_aps, host, dsp_client)
project_aps_updated = get_aps_of_project(shortcode, dsp_client)
serialize_aps_of_project(
project_aps=project_aps_updated,
shortcode=shortcode,
mode="modified",
host=host,
)


def update_doaps(host: str, shortcode: str, dsp_client: DspClient) -> None:
"""Sample function to modify the Default Object Access Permissions of a project."""
project_doaps = get_doaps_of_project(shortcode, dsp_client)
serialize_doaps_of_project(
project_doaps=project_doaps,
shortcode=shortcode,
mode="original",
host=host,
)
project_doaps_modified = modify_doaps(doaps=project_doaps)
if not project_doaps_modified:
logger.info("There are no DOAPs to update.")
return
apply_updated_doaps_on_server(project_doaps_modified, host, dsp_client)
project_doaps_updated = get_doaps_of_project(shortcode, dsp_client)
serialize_doaps_of_project(
project_doaps=project_doaps_updated,
shortcode=shortcode,
mode="modified",
host=host,
)


def update_oaps(host: str, shortcode: str, dsp_client: DspClient, oap_config: OapRetrieveConfig) -> None:
"""Sample function to modify the Object Access Permissions of a project."""
oaps = get_all_oaps_of_project(shortcode, dsp_client, oap_config)
Expand Down Expand Up @@ -142,28 +56,16 @@ def main() -> None:
and one to update the Object Access Permissions of a project.
All must first be adapted to your needs.
"""
host = Hosts.get_host("test")
shortcode = "F18E"
host = Hosts.get_host("localhost")
shortcode = "4123"
log_start_of_script(host, shortcode)
dsp_client = login(host)

oap_config = OapRetrieveConfig(
retrieve_resources="specified_res_classes",
specified_res_classes=["my-data-model:ImageThing"],
retrieve_values="specified_props",
specified_props=["knora-api:hasStillImageFileValue"],
retrieve_resources="all",
retrieve_values="all",
)

update_aps(
host=host,
shortcode=shortcode,
dsp_client=dsp_client,
)
update_doaps(
host=host,
shortcode=shortcode,
dsp_client=dsp_client,
)
update_oaps(
host=host,
shortcode=shortcode,
Expand Down
74 changes: 74 additions & 0 deletions tests/test_oap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,80 @@ def resource() -> dict[str, Any]:
}


@pytest.fixture()
def deleted_resource() -> dict[str, Any]:
# This isn't possible, because "Resources marked as deleted are not found in search queries."
# https://docs.dasch.swiss/latest/DSP-API/03-endpoints/api-v2/editing-resources/#requesting-deleted-resources
return {
"rdfs:label": "Deleted Resource",
"knora-api:isDeleted": True,
"@id": "http://rdfh.ch/0838/dBu563hjSN6RmJZp6NU3_Q",
"@type": "knora-api:DeletedResource",
"knora-api:hasPermissions": "CR knora-admin:ProjectMember|V knora-admin:UnknownUser",
"my-data-model:hasFirstProp": {
"knora-api:hasPermissions": "CR knora-admin:ProjectAdmin|V knora-admin:KnownUser",
"@id": "http://rdfh.ch/0838/dBu563hjSN6RmJZp6NU3_Q/values/o0313dsSQTSPGua4NSWkeQ",
"@type": "knora-api:TextValue",
},
"my-data-model:hasSecondProp": {
"knora-api:hasPermissions": "CR knora-admin:ProjectAdmin|V knora-admin:KnownUser",
"@id": "http://rdfh.ch/0838/dBu563hjSN6RmJZp6NU3_Q/values/ziOT-nhmQiqvCV8LSxAyHA",
"@type": "knora-api:TextValue",
},
}


@pytest.fixture()
def resource_with_one_deleted_value() -> dict[str, Any]:
return {
"@id": "http://rdfh.ch/0838/dBu563hjSN6RmJZp6NU3_Q",
"@type": "my-data-model:ImageThing",
"knora-api:hasPermissions": "CR knora-admin:ProjectMember|V knora-admin:UnknownUser",
"my-data-model:hasFirstProp": {
"knora-api:hasPermissions": "CR knora-admin:ProjectAdmin|V knora-admin:KnownUser",
"@id": "http://rdfh.ch/0838/dBu563hjSN6RmJZp6NU3_Q/values/o0313dsSQTSPGua4NSWkeQ",
"@type": "knora-api:TextValue",
},
"knora-api:DeletedValue": {
"knora-api:hasPermissions": "CR knora-admin:ProjectAdmin|M knora-admin:ProjectMember",
"knora-api:isDeleted": True,
"@type": "knora-api:DeletedValue",
"http://www.knora.org/ontology/knora-base#DeletedValue": "DeletedValue",
"@id": "http://rdfh.ch/4123/CXXSgnapQ9mNsjnAJNaiSA/values/Hrz9FEkARzO6R0G1JhjPwQ",
},
}


@pytest.fixture()
def resource_with_multiple_deleted_values() -> dict[str, Any]:
return {
"@id": "http://rdfh.ch/0838/dBu563hjSN6RmJZp6NU3_Q",
"@type": "my-data-model:ImageThing",
"knora-api:hasPermissions": "CR knora-admin:ProjectMember|V knora-admin:UnknownUser",
"my-data-model:hasFirstProp": {
"knora-api:hasPermissions": "CR knora-admin:ProjectAdmin|V knora-admin:KnownUser",
"@id": "http://rdfh.ch/0838/dBu563hjSN6RmJZp6NU3_Q/values/o0313dsSQTSPGua4NSWkeQ",
"@type": "knora-api:TextValue",
},
"knora-api:DeletedValue": [
{
"knora-api:hasPermissions": "CR knora-admin:ProjectAdmin|M knora-admin:ProjectMember",
"knora-api:isDeleted": True,
"@type": "knora-api:DeletedValue",
"http://www.knora.org/ontology/knora-base#DeletedValue": "DeletedValue",
"@id": "http://rdfh.ch/4123/CXXSgnapQ9mNsjnAJNaiSA/values/Hrz9FEkARzO6R0G1JhjPwQ",
},
{
"knora-api:hasPermissions": "CR knora-admin:ProjectAdmin|M knora-admin:ProjectMember",
"knora-api:isDeleted": True,
"@type": "knora-api:DeletedValue",
"http://www.knora.org/ontology/knora-base#DeletedValue": "DeletedValue",
"@id": "http://rdfh.ch/4123/CXXSgnapQ9mNsjnAJNaiSA/values/pu_15FiGT9OoGd_odCcqcQ",
},
],
}


@pytest.fixture()
def gravsearch_1_link_obj() -> dict[str, Any]:
return {
Expand Down

0 comments on commit 5a4e668

Please sign in to comment.