Skip to content

Commit 94a2551

Browse files
authored
Add basic integration tests for all endpoints + API Keys tests (#1241)
* Added basic integration tests for all endpoints * Added base class for tests. Added API keys tests * Added TESTING_MODE to the ci.yaml, removed from Makefile * Updated acquisition tests with missing api_key creation
1 parent 63243a8 commit 94a2551

28 files changed

+1179
-1
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
7676
- name: Start delphi_web_epidata
7777
run: |
78-
docker run --rm -d -p 10080:80 --env "MODULE_NAME=delphi.epidata.server.main" --env "SQLALCHEMY_DATABASE_URI=mysql+mysqldb://user:pass@delphi_database_epidata:3306/epidata" --env "FLASK_SECRET=abc" --env "FLASK_PREFIX=/epidata" --env "REDIS_HOST=delphi_redis" --env "REDIS_PASSWORD=1234" --env "API_KEY_REGISTER_WEBHOOK_TOKEN=abc" --env "API_KEY_ADMIN_PASSWORD=test_admin_password" --network delphi-net --name delphi_web_epidata delphi_web_epidata
78+
docker run --rm -d -p 10080:80 --env "MODULE_NAME=delphi.epidata.server.main" --env "SQLALCHEMY_DATABASE_URI=mysql+mysqldb://user:pass@delphi_database_epidata:3306/epidata" --env "FLASK_SECRET=abc" --env "FLASK_PREFIX=/epidata" --env "REDIS_HOST=delphi_redis" --env "REDIS_PASSWORD=1234" --env "API_KEY_REGISTER_WEBHOOK_TOKEN=abc" --env "API_KEY_ADMIN_PASSWORD=test_admin_password" --env "TESTING_MODE=True" --network delphi-net --name delphi_web_epidata delphi_web_epidata
7979
docker ps
8080
8181
- name: Run Unit Tests

dev/local/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ web:
104104
--env "REDIS_PASSWORD=1234" \
105105
--env "API_KEY_ADMIN_PASSWORD=test_admin_password" \
106106
--env "API_KEY_REGISTER_WEBHOOK_TOKEN=abc" \
107+
--env "TESTING_MODE=True" \
107108
--network delphi-net --name delphi_web_epidata \
108109
delphi_web_epidata >$(LOG_WEB) 2>&1 &
109110

integrations/acquisition/covidcast/test_covidcast_meta_caching.py

+13
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ def setUp(self):
5858
secrets.db.host = 'delphi_database_epidata'
5959
secrets.db.epi = ('user', 'pass')
6060

61+
epidata_cnx = mysql.connector.connect(
62+
user='user',
63+
password='pass',
64+
host='delphi_database_epidata',
65+
database='epidata')
66+
epidata_cur = epidata_cnx.cursor()
67+
68+
epidata_cur.execute("DELETE FROM `api_user`")
69+
epidata_cur.execute('INSERT INTO `api_user`(`api_key`, `email`) VALUES("key", "email")')
70+
epidata_cnx.commit()
71+
epidata_cur.close()
72+
epidata_cnx.close()
73+
6174
# use the local instance of the Epidata API
6275
Epidata.BASE_URL = BASE_URL
6376
Epidata.auth = ('epidata', 'key')

integrations/acquisition/covidcast/test_csv_uploading.py

+13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ def setUp(self):
5555
secrets.db.host = 'delphi_database_epidata'
5656
secrets.db.epi = ('user', 'pass')
5757

58+
epidata_cnx = mysql.connector.connect(
59+
user='user',
60+
password='pass',
61+
host='delphi_database_epidata',
62+
database='epidata')
63+
epidata_cur = epidata_cnx.cursor()
64+
65+
epidata_cur.execute("DELETE FROM `api_user`")
66+
epidata_cur.execute('INSERT INTO `api_user`(`api_key`, `email`) VALUES("key", "email")')
67+
epidata_cnx.commit()
68+
epidata_cur.close()
69+
epidata_cnx.close()
70+
5871
# use the local instance of the Epidata API
5972
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
6073
Epidata.auth = ('epidata', 'key')

integrations/server/test_api_keys.py

+249
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
"""Integration tests for the API Keys"""
2+
import requests
3+
4+
# first party
5+
from delphi.epidata.common.integration_test_base_class import DelphiTestBase
6+
7+
8+
class APIKeysTets(DelphiTestBase):
9+
"""Tests the API Keys behaviour"""
10+
11+
def localSetUp(self):
12+
self.role_name = "cdc"
13+
14+
def _make_request(self, url: str = None, params: dict = {}, auth: tuple = None):
15+
if not url:
16+
url = self.epidata_client.BASE_URL
17+
response = requests.get(url, params=params, auth=auth)
18+
return response
19+
20+
def test_public_route(self):
21+
"""Test public route"""
22+
public_route = "http://delphi_web_epidata/epidata/version"
23+
status_codes = set()
24+
for _ in range(10):
25+
status_codes.add(self._make_request(public_route).status_code)
26+
self.assertEqual(status_codes, {200})
27+
28+
def test_no_multiples_data_source(self):
29+
"""Test requests with no multiples and with provided `data_source` and `signal` as a separate query params."""
30+
params = {
31+
"source": "covidcast",
32+
"data_source": "fb-survey",
33+
"signal": "smoothed_wcli",
34+
"time_type": "day",
35+
"geo_type": "state",
36+
"geo_value": "pa",
37+
"time_values": "20200406",
38+
}
39+
status_codes = set()
40+
for _ in range(10):
41+
status_codes.add(self._make_request(params=params).status_code)
42+
self.assertEqual(status_codes, {200})
43+
44+
def test_no_multiples_source_signal(self):
45+
"""Test requests with colon-delimited source-signal param presentation."""
46+
params = {
47+
"source": "covidcast",
48+
"signal": "fb-survey:smoothed_wcli",
49+
"time_type": "day",
50+
"geo_type": "state",
51+
"geo_value": "pa",
52+
"time_values": "20200406",
53+
}
54+
status_codes = set()
55+
for _ in range(10):
56+
status_codes.add(self._make_request(params=params).status_code)
57+
self.assertEqual(status_codes, {200})
58+
59+
def test_multiples_allowed_signal_two_multiples(self):
60+
"""Test requests with 2 multiples and allowed dashboard signal"""
61+
params = {
62+
"source": "covidcast",
63+
"signal": "fb-survey:smoothed_wcli",
64+
"time_type": "day",
65+
"geo_type": "state",
66+
"geo_value": "pa,ny",
67+
"time_values": "20200406,20200407",
68+
}
69+
status_codes = set()
70+
for _ in range(10):
71+
status_codes.add(self._make_request(params=params).status_code)
72+
self.assertEqual(status_codes, {200})
73+
74+
def test_multiples_non_allowed_signal(self):
75+
"""Test requests with 2 multiples and non-allowed dashboard signal"""
76+
params = {
77+
"source": "covidcast",
78+
"signal": "hospital-admissions:smoothed_adj_covid19_from_claims",
79+
"time_type": "day",
80+
"geo_type": "state",
81+
"geo_value": "pa,ny",
82+
"time_values": "20200406,20200407",
83+
}
84+
status_codes = set()
85+
for _ in range(10):
86+
status_codes.add(self._make_request(params=params).status_code)
87+
self.assertEqual(status_codes, {200, 429})
88+
89+
def test_multiples_mixed_allowed_signal_two_multiples(self):
90+
"""Test requests with 2 multiples and mixed-allowed dashboard signal"""
91+
params = {
92+
"source": "covidcast",
93+
"signal": "fb-survey:smoothed_wcli,hospital-admissions:smoothed_adj_covid19_from_claims",
94+
"time_type": "day",
95+
"geo_type": "state",
96+
"geo_value": "pa",
97+
"time_values": "20200406,20200407",
98+
}
99+
status_codes = set()
100+
for _ in range(10):
101+
status_codes.add(self._make_request(params=params).status_code)
102+
self.assertEqual(status_codes, {200, 429})
103+
104+
def test_multiples_allowed_signal_three_multiples(self):
105+
"""Test requests with 3 multiples and allowed dashboard signal"""
106+
params = {
107+
"source": "covidcast",
108+
"signal": "fb-survey:smoothed_wcli,fb-survey:smoothed_wcli",
109+
"time_type": "day",
110+
"geo_type": "state",
111+
"geo_value": "pa,ny",
112+
"time_values": "20200406,20200407",
113+
}
114+
status_codes = set()
115+
for _ in range(10):
116+
status_codes.add(self._make_request(params=params).status_code)
117+
self.assertEqual(status_codes, {401})
118+
119+
def test_multiples_mixed_allowed_signal_three_multiples(self):
120+
"""Test requests with 3 multiples and mixed-allowed dashboard signal"""
121+
params = {
122+
"source": "covidcast",
123+
"signal": "fb-survey:smoothed_wcli,fb-survey:smoothed_wcli1",
124+
"time_type": "day",
125+
"geo_type": "state",
126+
"geo_value": "pa,ny",
127+
"time_values": "20200406,20200407",
128+
}
129+
status_codes = set()
130+
for _ in range(10):
131+
status_codes.add(self._make_request(params=params).status_code)
132+
self.assertEqual(status_codes, {401})
133+
134+
def test_multiples_mixed_allowed_signal_api_key(self):
135+
"""Test requests with 3 multiples and mixed-allowed dashboard signal + valid API Key"""
136+
params = {
137+
"source": "covidcast",
138+
"signal": "fb-survey:smoothed_wcli,fb-survey:smoothed_wcli1",
139+
"time_type": "day",
140+
"geo_type": "state",
141+
"geo_value": "pa,ny",
142+
"time_values": "20200406,20200407",
143+
}
144+
status_codes = set()
145+
for _ in range(10):
146+
status_codes.add(
147+
self._make_request(params=params, auth=self.epidata_client.auth).status_code
148+
)
149+
self.assertEqual(status_codes, {200})
150+
151+
def test_multiples_allowed_signal_api_key(self):
152+
"""Test requests with 3 multiples and allowed dashboard signal + valid API Key"""
153+
params = {
154+
"source": "covidcast",
155+
"signal": "fb-survey:smoothed_wcli,fb-survey:smoothed_wcli",
156+
"time_type": "day",
157+
"geo_type": "state",
158+
"geo_value": "pa,ny",
159+
"time_values": "20200406,20200407",
160+
}
161+
status_codes = set()
162+
for _ in range(10):
163+
status_codes.add(
164+
self._make_request(params=params, auth=self.epidata_client.auth).status_code
165+
)
166+
self.assertEqual(status_codes, {200})
167+
168+
def test_no_multiples_allowed_signal_api_key(self):
169+
"""Test requests with no multiples and allowed dashboard signal + valid API Key"""
170+
params = {
171+
"source": "covidcast",
172+
"signal": "fb-survey:smoothed_wcli",
173+
"time_type": "day",
174+
"geo_type": "state",
175+
"geo_value": "pa",
176+
"time_values": "20200406",
177+
}
178+
status_codes = set()
179+
for _ in range(10):
180+
status_codes.add(
181+
self._make_request(params=params, auth=self.epidata_client.auth).status_code
182+
)
183+
self.assertEqual(status_codes, {200})
184+
185+
def test_no_multiples_allowed_signal_bad_api_key(self):
186+
"""Test requests with no multiples and allowed dashboard signal + bad API Key"""
187+
params = {
188+
"source": "covidcast",
189+
"signal": "fb-survey:smoothed_wcli",
190+
"time_type": "day",
191+
"geo_type": "state",
192+
"geo_value": "pa",
193+
"time_values": "20200406",
194+
}
195+
status_codes = set()
196+
for _ in range(10):
197+
status_codes.add(
198+
self._make_request(
199+
params=params, auth=("bad_key", "bad_email")
200+
).status_code
201+
)
202+
self.assertEqual(status_codes, {200})
203+
204+
def test_restricted_endpoint_no_key(self):
205+
"""Test restricted endpoint with no auth key"""
206+
params = {"source": "cdc", "regions": "1as", "epiweeks": "202020"}
207+
status_codes = set()
208+
for _ in range(10):
209+
status_codes.add(self._make_request(params=params).status_code)
210+
self.assertEqual(status_codes, {401})
211+
212+
def test_restricted_endpoint_invalid_key(self):
213+
"""Test restricted endpoint with invalid auth key"""
214+
params = {
215+
"source": "cdc",
216+
"regions": "1as",
217+
"epiweeks": "202020",
218+
"auth": "invalid_key",
219+
}
220+
status_codes = set()
221+
for _ in range(10):
222+
status_codes.add(self._make_request(params=params).status_code)
223+
self.assertEqual(status_codes, {401})
224+
225+
def test_restricted_endpoint_no_roles_key(self):
226+
"""Test restricted endpoint with no roles key"""
227+
params = {
228+
"source": "cdc",
229+
"regions": "1as",
230+
"epiweeks": "202020",
231+
"auth": "key",
232+
}
233+
status_codes = set()
234+
for _ in range(10):
235+
status_codes.add(self._make_request(params=params).status_code)
236+
self.assertEqual(status_codes, {401})
237+
238+
def test_restricted_endpoint_valid_roles_key(self):
239+
"""Test restricted endpoint with valid auth key with required role"""
240+
params = {
241+
"source": "cdc",
242+
"regions": "1as",
243+
"epiweeks": "202020",
244+
"auth": "cdc_key",
245+
}
246+
status_codes = set()
247+
for _ in range(10):
248+
status_codes.add(self._make_request(params=params).status_code)
249+
self.assertEqual(status_codes, {200})

integrations/server/test_cdc.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# first party
2+
from delphi.epidata.common.integration_test_base_class import DelphiTestBase
3+
4+
5+
class CdcTest(DelphiTestBase):
6+
"""Basic integration tests for cdc endpint."""
7+
8+
def localSetUp(self) -> None:
9+
self.truncate_tables_list = ["cdc_extract"]
10+
self.role_name = "cdc"
11+
12+
def test_cdc(self):
13+
"""Basic integration test for cdc endpoint"""
14+
self.cur.execute(
15+
"INSERT INTO `cdc_extract`(`epiweek`, `state`, `num1`, `num2`, `num3`, `num4`, `num5`, `num6`, `num7`, `num8`, `total`) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
16+
("201102", "AK", "16", "35", "51", "96", "30", "748", "243", "433", "65"),
17+
)
18+
self.cnx.commit()
19+
response = self.epidata_client.cdc(auth="cdc_key", epiweeks=201102, locations="cen9")
20+
self.assertEqual(
21+
response,
22+
{
23+
"epidata": [
24+
{
25+
"location": "cen9",
26+
"epiweek": 201102,
27+
"num1": 16,
28+
"num2": 35,
29+
"num3": 51,
30+
"num4": 96,
31+
"num5": 30,
32+
"num6": 748,
33+
"num7": 243,
34+
"num8": 433,
35+
"total": 65,
36+
"value": None,
37+
}
38+
],
39+
"result": 1,
40+
"message": "success",
41+
},
42+
)

0 commit comments

Comments
 (0)