Skip to content

Commit

Permalink
Merge pull request #102 from oarepo/krist/be-596-cancel-transition-in…
Browse files Browse the repository at this point in the history
…-workflows

cancel transition fix
  • Loading branch information
mirekys authored Dec 18, 2024
2 parents 0b63417 + 517adec commit b6ce055
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
10 changes: 5 additions & 5 deletions oarepo_requests/actions/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

import abc
import contextlib
from typing import (
TYPE_CHECKING,
Any,
override,
)
from typing import TYPE_CHECKING, Any, override

from invenio_requests.customizations import RequestAction, RequestActions, RequestType
from invenio_requests.errors import CannotExecuteActionError
Expand Down Expand Up @@ -114,6 +110,10 @@ def apply(
from sqlalchemy.exc import NoResultFound

yield
if (
not topic
): # for example if we are cancelling requests after deleting draft, it does not make sense to attempt changing the state of the draft
return
try:
transitions = (
current_oarepo_workflows.get_workflow(topic)
Expand Down
11 changes: 8 additions & 3 deletions oarepo_requests/actions/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from oarepo_runtime.i18n import lazy_gettext as _

from oarepo_requests.proxies import current_oarepo_requests

from invenio_pidstore.errors import PersistentIdentifierError
if TYPE_CHECKING:
from flask_babel.speaklater import LazyString
from flask_principal import Identity
Expand Down Expand Up @@ -94,7 +94,10 @@ def execute(
"""Execute the action."""
request: Request = self.request # type: ignore
request_type = request.type
topic = request.topic.resolve()
try:
topic = request.topic.resolve()
except PersistentIdentifierError:
topic = None
self._execute_with_components(
self.components, identity, request_type, topic, uow, *args, **kwargs
)
Expand Down Expand Up @@ -154,8 +157,10 @@ class OARepoAcceptAction(OARepoGenericActionMixin, actions.AcceptAction):
name = _("Accept")


class OARepoCancelAction(actions.CancelAction):
class OARepoCancelAction(OARepoGenericActionMixin, actions.CancelAction):
"""Cancel action extended for oarepo requests."""

name = _("Cancel")

status_from = ["created", "submitted"]
status_to = "cancelled"
25 changes: 20 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,20 @@ class DefaultRequests(WorkflowRequestPolicy):
requesters=[IfInState("draft", [RecordOwners()])],
recipients=[UserGenerator(2)],
transitions=WorkflowTransitions(
submitted="publishing", accepted="published", declined="draft"
submitted="publishing",
accepted="published",
declined="draft",
cancelled="draft",
),
)
delete_published_record = WorkflowRequest(
requesters=[IfInState("published", [RecordOwners()])],
recipients=[UserGenerator(2)],
transitions=WorkflowTransitions(
submitted="deleting", accepted="deleted", declined="published"
submitted="deleting",
accepted="deleted",
declined="published",
cancelled="published",
),
)
delete_draft = WorkflowRequest(
Expand Down Expand Up @@ -158,23 +164,32 @@ class RequestsWithApprove(WorkflowRequestPolicy):
requesters=[IfInState("approved", [AutoRequest()])],
recipients=[UserGenerator(1)],
transitions=WorkflowTransitions(
submitted="publishing", accepted="published", declined="approved"
submitted="publishing",
accepted="published",
declined="approved",
cancelled="approved",
),
events=events_only_receiver_can_comment,
)
approve_draft = WorkflowRequest(
requesters=[IfInState("draft", [RecordOwners()])],
recipients=[UserGenerator(2)],
transitions=WorkflowTransitions(
submitted="approving", accepted="approved", declined="draft"
submitted="approving",
accepted="approved",
declined="draft",
cancelled="draft",
),
events=events_only_receiver_can_comment,
)
delete_published_record = WorkflowRequest(
requesters=[IfInState("published", [RecordOwners()])],
recipients=[UserGenerator(2)],
transitions=WorkflowTransitions(
submitted="deleting", accepted="deleted", declined="published"
submitted="deleting",
accepted="deleted",
declined="published",
cancelled="published",
),
events=events_only_receiver_can_comment,
)
Expand Down
53 changes: 49 additions & 4 deletions tests/test_requests/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,12 @@ def test_workflow_events_resource(
request_id = read_from_record.json["expanded"]["requests"][0]["id"]
json = {**events_resource_data, "type": TestEventType.type_id}
create_event_u1 = user1_client.post(
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}", json=json
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}",
json=json,
)
create_event_u2 = user2_client.post(
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}", json=json
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}",
json=json,
)

assert create_event_u1.status_code == 403
Expand Down Expand Up @@ -493,10 +495,12 @@ def test_workflow_events_resource(
request_id = publish_request["id"]

create_event_u1 = user1_client.post(
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}", json=json
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}",
json=json,
)
create_event_u2 = user2_client.post(
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}", json=json
f"{urls['BASE_URL_REQUESTS']}{request_id}/timeline/{TestEventType.type_id}",
json=json,
)
assert create_event_u1.status_code == 201
assert create_event_u2.status_code == 403
Expand Down Expand Up @@ -558,3 +562,44 @@ def test_delete_log(
break
else:
assert False


def test_cancel_transition(
vocab_cf,
logged_client,
users,
urls,
publish_request_data_function,
create_draft_via_resource,
search_clear,
):
creator = users[0]
creator_client = logged_client(creator)

draft1 = create_draft_via_resource(creator_client)

resp_request_create = creator_client.post(
urls["BASE_URL_REQUESTS"],
json=publish_request_data_function(draft1.json["id"]),
)
resp_request_submit = creator_client.post(
link2testclient(resp_request_create.json["links"]["actions"]["submit"]),
)

record = creator_client.get(
f"{urls['BASE_URL']}{draft1.json['id']}/draft?expand=true"
)
assert record.json["expanded"]["requests"][0]["links"]["actions"].keys() == {
"cancel",
}
assert record.json["state"] == "publishing"
creator_client.post(
link2testclient(
record.json["expanded"]["requests"][0]["links"]["actions"]["cancel"]
),
)

record = creator_client.get(
f"{urls['BASE_URL']}{draft1.json['id']}/draft?expand=true"
)
assert record.json["state"] == "draft"

0 comments on commit b6ce055

Please sign in to comment.