Skip to content

Commit 0f94ef5

Browse files
authored
Add sub wallet created event (#3946)
* add wallet created event Signed-off-by: Patrick St-Louis <patrick.st-louis@opsecid.ca> * linting Signed-off-by: Patrick St-Louis <patrick.st-louis@opsecid.ca> --------- Signed-off-by: Patrick St-Louis <patrick.st-louis@opsecid.ca>
1 parent 1b01fec commit 0f94ef5

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

acapy_agent/core/util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
STARTUP_EVENT_PATTERN = re.compile(f"^{STARTUP_EVENT_TOPIC}?$")
88
SHUTDOWN_EVENT_TOPIC = CORE_EVENT_PREFIX + "shutdown"
99
SHUTDOWN_EVENT_PATTERN = re.compile(f"^{SHUTDOWN_EVENT_TOPIC}?$")
10+
MULTITENANT_EVENT_PREFIX = CORE_EVENT_PREFIX + "multitenant::"
11+
MULTITENANT_WALLET_CREATED_TOPIC = MULTITENANT_EVENT_PREFIX + "wallet::created"
12+
MULTITENANT_WALLET_CREATED_PATTERN = re.compile(
13+
f"^{MULTITENANT_WALLET_CREATED_TOPIC}::[a-zA-Z0-9-]+$"
14+
)
1015
WARNING_DEGRADED_FEATURES = "version-with-degraded-features"
1116
WARNING_VERSION_MISMATCH = "fields-ignored-due-to-version-mismatch"
1217
WARNING_VERSION_NOT_SUPPORTED = "version-not-supported"

acapy_agent/multitenant/admin/routes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
from ...admin.decorators.auth import admin_authentication
1414
from ...admin.request_context import AdminRequestContext
1515
from ...core.error import BaseError
16+
from ...core.event_bus import Event, EventBus
1617
from ...core.profile import ProfileManagerProvider
18+
from ...core.util import MULTITENANT_WALLET_CREATED_TOPIC
1719
from ...messaging.models.base import BaseModelError
1820
from ...messaging.models.openapi import OpenAPISchema
1921
from ...messaging.models.paginated_query import (
@@ -486,6 +488,20 @@ async def wallet_create(request: web.BaseRequest):
486488
context, wallet_record, extra_settings=settings
487489
)
488490
await attempt_auto_author_with_endorser_setup(wallet_profile)
491+
492+
event_bus = context.profile.inject_or(EventBus)
493+
if event_bus:
494+
await event_bus.notify(
495+
context.profile,
496+
Event(
497+
f"{MULTITENANT_WALLET_CREATED_TOPIC}::{wallet_record.wallet_id}",
498+
{
499+
"wallet_id": wallet_record.wallet_id,
500+
"wallet_name": wallet_record.wallet_name,
501+
"settings": wallet_record.settings,
502+
},
503+
),
504+
)
489505
except BaseError as err:
490506
raise web.HTTPBadRequest(reason=err.roll_up) from err
491507

acapy_agent/multitenant/admin/tests/test_routes.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from marshmallow.exceptions import ValidationError
44

55
from ....admin.request_context import AdminRequestContext
6+
from ....core.event_bus import EventBus, MockEventBus
7+
from ....core.util import MULTITENANT_WALLET_CREATED_TOPIC
68
from ....messaging.models.base import BaseModelError
79
from ....storage.error import StorageError, StorageNotFoundError
810
from ....tests import mock
@@ -279,6 +281,59 @@ async def test_wallet_create(self):
279281
assert mock_multitenant_mgr.get_wallet_profile.called
280282
assert test_module.attempt_auto_author_with_endorser_setup.called
281283

284+
async def test_wallet_create_emits_wallet_created_event(self):
285+
body = {
286+
"wallet_name": "event-test",
287+
"wallet_type": "askar",
288+
"wallet_key": "test",
289+
"key_management_mode": "managed",
290+
"wallet_webhook_urls": [],
291+
"wallet_dispatch_type": "default",
292+
}
293+
self.request.json = mock.CoroutineMock(return_value=body)
294+
295+
test_module.attempt_auto_author_with_endorser_setup = mock.CoroutineMock()
296+
297+
mock_event_bus = MockEventBus()
298+
self.profile.context.injector.bind_instance(EventBus, mock_event_bus)
299+
300+
wallet_mock = mock.MagicMock(
301+
serialize=mock.MagicMock(
302+
return_value={
303+
"wallet_id": "event-wallet-id",
304+
"settings": {},
305+
"key_management_mode": body["key_management_mode"],
306+
}
307+
)
308+
)
309+
wallet_mock.wallet_id = "event-wallet-id"
310+
wallet_mock.wallet_name = body["wallet_name"]
311+
wallet_mock.settings = {"wallet.name": body["wallet_name"]}
312+
313+
mock_multitenant_mgr = mock.AsyncMock(BaseMultitenantManager, autospec=True)
314+
mock_multitenant_mgr.create_wallet = mock.CoroutineMock(return_value=wallet_mock)
315+
mock_multitenant_mgr.create_auth_token = mock.CoroutineMock(
316+
return_value="event-token"
317+
)
318+
mock_multitenant_mgr.get_wallet_profile = mock.CoroutineMock(
319+
return_value=mock.MagicMock()
320+
)
321+
self.profile.context.injector.bind_instance(
322+
BaseMultitenantManager, mock_multitenant_mgr
323+
)
324+
325+
await test_module.wallet_create(self.request)
326+
327+
await mock_event_bus.task_queue.wait_for_completion()
328+
329+
assert mock_event_bus.events
330+
_, event = mock_event_bus.events[-1]
331+
assert event.topic == (
332+
f"{MULTITENANT_WALLET_CREATED_TOPIC}::{wallet_mock.wallet_id}"
333+
)
334+
assert event.payload["wallet_id"] == wallet_mock.wallet_id
335+
assert event.payload["wallet_name"] == wallet_mock.wallet_name
336+
282337
async def test_wallet_create_x(self):
283338
body = {}
284339
self.request.json = mock.CoroutineMock(return_value=body)

0 commit comments

Comments
 (0)