Skip to content

Commit

Permalink
service: error when published record without draft
Browse files Browse the repository at this point in the history
* Raise a proper error when trying to read a draft for a published
  record when it's not being edited (i.e draft doesn't exists).
  • Loading branch information
lnielsen committed Oct 15, 2023
1 parent 64f08af commit 987c62f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion invenio_drafts_resources/services/records/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from flask import current_app
from invenio_db import db
from invenio_pidstore.errors import PIDDoesNotExistError
from invenio_records_resources.services import LinksTemplate
from invenio_records_resources.services import RecordService as RecordServiceBase
from invenio_records_resources.services import ServiceSchemaWrapper
Expand Down Expand Up @@ -197,7 +198,15 @@ def search_versions(
def read_draft(self, identity, id_, expand=False):
"""Retrieve a draft."""
# Resolve and require permission
draft = self.draft_cls.pid.resolve(id_, registered_only=False)
try:
draft = self.draft_cls.pid.resolve(id_, registered_only=False)
except NoResultFound:
# Happens when record is published and not being edited (i.e. PID
# with object id exists for the published record, but object
# getter fails to get the draft object because only the record
# object exists).
raise PIDDoesNotExistError(self.draft_cls.pid.field._pid_type, id_)

self.require_permission(identity, "read_draft", record=draft)

# Run components
Expand Down
2 changes: 1 addition & 1 deletion tests/services/test_record_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_publish_draft(app, service, identity_simple, input_data):
assert record[key] == value

# Check draft deletion
with pytest.raises(NoResultFound):
with pytest.raises(PIDDoesNotExistError):
# NOTE: Draft and Record have the same `id`
draft = service.read_draft(identity_simple, record.id)

Expand Down

0 comments on commit 987c62f

Please sign in to comment.