Skip to content

Commit

Permalink
Remove in-memory wallet class
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <[email protected]>
  • Loading branch information
jamshale committed Oct 29, 2024
1 parent 31417cb commit ff56261
Show file tree
Hide file tree
Showing 253 changed files with 7,941 additions and 12,037 deletions.
1 change: 1 addition & 0 deletions .devcontainer/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ markers = [
"postgres: Tests relating to the postgres storage plugin for Indy"]
junit_family = "xunit1"
asyncio_mode = auto
asyncio_default_fixture_loop_scope = session
EOF
2 changes: 1 addition & 1 deletion .github/actions/run-unit-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ runs:
- name: Tests
shell: bash
run: |
poetry run pytest --cov=acapy_agent --cov-report term-missing --cov-report xml --ignore-glob=/tests/* --ignore-glob=demo/* --ignore-glob=docker/* --ignore-glob=docs/* --ignore-glob=scripts/* --ignore-glob=scenarios/* 2>&1 | tee pytest.log
poetry run pytest -n auto --cov=acapy_agent --cov-report term-missing --cov-report xml --ignore-glob=/tests/* --ignore-glob=demo/* --ignore-glob=docker/* --ignore-glob=docs/* --ignore-glob=scripts/* --ignore-glob=scenarios/* 2>&1 | tee pytest.log
PYTEST_EXIT_CODE=${PIPESTATUS[0]}
if grep -Eq "RuntimeWarning: coroutine .* was never awaited" pytest.log; then
echo "Failure: Detected unawaited coroutine warning in pytest output."
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ coverage.xml
.hypothesis/
.pytest_cache/
test-reports/
test.lock

# Translations
*.mo
Expand Down
7 changes: 2 additions & 5 deletions acapy_agent/admin/request_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from ..config.injector import InjectionError, Injector, InjectType
from ..config.settings import Settings
from ..core.profile import Profile, ProfileSession
from ..utils.classloader import DeferLoad

IN_MEM = DeferLoad("acapy_agent.core.in_memory.InMemoryProfile")


class AdminRequestContext:
Expand Down Expand Up @@ -112,10 +109,10 @@ def update_settings(self, settings: Mapping[str, object]):

@classmethod
def test_context(
cls, session_inject: Optional[dict] = None, profile: Optional[Profile] = None
cls, session_inject: dict, profile: Profile
) -> "AdminRequestContext":
"""Quickly set up a new admin request context for tests."""
ctx = AdminRequestContext(profile or IN_MEM.resolved.test_profile())
ctx = AdminRequestContext(profile)
setattr(ctx, "session_inject", {} if session_inject is None else session_inject)
setattr(ctx, "session", ctx._test_session)
return ctx
Expand Down
64 changes: 29 additions & 35 deletions acapy_agent/admin/tests/test_admin_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
from aiohttp import ClientSession, DummyCookieJar, TCPConnector, web
from aiohttp.test_utils import unused_port

from acapy_agent.tests import mock
from acapy_agent.wallet import singletons

from ...askar.profile import AskarProfile
from ...config.default_context import DefaultContextBuilder
from ...config.injection_context import InjectionContext
from ...core.event_bus import Event
from ...core.goal_code_registry import GoalCodeRegistry
from ...core.in_memory import InMemoryProfile
from ...core.protocol_registry import ProtocolRegistry
from ...storage.base import BaseStorage
from ...storage.record import StorageRecord
from ...storage.type import RECORD_TYPE_ACAPY_UPGRADING
from ...tests import mock
from ...utils.stats import Collector
from ...utils.task_queue import TaskQueue
from ...utils.testing import create_test_profile
from ...wallet import singletons
from ...wallet.anoncreds_upgrade import UPGRADING_RECORD_IN_PROGRESS
from .. import server as test_module
from ..request_context import AdminRequestContext
Expand All @@ -43,11 +43,6 @@ async def asyncSetUp(self):
cookie_jar=DummyCookieJar(), connector=self.connector
)

async def asyncTearDown(self):
if self.client_session:
await self.client_session.close()
self.client_session = None

async def test_debug_middleware(self):
with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger:
mock_logger.isEnabledFor = mock.MagicMock(return_value=True)
Expand Down Expand Up @@ -108,7 +103,7 @@ async def test_ready_middleware(self):
with self.assertRaises(KeyError):
await test_module.ready_middleware(request, handler)

def get_admin_server(
async def get_admin_server(
self, settings: Optional[dict] = None, context: Optional[InjectionContext] = None
) -> AdminServer:
if not context:
Expand All @@ -122,18 +117,16 @@ def get_admin_server(
plugin_registry = mock.MagicMock(test_module.PluginRegistry, autospec=True)
plugin_registry.post_process_routes = mock.MagicMock()
context.injector.bind_instance(test_module.PluginRegistry, plugin_registry)
context.injector.bind_instance(test_module.Collector, Collector())

collector = Collector()
context.injector.bind_instance(test_module.Collector, collector)

profile = InMemoryProfile.test_profile(settings=settings)
self.profile = await create_test_profile(settings=settings)

self.port = unused_port()
return AdminServer(
"0.0.0.0",
self.port,
context,
profile,
self.profile,
self.outbound_message_router,
self.webhook_router,
conductor_stop=mock.CoroutineMock(),
Expand All @@ -151,25 +144,25 @@ def webhook_router(self, *args):

async def test_start_stop(self):
with self.assertRaises(AssertionError):
await self.get_admin_server().start()
await (await self.get_admin_server()).start()

settings = {"admin.admin_insecure_mode": False}
with self.assertRaises(AssertionError):
await self.get_admin_server(settings).start()
await (await self.get_admin_server(settings)).start()

settings = {
"admin.admin_insecure_mode": True,
"admin.admin_api_key": "test-api-key",
}
with self.assertRaises(AssertionError):
await self.get_admin_server(settings).start()
await (await self.get_admin_server(settings)).start()

settings = {
"admin.admin_insecure_mode": False,
"admin.admin_client_max_request_size": 4,
"admin.admin_api_key": "test-api-key",
}
server = self.get_admin_server(settings)
server = await self.get_admin_server(settings)
await server.start()
assert server.app._client_max_size == 4 * 1024 * 1024
with mock.patch.object(server, "websocket_queues", mock.MagicMock()) as mock_wsq:
Expand All @@ -181,7 +174,7 @@ async def test_start_stop(self):
with mock.patch.object(web.TCPSite, "start", mock.CoroutineMock()) as mock_start:
mock_start.side_effect = OSError("Failure to launch")
with self.assertRaises(AdminSetupError):
await self.get_admin_server(settings).start()
await (await self.get_admin_server(settings)).start()

async def test_import_routes(self):
# this test just imports all default admin routes
Expand All @@ -190,8 +183,8 @@ async def test_import_routes(self):
context.injector.bind_instance(ProtocolRegistry, ProtocolRegistry())
context.injector.bind_instance(GoalCodeRegistry, GoalCodeRegistry())
await DefaultContextBuilder().load_plugins(context)
server = self.get_admin_server({"admin.admin_insecure_mode": True}, context)
app = await server.make_application()
server = await self.get_admin_server({"admin.admin_insecure_mode": True}, context)
await server.make_application()

async def test_register_external_plugin_x(self):
context = InjectionContext()
Expand All @@ -206,7 +199,7 @@ async def test_register_external_plugin_x(self):

async def test_visit_insecure_mode(self):
settings = {"admin.admin_insecure_mode": True, "task_queue": True}
server = self.get_admin_server(settings)
server = await self.get_admin_server(settings)
await server.start()

async with self.client_session.post(
Expand Down Expand Up @@ -240,7 +233,7 @@ async def test_visit_secure_mode(self):
"admin.admin_insecure_mode": False,
"admin.admin_api_key": "test-api-key",
}
server = self.get_admin_server(settings)
server = await self.get_admin_server(settings)
await server.start()

async with self.client_session.get(
Expand Down Expand Up @@ -297,7 +290,7 @@ async def test_query_config(self):
"wallet.seed": "00000000000000000000000000000000",
"wallet.storage.creds": "secret",
}
server = self.get_admin_server(settings)
server = await self.get_admin_server(settings)
await server.start()

async with self.client_session.get(
Expand Down Expand Up @@ -326,7 +319,7 @@ async def test_visit_shutting_down(self):
settings = {
"admin.admin_insecure_mode": True,
}
server = self.get_admin_server(settings)
server = await self.get_admin_server(settings)
await server.start()

async with self.client_session.get(
Expand All @@ -349,7 +342,7 @@ async def test_server_health_state(self):
settings = {
"admin.admin_insecure_mode": True,
}
server = self.get_admin_server(settings)
server = await self.get_admin_server(settings)
await server.start()

async with self.client_session.get(
Expand Down Expand Up @@ -379,7 +372,7 @@ async def test_server_health_state(self):
await server.stop()

async def test_upgrade_middleware(self):
profile = InMemoryProfile.test_profile()
profile = await create_test_profile()
self.context = AdminRequestContext.test_context({}, profile)
self.request_dict = {
"context": self.context,
Expand Down Expand Up @@ -408,15 +401,15 @@ async def test_upgrade_middleware(self):
await test_module.upgrade_middleware(request, handler)

# Upgrade in progress with cache
singletons.UpgradeInProgressSingleton().set_wallet("test-profile")
singletons.UpgradeInProgressSingleton().set_wallet(profile.name)
with self.assertRaises(test_module.web.HTTPServiceUnavailable):
await test_module.upgrade_middleware(request, handler)

singletons.UpgradeInProgressSingleton().remove_wallet("test-profile")
singletons.UpgradeInProgressSingleton().remove_wallet(profile.name)
await storage.delete_record(upgrading_record)

# Upgrade in progress with cache
singletons.IsAnoncredsSingleton().set_wallet("test-profile")
singletons.IsAnoncredsSingleton().set_wallet(profile.name)
await test_module.upgrade_middleware(request, handler)


Expand All @@ -434,7 +427,8 @@ async def server():
[("acapy::record::topic", "topic"), ("acapy::record::topic::state", "topic")],
)
async def test_on_record_event(server, event_topic, webhook_topic):
profile = InMemoryProfile.test_profile()
profile = mock.MagicMock(AskarProfile, autospec=True)
server = await server
with mock.patch.object(
server, "send_webhook", mock.CoroutineMock()
) as mock_send_webhook:
Expand All @@ -444,11 +438,11 @@ async def test_on_record_event(server, event_topic, webhook_topic):

@pytest.mark.asyncio
async def test_admin_responder_profile_expired_x():
def _smaller_scope():
profile = InMemoryProfile.test_profile()
async def _smaller_scope():
profile = await create_test_profile()
return test_module.AdminResponder(profile, None)

responder = _smaller_scope()
responder = await _smaller_scope()
gc.collect() # help ensure collection of profile

with pytest.raises(RuntimeError):
Expand Down
13 changes: 6 additions & 7 deletions acapy_agent/admin/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

from aiohttp import web

from acapy_agent.tests import mock

from ...core.in_memory.profile import InMemoryProfile
from ...tests import mock
from ...utils.testing import create_test_profile
from ..decorators.auth import admin_authentication, tenant_authentication
from ..request_context import AdminRequestContext


class TestAdminAuthentication(IsolatedAsyncioTestCase):
def setUp(self) -> None:
self.profile = InMemoryProfile.test_profile(
async def asyncSetUp(self) -> None:
self.profile = await create_test_profile(
settings={
"admin.admin_api_key": "admin_api_key",
"admin.admin_insecure_mode": False,
Expand Down Expand Up @@ -62,8 +61,8 @@ async def test_valid_api_key(self):


class TestTenantAuthentication(IsolatedAsyncioTestCase):
def setUp(self) -> None:
self.profile = InMemoryProfile.test_profile(
async def asyncSetUp(self) -> None:
self.profile = await create_test_profile(
settings={
"admin.admin_api_key": "admin_api_key",
"admin.admin_insecure_mode": False,
Expand Down
26 changes: 13 additions & 13 deletions acapy_agent/admin/tests/test_request_context.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from unittest import IsolatedAsyncioTestCase

from ...core.in_memory import InMemoryProfile
from ...core.profile import ProfileSession
from ...askar.profile import AskarProfileSession
from ...utils.stats import Collector
from ...utils.testing import create_test_profile
from .. import request_context as test_module


class TestAdminRequestContext(IsolatedAsyncioTestCase):
def setUp(self):
self.ctx = test_module.AdminRequestContext(InMemoryProfile.test_profile())
async def asyncSetUp(self):
self.profile = await create_test_profile()
self.ctx = test_module.AdminRequestContext(self.profile)
assert self.ctx.__class__.__name__ in str(self.ctx)

self.ctx_with_added_attrs = test_module.AdminRequestContext(
profile=InMemoryProfile.test_profile(),
root_profile=InMemoryProfile.test_profile(),
profile=self.profile,
metadata={"test_attrib_key": "test_attrib_value"},
)
assert self.ctx_with_added_attrs.__class__.__name__ in str(
Expand All @@ -22,17 +22,17 @@ def setUp(self):

def test_session_transaction(self):
sesn = self.ctx.session()
assert isinstance(sesn, ProfileSession)
assert isinstance(sesn, AskarProfileSession)
txn = self.ctx.transaction()
assert isinstance(txn, ProfileSession)
assert isinstance(txn, AskarProfileSession)

sesn = self.ctx_with_added_attrs.session()
assert isinstance(sesn, ProfileSession)
assert isinstance(sesn, AskarProfileSession)
txn = self.ctx_with_added_attrs.transaction()
assert isinstance(txn, ProfileSession)
assert isinstance(txn, AskarProfileSession)

async def test_session_inject_x(self):
test_ctx = test_module.AdminRequestContext.test_context({Collector: None})
async with test_ctx.session() as test_sesn:
test_ctx = test_module.AdminRequestContext(self.profile)
async with test_ctx.session() as session:
with self.assertRaises(test_module.InjectionError):
test_sesn.inject(Collector)
session.inject(Collector)
Loading

0 comments on commit ff56261

Please sign in to comment.