diff --git a/oarepo_communities/identity_to_entity_references.py b/oarepo_communities/identity_to_entity_references.py new file mode 100644 index 0000000..eca5e6d --- /dev/null +++ b/oarepo_communities/identity_to_entity_references.py @@ -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 + ] diff --git a/setup.cfg b/setup.cfg index 9c4ea4c..206a3f5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 + diff --git a/tests/test_communities/conftest.py b/tests/test_communities/conftest.py index f6a88d8..e399ef8 100644 --- a/tests/test_communities/conftest.py +++ b/tests/test_communities/conftest.py @@ -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") @@ -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"), @@ -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, + ), } @@ -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 @@ -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 diff --git a/tests/test_communities/test_community_requests.py b/tests/test_communities/test_community_requests.py index 6470838..cffaace 100644 --- a/tests/test_communities/test_community_requests.py +++ b/tests/test_communities/test_community_requests.py @@ -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) @@ -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) @@ -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( @@ -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( @@ -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( @@ -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) diff --git a/tests/test_communities/test_param_interpreters.py b/tests/test_communities/test_param_interpreters.py new file mode 100644 index 0000000..7776004 --- /dev/null +++ b/tests/test_communities/test_param_interpreters.py @@ -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 diff --git a/tests/test_communities/test_permissions.py b/tests/test_communities/test_permissions.py index 6b850e9..f20d76f 100644 --- a/tests/test_communities/test_permissions.py +++ b/tests/test_communities/test_permissions.py @@ -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)