77from jupyter_matlab_proxy .util import mwi_exceptions
88from datetime import timedelta
99from collections import namedtuple
10- from aioresponses import aioresponses
1110
12-
13- """This file tests methods present in jupyter_matlab_proxy/util/mw.py
11+ """This file tests methods present in matlab_web_desktop_proxy/util/mw.py
1412"""
1513
1614
@@ -113,59 +111,72 @@ def fetch_access_token_valid_json_fixture():
113111 return json_data
114112
115113
116- @pytest .fixture (name = "mock_response" )
117- def mock_aiohttp_client_session ():
118- """A pytest fixture which yields an aioresponses() object
114+ class MockResponse :
115+ def __init__ (self , reason , payload = {}, status = 200 , text = "" ):
116+ self ._payload = payload
117+ self ._text = text
118+ self .reason = reason
119+ self .status = status
119120
120- Yields:
121- aioresponses : An aioresponses() object which mocks the HTTP Response object.
122- """
123- with aioresponses () as m :
124- yield m
121+ async def json (self ):
122+ return self ._payload
125123
124+ async def text (self ):
125+ return self ._text
126126
127- async def test_fetch_access_token (
128- mwa_api_data , fetch_access_token_valid_json , mock_response
129- ):
127+ async def __aenter__ (self ):
128+ return self
129+
130+ async def __aexit__ (self , exc_type , exc , tb ):
131+ pass
132+
133+
134+ async def test_fetch_access_token (mwa_api_data , fetch_access_token_valid_json , mocker ):
130135 """Test to check mw.fetch_access_token method returns valid json response.
131136
132137 The mock_response fixture mocks the aiohttp.ClientSession().post() method to return a custom HTTP response.
133138
134139 Args:
135140 mwa_api_data (namedtuple): A pytest fixture which returns a namedtuple containing values for MW servers.
136141 fetch_access_token_valid_json (Dict): A pytest fixture which returns a dict representing a valid json response.
137- mock_response: Pytest fixture which yields a aioresponses() object for mocking HTTP response
142+ mocker: Built in pytest fixture which can be used to mock functions.
138143 """
139144 json_data = fetch_access_token_valid_json
145+
140146 payload = dict (accessTokenString = json_data ["accessTokenString" ])
141147
148+ mock_resp = MockResponse (payload = payload , reason = "OK" )
149+
142150 url_pattern = mwa_api_data .mwa_api_endpoint_pattern
143- mock_response .post (url_pattern , payload = payload )
144151
145- resp = await mw .fetch_access_token (
152+ mocked = mocker .patch ("aiohttp.ClientSession.post" , return_value = mock_resp )
153+ res = await mw .fetch_access_token (
146154 mwa_api_data .mwa_api_endpoint ,
147155 mwa_api_data .identity_token ,
148156 mwa_api_data .source_id ,
149157 )
150158
151- assert json_data ["accessTokenString" ] == resp ["token" ]
159+ _ , args , _ = mocked .mock_calls [0 ]
160+ url = args [0 ]
161+
162+ assert re .match (url_pattern , url )
163+ assert json_data ["accessTokenString" ] == res ["token" ]
152164
153165
154- async def test_fetch_access_token_licensing_error (mwa_api_data , mock_response ):
166+ async def test_fetch_access_token_licensing_error (mwa_api_data , mocker ):
155167 """Test to check mw.fetch_access_token() method raises a mwi_exceptions.OnlineLicensingError.
156168
157169 When an invalid response is received from the server, this test checks if mwi_exceptions.OnlineLicensingError is raised.
158170 Args:
159- mock_response: Pytest fixture which yields a aioresponses() object for mocking HTTP response
171+ mocker: Built in pytest fixture which can be used to mock functions.
160172 mwa_api_data (namedtuple): A pytest fixture which returns a namedtuple containing values for MW authentication
161173 """
162174
163175 url_pattern = mwa_api_data .mwa_api_endpoint_pattern
164176
165- mock_response .post (
166- url_pattern ,
167- exception = mwi_exceptions .OnlineLicensingError ("Communication failed" ),
168- )
177+ mock_resp = MockResponse (payload = {}, reason = "NOT-OK" , status = 404 )
178+
179+ mocked = mocker .patch ("aiohttp.ClientSession.post" , return_value = mock_resp )
169180
170181 with pytest .raises (mwi_exceptions .OnlineLicensingError ):
171182 resp = await mw .fetch_access_token (
@@ -174,21 +185,21 @@ async def test_fetch_access_token_licensing_error(mwa_api_data, mock_response):
174185 mwa_api_data .source_id ,
175186 )
176187
188+ _ , args , _ = mocked .mock_calls [0 ]
189+ url = args [0 ]
190+ assert re .match (url_pattern , url )
191+
177192
178- async def test_fetch_expand_token_licensing_error (mock_response , mwa_api_data ):
193+ async def test_fetch_expand_token_licensing_error (mocker , mwa_api_data ):
179194 """Test to check fetch_expand_token raises mwi_exceptions.OnlineLicensing error.
180195
181196 Args:
182- mock_response: Pytest fixture which yields a aioresponses() object for mocking HTTP response
197+ mocker: Built in pytest fixture which can be used to mock functions.
183198 mwa_api_data (namedtuple): A pytest fixture which returns a namedtuple containing values for MW authentication
184199 """
185-
186200 url_pattern = mwa_api_data .mwa_api_endpoint_pattern
187-
188- mock_response .post (
189- url_pattern ,
190- exception = mwi_exceptions .OnlineLicensingError ("Communication failed" ),
191- )
201+ mock_resp = MockResponse (payload = {}, reason = "NOT-OK" , status = 503 )
202+ mocked = mocker .patch ("aiohttp.ClientSession.post" , return_value = mock_resp )
192203
193204 with pytest .raises (mwi_exceptions .OnlineLicensingError ):
194205 resp = await mw .fetch_expand_token (
@@ -197,6 +208,10 @@ async def test_fetch_expand_token_licensing_error(mock_response, mwa_api_data):
197208 mwa_api_data .source_id ,
198209 )
199210
211+ _ , args , _ = mocked .mock_calls [0 ]
212+ url = args [0 ]
213+ assert re .match (url_pattern , url )
214+
200215
201216@pytest .fixture (name = "fetch_expand_token_valid_json" )
202217def fetch_expand_token_valid_json_fixture ():
@@ -229,21 +244,18 @@ def fetch_expand_token_valid_json_fixture():
229244 return json_data
230245
231246
232- async def test_fetch_expand_token (
233- mock_response , fetch_expand_token_valid_json , mwa_api_data
234- ):
247+ async def test_fetch_expand_token (mocker , fetch_expand_token_valid_json , mwa_api_data ):
235248 """Test to check if mw.fetch_expand_token returns a correct json response
236249
237250 mock_response is used to mock ClientSession.post method to return a HTTP Response containing a valid json response.
238251 Args:
239- mock_response: Pytest fixture which yields a aioresponses() object for mocking HTTP response
252+ mocker: Built in pytest fixture which can be used to mock functions.
240253 fetch_expand_token_valid_json (namedtuple): Pytest fixture which returns a dict which is returned by the server when no exception is raised.
241254 mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
242255 """
243256 json_data = fetch_expand_token_valid_json
244257
245258 url_pattern = mwa_api_data .mwa_api_endpoint_pattern
246-
247259 referenceDetail = dict (
248260 firstName = json_data ["referenceDetail" ]["firstName" ],
249261 lastName = json_data ["referenceDetail" ]["lastName" ],
@@ -256,32 +268,34 @@ async def test_fetch_expand_token(
256268 expirationDate = json_data ["expirationDate" ], referenceDetail = referenceDetail
257269 )
258270
259- mock_response .post (url_pattern , payload = payload )
271+ mock_resp = MockResponse (payload = payload , reason = "OK" , status = 200 )
272+ mocked = mocker .patch ("aiohttp.ClientSession.post" , return_value = mock_resp )
260273
261274 resp = await mw .fetch_expand_token (
262275 mwa_api_data .mwa_api_endpoint ,
263276 mwa_api_data .identity_token ,
264277 mwa_api_data .source_id ,
265278 )
266279
280+ _ , args , _ = mocked .mock_calls [0 ]
281+ url = args [0 ]
282+
267283 assert resp is not None and len (resp .keys ()) > 0
284+ assert re .match (url_pattern , url )
268285
269286
270- async def test_fetch_entitlements_licensing_error (mock_response , mwa_api_data ):
287+ async def test_fetch_entitlements_licensing_error (mocker , mwa_api_data ):
271288 """Test to check if fetch_entitlements raises mwi_exceptions.OnlineLicensingError.
272289
273290 When an invalid response is received, this test checks if mwi_exceptions.OnlineLicenseError is raised.
274291
275292 Args:
276- mock_response: Pytest fixture which yields a aioresponses() object for mocking HTTP response
293+ mocker: Built in pytest fixture which can be used to mock functions.
277294 mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
278295 """
279296 url_pattern = mwa_api_data .mhlm_api_endpoint_pattern
280-
281- mock_response .post (
282- url_pattern ,
283- exception = mwi_exceptions .OnlineLicensingError ("Communication Error" ),
284- )
297+ mock_resp = MockResponse (payload = {}, reason = "NOT-OK" , status = 503 )
298+ mocked = mocker .patch ("aiohttp.ClientSession.post" , return_value = mock_resp )
285299
286300 with pytest .raises (mwi_exceptions .OnlineLicensingError ):
287301 resp = await mw .fetch_entitlements (
@@ -290,6 +304,10 @@ async def test_fetch_entitlements_licensing_error(mock_response, mwa_api_data):
290304 mwa_api_data .matlab_release ,
291305 )
292306
307+ _ , args , _ = mocked .mock_calls [0 ]
308+ url = args [0 ]
309+ assert re .match (url_pattern , url )
310+
293311
294312@pytest .fixture (
295313 name = "invalid_entitlements" ,
@@ -322,7 +340,7 @@ def invalid_entitlements_fixture(request):
322340
323341
324342async def test_fetch_entitlements_entitlement_error (
325- mock_response , mwa_api_data , invalid_entitlements
343+ mocker , mwa_api_data , invalid_entitlements
326344):
327345 """Test to check fetch_entitlements raises mwi_exceptions.EntitlementError.
328346
@@ -332,13 +350,16 @@ async def test_fetch_entitlements_entitlement_error(
332350
333351 Args:
334352
335- mock_response: Pytest fixture which yields a aioresponses() object for mocking HTTP response
353+ mocker: Built in pytest fixture which can be used to mock functions.
336354 mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
337355 invalid_entitlements (String): String containing invalid entitlements
338356 """
339357 url_pattern = mwa_api_data .mhlm_api_endpoint_pattern
340358
341- mock_response .post (url_pattern , body = invalid_entitlements )
359+ mock_resp = MockResponse (
360+ payload = {}, reason = "OK" , text = invalid_entitlements , status = 404
361+ )
362+ mocked = mocker .patch ("aiohttp.ClientSession.post" , return_value = mock_resp )
342363
343364 with pytest .raises (mwi_exceptions .EntitlementError ):
344365 resp = await mw .fetch_entitlements (
@@ -347,6 +368,10 @@ async def test_fetch_entitlements_entitlement_error(
347368 mwa_api_data .matlab_release ,
348369 )
349370
371+ _ , args , _ = mocked .mock_calls [0 ]
372+ url = args [0 ]
373+ assert re .match (url_pattern , url )
374+
350375
351376@pytest .fixture (name = "valid_entitlements" )
352377def valid_entitlements_fixture ():
@@ -371,28 +396,35 @@ def valid_entitlements_fixture():
371396 )
372397
373398
374- async def test_fetch_entitlements (mock_response , mwa_api_data , valid_entitlements ):
399+ async def test_fetch_entitlements (mocker , mwa_api_data , valid_entitlements ):
375400 """Test to check test_fetch_entitlements returns valid response.
376401
377402
378403 mock_response mocks aiohttpClientSession.post() method to return valid entitlements as a HTTP response
379404 Args:
380- mock_response: Pytest fixture which yields a aioresponses() object for mocking HTTP response
405+ mocker: Built in pytest fixture which can be used to mock functions.
381406 mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
382407 valid_entitlements (String): String containing valid entitlements as a response.
383408 """
384409
385410 url_pattern = mwa_api_data .mhlm_api_endpoint_pattern
386411
387- mock_response .post (url_pattern , body = valid_entitlements )
412+ mock_resp = MockResponse (
413+ payload = {}, reason = "OK" , text = valid_entitlements , status = 404
414+ )
415+ mocked = mocker .patch ("aiohttp.ClientSession.post" , return_value = mock_resp )
388416
389417 resp = await mw .fetch_entitlements (
390418 mwa_api_data .mhlm_api_endpoint ,
391419 mwa_api_data .access_token ,
392420 mwa_api_data .matlab_release ,
393421 )
394422
423+ _ , args , _ = mocked .mock_calls [0 ]
424+ url = args [0 ]
425+
395426 assert resp is not None and len (resp ) > 0
427+ assert re .match (url_pattern , url )
396428
397429
398430def test_parse_mhlm_no_error ():
0 commit comments