Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for crids in event payload to be integers #2819

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions aries_cloudagent/protocols/revocation_notification/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,6 +15,7 @@
REVOCATION_PUBLISHED_EVENT,
)
from .....storage.error import StorageError, StorageNotFoundError
from .. import routes as test_module


@pytest.fixture
Expand Down Expand Up @@ -52,13 +53,37 @@ 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)

MockRec.query_by_rev_reg_id.assert_called_once()
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,6 +15,7 @@
REVOCATION_PUBLISHED_EVENT,
)
from .....storage.error import StorageError, StorageNotFoundError
from .. import routes as test_module


@pytest.fixture
Expand Down Expand Up @@ -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(
Expand Down