Skip to content

Commit

Permalink
Rename util / fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <[email protected]>
  • Loading branch information
jamshale committed Feb 19, 2024
1 parent c661970 commit 977e38a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 13 deletions.
2 changes: 1 addition & 1 deletion aries_cloudagent/multitenant/admin/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ...messaging.valid import UUID4_EXAMPLE, JSONWebToken
from ...multitenant.base import BaseMultitenantManager
from ...storage.error import StorageError, StorageNotFoundError
from ...utils.endorsement import attempt_auto_author_with_endorser_setup
from ...utils.endorsement_setup import attempt_auto_author_with_endorser_setup
from ...wallet.error import WalletSettingsError
from ...wallet.models.wallet_record import WalletRecord, WalletRecordSchema
from ..error import WalletKeyMissingError
Expand Down
36 changes: 29 additions & 7 deletions aries_cloudagent/multitenant/admin/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from unittest import IsolatedAsyncioTestCase
from aries_cloudagent.tests import mock

import pytest
from marshmallow.exceptions import ValidationError

from ...base import BaseMultitenantManager, MultitenantManagerError
from aries_cloudagent.tests import mock

from ....admin.request_context import AdminRequestContext
from ....wallet.models.wallet_record import WalletRecord
from ....messaging.models.base import BaseModelError
from ....storage.error import StorageError, StorageNotFoundError

from ....wallet.models.wallet_record import WalletRecord
from ...base import BaseMultitenantManager, MultitenantManagerError
from .. import routes as test_module


Expand Down Expand Up @@ -139,6 +141,7 @@ async def test_wallets_list_query(self):
}
)

@pytest.mark.asyncio(scope="module")
async def test_wallet_create_tenant_settings(self):
body = {
"wallet_name": "test",
Expand Down Expand Up @@ -173,7 +176,10 @@ async def test_wallet_create_tenant_settings(self):
self.mock_multitenant_mgr.create_auth_token = mock.CoroutineMock(
return_value="test_token"
)
print(self.request["context"])
self.mock_multitenant_mgr.get_wallet_profile = mock.CoroutineMock(
return_value=mock.MagicMock()
)

await test_module.wallet_create(self.request)

self.mock_multitenant_mgr.create_wallet.assert_called_once_with(
Expand All @@ -195,6 +201,7 @@ async def test_wallet_create_tenant_settings(self):
mock_response.assert_called_once_with(
{**test_module.format_wallet_record(wallet_mock), "token": "test_token"}
)
assert self.mock_multitenant_mgr.get_wallet_profile.called

async def test_wallet_create(self):
body = {
Expand Down Expand Up @@ -225,7 +232,10 @@ async def test_wallet_create(self):
self.mock_multitenant_mgr.create_auth_token = mock.CoroutineMock(
return_value="test_token"
)
print(self.request["context"])
self.mock_multitenant_mgr.get_wallet_profile = mock.CoroutineMock(
return_value=mock.MagicMock()
)

await test_module.wallet_create(self.request)

self.mock_multitenant_mgr.create_wallet.assert_called_once_with(
Expand All @@ -242,8 +252,12 @@ async def test_wallet_create(self):
wallet_mock, body["wallet_key"]
)
mock_response.assert_called_once_with(
{**test_module.format_wallet_record(wallet_mock), "token": "test_token"}
{
**test_module.format_wallet_record(wallet_mock),
"token": "test_token",
}
)
assert self.mock_multitenant_mgr.get_wallet_profile.called

async def test_wallet_create_x(self):
body = {}
Expand Down Expand Up @@ -277,6 +291,9 @@ async def test_wallet_create_optional_default_fields(self):
return_value=mock.MagicMock()
)
self.mock_multitenant_mgr.create_auth_token = mock.CoroutineMock()
self.mock_multitenant_mgr.get_wallet_profile = mock.CoroutineMock(
return_value=mock.MagicMock()
)

await test_module.wallet_create(self.request)
self.mock_multitenant_mgr.create_wallet.assert_called_once_with(
Expand All @@ -292,6 +309,7 @@ async def test_wallet_create_optional_default_fields(self):
},
WalletRecord.MODE_MANAGED,
)
assert self.mock_multitenant_mgr.get_wallet_profile.called

async def test_wallet_create_raw_key_derivation(self):
body = {
Expand All @@ -306,6 +324,9 @@ async def test_wallet_create_raw_key_derivation(self):
return_value=mock.MagicMock()
)
self.mock_multitenant_mgr.create_auth_token = mock.CoroutineMock()
self.mock_multitenant_mgr.get_wallet_profile = mock.CoroutineMock(
return_value=mock.MagicMock()
)

await test_module.wallet_create(self.request)
self.mock_multitenant_mgr.create_wallet.assert_called_once_with(
Expand All @@ -319,6 +340,7 @@ async def test_wallet_create_raw_key_derivation(self):
},
WalletRecord.MODE_MANAGED,
)
assert self.mock_multitenant_mgr.get_wallet_profile.called

