7
7
from jupyter_matlab_proxy .util import mwi_exceptions
8
8
from datetime import timedelta
9
9
from collections import namedtuple
10
- from aioresponses import aioresponses
11
10
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
14
12
"""
15
13
16
14
@@ -113,59 +111,72 @@ def fetch_access_token_valid_json_fixture():
113
111
return json_data
114
112
115
113
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
119
120
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
125
123
124
+ async def text (self ):
125
+ return self ._text
126
126
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 ):
130
135
"""Test to check mw.fetch_access_token method returns valid json response.
131
136
132
137
The mock_response fixture mocks the aiohttp.ClientSession().post() method to return a custom HTTP response.
133
138
134
139
Args:
135
140
mwa_api_data (namedtuple): A pytest fixture which returns a namedtuple containing values for MW servers.
136
141
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.
138
143
"""
139
144
json_data = fetch_access_token_valid_json
145
+
140
146
payload = dict (accessTokenString = json_data ["accessTokenString" ])
141
147
148
+ mock_resp = MockResponse (payload = payload , reason = "OK" )
149
+
142
150
url_pattern = mwa_api_data .mwa_api_endpoint_pattern
143
- mock_response .post (url_pattern , payload = payload )
144
151
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 (
146
154
mwa_api_data .mwa_api_endpoint ,
147
155
mwa_api_data .identity_token ,
148
156
mwa_api_data .source_id ,
149
157
)
150
158
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" ]
152
164
153
165
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 ):
155
167
"""Test to check mw.fetch_access_token() method raises a mwi_exceptions.OnlineLicensingError.
156
168
157
169
When an invalid response is received from the server, this test checks if mwi_exceptions.OnlineLicensingError is raised.
158
170
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.
160
172
mwa_api_data (namedtuple): A pytest fixture which returns a namedtuple containing values for MW authentication
161
173
"""
162
174
163
175
url_pattern = mwa_api_data .mwa_api_endpoint_pattern
164
176
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 )
169
180
170
181
with pytest .raises (mwi_exceptions .OnlineLicensingError ):
171
182
resp = await mw .fetch_access_token (
@@ -174,21 +185,21 @@ async def test_fetch_access_token_licensing_error(mwa_api_data, mock_response):
174
185
mwa_api_data .source_id ,
175
186
)
176
187
188
+ _ , args , _ = mocked .mock_calls [0 ]
189
+ url = args [0 ]
190
+ assert re .match (url_pattern , url )
191
+
177
192
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 ):
179
194
"""Test to check fetch_expand_token raises mwi_exceptions.OnlineLicensing error.
180
195
181
196
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.
183
198
mwa_api_data (namedtuple): A pytest fixture which returns a namedtuple containing values for MW authentication
184
199
"""
185
-
186
200
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 )
192
203
193
204
with pytest .raises (mwi_exceptions .OnlineLicensingError ):
194
205
resp = await mw .fetch_expand_token (
@@ -197,6 +208,10 @@ async def test_fetch_expand_token_licensing_error(mock_response, mwa_api_data):
197
208
mwa_api_data .source_id ,
198
209
)
199
210
211
+ _ , args , _ = mocked .mock_calls [0 ]
212
+ url = args [0 ]
213
+ assert re .match (url_pattern , url )
214
+
200
215
201
216
@pytest .fixture (name = "fetch_expand_token_valid_json" )
202
217
def fetch_expand_token_valid_json_fixture ():
@@ -229,21 +244,18 @@ def fetch_expand_token_valid_json_fixture():
229
244
return json_data
230
245
231
246
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 ):
235
248
"""Test to check if mw.fetch_expand_token returns a correct json response
236
249
237
250
mock_response is used to mock ClientSession.post method to return a HTTP Response containing a valid json response.
238
251
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.
240
253
fetch_expand_token_valid_json (namedtuple): Pytest fixture which returns a dict which is returned by the server when no exception is raised.
241
254
mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
242
255
"""
243
256
json_data = fetch_expand_token_valid_json
244
257
245
258
url_pattern = mwa_api_data .mwa_api_endpoint_pattern
246
-
247
259
referenceDetail = dict (
248
260
firstName = json_data ["referenceDetail" ]["firstName" ],
249
261
lastName = json_data ["referenceDetail" ]["lastName" ],
@@ -256,32 +268,34 @@ async def test_fetch_expand_token(
256
268
expirationDate = json_data ["expirationDate" ], referenceDetail = referenceDetail
257
269
)
258
270
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 )
260
273
261
274
resp = await mw .fetch_expand_token (
262
275
mwa_api_data .mwa_api_endpoint ,
263
276
mwa_api_data .identity_token ,
264
277
mwa_api_data .source_id ,
265
278
)
266
279
280
+ _ , args , _ = mocked .mock_calls [0 ]
281
+ url = args [0 ]
282
+
267
283
assert resp is not None and len (resp .keys ()) > 0
284
+ assert re .match (url_pattern , url )
268
285
269
286
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 ):
271
288
"""Test to check if fetch_entitlements raises mwi_exceptions.OnlineLicensingError.
272
289
273
290
When an invalid response is received, this test checks if mwi_exceptions.OnlineLicenseError is raised.
274
291
275
292
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.
277
294
mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
278
295
"""
279
296
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 )
285
299
286
300
with pytest .raises (mwi_exceptions .OnlineLicensingError ):
287
301
resp = await mw .fetch_entitlements (
@@ -290,6 +304,10 @@ async def test_fetch_entitlements_licensing_error(mock_response, mwa_api_data):
290
304
mwa_api_data .matlab_release ,
291
305
)
292
306
307
+ _ , args , _ = mocked .mock_calls [0 ]
308
+ url = args [0 ]
309
+ assert re .match (url_pattern , url )
310
+
293
311
294
312
@pytest .fixture (
295
313
name = "invalid_entitlements" ,
@@ -322,7 +340,7 @@ def invalid_entitlements_fixture(request):
322
340
323
341
324
342
async def test_fetch_entitlements_entitlement_error (
325
- mock_response , mwa_api_data , invalid_entitlements
343
+ mocker , mwa_api_data , invalid_entitlements
326
344
):
327
345
"""Test to check fetch_entitlements raises mwi_exceptions.EntitlementError.
328
346
@@ -332,13 +350,16 @@ async def test_fetch_entitlements_entitlement_error(
332
350
333
351
Args:
334
352
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.
336
354
mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
337
355
invalid_entitlements (String): String containing invalid entitlements
338
356
"""
339
357
url_pattern = mwa_api_data .mhlm_api_endpoint_pattern
340
358
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 )
342
363
343
364
with pytest .raises (mwi_exceptions .EntitlementError ):
344
365
resp = await mw .fetch_entitlements (
@@ -347,6 +368,10 @@ async def test_fetch_entitlements_entitlement_error(
347
368
mwa_api_data .matlab_release ,
348
369
)
349
370
371
+ _ , args , _ = mocked .mock_calls [0 ]
372
+ url = args [0 ]
373
+ assert re .match (url_pattern , url )
374
+
350
375
351
376
@pytest .fixture (name = "valid_entitlements" )
352
377
def valid_entitlements_fixture ():
@@ -371,28 +396,35 @@ def valid_entitlements_fixture():
371
396
)
372
397
373
398
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 ):
375
400
"""Test to check test_fetch_entitlements returns valid response.
376
401
377
402
378
403
mock_response mocks aiohttpClientSession.post() method to return valid entitlements as a HTTP response
379
404
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.
381
406
mwa_api_data (namedtuple): A namedtuple which contains info related to mwa.
382
407
valid_entitlements (String): String containing valid entitlements as a response.
383
408
"""
384
409
385
410
url_pattern = mwa_api_data .mhlm_api_endpoint_pattern
386
411
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 )
388
416
389
417
resp = await mw .fetch_entitlements (
390
418
mwa_api_data .mhlm_api_endpoint ,
391
419
mwa_api_data .access_token ,
392
420
mwa_api_data .matlab_release ,
393
421
)
394
422
423
+ _ , args , _ = mocked .mock_calls [0 ]
424
+ url = args [0 ]
425
+
395
426
assert resp is not None and len (resp ) > 0
427
+ assert re .match (url_pattern , url )
396
428
397
429
398
430
def test_parse_mhlm_no_error ():
0 commit comments