-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_api.py
238 lines (188 loc) · 7.99 KB
/
test_api.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from urllib3 import exceptions
from unittest import TestCase
from unittest.mock import MagicMock, patch, Mock
from grafana_api.model import APIModel, RequestsMethods
from grafana_api.api import Api
class ApiTestCase(TestCase):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
api: Api = Api(grafana_api_model=model)
def test_call_the_api_non_method(self):
with self.assertRaises(Exception):
self.api.call_the_api(api_call=MagicMock(), method=None)
def test_call_the_api_non_valid_method(self):
with self.assertRaises(Exception):
self.api.call_the_api(api_call=MagicMock(), method=MagicMock())
@patch("urllib3.PoolManager")
def test_call_the_api_basic_auth(self, pool_mock):
model: APIModel = APIModel(
host="https://test.test.de", username="test", password="test"
)
api: Api = Api(grafana_api_model=model)
pool_mock.return_value.request.return_value.data = b'{"status": 200}'
self.assertEqual(
200,
api.call_the_api(api_call=MagicMock())["status"],
)
@patch("urllib3.PoolManager")
def test_call_the_api_org_id(self, pool_mock):
pool_mock.return_value.request.return_value.data = b'{"status": "success"}'
self.assertEqual(
"success",
self.api.call_the_api(api_call=MagicMock(), org_id_header=1)["status"],
)
@patch("urllib3.PoolManager")
def test_call_the_api_disable_provenance(self, pool_mock):
pool_mock.return_value.request.return_value.data = b'{"status": "success"}'
self.assertEqual(
"success",
self.api.call_the_api(api_call=MagicMock(), disable_provenance_header=True)["status"],
)
@patch("urllib3.PoolManager")
def test_call_the_api_get_valid(self, pool_mock):
pool_mock.return_value.request.return_value.data = b'{"status": "success"}'
self.assertEqual(
"success",
self.api.call_the_api(api_call=MagicMock())["status"],
)
def test_call_the_api_get_not_valid(self):
with self.assertRaises(exceptions.MaxRetryError):
self.api.call_the_api(api_call=MagicMock(), method=RequestsMethods.GET)
@patch("urllib3.PoolManager")
def test_call_the_api_put_valid(self, pool_mock):
pool_mock.return_value.request.return_value.data = b'{"status": "success"}'
self.assertEqual(
"success",
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.PUT,
json_complete=MagicMock(),
)["status"],
)
def test_call_the_api_put_not_valid(self):
with self.assertRaises(Exception):
self.api.call_the_api(api_call=MagicMock(), method=RequestsMethods.PUT)
@patch("urllib3.PoolManager")
def test_call_the_api_post_valid(self, pool_mock):
pool_mock.return_value.request.return_value.data = b'{"status": "success"}'
self.assertEqual(
"success",
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.POST,
json_complete=MagicMock(),
)["status"],
)
def test_call_the_api_post_not_valid(self):
with self.assertRaises(exceptions.ProtocolError):
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.POST,
json_complete=MagicMock(),
)
def test_call_the_api_post_no_data(self):
with self.assertRaises(Exception):
self.api.call_the_api(api_call=MagicMock(), method=RequestsMethods.POST)
@patch("urllib3.PoolManager")
def test_call_the_api_patch_valid(self, pool_mock):
pool_mock.return_value.request.return_value.data = b'{"status": "success"}'
self.assertEqual(
"success",
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.PATCH,
json_complete=MagicMock(),
)["status"],
)
def test_call_the_api_patch_not_valid(self):
with self.assertRaises(exceptions.ProtocolError):
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.PATCH,
json_complete=MagicMock(),
)
def test_call_the_api_patch_no_data(self):
with self.assertRaises(Exception):
self.api.call_the_api(api_call=MagicMock(), method=RequestsMethods.PATCH)
@patch("urllib3.PoolManager")
def test_call_the_api_delete_valid(self, pool_mock):
pool_mock.return_value.request.return_value.data = (
b'{"message": "Deletion successful"}'
)
self.assertEqual(
"Deletion successful",
self.api.call_the_api(api_call=MagicMock(), method=RequestsMethods.DELETE)[
"message"
],
)
def test_call_the_api_delete_not_valid(self):
with self.assertRaises(Exception):
self.api.call_the_api(api_call=MagicMock(), method=RequestsMethods.DELETE)
def test_check_the_api_call_response(self):
mock: Mock = Mock()
mock.data = b'{"test": "test"}'
self.assertEqual(
dict({"test": "test"}),
self.api._Api__check_the_api_call_response(response=mock),
)
def test_check_the_api_call_response_no_error_message(self):
mock: Mock = Mock()
mock.data = b'{"message": "test"}'
self.assertEqual(
dict({"message": "test"}),
self.api._Api__check_the_api_call_response(response=mock),
)
def test_check_the_api_call_response_no_json_response_value(self):
mock: Mock = Mock()
mock.data = b"test"
self.assertEqual(
b"test", self.api._Api__check_the_api_call_response(response=mock).data
)
def test_check_the_api_call_response_exception(self):
mock: Mock = Mock()
mock.data = b'{"message": "invalid API key"}'
with self.assertRaises(exceptions.ProtocolError):
self.api._Api__check_the_api_call_response(response=mock)
@patch("grafana_api.api.Api._Api__check_if_valid_json")
def test_check_the_api_call_response_valid_json(self, check_if_valid_json_mock):
check_if_valid_json_mock.return_value = True
mock: Mock = Mock()
mock.data = b"{}"
self.assertEqual({}, self.api._Api__check_the_api_call_response(response=mock))
@patch("grafana_api.api.Api._Api__check_if_valid_json")
def test_check_the_api_call_response_no_valid_json_status_code_result(
self, check_if_valid_json_mock
):
check_if_valid_json_mock.return_value = False
mock: Mock = Mock()
mock.data = b""
mock.status = 200
self.assertEqual(
dict({"status": 200, "data": ""}),
self.api._Api__check_the_api_call_response(
response=mock, response_status_code=True
),
)
def test_check_the_api_call_response_return_status_code_dict(self):
mock: Mock = Mock()
mock.data = b'{"test": "test"}'
mock.status = 200
self.assertEqual(
dict({"status": 200, "test": "test"}),
self.api._Api__check_the_api_call_response(
response=mock, response_status_code=True
),
)
def test_check_the_api_call_response_return_status_code_list(self):
mock: Mock = Mock()
mock.data = b'[{"test": "test"}, {"test": "test"}]'
mock.status = 200
self.assertEqual(
list([{"status": 200, "test": "test"}, {"test": "test"}]),
self.api._Api__check_the_api_call_response(
response=mock, response_status_code=True
),
)
def test_prepare_api_string(self):
self.assertEqual("test&", self.api.prepare_api_string("test"))
def test_prepare_api_string_no_real_value(self):
self.assertEqual("", self.api.prepare_api_string(""))