async def test_wallet_update_tenant_settings(self):
self.request.match_info = {"wallet_id": "test-wallet-id"}
Expand Down
13 changes: 10 additions & 3 deletions aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from aries_cloudagent.tests import mock

from .....admin.server import AdminResponder
from .....cache.base import BaseCache
from .....cache.in_memory import InMemoryCache
from .....connections.models.conn_record import ConnRecord
Expand Down Expand Up @@ -72,7 +73,7 @@ def make_did_doc(self, did, verkey):
class TestDidExchangeManager(IsolatedAsyncioTestCase, TestConfig):
async def asyncSetUp(self):
self.responder = MockResponder()

self.responder.send_fn = mock.CoroutineMock()
self.oob_mock = mock.MagicMock(
clean_finished_oob_record=mock.CoroutineMock(return_value=None)
)
Expand Down Expand Up @@ -181,7 +182,9 @@ async def test_receive_invitation(self):
test_module, "AttachDecorator", autospec=True
) as mock_attach_deco, mock.patch.object(
self.multitenant_mgr, "get_default_mediator"
) as mock_get_default_mediator:
) as mock_get_default_mediator, mock.patch.object(
AdminResponder, "send_reply"
) as mock_send_reply:
mock_get_default_mediator.return_value = mediation_record
invi_rec = await self.oob_manager.create_invitation(
my_endpoint="testendpoint",
Expand All @@ -195,6 +198,7 @@ async def test_receive_invitation(self):
)
invitee_record = await self.manager.receive_invitation(invi_msg)
assert invitee_record.state == ConnRecord.State.REQUEST.rfc23
assert mock_send_reply.called

async def test_receive_invitation_oob_public_did(self):
async with self.profile.session() as session:
Expand All @@ -211,7 +215,9 @@ async def test_receive_invitation_oob_public_did(self):
self.multitenant_mgr, "get_default_mediator"
) as mock_get_default_mediator, mock.patch.object(
self.manager, "resolve_connection_targets", mock.CoroutineMock()
) as mock_resolve_targets:
) as mock_resolve_targets, mock.patch.object(
AdminResponder, "send_reply"
) as mock_send_reply:
mock_resolve_targets.return_value = [
mock.MagicMock(recipient_keys=["test"])
]
Expand All @@ -231,6 +237,7 @@ async def test_receive_invitation_oob_public_did(self):
invi_msg, their_public_did=public_did_info.did
)
assert invitee_record.state == ConnRecord.State.REQUEST.rfc23
assert mock_send_reply.called

async def test_receive_invitation_no_auto_accept(self):
async with self.profile.session() as session:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ....messaging.models.openapi import OpenAPISchema
from ....messaging.valid import UUID4_EXAMPLE
from ....storage.error import StorageError, StorageNotFoundError
from ....utils.endorsement import attempt_auto_author_with_endorser_setup
from ....utils.endorsement_setup import attempt_auto_author_with_endorser_setup
from .manager import TransactionManager, TransactionManagerError
from .models.transaction_record import TransactionRecord, TransactionRecordSchema
from .transaction_jobs import TransactionJob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ async def attempt_auto_author_with_endorser_setup(profile: Profile):
)

