Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: warnings in tests from IndySdkProfile #1865

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion aries_cloudagent/indy/sdk/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,15 @@ def _make_finalizer(self, opened: IndyOpenWallet) -> finalize:
See docs for weakref.finalize for more details on behavior of finalizers.
"""

async def _closer(opened: IndyOpenWallet):
try:
await opened.close()
except Exception:
LOGGER.exception("Failed to close wallet from finalizer")

def _finalize(opened: IndyOpenWallet):
LOGGER.debug("Profile finalizer called; closing wallet")
asyncio.get_event_loop().create_task(opened.close())
asyncio.get_event_loop().create_task(_closer(opened))

return finalize(self, _finalize, opened)

Expand Down
3 changes: 2 additions & 1 deletion aries_cloudagent/indy/sdk/tests/test_issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ async def setUp(self):
"name": "test-wallet",
}
).create_wallet()
self.profile = IndySdkProfile(self.wallet, self.context)
with async_mock.patch.object(IndySdkProfile, "_make_finalizer"):
self.profile = IndySdkProfile(self.wallet, self.context)
self.issuer = test_module.IndySdkIssuer(self.profile)

async def tearDown(self):
Expand Down
19 changes: 13 additions & 6 deletions aries_cloudagent/indy/sdk/tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import asyncio
import logging
import pytest

from asynctest import mock as async_mock
import pytest

from ....config.injection_context import InjectionContext
from ....core.error import ProfileError
from ....ledger.indy import IndySdkLedgerPool

from ..profile import IndySdkProfile
from ..wallet_setup import IndyWalletConfig, IndyOpenWallet
from ..wallet_setup import IndyOpenWallet, IndyWalletConfig


@pytest.fixture
async def open_wallet():
yield IndyOpenWallet(
opened = IndyOpenWallet(
config=IndyWalletConfig({"name": "test-profile"}),
created=True,
handle=1,
master_secret_id="master-secret",
)
with async_mock.patch.object(opened, "close", async_mock.CoroutineMock()):
yield opened


@pytest.fixture()
async def profile(open_wallet):
context = InjectionContext()
context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name"))
yield IndySdkProfile(open_wallet, context)
profile = IndySdkProfile(open_wallet, context)

yield profile

# Trigger finalizer before event loop fixture is closed
profile._finalizer()


@pytest.mark.asyncio
async def test_properties(profile):
async def test_properties(profile: IndySdkProfile):
assert profile.name == "test-profile"
assert profile.backend == "indy"
assert profile.wallet and profile.wallet.handle == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ async def test_provide_invalid_manager(self):
@pytest.mark.indy
async def test_provide_indy_manager(self):
context = InjectionContext()
profile = IndySdkProfile(
IndyOpenWallet(
config=IndyWalletConfig({"name": "test-profile"}),
created=True,
handle=1,
master_secret_id="master-secret",
),
context,
)
with async_mock.patch.object(IndySdkProfile, "_make_finalizer"):
profile = IndySdkProfile(
IndyOpenWallet(
config=IndyWalletConfig({"name": "test-profile"}),
created=True,
handle=1,
master_secret_id="master-secret",
),
context,
)
context.injector.bind_instance(
BaseLedger, IndySdkLedger(IndySdkLedgerPool("name"), profile)
)
Expand Down
9 changes: 5 additions & 4 deletions aries_cloudagent/ledger/tests/test_indy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ async def setUp(self):
self.test_verkey = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
context = InjectionContext()
context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name"))
self.profile = IndySdkProfile(
async_mock.CoroutineMock(),
context,
)
with async_mock.patch.object(IndySdkProfile, "_make_finalizer"):
self.profile = IndySdkProfile(
async_mock.CoroutineMock(),
context,
)
self.session = await self.profile.session()

@async_mock.patch("indy.pool.create_pool_ledger_config")
Expand Down
2 changes: 0 additions & 2 deletions aries_cloudagent/ledger/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from asynctest import mock as async_mock, TestCase as AsyncTestCase
from typing import Tuple

from ...admin.request_context import AdminRequestContext
from ...core.in_memory import InMemoryProfile
from ...ledger.base import BaseLedger
from ...ledger.endpoint_type import EndpointType
Expand All @@ -10,7 +9,6 @@
)
from ...ledger.multiple_ledger.base_manager import (
BaseMultipleLedgerManager,
MultipleLedgerManagerError,
)
from ...multitenant.base import BaseMultitenantManager
from ...multitenant.manager import MultitenantManager
Expand Down
35 changes: 21 additions & 14 deletions aries_cloudagent/storage/tests/test_indy_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from asynctest import mock as async_mock

from ...config.injection_context import InjectionContext
from ...indy.sdk.profile import IndySdkProfileManager
from ...indy.sdk.profile import IndySdkProfileManager, IndySdkProfile
from ...storage.base import BaseStorage
from ...storage.error import StorageError, StorageSearchError
from ...storage.indy import IndySdkStorage
Expand All @@ -28,16 +28,17 @@ async def make_profile():
key = await IndySdkWallet.generate_wallet_key()
context = InjectionContext()
context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name"))
return await IndySdkProfileManager().provision(
context,
{
"auto_recreate": True,
"auto_remove": True,
"name": "test-wallet",
"key": key,
"key_derivation_method": "RAW", # much slower tests with argon-hashed keys
},
)
with async_mock.patch.object(IndySdkProfile, "_make_finalizer"):
return await IndySdkProfileManager().provision(
context,
{
"auto_recreate": True,
"auto_remove": True,
"name": "test-wallet",
"key": key,
"key_derivation_method": "RAW", # much slower tests with argon-hashed keys
},
)


@pytest.fixture()
Expand Down Expand Up @@ -75,7 +76,9 @@ async def test_record(self):
indy.wallet, "close_wallet", async_mock.CoroutineMock()
) as mock_close, async_mock.patch.object(
indy.wallet, "delete_wallet", async_mock.CoroutineMock()
) as mock_delete:
) as mock_delete, async_mock.patch.object(
IndySdkProfile, "_make_finalizer"
):
config = {
"auto_recreate": True,
"auto_remove": True,
Expand Down Expand Up @@ -244,7 +247,9 @@ async def test_storage_search_x(self):
indy.wallet, "close_wallet", async_mock.CoroutineMock()
) as mock_close, async_mock.patch.object(
indy.wallet, "delete_wallet", async_mock.CoroutineMock()
) as mock_delete:
) as mock_delete, async_mock.patch.object(
IndySdkProfile, "_make_finalizer"
):
context = InjectionContext()
context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name"))
fake_profile = await IndySdkProfileManager().provision(
Expand Down Expand Up @@ -322,7 +327,9 @@ async def test_storage_del_close(self):
indy.wallet, "close_wallet", async_mock.CoroutineMock()
) as mock_close, async_mock.patch.object(
indy.wallet, "delete_wallet", async_mock.CoroutineMock()
) as mock_delete:
) as mock_delete, async_mock.patch.object(
IndySdkProfile, "_make_finalizer"
):
context = InjectionContext()
context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name"))
fake_profile = await IndySdkProfileManager().provision(
Expand Down
25 changes: 14 additions & 11 deletions aries_cloudagent/storage/vc_holder/tests/test_indy_vc_holder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
from asynctest import mock as async_mock


from ....config.injection_context import InjectionContext
from ....indy.sdk.profile import IndySdkProfileManager
from ....indy.sdk.profile import IndySdkProfileManager, IndySdkProfile
from ....ledger.indy import IndySdkLedgerPool
from ....wallet.indy import IndySdkWallet

Expand All @@ -25,16 +26,18 @@ async def make_profile():
key = await IndySdkWallet.generate_wallet_key()
context = InjectionContext()
context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name"))
return await IndySdkProfileManager().provision(
context,
{
"auto_recreate": True,
"auto_remove": True,
"name": "test-wallet",
"key": key,
"key_derivation_method": "RAW", # much slower tests with argon-hashed keys
},
)

with async_mock.patch.object(IndySdkProfile, "_make_finalizer"):
return await IndySdkProfileManager().provision(
context,
{
"auto_recreate": True,
"auto_remove": True,
"name": "test-wallet",
"key": key,
"key_derivation_method": "RAW", # much slower tests with argon-hashed keys
},
)


@pytest.fixture()
Expand Down
27 changes: 16 additions & 11 deletions aries_cloudagent/wallet/tests/test_indy_wallet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
from typing import cast

import indy.anoncreds
import indy.crypto
Expand All @@ -13,7 +14,7 @@
from ...config.injection_context import InjectionContext
from ...core.error import ProfileError, ProfileDuplicateError, ProfileNotFoundError
from ...indy.sdk import wallet_setup as test_setup_module
from ...indy.sdk.profile import IndySdkProfileManager
from ...indy.sdk.profile import IndySdkProfile, IndySdkProfileManager
from ...indy.sdk.wallet_setup import IndyWalletConfig
from ...ledger.endpoint_type import EndpointType
from ...wallet.key_type import KeyType
Expand All @@ -40,16 +41,20 @@ async def wallet():
key = await IndySdkWallet.generate_wallet_key()
context = InjectionContext()
context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name"))
profile = await IndySdkProfileManager().provision(
context,
{
"auto_recreate": True,
"auto_remove": True,
"name": "test-wallet",
"key": key,
"key_derivation_method": "RAW", # much slower tests with argon-hashed keys
},
)
with async_mock.patch.object(IndySdkProfile, "_make_finalizer"):
profile = cast(
IndySdkProfile,
await IndySdkProfileManager().provision(
context,
{
"auto_recreate": True,
"auto_remove": True,
"name": "test-wallet",
"key": key,
"key_derivation_method": "RAW", # much slower tests with argon-hashed keys
},
),
)
async with profile.session() as session:
yield session.inject(BaseWallet)
await profile.close()
Expand Down