Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Dec 20, 2024
1 parent ef18d20 commit 725d744
Show file tree
Hide file tree
Showing 28 changed files with 207 additions and 99 deletions.
3 changes: 2 additions & 1 deletion oarepo_requests/actions/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any

from invenio_pidstore.errors import PersistentIdentifierError
from invenio_requests.customizations import actions
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
6 changes: 4 additions & 2 deletions oarepo_requests/resolvers/interface.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from __future__ import annotations

from typing import Any, TYPE_CHECKING
import logging
from typing import TYPE_CHECKING, Any

from invenio_pidstore.errors import PersistentIdentifierError

from oarepo_requests.resolvers.ui import resolve
import logging

if TYPE_CHECKING:
from invenio_requests.records import Request
log = logging.getLogger(__name__)


# todo consider - we are not using this strictly in the ui context - so how should we separate these things in the future
def resolve_entity(entity: str, obj: Request, ctx: dict[str, Any]) -> dict:
"""Resolve the entity and put it into the context cache.
Expand Down
8 changes: 4 additions & 4 deletions oarepo_requests/resources/events/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

from __future__ import annotations

import marshmallow as ma
from flask_resources import ResponseHandler
from invenio_records_resources.services.base.config import ConfiguratorMixin
from invenio_requests.resources.events.config import RequestCommentsResourceConfig

from oarepo_requests.resources.ui import OARepoRequestEventsUIJSONSerializer
import marshmallow as ma


class OARepoRequestsCommentsResourceConfig(
Expand All @@ -36,9 +36,9 @@ class OARepoRequestsCommentsResourceConfig(
@property
def request_item_view_args(self):
return {
**super().request_item_view_args,
"event_type": ma.fields.Str(),
}
**super().request_item_view_args,
"event_type": ma.fields.Str(),
}

@property
def response_handlers(self) -> dict[str, ResponseHandler]:
Expand Down
8 changes: 3 additions & 5 deletions oarepo_requests/resources/events/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

from __future__ import annotations

from flask_resources import route
from copy import deepcopy

from flask import g
from flask_resources import (
from_conf,
Expand Down Expand Up @@ -81,15 +78,16 @@ def search_extended(self) -> tuple[dict, int]:
"""Search for comments."""
return super().search()


# list args parser in invenio parses request_id input through UUID instead of Str; does this have any relevance for us?
@item_view_args_parser
@request_extra_args
@data_parser
@response_handler()
def create_event(self):
"""Create a comment."""
type_ = current_event_type_registry.lookup(resource_requestctx.view_args["event_type"], quiet=True)
type_ = current_event_type_registry.lookup(
resource_requestctx.view_args["event_type"], quiet=True
)
item = self.service.create(
identity=g.identity,
request_id=resource_requestctx.view_args["request_id"],
Expand Down
12 changes: 3 additions & 9 deletions oarepo_requests/services/permissions/generators/conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@
from invenio_requests.resolvers.registry import ResolverRegistry
from oarepo_runtime.datastreams.utils import get_record_service_for_record
from oarepo_workflows.requests import RecipientGeneratorMixin
from oarepo_workflows.requests.generators import (
IfEventType as WorkflowIfEventType,
)
from oarepo_workflows.requests.generators import (
IfRequestType as WorkflowIfRequestType,
)
from oarepo_workflows.requests.generators import (
IfRequestTypeBase,
)
from oarepo_workflows.requests.generators import IfEventType as WorkflowIfEventType
from oarepo_workflows.requests.generators import IfRequestType as WorkflowIfRequestType
from oarepo_workflows.requests.generators import IfRequestTypeBase
from sqlalchemy.exc import NoResultFound
from typing_extensions import deprecated

Expand Down
4 changes: 1 addition & 3 deletions oarepo_requests/services/record/types/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
from invenio_records_resources.services import LinksTemplate
from invenio_records_resources.services.base.links import Link

from oarepo_requests.services.results import (
RequestTypesList,
)
from oarepo_requests.services.results import RequestTypesList
from oarepo_requests.services.schema import RequestTypeSchema
from oarepo_requests.utils import allowed_request_types_for_record

Expand Down
61 changes: 45 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class WithApprovalPermissions(RequestBasedWorkflowPermissions):
),
}


@pytest.fixture(scope="module")
def create_app(instance_path, entry_points):
"""Application factory fixture."""
Expand All @@ -334,10 +335,12 @@ def vocab_cf(app, db, cache):
prepare_cf_indices()
ThesisDraft.index.refresh()


@pytest.fixture()
def urls():
return {"BASE_URL": "/thesis/", "BASE_URL_REQUESTS": "/requests/"}


@pytest.fixture()
def serialization_result():
def _result(topic_id, request_id):
Expand Down Expand Up @@ -472,6 +475,7 @@ def request_events_service(app):
service = current_requests.request_events_service
return service


@pytest.fixture(scope="module")
def record_service():
return current_service
Expand Down Expand Up @@ -547,12 +551,14 @@ def _logged_client(user):

return _logged_client


@pytest.fixture()
def example_topic_draft(record_service, users, default_workflow_json): # needed for ui
identity = users[0].identity
draft = record_service.create(identity, default_workflow_json)
return draft._obj


@pytest.fixture()
def merge_record_data(default_workflow_json):
def _merge_data(custom_workflow=None, additional_data=None):
Expand All @@ -572,6 +578,7 @@ def _merge_data(custom_workflow=None, additional_data=None):
if additional_data:
always_merger.merge(json, additional_data)
return json

return _merge_data


Expand All @@ -584,21 +591,25 @@ def record(identity, custom_workflow=None, additional_data=None):

return record


@pytest.fixture()
def record_factory(record_service, draft_factory, urls):
# bypassing request pattern with system identity
def record(client, custom_workflow=None, additional_data=None):
draft = draft_factory(client.user_fixture.identity, custom_workflow, additional_data)
record = record_service.publish(system_identity, draft['id'])
ret = client.get(f"{urls['BASE_URL']}{record['id']}") # unified return value
draft = draft_factory(
client.user_fixture.identity, custom_workflow, additional_data
)
record = record_service.publish(system_identity, draft["id"])
ret = client.get(f"{urls['BASE_URL']}{record['id']}") # unified return value
return ret

return record



@pytest.fixture()
def record_with_files_factory(record_service, draft_factory, default_workflow_json, urls):
def record_with_files_factory(
record_service, draft_factory, default_workflow_json, urls
):
def record(client, custom_workflow=None, additional_data=None):
identity = client.user_fixture.identity
if (
Expand All @@ -607,7 +618,7 @@ def record(client, custom_workflow=None, additional_data=None):
):
if not additional_data:
additional_data = {}
additional_data.setdefault("files",{}).setdefault("enabled", True)
additional_data.setdefault("files", {}).setdefault("enabled", True)
draft = draft_factory(identity, custom_workflow, additional_data)

# upload file
Expand All @@ -625,15 +636,14 @@ def record(client, custom_workflow=None, additional_data=None):
)
commit = files_service.commit_file(identity, draft["id"], "test.pdf")

#publish record
# publish record
record = record_service.publish(system_identity, draft["id"])
ret = client.get(f"{urls['BASE_URL']}{record['id']}") # unified return value
ret = client.get(f"{urls['BASE_URL']}{record['id']}") # unified return value
return ret

return record



@pytest.fixture()
def create_draft_via_resource(merge_record_data, urls):
def _create_draft(
Expand All @@ -645,6 +655,7 @@ def _create_draft(

return _create_draft


@pytest.fixture()
def events_resource_data():
"""Input data for the Request Events Resource (REST body)."""
Expand All @@ -655,6 +666,7 @@ def events_resource_data():
}
}


def _create_role(id, name, description, is_managed, database):
"""Creates a Role/Group."""
r = current_datastore.create_role(
Expand Down Expand Up @@ -726,13 +738,17 @@ def _create_request_from_link(request_types_json, request_type):

return _create_request_from_link


@pytest.fixture()
def request_type_additional_data():
return {"publish_draft":{"payload": {"version": "1.0"}}}
return {"publish_draft": {"payload": {"version": "1.0"}}}


@pytest.fixture
def create_request_by_link(get_request_link, request_type_additional_data):
def _create_request(client, record, request_type, additional_data=None, **request_kwargs):
def _create_request(
client, record, request_type, additional_data=None, **request_kwargs
):
if additional_data is None:
additional_data = {}
applicable_requests = client.get(
Expand All @@ -742,22 +758,35 @@ def _create_request(client, record, request_type, additional_data=None, **reques
get_request_link(applicable_requests, request_type)
)
if request_type in request_type_additional_data:
additional_data = always_merger.merge(additional_data, request_type_additional_data[request_type])
additional_data = always_merger.merge(
additional_data, request_type_additional_data[request_type]
)
if not additional_data:
create_response = client.post(create_link, **request_kwargs)
else:
create_response = client.post(create_link, json=additional_data, **request_kwargs)
create_response = client.post(
create_link, json=additional_data, **request_kwargs
)
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_additional_data=None, submit_additional_data=None):
create_response = create_request_by_link(client, record, request_type, additional_data=create_additional_data)
def _submit_request(
client,
record,
request_type,
create_additional_data=None,
submit_additional_data=None,
):
create_response = create_request_by_link(
client, record, request_type, additional_data=create_additional_data
)
submit_response = client.post(
link2testclient(create_response.json["links"]["actions"]["submit"]), json=submit_additional_data
link2testclient(create_response.json["links"]["actions"]["submit"]),
json=submit_additional_data,
)
return submit_response

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#
from flask import current_app
from thesis.ext import ThesisExt
from thesis.records.api import ThesisDraft, ThesisRecord

from tests.test_requests.utils import link2testclient

Expand Down Expand Up @@ -104,9 +103,7 @@ def publish_record(
):
id_ = draft1.json["id"]
submit_request_by_link(creator_client, draft1, "publish_draft")
record = receiver_client.get(
f"{urls['BASE_URL']}{id_}/draft?expand=true"
)
record = receiver_client.get(f"{urls['BASE_URL']}{id_}/draft?expand=true")
assert record.json["expanded"]["requests"][0]["links"]["actions"].keys() == {
"accept",
"decline",
Expand All @@ -125,7 +122,7 @@ def test_allowed_request_types_on_published_resource(
urls,
create_draft_via_resource,
submit_request_by_link,
search_clear
search_clear,
):
creator = users[0]
receiver = users[1]
Expand Down
12 changes: 9 additions & 3 deletions tests/test_requests/test_cascade_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ def test_cascade_update(
draft1 = create_draft_via_resource(creator_client, custom_workflow="cascade_update")
draft2 = create_draft_via_resource(creator_client, custom_workflow="cascade_update")

publish_request_create = submit_request_by_link(creator_client, draft1, "publish_draft")
another_request_create = create_request_by_link(creator_client, draft1, "another_topic_updating")
publish_request_on_second_draft = create_request_by_link(creator_client, draft2, "publish_draft")
publish_request_create = submit_request_by_link(
creator_client, draft1, "publish_draft"
)
another_request_create = create_request_by_link(
creator_client, draft1, "another_topic_updating"
)
publish_request_on_second_draft = create_request_by_link(
creator_client, draft2, "publish_draft"
)

record = receiver_client.get(
f"{urls['BASE_URL']}{draft1.json['id']}/draft?expand=true"
Expand Down
8 changes: 6 additions & 2 deletions tests/test_requests/test_conditional_recipient.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def test_conditional_receiver_creator_matches(

draft1 = create_draft_via_resource(creator_client, custom_workflow="with_ct")

resp_request_create = create_request_by_link(creator_client, draft1, "conditional_recipient_rt")
resp_request_create = create_request_by_link(
creator_client, draft1, "conditional_recipient_rt"
)

assert resp_request_create.status_code == 201
assert resp_request_create.json["receiver"] == {"user": "2"}
Expand All @@ -47,7 +49,9 @@ def test_conditional_receiver_creator_does_not_match(

draft1 = create_draft_via_resource(creator_client, custom_workflow="with_ct")

resp_request_create = create_request_by_link(creator_client, draft1, "conditional_recipient_rt")
resp_request_create = create_request_by_link(
creator_client, draft1, "conditional_recipient_rt"
)

assert resp_request_create.status_code == 201
assert resp_request_create.json["receiver"] == {"user": "3"}
4 changes: 1 addition & 3 deletions tests/test_requests/test_create_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def test_can_create(

# try declining the request for draft2, we should be able to create again then
resp_request_submit = creator_client.post(
link2testclient(
create_for_request_draft2.json["links"]["actions"]["submit"]
),
link2testclient(create_for_request_draft2.json["links"]["actions"]["submit"]),
)

with pytest.raises(OpenRequestAlreadyExists):
Expand Down
Loading

0 comments on commit 725d744

Please sign in to comment.