-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtest_differentiated_access.py
118 lines (99 loc) · 3.68 KB
/
test_differentiated_access.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
# third party
import mysql.connector
# frirst party
from delphi.epidata.acquisition.covidcast.test_utils import (
CovidcastBase,
CovidcastTestRow,
)
class DifferentiatedAccessTests(CovidcastBase):
def localSetUp(self):
"""Perform per-test setup"""
self._db._cursor.execute(
'update covidcast_meta_cache set timestamp = 0, epidata = "[]"'
)
def setUp(self):
# connect to the `epidata` database
super().setUp()
self.maxDiff = None
cnx = mysql.connector.connect(
user="user",
password="pass",
host="delphi_database_epidata",
database="epidata",
)
cur = cnx.cursor()
cur.execute("DELETE FROM `api_user`")
cur.execute("TRUNCATE TABLE `user_role`")
cur.execute("TRUNCATE TABLE `user_role_link`")
cur.execute(
'INSERT INTO `api_user`(`api_key`, `email`) VALUES ("api_key", "[email protected]")'
)
cur.execute(
'INSERT INTO `api_user`(`api_key`, `email`) VALUES("ny_key", "[email protected]")'
)
cur.execute('INSERT INTO `user_role`(`name`) VALUES("state:ny")')
cur.execute(
'INSERT INTO `user_role_link`(`user_id`, `role_id`) SELECT `api_user`.`id`, 1 FROM `api_user` WHERE `api_key` = "ny_key"'
)
cnx.commit()
cur.close()
cnx.close()
def request_based_on_row(self, row: CovidcastTestRow, **kwargs):
params = self.params_from_row(row, endpoint="differentiated_access", **kwargs)
# use local instance of the Epidata API
response = requests.get(
"http://delphi_web_epidata/epidata/api.php", params=params
)
response.raise_for_status()
return response.json()
def _insert_placeholder_restricted_geo(self):
geo_values = ["36029", "36047", "36097", "36103", "36057", "36041", "36033"]
rows = [
CovidcastTestRow.make_default_row(
source="restricted-source",
geo_type="county",
geo_value=geo_values[i],
time_value=2000_01_01 + i,
value=i * 1.0,
stderr=i * 10.0,
sample_size=i * 100.0,
issue=2000_01_03,
lag=2 - i,
)
for i in [1, 2, 3]
] + [
# time value intended to overlap with the time values above, with disjoint geo values
CovidcastTestRow.make_default_row(
source="restricted-source",
geo_type="county",
geo_value=geo_values[i],
time_value=2000_01_01 + i - 3,
value=i * 1.0,
stderr=i * 10.0,
sample_size=i * 100.0,
issue=2000_01_03,
lag=5 - i,
)
for i in [4, 5, 6]
]
self._insert_rows(rows)
return rows
def test_restricted_geo_ny_role(self):
# insert placeholder data
rows = self._insert_placeholder_restricted_geo()
# make request
response = self.request_based_on_row(rows[0], token="ny_key")
expected = {
"result": 1,
"epidata": [rows[0].as_api_compatibility_row_dict()],
"message": "success",
}
self.assertEqual(response, expected)
def test_restricted_geo_default_role(self):
# insert placeholder data
rows = self._insert_placeholder_restricted_geo()
# make request
response = self.request_based_on_row(rows[0], token="api_key")
expected = {"result": -2, "message": "no results"}
self.assertEqual(response, expected)