Skip to content

Commit

Permalink
Merge pull request #77 from oarepo/Krist/be-474-filters-on-apirequests
Browse files Browse the repository at this point in the history
Krist/be 474 filters on apirequests
  • Loading branch information
mesemus authored Oct 18, 2024
2 parents 447bcfe + f4a6e4b commit de4a6ad
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 24 deletions.
8 changes: 8 additions & 0 deletions oarepo_communities/identity_to_entity_references.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def community_role_mappings(identity):
community_roles = [
(n.value, n.role) for n in identity.provides if n.method == "community"
]
return [
{"community_role": f"{community_role[0]}:{community_role[1]}"}
for community_role in community_roles
]
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ invenio_assets.webpack =
invenio_i18n.translations =
oarepo_communities_messages = oarepo_communities.i18n

oarepo_requests.identity_to_entity_references =
community_role = oarepo_communities.identity_to_entity_references:community_role_mappings

99 changes: 82 additions & 17 deletions tests/test_communities/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,8 @@
from oarepo_communities.services.permissions.policy import (
CommunityDefaultWorkflowPermissions,
)


@pytest.fixture()
def scenario_permissions():

return CreatorsFromWorkflowRequestsPermissionPolicy


@pytest.fixture
def patch_requests_permissions(
requests_service_config,
scenario_permissions,
):
setattr(requests_service_config, "permission_policy_cls", scenario_permissions)
from tests.test_communities.utils import link_api2testclient
from oarepo_requests.services.permissions.workflow_policies import CreatorsFromWorkflowRequestsPermissionPolicy


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -285,6 +273,16 @@ class NoRequests(WorkflowRequestPolicy):
)


class CuratorPublishRequests(DefaultRequests):
publish_draft = WorkflowRequest(
requesters=[IfInState("draft", [CommunityRole("owner")])],
recipients=[DefaultCommunityRole("curator")],
transitions=WorkflowTransitions(
submitted="publishing", accepted="published", declined="draft"
),
)


WORKFLOWS = {
"default": Workflow(
label=_("Default workflow"),
Expand All @@ -311,6 +309,11 @@ class NoRequests(WorkflowRequestPolicy):
permission_policy_cls=TestCommunityWorkflowPermissions,
request_policy_cls=NoRequests,
),
"curator_publish": Workflow(
label=_("For testing assigned param filter."),
permission_policy_cls=TestCommunityWorkflowPermissions,
request_policy_cls=CuratorPublishRequests,
),
}


Expand Down Expand Up @@ -402,6 +405,7 @@ def app_config(app_config):
app_config["COMMUNITIES_CUSTOM_FIELDS"] = [WorkflowCF(name="workflow")]
app_config["COMMUNITIES_CUSTOM_FIELDS_UI"] = []

app_config["REQUESTS_PERMISSION_POLICY"] = CreatorsFromWorkflowRequestsPermissionPolicy
return app_config


Expand Down Expand Up @@ -637,16 +641,77 @@ def default_workflow_json():


@pytest.fixture()
def create_draft_via_resource(default_workflow_json, urls):
def create_draft_via_resource(default_workflow_json):
def _create_draft(
client, expand=True, custom_workflow=None, additional_data=None, **kwargs
client,
community,
expand=True,
custom_workflow=None,
additional_data=None,
**kwargs,
):
json = copy.deepcopy(default_workflow_json)
if custom_workflow:
json["parent"]["workflow"] = custom_workflow
if additional_data:
json |= additional_data
url = urls["BASE_URL"] + "?expand=true" if expand else urls["BASE_URL"]
# url = "/thesis/" + "?expand=true" if expand else "/thesis/"
url = f"/communities/{community.id}/thesis"
return client.post(url, json=json, **kwargs)

return _create_draft


@pytest.fixture()
def get_request_type():
"""
gets request create link from serialized request types
"""

def _get_request_type(request_types_json, request_type):
selected_entry = [
entry for entry in request_types_json if entry["type_id"] == request_type
][0]
return selected_entry

return _get_request_type


@pytest.fixture()
def get_request_link(get_request_type):
"""
gets request create link from serialized request types
"""

