11"""Test MediationManager."""
22import logging
3-
4- import pytest
3+ from typing import AsyncIterable , Iterable
54
65from asynctest import mock as async_mock
6+ import pytest
77
8+ from .. import manager as test_module
9+ from .....core .event_bus import EventBus , MockEventBus
10+ from .....core .in_memory import InMemoryProfile
811from .....core .profile import Profile , ProfileSession
9- from .....connections .models .conn_record import ConnRecord
10- from .....messaging .request_context import RequestContext
1112from .....storage .error import StorageNotFoundError
12- from .....transport .inbound .receipt import MessageReceipt
13-
1413from ....routing .v1_0 .models .route_record import RouteRecord
15-
16- from .. import manager as test_module
1714from ..manager import (
1815 MediationAlreadyExists ,
1916 MediationManager ,
2219)
2320from ..messages .inner .keylist_update_rule import KeylistUpdateRule
2421from ..messages .inner .keylist_updated import KeylistUpdated
22+ from ..messages .keylist_update_response import KeylistUpdateResponse
2523from ..messages .mediate_deny import MediationDeny
2624from ..messages .mediate_grant import MediationGrant
2725from ..messages .mediate_request import MediationRequest
2826from ..models .mediation_record import MediationRecord
2927
3028TEST_CONN_ID = "conn-id"
29+ TEST_THREAD_ID = "thread-id"
3130TEST_ENDPOINT = "https://example.com"
3231TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
3332TEST_ROUTE_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"
3635
3736
3837@pytest .fixture
39- async def profile () -> Profile :
38+ def profile () -> Iterable [ Profile ] :
4039 """Fixture for profile used in tests."""
4140 # pylint: disable=W0621
42- context = RequestContext .test_context ()
43- context .message_receipt = MessageReceipt (sender_verkey = TEST_VERKEY )
44- context .connection_record = ConnRecord (connection_id = TEST_CONN_ID )
45- yield context .profile
41+ yield InMemoryProfile .test_profile (bind = {EventBus : MockEventBus ()})
42+
43+
44+ @pytest .fixture
45+ def mock_event_bus (profile : Profile ):
46+ yield profile .inject (EventBus )
4647
4748
4849@pytest .fixture
49- async def session (profile ) -> ProfileSession : # pylint: disable=W0621
50+ async def session (profile ) -> AsyncIterable [ ProfileSession ] : # pylint: disable=W0621
5051 """Fixture for profile session used in tests."""
51- yield await profile .session ()
52+ async with profile .session () as session :
53+ yield session
5254
5355
5456@pytest .fixture
55- async def manager (profile ) -> MediationManager : # pylint: disable=W0621
57+ def manager (profile ) -> Iterable [ MediationManager ] : # pylint: disable=W0621
5658 """Fixture for manager used in tests."""
5759 yield MediationManager (profile )
5860
5961
6062@pytest .fixture
61- def record () -> MediationRecord :
63+ def record () -> Iterable [ MediationRecord ] :
6264 """Fixture for record used in tests."""
6365 yield MediationRecord (
6466 state = MediationRecord .STATE_GRANTED , connection_id = TEST_CONN_ID
@@ -71,7 +73,7 @@ class TestMediationManager: # pylint: disable=R0904,W0621
7173 async def test_create_manager_no_profile (self ):
7274 """test_create_manager_no_profile."""
7375 with pytest .raises (MediationManagerError ):
74- await MediationManager (None )
76+ MediationManager (None )
7577
7678 async def test_create_did (self , manager , session ):
7779 """test_create_did."""
@@ -363,7 +365,11 @@ async def test_add_remove_key_mix(self, manager):
363365 assert update .updates [0 ].recipient_key == TEST_VERKEY
364366 assert update .updates [1 ].recipient_key == TEST_ROUTE_VERKEY
365367
366- async def test_store_update_results (self , session , manager ):
368+ async def test_store_update_results (
369+ self ,
370+ session : ProfileSession ,
371+ manager : MediationManager ,
372+ ):
367373 """test_store_update_results."""
368374 await RouteRecord (
369375 role = RouteRecord .ROLE_CLIENT ,
@@ -467,6 +473,34 @@ async def test_store_update_results_errors(self, caplog, manager):
467473 assert "server_error" in caplog .text
468474 print (caplog .text )
469475
476+ async def test_notify_keylist_updated (
477+ self , manager : MediationManager , mock_event_bus : MockEventBus
478+ ):
479+ """test notify_keylist_updated."""
480+ response = KeylistUpdateResponse (
481+ updated = [
482+ KeylistUpdated (
483+ recipient_key = TEST_ROUTE_VERKEY ,
484+ action = KeylistUpdateRule .RULE_ADD ,
485+ result = KeylistUpdated .RESULT_SUCCESS ,
486+ ),
487+ KeylistUpdated (
488+ recipient_key = TEST_VERKEY ,
489+ action = KeylistUpdateRule .RULE_REMOVE ,
490+ result = KeylistUpdated .RESULT_SUCCESS ,
491+ ),
492+ ],
493+ )
494+ response .assign_thread_id (TEST_THREAD_ID )
495+ await manager .notify_keylist_updated (TEST_CONN_ID , response )
496+ assert mock_event_bus .events
497+ assert mock_event_bus .events [0 ][1 ].topic == manager .KEYLIST_UPDATED_EVENT
498+ assert mock_event_bus .events [0 ][1 ].payload == {
499+ "connection_id" : TEST_CONN_ID ,
500+ "thread_id" : TEST_THREAD_ID ,
501+ "updated" : [result .serialize () for result in response .updated ],
502+ }
503+
470504 async def test_get_my_keylist (self , session , manager ):
471505 """test_get_my_keylist."""
472506 await RouteRecord (
0 commit comments