Skip to content

Commit

Permalink
Allow for crids in event payload to be integers (openwallet-foundatio…
Browse files Browse the repository at this point in the history
…n#2819)

Signed-off-by: jamshale <[email protected]>
  • Loading branch information
jamshale authored and gvelez17 committed Mar 8, 2024
1 parent 90a5f87 commit 4508e5c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
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

0 comments on commit 4508e5c

Please sign in to comment.