def _create_request_from_link(request_types_json, request_type):
selected_entry = get_request_type(request_types_json, request_type)
return selected_entry["links"]["actions"]["create"]

return _create_request_from_link


@pytest.fixture
def create_request_by_link(get_request_link):
def _create_request(client, record, request_type):
applicable_requests = client.get(
link_api2testclient(record.json["links"]["applicable-requests"])
).json["hits"]["hits"]
create_link = link_api2testclient(
get_request_link(applicable_requests, request_type)
)
create_response = client.post(create_link)
return create_response

return _create_request


@pytest.fixture
def submit_request_by_link(create_request_by_link):
def _submit_request(client, record, request_type):
create_response = create_request_by_link(client, record, request_type)
submit_response = client.post(
link_api2testclient(create_response.json["links"]["actions"]["submit"])
)
return submit_response

return _submit_request
6 changes: 0 additions & 6 deletions tests/test_communities/test_community_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def test_community_publish(
community,
request_data_factory,
record_service,
patch_requests_permissions,
search_clear,
):
reader_client = logged_client(community_reader)
Expand Down Expand Up @@ -163,7 +162,6 @@ def test_community_delete(
community,
request_data_factory,
record_service,
patch_requests_permissions,
search_clear,
):
reader_client = logged_client(community_reader)
Expand Down Expand Up @@ -210,7 +208,6 @@ def test_community_migration(
request_data_factory,
record_service,
inviter,
patch_requests_permissions,
search_clear,
):
reader_client, owner_client, community_1, community_2 = _init_env(
Expand Down Expand Up @@ -269,7 +266,6 @@ def test_community_submission_secondary(
inviter,
request_data_factory,
record_service,
patch_requests_permissions,
search_clear,
):
reader_client, owner_client, community_1, community_2 = _init_env(
Expand Down Expand Up @@ -332,7 +328,6 @@ def test_remove_secondary(
inviter,
request_data_factory,
record_service,
patch_requests_permissions,
search_clear,
):
reader_client, owner_client, community_1, community_2 = _init_env(
Expand Down Expand Up @@ -420,7 +415,6 @@ def test_community_role_ui_serialization(
request_data_factory,
record_service,
ui_serialized_community_role,
patch_requests_permissions,
search_clear,
):
reader_client = logged_client(community_reader)
Expand Down
41 changes: 41 additions & 0 deletions tests/test_communities/test_param_interpreters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


def test_community_role_param_interpreter(
logged_client,
community_owner,
community_reader,
community_curator,
community_with_workflow_factory,
record_service,
create_draft_via_resource,
create_request_by_link,
submit_request_by_link,
inviter,
search_clear,
):
owner_client = logged_client(community_owner)

community_1 = community_with_workflow_factory("comm1", community_owner)
community_2 = community_with_workflow_factory("comm2", community_owner)
community_3 = community_with_workflow_factory("comm3", community_curator)
inviter("2", community_1.id, "reader")
inviter("2", community_2.id, "reader")
inviter("2", community_3.id, "reader")

record1 = create_draft_via_resource(owner_client, community=community_1)
record2 = create_draft_via_resource(owner_client, community=community_2)
record3 = create_draft_via_resource(
owner_client, community=community_2, custom_workflow="curator_publish" # owner is creator but not receiver of the third request
)

response_1 = submit_request_by_link(owner_client, record1, "publish_draft")
response_2 = create_request_by_link(owner_client, record2, "publish_draft")
response_3 = submit_request_by_link(owner_client, record3, "publish_draft")

search_unfiltered = owner_client.get("/requests/")
assert len(search_unfiltered.json["hits"]["hits"]) == 3

search_filtered = owner_client.get("/requests/?assigned=true")


assert len(search_filtered.json["hits"]["hits"]) == 2
1 change: 0 additions & 1 deletion tests/test_communities/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def test_scenario_change(
inviter,
set_community_workflow,
service_config,
patch_requests_permissions,
search_clear,
):
owner_client = logged_client(community_owner)
Expand Down

0 comments on commit de4a6ad

Please sign in to comment.