Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Jul 24, 2024
1 parent 54a4f10 commit f0553d0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
25 changes: 18 additions & 7 deletions oarepo_workflows/permissions/policy.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import operator
from functools import reduce

from invenio_records_permissions import RecordPermissionPolicy
from invenio_records_permissions.generators import AuthenticatedUser, SystemProcess, AnyUser
from invenio_records_permissions.generators import (
AnyUser,
AuthenticatedUser,
SystemProcess,
)
from invenio_search.engine import dsl
from oarepo_runtime.services.generators import RecordOwners

from oarepo_workflows.permissions.generators import WorkflowPermission
from .generators import IfInState

from ..proxies import current_oarepo_workflows
from invenio_search.engine import dsl
import operator
from functools import reduce
from .generators import IfInState


class DefaultWorkflowPermissionPolicy(RecordPermissionPolicy):
Expand Down Expand Up @@ -80,7 +87,11 @@ def query_filters(self):
queries = []
for workflow in workflows:
q_inworkflow = dsl.Q("match", **{"parent.workflow": workflow})
query = WorkflowPermission("can_read").query_filter(data={"parent": {"workflow_id": workflow}}, **self.over) & q_inworkflow
query = (
WorkflowPermission("can_read").query_filter(
data={"parent": {"workflow_id": workflow}}, **self.over
)
& q_inworkflow
)
queries.append(query)
return [q for q in queries if q]

1 change: 0 additions & 1 deletion oarepo_workflows/requests/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from invenio_records_permissions.generators import Generator



@dataclasses.dataclass
class WorkflowRequest:
requesters: List[Generator] | Tuple[Generator]
Expand Down
3 changes: 2 additions & 1 deletion oarepo_workflows/services/components/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from invenio_records_resources.services.records.components.base import ServiceComponent


class WorkflowComponent(ServiceComponent):

def create(self, identity, data=None, record=None, **kwargs):
workflow_id = data["parent"]["workflow_id"]
record.parent.workflow = workflow_id
record.parent.workflow = workflow_id
12 changes: 7 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from invenio_app.factory import create_api
from invenio_i18n import lazy_gettext as _
from invenio_records_permissions.generators import (
AnyUser,
AuthenticatedUser,
Generator,
SystemProcess, AnyUser,
SystemProcess,
)
from invenio_users_resources.records import UserAggregate
from oarepo_runtime.services.generators import RecordOwners
Expand All @@ -23,10 +24,9 @@
WorkflowTransitions,
)


class RecordOwnersReadTestWorkflowPermissionPolicy(DefaultWorkflowPermissionPolicy):
can_read = [
RecordOwners()
]
can_read = [RecordOwners()]


class Administration(Generator):
Expand Down Expand Up @@ -64,7 +64,7 @@ class MyWorkflowRequests(WorkflowRequestPolicy):
label=_("Record owners read workflow"),
permissions_cls=RecordOwnersReadTestWorkflowPermissionPolicy,
requests_cls=MyWorkflowRequests,
)
),
}


Expand Down Expand Up @@ -177,6 +177,7 @@ def _logged_client(user):

return _logged_client


@pytest.fixture(scope="module")
def app_config(app_config):
"""Mimic an instance's configuration."""
Expand All @@ -202,6 +203,7 @@ def app_config(app_config):

return app_config


@pytest.fixture()
def default_workflow_json():
return {"parent": {"workflow_id": "my_workflow"}}
21 changes: 16 additions & 5 deletions tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def test_workflow_read(users, logged_client, default_workflow_json, search_clear
user_client1 = logged_client(users[0])
user_client2 = logged_client(users[1])

create_response = user_client1.post(ThesisResourceConfig.url_prefix, json=default_workflow_json)
create_response = user_client1.post(

Check failure on line 14 in tests/test_workflow.py

View workflow job for this annotation

GitHub Actions / build12 / build

test_workflow_read KeyError: None
ThesisResourceConfig.url_prefix, json=default_workflow_json
)
draft_json = create_response.json
assert create_response.status_code == 201

Expand Down Expand Up @@ -42,7 +44,9 @@ def test_workflow_publish(users, logged_client, default_workflow_json, search_cl
user_client1 = logged_client(users[0])
user_client2 = logged_client(users[1])

create_response = user_client1.post(ThesisResourceConfig.url_prefix, json=default_workflow_json)
create_response = user_client1.post(

Check failure on line 47 in tests/test_workflow.py

View workflow job for this annotation

GitHub Actions / build12 / build

test_workflow_publish KeyError: None
ThesisResourceConfig.url_prefix, json=default_workflow_json
)
draft_json = create_response.json
user_client1.post(
f"{ThesisResourceConfig.url_prefix}{draft_json['id']}/draft/actions/publish"
Expand All @@ -65,8 +69,13 @@ def test_query_filter(users, logged_client, default_workflow_json, search_clear)
user_client1 = logged_client(users[0])
user_client2 = logged_client(users[1])

record_w1 = user_client1.post(ThesisResourceConfig.url_prefix, json=default_workflow_json)
record_w2 = user_client1.post(ThesisResourceConfig.url_prefix, json={"parent": {"workflow_id": "record_owners_can_read"}})
record_w1 = user_client1.post(

Check failure on line 72 in tests/test_workflow.py

View workflow job for this annotation

GitHub Actions / build12 / build

test_query_filter KeyError: None
ThesisResourceConfig.url_prefix, json=default_workflow_json
)
record_w2 = user_client1.post(
ThesisResourceConfig.url_prefix,
json={"parent": {"workflow_id": "record_owners_can_read"}},
)

draft_json = record_w1.json
user_client1.post(
Expand All @@ -87,7 +96,9 @@ def test_query_filter(users, logged_client, default_workflow_json, search_clear)
assert len(search_u2["hits"]["hits"]) == 1


def test_state_change(users, record_service, state_change_function, default_workflow_json, search_clear):
def test_state_change(
users, record_service, state_change_function, default_workflow_json, search_clear
):
record = record_service.create(users[0].identity, default_workflow_json)._record

Check failure on line 102 in tests/test_workflow.py

View workflow job for this annotation

GitHub Actions / build12 / build

test_state_change KeyError: None
state_change_function(users[0].identity, record, "approving")
assert record["state"] == "approving"
Expand Down
4 changes: 3 additions & 1 deletion tests/test_workflow_field.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from invenio_access.permissions import system_identity


def test_workflow_read(users, logged_client, default_workflow_json, search_clear, record_service):
def test_workflow_read(
users, logged_client, default_workflow_json, search_clear, record_service
):
data = record_service.create(system_identity, default_workflow_json)

Check failure on line 7 in tests/test_workflow_field.py

View workflow job for this annotation

GitHub Actions / build12 / build

test_workflow_read KeyError: None
assert data._record.parent.workflow == "my_workflow"
assert data._record.state == "draft"

0 comments on commit f0553d0

Please sign in to comment.