Debian Codegraph @

tests/api_test.py

raw
1 import pytest
2 from packaging.version import Version
3
4 from pyzabbix import ZabbixAPI, ZabbixAPIException
5
6
7 @pytest.mark.parametrize(
8 "server, expected",
9 [
10 ("http://example.com", "http://example.com/api_jsonrpc.php"),
11 ("http://example.com/", "http://example.com/api_jsonrpc.php"),
12 ("http://example.com/base", "http://example.com/base/api_jsonrpc.php"),
13 ("http://example.com/base/", "http://example.com/base/api_jsonrpc.php"),
14 ],
15 )
16 def test_server_url_correction(server, expected):
17 assert ZabbixAPI(server).url == expected
18
19
20 def _zabbix_requests_mock_factory(requests_mock, *args, **kwargs):
21 requests_mock.post(
22 "http://example.com/api_jsonrpc.php",
23 request_headers={
24 "Content-Type": "application/json-rpc",
25 "User-Agent": "python/pyzabbix",
26 "Cache-Control": "no-cache",
27 },
28 *args,
29 **kwargs,
30 )
31
32
33 def test_login(requests_mock):
34 _zabbix_requests_mock_factory(
35 requests_mock,
36 json={
37 "jsonrpc": "2.0",
38 "result": "0424bd59b807674191e7d77572075f33",
39 "id": 0,
40 },
41 )
42
43 zapi = ZabbixAPI("http://example.com", detect_version=False)
44 zapi.login("mylogin", "mypass")
45
46 # Check request
47 assert requests_mock.last_request.json() == {
48 "jsonrpc": "2.0",
49 "method": "user.login",
50 "params": {"user": "mylogin", "password": "mypass"},
51 "id": 0,
52 }
53
54 # Check login
55 assert zapi.auth == "0424bd59b807674191e7d77572075f33"
56
57
58 def test_login_with_context(requests_mock):
59 _zabbix_requests_mock_factory(
60 requests_mock,
61 json={
62 "jsonrpc": "2.0",
63 "result": "0424bd59b807674191e7d77572075f33",
64 "id": 0,
65 },
66 )
67
68 with ZabbixAPI("http://example.com", detect_version=False) as zapi:
69 zapi.login("mylogin", "mypass")
70 assert zapi.auth == "0424bd59b807674191e7d77572075f33"
71
72
73 @pytest.mark.parametrize(
74 "version",
75 [
76 ("4.0.0"),
77 ("5.4.0"),
78 ("6.2.0"),
79 ("6.2.0beta1"),
80 ("6.2.2alpha1"),
81 ],
82 )
83 def test_login_with_version_detect(requests_mock, version):
84 _zabbix_requests_mock_factory(
85 requests_mock,
86 [
87 {
88 "json": {
89 "jsonrpc": "2.0",
90 "result": version,
91 "id": 0,
92 }
93 },
94 {
95 "json": {
96 "jsonrpc": "2.0",
97 "result": "0424bd59b807674191e7d77572075f33",
98 "id": 0,
99 }
100 },
101 ],
102 )
103
104 with ZabbixAPI("http://example.com") as zapi:
105 zapi.login("mylogin", "mypass")
106 assert zapi.auth == "0424bd59b807674191e7d77572075f33"
107
108
109 def test_attr_syntax_kwargs(requests_mock):
110 _zabbix_requests_mock_factory(
111 requests_mock,
112 json={
113 "jsonrpc": "2.0",
114 "result": [{"hostid": 1234}],
115 "id": 0,
116 },
117 )
118
119 zapi = ZabbixAPI("http://example.com", detect_version=False)
120 zapi.auth = "some_auth_key"
121 result = zapi.host.get(hostids=5)
122
123 # Check request
124 assert requests_mock.last_request.json() == {
125 "jsonrpc": "2.0",
126 "method": "host.get",
127 "params": {"hostids": 5},
128 "auth": "some_auth_key",
129 "id": 0,
130 }
131
132 # Check response
133 assert result == [{"hostid": 1234}]
134
135
136 def test_attr_syntax_args(requests_mock):
137 _zabbix_requests_mock_factory(
138 requests_mock,
139 json={
140 "jsonrpc": "2.0",
141 "result": {"itemids": ["22982", "22986"]},
142 "id": 0,
143 },
144 )
145
146 zapi = ZabbixAPI("http://example.com", detect_version=False)
147 zapi.auth = "some_auth_key"
148 result = zapi.host.delete("22982", "22986")
149
150 # Check request
151 assert requests_mock.last_request.json() == {
152 "jsonrpc": "2.0",
153 "method": "host.delete",
154 "params": ["22982", "22986"],
155 "auth": "some_auth_key",
156 "id": 0,
157 }
158
159 # Check response
160 assert result == {"itemids": ["22982", "22986"]}
161
162
163 def test_attr_syntax_args_and_kwargs_raises():
164 with pytest.raises(
165 TypeError,
166 match="Found both args and kwargs",
167 ):
168 zapi = ZabbixAPI("http://example.com")
169 zapi.host.delete("22982", hostids=5)
170
171
172 @pytest.mark.parametrize(
173 "version",
174 [
175 ("4.0.0"),
176 ("4.0.0rc1"),
177 ("6.2.0beta1"),
178 ("6.2.2alpha1"),
179 ],
180 )
181 def test_detecting_version(requests_mock, version):
182 _zabbix_requests_mock_factory(
183 requests_mock,
184 json={
185 "jsonrpc": "2.0",
186 "result": version,
187 "id": 0,
188 },
189 )
190
191 zapi = ZabbixAPI("http://example.com")
192 zapi.login("mylogin", "mypass")
193
194 assert zapi.api_version() == version
195
196
197 @pytest.mark.parametrize(
198 "data",
199 [
200 (None),
201 ('No groups for host "Linux server".'),
202 ],
203 )
204 def test_error_response(requests_mock, data):
205 _zabbix_requests_mock_factory(
206 requests_mock,
207 json={
208 "jsonrpc": "2.0",
209 "error": {
210 "code": -32602,
211 "message": "Invalid params.",
212 **({} if data is None else {"data": data}),
213 },
214 "id": 0,
215 },
216 )
217
218 with pytest.raises(
219 ZabbixAPIException,
220 match="Error -32602: Invalid params., No data."
221 if data is None
222 else f"Error -32602: Invalid params., {data}",
223 ):
224 zapi = ZabbixAPI("http://example.com")
225 zapi.host.get()
226
227
228 def test_empty_response(requests_mock):
229 _zabbix_requests_mock_factory(
230 requests_mock,
231 body="",
232 )
233
234 with pytest.raises(ZabbixAPIException, match="Received empty response"):
235 zapi = ZabbixAPI("http://example.com")
236 zapi.login("mylogin", "mypass")
237
238
239 @pytest.mark.parametrize(
240 "version",
241 [
242 ("4.0.0"),
243 ("5.4.0"),
244 ("6.2.0"),
245 ],
246 )
247 def test_do_request(requests_mock, version):
248 _zabbix_requests_mock_factory(
249 requests_mock,
250 json={
251 "jsonrpc": "2.0",
252 "result": [{"hostid": 1234}],
253 "id": 0,
254 },
255 )
256
257 zapi = ZabbixAPI("http://example.com", detect_version=False)
258 zapi.version = Version(version)
259 zapi.auth = "some_auth_key"
260 result = zapi["host"]["get"]()
261
262 # Check response
263 assert result == [{"hostid": 1234}]
264
265 # Check request
266 found = requests_mock.last_request
267 expect_json = {
268 "jsonrpc": "2.0",
269 "method": "host.get",
270 "params": {},
271 "id": 0,
272 }
273 expect_headers = {
274 "Cache-Control": "no-cache",
275 "Content-Type": "application/json-rpc",
276 "User-Agent": "python/pyzabbix",
277 }
278
279 if zapi.version < Version("6.4.0"):
280 expect_json["auth"] = "some_auth_key"
281 else:
282 expect_headers["Authorization"] = "Bearer some_auth_key"
283
284 assert found.json() == expect_json
285 assert found.headers.items() >= expect_headers.items()