Skip to content

Commit

Permalink
openaire: added delete and restore components
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo committed Sep 26, 2023
1 parent 74cd764 commit a7dd622
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 11 deletions.
5 changes: 2 additions & 3 deletions site/tests/openaire/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def openaire_serializer():
yield OpenAIREV1Serializer()


@pytest.fixture(scope="module")
@pytest.fixture(scope="function")
def mocked_session():
"""Mock requests.Session() object."""

Expand Down Expand Up @@ -51,8 +51,7 @@ def enable_openaire_indexing(running_app, monkeypatch):
@pytest.fixture(scope="function")
def openaire_api_endpoint(running_app):
"""Return OpenAIRE endpoint."""
openaire_api_url = running_app.app.config["OPENAIRE_API_URL"]
return f"{openaire_api_url}/feedObject"
return running_app.app.config["OPENAIRE_API_URL"]


@pytest.fixture(scope="function")
Expand Down
106 changes: 105 additions & 1 deletion site/tests/openaire/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"""Test OpenAIRE components."""

import json
from unittest.mock import call

from invenio_access.permissions import system_identity
from invenio_rdm_records.proxies import current_rdm_records_service as records_service
from invenio_rdm_records.services.components import DefaultRecordsComponents

from zenodo_rdm.openaire.records.components import OpenAIREComponent
Expand Down Expand Up @@ -38,6 +41,107 @@ def test_on_publish(

serialized_record = openaire_serializer.dump_obj(record.data)

post_endpoint = f"{openaire_api_endpoint}/feedObject"
mocked_session.post.assert_called_once_with(
openaire_api_endpoint, data=json.dumps(serialized_record), timeout=10
post_endpoint,
data=json.dumps(serialized_record),
timeout=10,
)


def test_on_delete(
running_app,
monkeypatch,
openaire_record_data,
community,
create_record,
openaire_serializer,
mocked_session,
openaire_api_endpoint,
enable_openaire_indexing,
):
"""Test on delete component."""

# Only for this test, modify record service components
monkeypatch.setitem(
running_app.app.config,
"RDM_RECORDS_SERVICE_COMPONENTS",
DefaultRecordsComponents + [OpenAIREComponent],
)

record = create_record(openaire_record_data, community)
assert record

recid = record["id"]
records_service.delete_record(system_identity, recid, {})

serialized_record = openaire_serializer.dump_obj(record.data)

post_endpoint = f"{openaire_api_endpoint}/feedObject"
mocked_session.post.assert_called_once_with(
post_endpoint, data=json.dumps(serialized_record), timeout=10
)

mocked_session.delete.assert_called_once_with(
openaire_api_endpoint,
data=json.dumps(
{
"originalId": f"10.5281/zenodo.{recid}",
"collectedFromId": "opendoar____::2659",
}
),
)


def test_on_restore(
running_app,
monkeypatch,
openaire_record_data,
community,
create_record,
openaire_serializer,
mocked_session,
openaire_api_endpoint,
enable_openaire_indexing,
):
"""Test on restore component."""

# Only for this test, modify record service components
monkeypatch.setitem(
running_app.app.config,
"RDM_RECORDS_SERVICE_COMPONENTS",
DefaultRecordsComponents + [OpenAIREComponent],
)

record = create_record(openaire_record_data, community)
assert record

recid = record["id"]

records_service.delete_record(system_identity, recid, {})
records_service.restore_record(system_identity, recid)

serialized_record = openaire_serializer.dump_obj(record.data)

post_endpoint = f"{openaire_api_endpoint}/feedObject"
# HTTP POST will be called twice (one to create the record and one to restore it)
post_calls = [
call(post_endpoint, data=json.dumps(serialized_record), timeout=10),
call(post_endpoint, data=json.dumps(serialized_record), timeout=10),
]

# HTTP DELETE will be called once, when the record is deleted
delete_calls = [
call(
openaire_api_endpoint,
data=json.dumps(
{
"originalId": f"10.5281/zenodo.{recid}",
"collectedFromId": "opendoar____::2659",
}
),
)
]

mocked_session.post.assert_has_calls(post_calls)
mocked_session.delete.assert_has_calls(delete_calls)
6 changes: 4 additions & 2 deletions site/tests/openaire/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def test_openaire_direct_index_task(

# Will be executed synchronously in tests
openaire_direct_index.delay(openaire_record.id)
post_endpoint = f"{openaire_api_endpoint}/feedObject"
mocked_session.post.assert_called_once_with(
openaire_api_endpoint, data=json.dumps(serialized_record), timeout=10
post_endpoint, data=json.dumps(serialized_record), timeout=10
)

# Assert key is not in cache : means success
Expand Down Expand Up @@ -68,9 +69,10 @@ def test_openaire_direct_index_task_with_beta(
openaire_direct_index.delay(openaire_record.id)

# Assert two ``post`` requests were issued: one to production and one to beta
post_endpoint = f"{openaire_api_endpoint}/feedObject"
beta_endpoint = f"{beta_url}/feedObject"
calls = [
call(openaire_api_endpoint, data=json.dumps(serialized_record), timeout=10),
call(post_endpoint, data=json.dumps(serialized_record), timeout=10),
call(beta_endpoint, data=json.dumps(serialized_record), timeout=10),
]
mocked_session.post.assert_has_calls(calls)
Expand Down
22 changes: 19 additions & 3 deletions site/zenodo_rdm/openaire/records/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@
"""OpenAIRE record component."""

from flask import current_app
from invenio_drafts_resources.services.records.components import ServiceComponent
from invenio_rdm_records.services.components.record_deletion import (
RecordDeletionComponent,
)
from invenio_records_resources.services.uow import TaskOp

from zenodo_rdm.openaire.tasks import openaire_direct_index
from zenodo_rdm.openaire.tasks import openaire_direct_index, openaire_delete


class OpenAIREComponent(ServiceComponent):
class OpenAIREComponent(RecordDeletionComponent):
"""Service component for custom fields."""

def publish(self, identity, draft=None, record=None):
"""Publish handler."""
is_openaire_enabled = current_app.config.get("OPENAIRE_DIRECT_INDEXING_ENABLED")
if is_openaire_enabled:
self.uow.register(TaskOp(openaire_direct_index, record_id=record["id"]))

def delete_record(self, identity, data=None, record=None, **kwargs):
"""Remove record from OpenAIRE."""
is_openaire_enabled = current_app.config.get("OPENAIRE_DIRECT_INDEXING_ENABLED")
if is_openaire_enabled:
self.uow.register(TaskOp(openaire_delete, record_id=record["id"]))

def restore_record(self, identity, data=None, record=None, **kwargs):
"""Restored record from OpenAIRE."""

# TODO check if this is allowed
is_openaire_enabled = current_app.config.get("OPENAIRE_DIRECT_INDEXING_ENABLED")
if is_openaire_enabled:
self.uow.register(TaskOp(openaire_direct_index, record_id=record["id"]))
4 changes: 2 additions & 2 deletions site/zenodo_rdm/openaire/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ def openaire_direct_index(record_id, retry=True):
rate_limit="100/m",
)
@execute_if_openaire_enabled()
def openaire_delete(record_id=None, retry=True): # TODO feature flag
def openaire_delete(record_id=None, retry=True):
"""Delete record from OpenAIRE index.
:param record_id: Record Metadata UUID.
:type record_id: str
"""
try:
record = records_service.read(system_identity, record_id)
record = records_service.read(system_identity, record_id, include_deleted=True)

original_id = openaire_original_id(record.data)[1]
datasource_id = openaire_datasource_id(record.data)
Expand Down

0 comments on commit a7dd622

Please sign in to comment.