1
1
import unittest
2
- from aiohttp .test_utils import AioHTTPTestCase , unittest_run_loop
3
- from aiohttp import web
4
- from beacon_api .app import init , main , initialize
5
- from unittest import mock
2
+ from aiohttp .test_utils import AioHTTPTestCase
3
+ from beacon_api .app import init , initialize
6
4
import asyncpg
7
- import asynctest
8
5
import json
9
6
from authlib .jose import jwt
10
7
import os
17
14
18
15
def generate_token (issuer ):
19
16
"""Mock ELIXIR AAI token."""
20
- pem = {"kty" : "oct" , "kid" : "018c0ae5-4d9b-471b-bfd6-eef314bc7037" , "use" : "sig" , "alg" : "HS256" , "k" : "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg" }
17
+ pem = {
18
+ "kty" : "oct" ,
19
+ "kid" : "018c0ae5-4d9b-471b-bfd6-eef314bc7037" ,
20
+ "use" : "sig" ,
21
+ "alg" : "HS256" ,
22
+ "k" : "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg" ,
23
+ }
21
24
header = {"jku" : "http://test.csc.fi/jwk" , "kid" : "018c0ae5-4d9b-471b-bfd6-eef314bc7037" , "alg" : "HS256" }
22
25
payload = {
"iss" :
issuer ,
"aud" :
"audience" ,
"exp" :
9999999999 ,
"sub" :
"[email protected] " }
23
26
token = jwt .encode (header , payload , pem ).decode ("utf-8" )
@@ -26,7 +29,13 @@ def generate_token(issuer):
26
29
27
30
def generate_bad_token ():
28
31
"""Mock AAI token."""
29
- pem = {"kty" : "oct" , "kid" : "018c0ae5-4d9b-471b-bfd6-eef314bc7037" , "use" : "sig" , "alg" : "HS256" , "k" : "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg" }
32
+ pem = {
33
+ "kty" : "oct" ,
34
+ "kid" : "018c0ae5-4d9b-471b-bfd6-eef314bc7037" ,
35
+ "use" : "sig" ,
36
+ "alg" : "HS256" ,
37
+ "k" : "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg" ,
38
+ }
30
39
header = {"jku" : "http://test.csc.fi/jwk" , "kid" : "018c0ae5-4d9b-471b-bfd6-eef314bc7037" , "alg" : "HS256" }
31
40
payload = {
"iss" :
"bad_issuer" ,
"aud" :
"audience" ,
"exp" :
0 ,
"sub" :
"[email protected] " }
32
41
token = jwt .encode (header , payload , pem ).decode ("utf-8" )
@@ -35,7 +44,7 @@ def generate_bad_token():
35
44
36
45
async def create_db_mock (app ):
37
46
"""Mock the db connection pool."""
38
- app ["pool" ] = asynctest .mock .Mock (asyncpg .create_pool ())
47
+ app ["pool" ] = unittest .mock .Mock (asyncpg .create_pool ())
39
48
return app
40
49
41
50
@@ -50,7 +59,7 @@ class AppTestCase(AioHTTPTestCase):
50
59
Testing web app endpoints.
51
60
"""
52
61
53
- @asynctest .mock .patch ("beacon_api.app.initialize" , side_effect = create_db_mock )
62
+ @unittest .mock .patch ("beacon_api.app.initialize" , side_effect = create_db_mock )
54
63
async def get_application (self , pool_mock ):
55
64
"""Retrieve web Application for test."""
56
65
token , public_key = generate_token ("http://test.csc.fi" )
@@ -60,34 +69,30 @@ async def get_application(self, pool_mock):
60
69
self .env .set ("TOKEN" , token )
61
70
return await init ()
62
71
63
- @unittest_run_loop
64
- async def tearDown (self ):
72
+ async def tearDownAsync (self ):
65
73
"""Finish up tests."""
66
74
self .env .unset ("PUBLIC_KEY" )
67
75
self .env .unset ("TOKEN" )
68
76
await caches .get ("default" ).delete ("jwk_key" )
69
77
70
- @unittest_run_loop
71
78
async def test_beacon_info (self ):
72
79
"""Test the Beacon info endpoint.
73
80
74
81
The status should always be 200.
75
82
"""
76
- with asynctest .mock .patch ("beacon_api.app.beacon_info" , return_value = {"id" : "value" }):
83
+ with unittest .mock .patch ("beacon_api.app.beacon_info" , return_value = {"id" : "value" }):
77
84
resp = await self .client .request ("GET" , "/" )
78
85
self .assertEqual (200 , resp .status )
79
86
80
- @unittest_run_loop
81
87
async def test_ga4gh_info (self ):
82
88
"""Test the GA4GH Discovery info endpoint.
83
89
84
90
The status should always be 200.
85
91
"""
86
- with asynctest .mock .patch ("beacon_api.app.ga4gh_info" , return_value = {"id" : "value" }):
92
+ with unittest .mock .patch ("beacon_api.app.ga4gh_info" , return_value = {"id" : "value" }):
87
93
resp = await self .client .request ("GET" , "/service-info" )
88
94
self .assertEqual (200 , resp .status )
89
95
90
- @unittest_run_loop
91
96
async def test_post_info (self ):
92
97
"""Test the info endpoint with POST.
93
98
@@ -96,7 +101,6 @@ async def test_post_info(self):
96
101
resp = await self .client .request ("POST" , "/" )
97
102
self .assertEqual (405 , resp .status )
98
103
99
- @unittest_run_loop
100
104
async def test_post_service_info (self ):
101
105
"""Test the service-info endpoint with POST.
102
106
@@ -105,19 +109,16 @@ async def test_post_service_info(self):
105
109
resp = await self .client .request ("POST" , "/service-info" )
106
110
self .assertEqual (405 , resp .status )
107
111
108
- @unittest_run_loop
109
112
async def test_empty_get_query (self ):
110
113
"""Test empty GET query endpoint."""
111
114
resp = await self .client .request ("GET" , "/query" )
112
115
self .assertEqual (400 , resp .status )
113
116
114
- @unittest_run_loop
115
117
async def test_empty_post_query (self ):
116
118
"""Test empty POST query endpoint."""
117
119
resp = await self .client .request ("POST" , "/query" , data = json .dumps ({}))
118
120
self .assertEqual (400 , resp .status )
119
121
120
- @unittest_run_loop
121
122
async def test_bad_start_post_query (self ):
122
123
"""Test bad start combination POST query endpoint."""
123
124
bad_start = {
@@ -134,7 +135,6 @@ async def test_bad_start_post_query(self):
134
135
resp = await self .client .request ("POST" , "/query" , data = json .dumps (bad_start ))
135
136
self .assertEqual (400 , resp .status )
136
137
137
- @unittest_run_loop
138
138
async def test_bad_start2_post_query (self ):
139
139
"""Test bad start combination 2 POST query endpoint."""
140
140
bad_start = {
@@ -151,7 +151,6 @@ async def test_bad_start2_post_query(self):
151
151
resp = await self .client .request ("POST" , "/query" , data = json .dumps (bad_start ))
152
152
self .assertEqual (400 , resp .status )
153
153
154
- @unittest_run_loop
155
154
async def test_bad_startend_post_query (self ):
156
155
"""Test end smaller than start POST query endpoint."""
157
156
bad_start = {
@@ -166,7 +165,6 @@ async def test_bad_startend_post_query(self):
166
165
resp = await self .client .request ("POST" , "/query" , data = json .dumps (bad_start ))
167
166
self .assertEqual (400 , resp .status )
168
167
169
- @unittest_run_loop
170
168
async def test_bad_startminmax_post_query (self ):
171
169
"""Test start min greater than start Max POST query endpoint."""
172
170
bad_start = {
@@ -181,7 +179,6 @@ async def test_bad_startminmax_post_query(self):
181
179
resp = await self .client .request ("POST" , "/query" , data = json .dumps (bad_start ))
182
180
self .assertEqual (400 , resp .status )
183
181
184
- @unittest_run_loop
185
182
async def test_bad_endminmax_post_query (self ):
186
183
"""Test end min greater than start Max POST query endpoint."""
187
184
bad_start = {
@@ -196,19 +193,24 @@ async def test_bad_endminmax_post_query(self):
196
193
resp = await self .client .request ("POST" , "/query" , data = json .dumps (bad_start ))
197
194
self .assertEqual (400 , resp .status )
198
195
199
- @asynctest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
200
- @asynctest .mock .patch ("beacon_api.app.query_request_handler" )
201
- @unittest_run_loop
196
+ @unittest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
197
+ @unittest .mock .patch ("beacon_api.app.query_request_handler" )
202
198
async def test_good_start_post_query (self , mock_handler , mock_object ):
203
199
"""Test good start combination POST query endpoint."""
204
- good_start = {"referenceName" : "MT" , "start" : 10 , "referenceBases" : "T" , "variantType" : "MNP" , "assemblyId" : "GRCh38" , "includeDatasetResponses" : "HIT" }
200
+ good_start = {
201
+ "referenceName" : "MT" ,
202
+ "start" : 10 ,
203
+ "referenceBases" : "T" ,
204
+ "variantType" : "MNP" ,
205
+ "assemblyId" : "GRCh38" ,
206
+ "includeDatasetResponses" : "HIT" ,
207
+ }
205
208
mock_handler .side_effect = json .dumps (good_start )
206
209
resp = await self .client .request ("POST" , "/query" , data = json .dumps (good_start ))
207
210
self .assertEqual (200 , resp .status )
208
211
209
- @asynctest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
210
- @asynctest .mock .patch ("beacon_api.app.query_request_handler" )
211
- @unittest_run_loop
212
+ @unittest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
213
+ @unittest .mock .patch ("beacon_api.app.query_request_handler" )
212
214
async def test_good_start2_post_query (self , mock_handler , mock_object ):
213
215
"""Test good start combination 2 POST query endpoint."""
214
216
good_start = {
@@ -224,9 +226,8 @@ async def test_good_start2_post_query(self, mock_handler, mock_object):
224
226
resp = await self .client .request ("POST" , "/query" , data = json .dumps (good_start ))
225
227
self .assertEqual (200 , resp .status )
226
228
227
- @asynctest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
228
- @asynctest .mock .patch ("beacon_api.app.query_request_handler" )
229
- @unittest_run_loop
229
+ @unittest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
230
+ @unittest .mock .patch ("beacon_api.app.query_request_handler" )
230
231
async def test_good_start3_post_query (self , mock_handler , mock_object ):
231
232
"""Test good start combination 3 POST query endpoint."""
232
233
good_start = {
@@ -242,53 +243,8 @@ async def test_good_start3_post_query(self, mock_handler, mock_object):
242
243
resp = await self .client .request ("POST" , "/query" , data = json .dumps (good_start ))
243
244
self .assertEqual (200 , resp .status )
244
245
245
- @unittest_run_loop
246
- async def test_unauthorized_no_token_post_query (self ):
247
- """Test unauthorized POST query endpoint, with no token."""
248
- resp = await self .client .request ("POST" , "/query" , data = json .dumps (PARAMS ), headers = {"Authorization" : "Bearer" })
249
- self .assertEqual (401 , resp .status )
250
-
251
- @unittest_run_loop
252
- async def test_unauthorized_token_post_query (self ):
253
- """Test unauthorized POST query endpoint, bad token."""
254
- resp = await self .client .request ("POST" , "/query" , data = json .dumps (PARAMS ), headers = {"Authorization" : f"Bearer { self .bad_token } " })
255
- self .assertEqual (403 , resp .status )
256
-
257
- @unittest_run_loop
258
- async def test_invalid_scheme_get_query (self ):
259
- """Test unauthorized GET query endpoint, invalid scheme."""
260
- params = "?assemblyId=GRCh38&referenceName=1&start=10000&referenceBases=A&alternateBases=T&datasetIds=dataset1"
261
- resp = await self .client .request ("GET" , f"/query{ params } " , headers = {"Authorization" : "SMTH x" })
262
- self .assertEqual (401 , resp .status )
263
-
264
- @asynctest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
265
- @asynctest .mock .patch ("beacon_api.app.query_request_handler" , side_effect = json .dumps (PARAMS ))
266
- @unittest_run_loop
267
- async def test_valid_token_get_query (self , mock_handler , mock_object ):
268
- """Test valid token GET query endpoint."""
269
- token = os .environ .get ("TOKEN" )
270
- resp = await self .client .request ("POST" , "/query" , data = json .dumps (PARAMS ), headers = {"Authorization" : f"Bearer { token } " })
271
- self .assertEqual (200 , resp .status )
272
-
273
- @unittest_run_loop
274
- async def test_bad_json_post_query (self ):
275
- """Test bad json POST query endpoint."""
276
- resp = await self .client .request ("POST" , "/query" , data = "" )
277
- self .assertEqual (500 , resp .status )
278
-
279
- @asynctest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
280
- @asynctest .mock .patch ("beacon_api.app.query_request_handler" , side_effect = json .dumps (PARAMS ))
281
- @unittest_run_loop
282
- async def test_valid_get_query (self , mock_handler , mock_object ):
283
- """Test valid GET query endpoint."""
284
- params = "?assemblyId=GRCh38&referenceName=1&start=10000&referenceBases=A&alternateBases=T"
285
- with asynctest .mock .patch ("beacon_api.app.initialize" , side_effect = create_db_mock ):
286
- resp = await self .client .request ("GET" , f"/query{ params } " )
287
- self .assertEqual (200 , resp .status )
288
-
289
- @asynctest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
290
- @asynctest .mock .patch ("beacon_api.app.query_request_handler" , side_effect = json .dumps (PARAMS ))
291
- @unittest_run_loop
246
+ @unittest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
247
+ @unittest .mock .patch ("beacon_api.app.query_request_handler" , side_effect = json .dumps (PARAMS ))
292
248
async def test_valid_post_query (self , mock_handler , mock_object ):
293
249
"""Test valid POST query endpoint."""
294
250
resp = await self .client .request ("POST" , "/query" , data = json .dumps (PARAMS ))
@@ -301,7 +257,13 @@ class AppTestCaseForbidden(AioHTTPTestCase):
301
257
Testing web app for wrong issuer.
302
258
"""
303
259
304
- @asynctest .mock .patch ("beacon_api.app.initialize" , side_effect = create_db_mock )
260
+ async def tearDownAsync (self ):
261
+ """Finish up tests."""
262
+ self .env .unset ("PUBLIC_KEY" )
263
+ self .env .unset ("TOKEN" )
264
+ await caches .get ("default" ).delete ("jwk_key" )
265
+
266
+ @unittest .mock .patch ("beacon_api.app.initialize" , side_effect = create_db_mock )
305
267
async def get_application (self , pool_mock ):
306
268
"""Retrieve web Application for test."""
307
269
token , public_key = generate_token ("something" )
@@ -310,56 +272,36 @@ async def get_application(self, pool_mock):
310
272
self .env .set ("TOKEN" , token )
311
273
return await init ()
312
274
313
- @unittest_run_loop
314
- async def tearDown (self ):
315
- """Finish up tests."""
316
- self .env .unset ("PUBLIC_KEY" )
317
- self .env .unset ("TOKEN" )
318
- await caches .get ("default" ).delete ("jwk_key" )
319
-
320
- @asynctest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
321
- @asynctest .mock .patch ("beacon_api.app.query_request_handler" , side_effect = json .dumps (PARAMS ))
322
- @unittest_run_loop
275
+ @unittest .mock .patch ("beacon_api.app.parse_request_object" , side_effect = mock_parse_request_object )
276
+ @unittest .mock .patch ("beacon_api.app.query_request_handler" , side_effect = json .dumps (PARAMS ))
323
277
async def test_forbidden_token_get_query (self , mock_handler , mock_object ):
324
278
"""Test forbidden GET query endpoint, invalid scheme."""
325
279
token = os .environ .get ("TOKEN" )
326
280
resp = await self .client .request ("POST" , "/query" , data = json .dumps (PARAMS ), headers = {"Authorization" : f"Bearer { token } " })
327
281
self .assertEqual (403 , resp .status )
328
282
329
283
330
- class TestBasicFunctionsApp (asynctest . TestCase ):
284
+ class TestBasicFunctionsApp (unittest . IsolatedAsyncioTestCase ):
331
285
"""Test App Base.
332
286
333
287
Testing basic functions from web app.
334
288
"""
335
289
336
- def setUp (self ):
337
- """Initialise fixtures."""
338
- pass
339
-
340
- def tearDown (self ):
341
- """Remove setup variables."""
342
- pass
343
-
344
- @mock .patch ("beacon_api.app.web" )
345
- def test_main (self , mock_webapp ):
346
- """Should start the webapp."""
347
- main ()
348
- mock_webapp .run_app .assert_called ()
349
-
350
- async def test_init (self ):
351
- """Test init type."""
352
- server = await init ()
353
- self .assertIs (type (server ), web .Application )
290
+ async def test_servinit (self ):
291
+ """Test server initialization function execution."""
292
+ # Don't really need much testing here, if the server initialization
293
+ # executes to the end all is fine.
294
+ app = await init ()
295
+ self .assertTrue (app is not None )
354
296
355
- @asynctest .mock .patch ("beacon_api.app.set_cors" )
297
+ @unittest .mock .patch ("beacon_api.app.set_cors" )
356
298
async def test_initialize (self , mock_cors ):
357
299
"""Test create db pool, should just return the result of init_db_pool.
358
300
359
301
We will mock the init_db_pool, thus we assert we just call it.
360
302
"""
361
303
app = {}
362
- with asynctest .mock .patch ("beacon_api.app.init_db_pool" ) as db_mock :
304
+ with unittest .mock .patch ("beacon_api.app.init_db_pool" ) as db_mock :
363
305
await initialize (app )
364
306
db_mock .assert_called ()
365
307
0 commit comments