diff --git a/aries_cloudagent/protocols/revocation_notification/v1_0/routes.py b/aries_cloudagent/protocols/revocation_notification/v1_0/routes.py index 4b577315a2..a71c95125d 100644 --- a/aries_cloudagent/protocols/revocation_notification/v1_0/routes.py +++ b/aries_cloudagent/protocols/revocation_notification/v1_0/routes.py @@ -8,8 +8,8 @@ from ....messaging.responder import BaseResponder from ....revocation.util import ( REVOCATION_CLEAR_PENDING_EVENT, - REVOCATION_PUBLISHED_EVENT, REVOCATION_EVENT_PREFIX, + REVOCATION_PUBLISHED_EVENT, ) from ....storage.error import StorageError, StorageNotFoundError from .models.rev_notification_record import RevNotificationRecord @@ -31,10 +31,14 @@ def register_events(event_bus: EventBus): async def on_revocation_published(profile: Profile, event: Event): """Handle issuer revoke event.""" - LOGGER.debug("Sending notification of revocation to recipient: %s", event.payload) + LOGGER.debug("Received notification of revocation publication: %s", event.payload) + should_notify = profile.settings.get("revocation.notify", False) responder = profile.inject(BaseResponder) crids = event.payload.get("crids") or [] + # Allow for crids to be integers + if crids and isinstance(crids[0], int): + crids = [str(crid) for crid in crids] try: async with profile.session() as session: @@ -46,9 +50,10 @@ async def on_revocation_published(profile: Profile, event: Event): for record in records: await record.delete_record(session) - await responder.send( - record.to_message(), connection_id=record.connection_id - ) + if should_notify: + await responder.send( + record.to_message(), connection_id=record.connection_id + ) except StorageNotFoundError: LOGGER.info( diff --git a/aries_cloudagent/protocols/revocation_notification/v1_0/tests/test_routes.py b/aries_cloudagent/protocols/revocation_notification/v1_0/tests/test_routes.py index 0cd23a8b04..4a4e0145dd 100644 --- a/aries_cloudagent/protocols/revocation_notification/v1_0/tests/test_routes.py +++ b/aries_cloudagent/protocols/revocation_notification/v1_0/tests/test_routes.py @@ -1,9 +1,9 @@ """Test routes.py""" -from aries_cloudagent.tests import mock import pytest -from .. import routes as test_module +from aries_cloudagent.tests import mock + from .....config.settings import Settings from .....core.event_bus import Event, MockEventBus from .....core.in_memory import InMemoryProfile @@ -15,6 +15,7 @@ REVOCATION_PUBLISHED_EVENT, ) from .....storage.error import StorageError, StorageNotFoundError +from .. import routes as test_module @pytest.fixture @@ -52,6 +53,8 @@ async def test_on_revocation_published(profile: Profile, responder: MockResponde assert isinstance(profile.settings, Settings) + profile.settings.set_value("revocation.notify", True) + with mock.patch.object(test_module, "RevNotificationRecord", MockRec): await test_module.on_revocation_published(profile, event) @@ -59,6 +62,28 @@ async def test_on_revocation_published(profile: Profile, responder: MockResponde mock_rec.delete_record.assert_called_once() assert responder.messages + # Test with integer crids + mock_rec.cred_rev_id = "1" + MockRec.query_by_rev_reg_id = mock.CoroutineMock(return_value=[mock_rec]) + event = Event(topic, {"rev_reg_id": "mock", "crids": [1]}) + + with mock.patch.object(test_module, "RevNotificationRecord", MockRec): + await test_module.on_revocation_published(profile, event) + + MockRec.query_by_rev_reg_id.assert_called_once() + assert mock_rec.delete_record.call_count == 2 + + # Test with empty crids + mock_rec.cred_rev_id = "1" + MockRec.query_by_rev_reg_id = mock.CoroutineMock(return_value=[mock_rec]) + event = Event(topic, {"rev_reg_id": "mock", "crids": []}) + + with mock.patch.object(test_module, "RevNotificationRecord", MockRec): + await test_module.on_revocation_published(profile, event) + + MockRec.query_by_rev_reg_id.assert_called_once() + assert mock_rec.delete_record.call_count == 2 + @pytest.mark.asyncio async def test_on_revocation_published_x_not_found( diff --git a/aries_cloudagent/protocols/revocation_notification/v2_0/routes.py b/aries_cloudagent/protocols/revocation_notification/v2_0/routes.py index 9c62a16c15..3ab25fec76 100644 --- a/aries_cloudagent/protocols/revocation_notification/v2_0/routes.py +++ b/aries_cloudagent/protocols/revocation_notification/v2_0/routes.py @@ -8,8 +8,8 @@ from ....messaging.responder import BaseResponder from ....revocation.util import ( REVOCATION_CLEAR_PENDING_EVENT, - REVOCATION_PUBLISHED_EVENT, REVOCATION_EVENT_PREFIX, + REVOCATION_PUBLISHED_EVENT, ) from ....storage.error import StorageError, StorageNotFoundError from .models.rev_notification_record import RevNotificationRecord @@ -31,11 +31,14 @@ def register_events(event_bus: EventBus): async def on_revocation_published(profile: Profile, event: Event): """Handle issuer revoke event.""" - LOGGER.debug("Sending notification of revocation to recipient: %s", event.payload) + LOGGER.debug("Received notification of revocation publication: %s", event.payload) should_notify = profile.settings.get("revocation.notify", False) responder = profile.inject(BaseResponder) crids = event.payload.get("crids") or [] + # Allow for crids to be integers + if crids and isinstance(crids[0], int): + crids = [str(crid) for crid in crids] try: async with profile.session() as session: @@ -51,6 +54,10 @@ async def on_revocation_published(profile: Profile, event: Event): await responder.send( record.to_message(), connection_id=record.connection_id ) + LOGGER.info( + "Sent revocation notification for credential to %s", + record.connection_id, + ) except StorageNotFoundError: LOGGER.info( diff --git a/aries_cloudagent/protocols/revocation_notification/v2_0/tests/test_routes.py b/aries_cloudagent/protocols/revocation_notification/v2_0/tests/test_routes.py index 526ed00e72..80f6e34b11 100644 --- a/aries_cloudagent/protocols/revocation_notification/v2_0/tests/test_routes.py +++ b/aries_cloudagent/protocols/revocation_notification/v2_0/tests/test_routes.py @@ -1,9 +1,9 @@ """Test routes.py""" -from aries_cloudagent.tests import mock import pytest -from .. import routes as test_module +from aries_cloudagent.tests import mock + from .....config.settings import Settings from .....core.event_bus import Event, MockEventBus from .....core.in_memory import InMemoryProfile @@ -15,6 +15,7 @@ REVOCATION_PUBLISHED_EVENT, ) from .....storage.error import StorageError, StorageNotFoundError +from .. import routes as test_module @pytest.fixture @@ -60,6 +61,28 @@ async def test_on_revocation_published(profile: Profile, responder: MockResponde mock_rec.delete_record.assert_called_once() assert responder.messages + # Test with integer crids + mock_rec.cred_rev_id = "1" + MockRec.query_by_rev_reg_id = mock.CoroutineMock(return_value=[mock_rec]) + event = Event(topic, {"rev_reg_id": "mock", "crids": [1]}) + + with mock.patch.object(test_module, "RevNotificationRecord", MockRec): + await test_module.on_revocation_published(profile, event) + + MockRec.query_by_rev_reg_id.assert_called_once() + assert mock_rec.delete_record.call_count == 2 + + # Test with empty crids + mock_rec.cred_rev_id = "1" + MockRec.query_by_rev_reg_id = mock.CoroutineMock(return_value=[mock_rec]) + event = Event(topic, {"rev_reg_id": "mock", "crids": []}) + + with mock.patch.object(test_module, "RevNotificationRecord", MockRec): + await test_module.on_revocation_published(profile, event) + + MockRec.query_by_rev_reg_id.assert_called_once() + assert mock_rec.delete_record.call_count == 2 + @pytest.mark.asyncio async def test_on_revocation_published_no_notify(