Skip to content

Commit

Permalink
Merge pull request #97 from oarepo/miroslavsimek/be-579-invitation-re…
Browse files Browse the repository at this point in the history
…quest-fails-on-npe

bug: NPE in requests without recipients
  • Loading branch information
mesemus authored Dec 6, 2024
2 parents 2084fcd + 06201a0 commit ab9b866
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
15 changes: 9 additions & 6 deletions oarepo_requests/services/oarepo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ def _resolve(self, obj: Request, ctx: dict[str, Any]) -> dict:
:param ctx: Context cache
:return: The resolved entity
"""
reference_dict: dict = getattr(obj, self._entity).reference_dict
entity_field_value = getattr(obj, self._entity)
if not entity_field_value:
return {}

reference_dict: dict = entity_field_value.reference_dict
key = "entity:" + ":".join(
f"{x[0]}:{x[1]}" for x in sorted(reference_dict.items())
)
Expand All @@ -61,23 +65,22 @@ def __init__(self, entity: str, when: callable = None):
def expand(self, obj: Request, context: dict) -> dict:
"""Create the request links."""
res = {}
res.update(self._resolve(obj, context)["links"])
resolved = self._resolve(obj, context)
if "links" in resolved:
res.update(resolved["links"])

if hasattr(obj.type, "extra_entity_links"):
entity = self._resolve(obj, context)
res.update(
obj.type.extra_entity_links(
request=obj,
entity=entity,
entity_type=self._entity
request=obj, entity=entity, entity_type=self._entity
)
)

return res


class RedirectLinks(Link):

def __init__(self, when: callable = None):
"""Constructor."""
self._when_func = when
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ class RequestsWithCT(WorkflowRequestPolicy):
IfRequestedBy(UserGenerator(1), [UserGenerator(2)], [UserGenerator(3)])
],
)
approve_draft = WorkflowRequest(
requesters=[IfInState("draft", [RecordOwners()])],
recipients=[],
)


class RequestsWithAnotherTopicUpdatingRequestType(DefaultRequests):
Expand Down
31 changes: 31 additions & 0 deletions tests/test_requests/test_no_recipient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Copyright (C) 2024 CESNET z.s.p.o.
#
# oarepo-requests is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.
#
def test_no_recipient(
logged_client,
users,
urls,
create_draft_via_resource,
search_clear,
):
creator = users[0]
assert creator.id == '1'

creator_client = logged_client(creator)

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

resp_request_create = creator_client.post(
urls["BASE_URL_REQUESTS"],
json={
"request_type": "approve_draft",
"topic": {"thesis_draft": draft1.json["id"]},
}
)
assert resp_request_create.status_code == 201
assert resp_request_create.json['receiver'] is None
assert resp_request_create.json['links']['receiver'] == {}

0 comments on commit ab9b866

Please sign in to comment.