Skip to content

Commit

Permalink
refactor: don't inject profile into context
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Jul 6, 2022
1 parent 44ef00c commit d1ea9dc
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 133 deletions.
3 changes: 0 additions & 3 deletions aries_cloudagent/config/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging
from typing import Tuple
import weakref

from ..core.error import ProfileNotFoundError
from ..core.profile import Profile, ProfileManager, ProfileSession
Expand Down Expand Up @@ -137,8 +136,6 @@ async def wallet_config(

await txn.commit()

context.injector.bind_instance(Profile, weakref.ref(profile))

return (profile, public_did_info)


Expand Down
2 changes: 1 addition & 1 deletion aries_cloudagent/multitenant/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async def create_wallet(

if public_did_info:
await profile.inject(RouteManager).route_public_did(
public_did_info.verkey
profile, public_did_info.verkey
)
except Exception:
await wallet_record.delete_record(session)
Expand Down
23 changes: 12 additions & 11 deletions aries_cloudagent/multitenant/route_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,29 @@
class MultitenantRouteManager(RouteManager):
"""Multitenancy route manager."""

def __init__(self, root_profile: Profile, sub_profile: Profile, wallet_id: str):
def __init__(
self,
root_profile: Profile,
):
"""Initialize multitenant route manager."""
self.root_profile = root_profile
self.wallet_id = wallet_id
super().__init__(sub_profile)

@property
def sub_profile(self) -> Profile:
"""Return reference to sub wallet profile."""
return self.profile

async def get_base_wallet_mediator(self) -> Optional[MediationRecord]:
"""Get base wallet's default mediator."""
return await MediationManager(self.root_profile).get_default_mediator()

async def _route_for_key(
self,
profile: Profile,
recipient_key: str,
mediation_record: Optional[MediationRecord] = None,
*,
skip_if_exists: bool = False,
replace_key: Optional[str] = None,
):
wallet_id = profile.settings["wallet.id"]
LOGGER.info(
f"Add route record for recipient {recipient_key} to wallet {self.wallet_id}"
f"Add route record for recipient {recipient_key} to wallet {wallet_id}"
)
routing_mgr = RoutingManager(self.root_profile)
mediation_mgr = MediationManager(self.root_profile)
Expand All @@ -66,7 +64,7 @@ async def _route_for_key(
pass

await routing_mgr.create_route_record(
recipient_key=recipient_key, internal_wallet_id=self.wallet_id
recipient_key=recipient_key, internal_wallet_id=wallet_id
)

# External mediation
Expand All @@ -86,7 +84,10 @@ async def _route_for_key(
return keylist_updates

async def routing_info(
self, my_endpoint: str, mediation_record: Optional[MediationRecord] = None
self,
profile: Profile,
my_endpoint: str,
mediation_record: Optional[MediationRecord] = None,
) -> Tuple[List[str], str]:
"""Return routing info."""
routing_keys = []
Expand Down
43 changes: 31 additions & 12 deletions aries_cloudagent/multitenant/tests/test_route_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,15 @@ def sub_profile(mock_responder: MockResponder, wallet_id: str):

@pytest.fixture
def route_manager(root_profile: Profile, sub_profile: Profile, wallet_id: str):
yield MultitenantRouteManager(root_profile, sub_profile, wallet_id)


def test_sub_profile_access(
route_manager: MultitenantRouteManager, sub_profile: Profile
):
assert route_manager.sub_profile == sub_profile
yield MultitenantRouteManager(root_profile)


@pytest.mark.asyncio
async def test_route_for_key_sub_mediator_no_base_mediator(
route_manager: MultitenantRouteManager,
mock_responder: MockResponder,
wallet_id: str,
sub_profile: Profile,
):
mediation_record = MediationRecord(
mediation_id="test-mediation-id", connection_id="test-mediator-conn-id"
Expand All @@ -72,6 +67,7 @@ async def test_route_for_key_sub_mediator_no_base_mediator(
RoutingManager, "create_route_record", mock.CoroutineMock()
) as mock_create_route_record:
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
mediation_record,
skip_if_exists=False,
Expand All @@ -94,6 +90,7 @@ async def test_route_for_key_sub_mediator_no_base_mediator(

@pytest.mark.asyncio
async def test_route_for_key_sub_mediator_and_base_mediator(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
mock_responder: MockResponder,
wallet_id: str,
Expand All @@ -114,6 +111,7 @@ async def test_route_for_key_sub_mediator_and_base_mediator(
RoutingManager, "create_route_record", mock.CoroutineMock()
) as mock_create_route_record:
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
mediation_record,
skip_if_exists=False,
Expand All @@ -136,6 +134,7 @@ async def test_route_for_key_sub_mediator_and_base_mediator(

@pytest.mark.asyncio
async def test_route_for_key_base_mediator_no_sub_mediator(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
mock_responder: MockResponder,
wallet_id: str,
Expand All @@ -153,7 +152,11 @@ async def test_route_for_key_base_mediator_no_sub_mediator(
RoutingManager, "create_route_record", mock.CoroutineMock()
) as mock_create_route_record:
keylist_update = await route_manager._route_for_key(
"test-recipient-key", None, skip_if_exists=False, replace_key=None
sub_profile,
"test-recipient-key",
None,
skip_if_exists=False,
replace_key=None,
)

mock_create_route_record.assert_called_once_with(
Expand All @@ -172,6 +175,7 @@ async def test_route_for_key_base_mediator_no_sub_mediator(

@pytest.mark.asyncio
async def test_route_for_key_skip_if_exists_and_exists(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
mock_responder: MockResponder,
):
Expand All @@ -182,6 +186,7 @@ async def test_route_for_key_skip_if_exists_and_exists(
RouteRecord, "retrieve_by_recipient_key", mock.CoroutineMock()
):
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
mediation_record,
skip_if_exists=True,
Expand All @@ -193,6 +198,7 @@ async def test_route_for_key_skip_if_exists_and_exists(

@pytest.mark.asyncio
async def test_route_for_key_skip_if_exists_and_absent(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
mock_responder: MockResponder,
):
Expand All @@ -205,6 +211,7 @@ async def test_route_for_key_skip_if_exists_and_absent(
mock.CoroutineMock(side_effect=StorageNotFoundError),
):
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
mediation_record,
skip_if_exists=True,
Expand All @@ -223,13 +230,15 @@ async def test_route_for_key_skip_if_exists_and_absent(

@pytest.mark.asyncio
async def test_route_for_key_replace_key(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
mock_responder: MockResponder,
):
mediation_record = MediationRecord(
mediation_id="test-mediation-id", connection_id="test-mediator-conn-id"
)
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
mediation_record,
skip_if_exists=False,
Expand All @@ -249,10 +258,12 @@ async def test_route_for_key_replace_key(

@pytest.mark.asyncio
async def test_route_for_key_no_mediator(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
):
assert (
await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
None,
skip_if_exists=True,
Expand All @@ -264,6 +275,7 @@ async def test_route_for_key_no_mediator(

@pytest.mark.asyncio
async def test_routing_info_with_mediator(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
):
mediation_record = MediationRecord(
Expand All @@ -273,23 +285,27 @@ async def test_routing_info_with_mediator(
endpoint="http://mediator.example.com",
)
keys, endpoint = await route_manager.routing_info(
"http://example.com", mediation_record
sub_profile, "http://example.com", mediation_record
)
assert keys == mediation_record.routing_keys
assert endpoint == mediation_record.endpoint


@pytest.mark.asyncio
async def test_routing_info_no_mediator(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
):
keys, endpoint = await route_manager.routing_info("http://example.com", None)
keys, endpoint = await route_manager.routing_info(
sub_profile, "http://example.com", None
)
assert keys == []
assert endpoint == "http://example.com"


@pytest.mark.asyncio
async def test_routing_info_with_base_mediator(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
):
base_mediation_record = MediationRecord(
Expand All @@ -304,13 +320,16 @@ async def test_routing_info_with_base_mediator(
"get_base_wallet_mediator",
mock.CoroutineMock(return_value=base_mediation_record),
):
keys, endpoint = await route_manager.routing_info("http://example.com", None)
keys, endpoint = await route_manager.routing_info(
sub_profile, "http://example.com", None
)
assert keys == base_mediation_record.routing_keys
assert endpoint == base_mediation_record.endpoint


@pytest.mark.asyncio
async def test_routing_info_with_base_mediator_and_sub_mediator(
sub_profile: Profile,
route_manager: MultitenantRouteManager,
):
mediation_record = MediationRecord(
Expand All @@ -332,7 +351,7 @@ async def test_routing_info_with_base_mediator_and_sub_mediator(
mock.CoroutineMock(return_value=base_mediation_record),
):
keys, endpoint = await route_manager.routing_info(
"http://example.com", mediation_record
sub_profile, "http://example.com", mediation_record
)
assert keys == [*base_mediation_record.routing_keys, *mediation_record.routing_keys]
assert endpoint == mediation_record.endpoint
23 changes: 15 additions & 8 deletions aries_cloudagent/protocols/connections/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ async def create_invitation(
# Mediation Record can still be None after this operation if no
# mediation id passed and no default
mediation_record = await self._route_manager.mediation_record_if_id(
self.profile,
mediation_id,
or_default=True,
)
Expand Down Expand Up @@ -159,7 +160,7 @@ async def create_invitation(

# Add mapping for multitenant relaying.
# Mediation of public keys is not supported yet
await self._route_manager.route_public_did(public_did.verkey)
await self._route_manager.route_public_did(self.profile, public_did.verkey)

return None, invitation

Expand Down Expand Up @@ -206,8 +207,11 @@ async def create_invitation(
async with self.profile.session() as session:
await connection.save(session, reason="Created new invitation")

await self._route_manager.route_invitation(connection, mediation_record)
await self._route_manager.route_invitation(
self.profile, connection, mediation_record
)
routing_keys, my_endpoint = await self._route_manager.routing_info(
self.profile,
my_endpoint or cast(str, self.profile.settings.get("default_endpoint")),
mediation_record,
)
Expand Down Expand Up @@ -297,7 +301,7 @@ async def receive_invitation(
await connection.attach_invitation(session, invitation)

await self._route_manager.save_mediator_for_connection(
connection, mediation_id=mediation_id
self.profile, connection, mediation_id=mediation_id
)

if connection.accept == ConnRecord.ACCEPT_AUTO:
Expand Down Expand Up @@ -335,6 +339,7 @@ async def create_request(
"""

mediation_record = await self._route_manager.mediation_record_for_connection(
self.profile,
connection,
mediation_id,
or_default=True,
Expand All @@ -360,7 +365,7 @@ async def create_request(

# Idempotent; if routing has already been set up, no action taken
await self._route_manager.route_connection_as_invitee(
connection, mediation_record
self.profile, connection, mediation_record
)

# Create connection request message
Expand Down Expand Up @@ -579,7 +584,7 @@ async def create_response(
)

mediation_record = await self._route_manager.mediation_record_for_connection(
connection, mediation_id
self.profile, connection, mediation_id
)

# Multitenancy setup
Expand Down Expand Up @@ -613,7 +618,7 @@ async def create_response(

# Idempotent; if routing has already been set up, no action taken
await self._route_manager.route_connection_as_inviter(
connection, mediation_record
self.profile, connection, mediation_record
)

# Create connection response message
Expand Down Expand Up @@ -863,7 +868,7 @@ async def create_static_connection(

# Routing
mediation_record = await self._route_manager.mediation_record_if_id(
mediation_id, or_default=True
self.profile, mediation_id, or_default=True
)

multitenant_mgr = self.profile.inject_or(BaseMultitenantManager)
Expand All @@ -873,7 +878,9 @@ async def create_static_connection(
if multitenant_mgr and wallet_id:
base_mediation_record = await multitenant_mgr.get_default_mediator()

await self._route_manager.route_static(connection, mediation_record)
await self._route_manager.route_static(
self.profile, connection, mediation_record
)

# Synthesize their DID doc
did_doc = await self.create_did_document(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async def test_create_invitation_public(self):
assert connect_record is None
assert connect_invite.did.endswith(self.test_did)
self.route_manager.route_public_did.assert_called_once_with(
self.test_verkey
self.profile, self.test_verkey
)

async def test_create_invitation_public_no_public_invites(self):
Expand Down Expand Up @@ -356,7 +356,7 @@ async def test_create_invitation_mediation_using_default(self):
assert invite.routing_keys == self.test_mediator_routing_keys
assert invite.endpoint == self.test_mediator_endpoint
self.route_manager.routing_info.assert_awaited_once_with(
self.test_endpoint, mediation_record
self.profile, self.test_endpoint, mediation_record
)

async def test_receive_invitation(self):
Expand Down
Loading

0 comments on commit d1ea9dc

Please sign in to comment.