11import json
2+
23import pytest
4+ import mock as async_mock
5+ from async_case import IsolatedAsyncioTestCase
36
47from aiohttp import ClientSession , DummyCookieJar , TCPConnector , web
58from aiohttp .test_utils import unused_port
69
7- from asynctest import TestCase as AsyncTestCase
8- from asynctest import mock as async_mock
9-
1010from ...config .default_context import DefaultContextBuilder
1111from ...config .injection_context import InjectionContext
1212from ...core .event_bus import Event
1313from ...core .in_memory import InMemoryProfile
1414from ...core .protocol_registry import ProtocolRegistry
1515from ...core .goal_code_registry import GoalCodeRegistry
16- from ...transport .outbound .message import OutboundMessage
1716from ...utils .stats import Collector
1817from ...utils .task_queue import TaskQueue
1918
2019from .. import server as test_module
2120from ..server import AdminServer , AdminSetupError
2221
2322
24- class TestAdminServer (AsyncTestCase ):
25- async def setUp (self ):
23+ class TestAdminServer (IsolatedAsyncioTestCase ):
24+ async def asyncSetUp (self ):
2625 self .message_results = []
2726 self .webhook_results = []
2827 self .port = 0
2928
3029 self .connector = TCPConnector (limit = 16 , limit_per_host = 4 )
31- session_args = {"cookie_jar" : DummyCookieJar (), "connector" : self .connector }
3230 self .client_session = ClientSession (
3331 cookie_jar = DummyCookieJar (), connector = self .connector
3432 )
3533
36- async def tearDown (self ):
34+ async def asyncTearDown (self ):
3735 if self .client_session :
3836 await self .client_session .close ()
3937 self .client_session = None
@@ -49,9 +47,9 @@ async def test_debug_middleware(self):
4947 method = "GET" ,
5048 path_qs = "/hello/world?a=1&b=2" ,
5149 match_info = {"match" : "info" },
52- text = async_mock .CoroutineMock (return_value = "abc123" ),
50+ text = async_mock .AsyncMock (return_value = "abc123" ),
5351 )
54- handler = async_mock .CoroutineMock ()
52+ handler = async_mock .AsyncMock ()
5553
5654 await test_module .debug_middleware (request , handler )
5755 mock_logger .isEnabledFor .assert_called_once ()
@@ -69,36 +67,36 @@ async def test_ready_middleware(self):
6967 request = async_mock .MagicMock (
7068 rel_url = "/" , app = async_mock .MagicMock (_state = {"ready" : False })
7169 )
72- handler = async_mock .CoroutineMock (return_value = "OK" )
70+ handler = async_mock .AsyncMock (return_value = "OK" )
7371 with self .assertRaises (test_module .web .HTTPServiceUnavailable ):
7472 await test_module .ready_middleware (request , handler )
7573
7674 request .app ._state ["ready" ] = True
7775 assert await test_module .ready_middleware (request , handler ) == "OK"
7876
7977 request .app ._state ["ready" ] = True
80- handler = async_mock .CoroutineMock (
78+ handler = async_mock .AsyncMock (
8179 side_effect = test_module .LedgerConfigError ("Bad config" )
8280 )
8381 with self .assertRaises (test_module .LedgerConfigError ):
8482 await test_module .ready_middleware (request , handler )
8583
8684 request .app ._state ["ready" ] = True
87- handler = async_mock .CoroutineMock (
85+ handler = async_mock .AsyncMock (
8886 side_effect = test_module .web .HTTPFound (location = "/api/doc" )
8987 )
9088 with self .assertRaises (test_module .web .HTTPFound ):
9189 await test_module .ready_middleware (request , handler )
9290
9391 request .app ._state ["ready" ] = True
94- handler = async_mock .CoroutineMock (
92+ handler = async_mock .AsyncMock (
9593 side_effect = test_module .asyncio .CancelledError ("Cancelled" )
9694 )
9795 with self .assertRaises (test_module .asyncio .CancelledError ):
9896 await test_module .ready_middleware (request , handler )
9997
10098 request .app ._state ["ready" ] = True
101- handler = async_mock .CoroutineMock (side_effect = KeyError ("No such thing" ))
99+ handler = async_mock .AsyncMock (side_effect = KeyError ("No such thing" ))
102100 with self .assertRaises (KeyError ):
103101 await test_module .ready_middleware (request , handler )
104102
@@ -132,10 +130,10 @@ def get_admin_server(
132130 profile ,
133131 self .outbound_message_router ,
134132 self .webhook_router ,
135- conductor_stop = async_mock .CoroutineMock (),
133+ conductor_stop = async_mock .AsyncMock (),
136134 task_queue = TaskQueue (max_active = 4 ) if task_queue else None ,
137135 conductor_stats = (
138- None if task_queue else async_mock .CoroutineMock (return_value = {"a" : 1 })
136+ None if task_queue else async_mock .AsyncMock (return_value = {"a" : 1 })
139137 ),
140138 )
141139
@@ -177,7 +175,7 @@ async def test_start_stop(self):
177175 await server .stop ()
178176
179177 with async_mock .patch .object (
180- web .TCPSite , "start" , async_mock .CoroutineMock ()
178+ web .TCPSite , "start" , async_mock .AsyncMock ()
181179 ) as mock_start :
182180 mock_start .side_effect = OSError ("Failure to launch" )
183181 with self .assertRaises (AdminSetupError ):
@@ -227,7 +225,7 @@ async def test_import_routes_multitenant_middleware(self):
227225 method = "GET" ,
228226 headers = {"Authorization" : "Bearer ..." },
229227 path = "/multitenancy/etc" ,
230- text = async_mock .CoroutineMock (return_value = "abc123" ),
228+ text = async_mock .AsyncMock (return_value = "abc123" ),
231229 )
232230 with self .assertRaises (test_module .web .HTTPUnauthorized ):
233231 await mt_authz_middle (mock_request , None )
@@ -236,7 +234,7 @@ async def test_import_routes_multitenant_middleware(self):
236234 method = "GET" ,
237235 headers = {},
238236 path = "/protected/non-multitenancy/non-server" ,
239- text = async_mock .CoroutineMock (return_value = "abc123" ),
237+ text = async_mock .AsyncMock (return_value = "abc123" ),
240238 )
241239 with self .assertRaises (test_module .web .HTTPUnauthorized ):
242240 await mt_authz_middle (mock_request , None )
@@ -245,19 +243,19 @@ async def test_import_routes_multitenant_middleware(self):
245243 method = "GET" ,
246244 headers = {"Authorization" : "Bearer ..." },
247245 path = "/protected/non-multitenancy/non-server" ,
248- text = async_mock .CoroutineMock (return_value = "abc123" ),
246+ text = async_mock .AsyncMock (return_value = "abc123" ),
249247 )
250- mock_handler = async_mock .CoroutineMock ()
248+ mock_handler = async_mock .AsyncMock ()
251249 await mt_authz_middle (mock_request , mock_handler )
252250 assert mock_handler .called_once_with (mock_request )
253251
254252 mock_request = async_mock .MagicMock (
255253 method = "GET" ,
256254 headers = {"Authorization" : "Non-bearer ..." },
257255 path = "/test" ,
258- text = async_mock .CoroutineMock (return_value = "abc123" ),
256+ text = async_mock .AsyncMock (return_value = "abc123" ),
259257 )
260- mock_handler = async_mock .CoroutineMock ()
258+ mock_handler = async_mock .AsyncMock ()
261259 await mt_authz_middle (mock_request , mock_handler )
262260 assert mock_handler .called_once_with (mock_request )
263261
@@ -268,7 +266,7 @@ async def test_import_routes_multitenant_middleware(self):
268266 method = "GET" ,
269267 headers = {"Authorization" : "Non-bearer ..." },
270268 path = "/protected/non-multitenancy/non-server" ,
271- text = async_mock .CoroutineMock (return_value = "abc123" ),
269+ text = async_mock .AsyncMock (return_value = "abc123" ),
272270 )
273271 with self .assertRaises (test_module .web .HTTPUnauthorized ):
274272 await setup_ctx_middle (mock_request , None )
@@ -277,12 +275,12 @@ async def test_import_routes_multitenant_middleware(self):
277275 method = "GET" ,
278276 headers = {"Authorization" : "Bearer ..." },
279277 path = "/protected/non-multitenancy/non-server" ,
280- text = async_mock .CoroutineMock (return_value = "abc123" ),
278+ text = async_mock .AsyncMock (return_value = "abc123" ),
281279 )
282280 with async_mock .patch .object (
283281 server .multitenant_manager ,
284282 "get_profile_for_token" ,
285- async_mock .CoroutineMock (),
283+ async_mock .AsyncMock (),
286284 ) as mock_get_profile :
287285 mock_get_profile .side_effect = [
288286 test_module .MultitenantManagerError ("corrupt token" ),
@@ -295,11 +293,15 @@ async def test_import_routes_multitenant_middleware(self):
295293 async def test_register_external_plugin_x (self ):
296294 context = InjectionContext ()
297295 context .injector .bind_instance (ProtocolRegistry , ProtocolRegistry ())
298- with self .assertRaises (ValueError ):
296+ context .injector .bind_instance (GoalCodeRegistry , GoalCodeRegistry ())
297+ with self .assertLogs (level = "ERROR" ) as logs :
299298 builder = DefaultContextBuilder (
300- settings = {"external_plugins" : "aries_cloudagent.nosuchmodule" }
299+ settings = {"external_plugins" : [ "aries_cloudagent.nosuchmodule" ] }
301300 )
302301 await builder .load_plugins (context )
302+ assert "Module doesn't exist: aries_cloudagent.nosuchmodule" in "\n " .join (
303+ logs .output
304+ )
303305
304306 async def test_visit_insecure_mode (self ):
305307 settings = {"admin.admin_insecure_mode" : True , "task_queue" : True }
@@ -480,9 +482,9 @@ async def test_server_health_state(self):
480482@pytest .fixture
481483async def server ():
482484 test_class = TestAdminServer ()
483- await test_class .setUp ()
485+ await test_class .asyncSetUp ()
484486 yield test_class .get_admin_server ()
485- await test_class .tearDown ()
487+ await test_class .asyncTearDown ()
486488
487489
488490@pytest .mark .asyncio
@@ -493,7 +495,7 @@ async def server():
493495async def test_on_record_event (server , event_topic , webhook_topic ):
494496 profile = InMemoryProfile .test_profile ()
495497 with async_mock .patch .object (
496- server , "send_webhook" , async_mock .CoroutineMock ()
498+ server , "send_webhook" , async_mock .AsyncMock ()
497499 ) as mock_send_webhook :
498500 await server ._on_record_event (profile , Event (event_topic , None ))
499501 mock_send_webhook .assert_called_once_with (profile , webhook_topic , None )
0 commit comments