except Exception:
# log the error, but continue
LOGGER.info(
"Error accepting endorser invitation/configuring endorser connection"
)
64 changes: 64 additions & 0 deletions aries_cloudagent/utils/tests/test_endorsement_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from unittest import IsolatedAsyncioTestCase

from aries_cloudagent.tests import mock

from ...connections.models.conn_record import ConnRecord
from ...core.in_memory.profile import InMemoryProfile
from .. import endorsement_setup
from ..endorsement_setup import attempt_auto_author_with_endorser_setup

mock_invitation = "http://localhost:9030?oob=eyJAdHlwZSI6ICJodHRwczovL2RpZGNvbW0ub3JnL291dC1vZi1iYW5kLzEuMS9pbnZpdGF0aW9uIiwgIkBpZCI6ICI2MWU1MmYzZS1kNTliLTQ3OWYtYmYwNC04NjJlOTk1MmM4MDYiLCAibGFiZWwiOiAiZW5kb3JzZXIiLCAiaGFuZHNoYWtlX3Byb3RvY29scyI6IFsiaHR0cHM6Ly9kaWRjb21tLm9yZy9kaWRleGNoYW5nZS8xLjAiXSwgInNlcnZpY2VzIjogW3siaWQiOiAiI2lubGluZSIsICJ0eXBlIjogImRpZC1jb21tdW5pY2F0aW9uIiwgInJlY2lwaWVudEtleXMiOiBbImRpZDprZXk6ejZNa2VkRDMyZlZmOG5ReG5SS2QzUmQ5S1hZQnVETEJiOHUyM1JWMm1ReFlpanR2I3o2TWtlZEQzMmZWZjhuUXhuUktkM1JkOUtYWUJ1RExCYjh1MjNSVjJtUXhZaWp0diJdLCAic2VydmljZUVuZHBvaW50IjogImh0dHA6Ly9sb2NhbGhvc3Q6OTAzMCJ9XX0="


class MockConnRecord:
connection_id = "test-connection-id"


class TestEndorsementSetupUtil(IsolatedAsyncioTestCase):
def setUp(self) -> None:
self.profile = InMemoryProfile.test_profile()

@mock.patch.object(endorsement_setup.LOGGER, "info", return_value=mock.MagicMock())
async def test_not_enough_configs_for_connection(self, mock_logger):
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

# No invitation
self.profile.settings.set_value("endorser.author", True)
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

# No endorser alias
self.profile.settings.set_value("endorser.endorser_invitation", mock_invitation)
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

# No endorser DID
self.profile.settings.set_value("endorser.endorser_alias", "test-alias")
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

assert mock_logger.call_count == 3
for call in mock_logger.call_args_list:
assert "Error accepting endorser invitation" not in call[0][0]

@mock.patch.object(endorsement_setup.LOGGER, "info", return_value=mock.MagicMock())
@mock.patch.object(endorsement_setup, "OutOfBandManager")
@mock.patch.object(
ConnRecord,
"retrieve_by_id",
return_value=ConnRecord(connection_id="test-connection-id"),
)
async def test_create_connection_with_valid_invitation(
self, mock_conn_record, mock_oob_manager, mock_logger
):
mock_oob_manager.return_value.receive_invitation = mock.CoroutineMock(
return_value=MockConnRecord()
)
self.profile.settings.set_value("endorser.author", True)
self.profile.settings.set_value("endorser.endorser_invitation", mock_invitation)
self.profile.settings.set_value("endorser.endorser_alias", "test-alias")
self.profile.settings.set_value("endorser.endorser_public_did", "test-did")

await attempt_auto_author_with_endorser_setup(self.profile)

for call in mock_logger.call_args_list:
assert "Error accepting endorser invitation" not in call[0][0]

assert mock_conn_record.called

0 comments on commit 977e38a

Please sign in to comment.