-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_api.py
182 lines (141 loc) · 5.86 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
import requests
from unittest import TestCase
from unittest.mock import MagicMock, patch, Mock
from requests.exceptions import MissingSchema
from src.grafana_api.model import APIModel, RequestsMethods
from src.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("requests.get")
def test_call_the_api_basic_auth(self, get_mock):
model: APIModel = APIModel(
host="https://test.test.de", username="test", password="test"
)
api: Api = Api(grafana_api_model=model)
mock: Mock = Mock()
mock.json = Mock(return_value={"status": "success"})
get_mock.return_value = mock
self.assertEqual(
"success",
api.call_the_api(api_call=MagicMock()).json()["status"],
)
@patch("requests.get")
def test_call_the_api_get_valid(self, get_mock):
mock: Mock = Mock()
mock.json = Mock(return_value={"status": "success"})
get_mock.return_value = mock
self.assertEqual(
"success",
self.api.call_the_api(api_call=MagicMock()).json()["status"],
)
def test_call_the_api_get_not_valid(self):
with self.assertRaises(MissingSchema):
self.api.call_the_api(api_call=MagicMock(), method=RequestsMethods.GET)
@patch("requests.put")
def test_call_the_api_put_valid(self, put_mock):
mock: Mock = Mock()
mock.json = Mock(return_value={"status": "success"})
put_mock.return_value = mock
self.assertEqual(
"success",
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.PUT,
json_complete=MagicMock(),
).json()["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("requests.post")
def test_call_the_api_post_valid(self, post_mock):
mock: Mock = Mock()
mock.json = Mock(return_value={"status": "success"})
post_mock.return_value = mock
self.assertEqual(
"success",
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.POST,
json_complete=MagicMock(),
).json()["status"],
)
def test_call_the_api_post_not_valid(self):
with self.assertRaises(MissingSchema):
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("requests.patch")
def test_call_the_api_patch_valid(self, post_mock):
mock: Mock = Mock()
mock.json = Mock(return_value={"status": "success"})
post_mock.return_value = mock
self.assertEqual(
"success",
self.api.call_the_api(
api_call=MagicMock(),
method=RequestsMethods.PATCH,
json_complete=MagicMock(),
).json()["status"],
)
def test_call_the_api_patch_not_valid(self):
with self.assertRaises(MissingSchema):
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("requests.delete")
def test_call_the_api_delete_valid(self, delete_mock):
mock: Mock = Mock()
mock.json = Mock(return_value={"message": "Deletion successful"})
delete_mock.return_value = mock
self.assertEqual(
"Deletion successful",
self.api.call_the_api(
api_call=MagicMock(), method=RequestsMethods.DELETE
).json()["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.json = Mock(return_value=dict({"test": "test"}))
self.assertEqual(
dict({"test": "test"}),
self.api._Api__check_the_api_call_response(response=mock).json(),
)
def test_check_the_api_call_response_no_error_message(self):
mock: Mock = Mock()
mock.json = Mock(return_value=dict({"message": "test"}))
self.assertEqual(
dict({"message": "test"}),
self.api._Api__check_the_api_call_response(response=mock).json(),
)
def test_check_the_api_call_response_no_json_response_value(self):
mock: Mock = Mock()
mock.text = Mock(return_value="test")
self.assertEqual(
"test", self.api._Api__check_the_api_call_response(response=mock).text()
)
def test_check_the_api_call_response_exception(self):
mock: Mock = Mock()
mock.json = Mock(return_value=dict({"message": "invalid API key"}))
with self.assertRaises(requests.exceptions.ConnectionError):
self.api._Api__check_the_api_call_response(response=mock)