6
6
7
7
import sys
8
8
import ssl
9
- import base64
10
9
import unittest
11
10
from aiohttp import ClientSession , TCPConnector
12
11
18
17
ZABBIX_URL = 'https://127.0.0.1:443'
19
18
ZABBIX_USER = 'Admin'
20
19
ZABBIX_PASSWORD = 'zabbix'
21
- HTTP_USER = 'http_user'
22
- HTTP_PASSWORD = 'http_pass'
23
-
24
-
25
- class IntegrationAPITest (unittest .TestCase ):
26
- """Test working with a real Zabbix API instance synchronously"""
27
-
28
- def setUp (self ):
29
- self .user = ZABBIX_USER
30
- self .password = ZABBIX_PASSWORD
31
- self .url = ZABBIX_URL + '/http_auth/'
32
- self .api = ZabbixAPI (
33
- url = self .url ,
34
- user = self .user ,
35
- password = self .password ,
36
- skip_version_check = True ,
37
- validate_certs = False ,
38
- http_user = HTTP_USER ,
39
- http_password = HTTP_PASSWORD
40
- )
41
-
42
- def tearDown (self ):
43
- if self .api :
44
- self .api .logout ()
45
-
46
- def test_login (self ):
47
- """Tests login function works properly"""
48
-
49
- self .assertEqual (
50
- type (self .api ), ZabbixAPI , "Login was going wrong" )
51
- self .assertEqual (
52
- type (self .api .api_version ()), APIVersion , "Version getting was going wrong" )
53
-
54
- def test_basic_auth (self ):
55
- """Tests __basic_auth function works properly"""
56
-
57
- self .assertEqual (
58
- self .api ._ZabbixAPI__basic_cred , base64 .b64encode (
59
- "http_user:http_pass" .encode ()
60
- ).decode (), "Basic auth credentials generation was going wrong" )
61
-
62
- def test_version_get (self ):
63
- """Tests getting version info works properly"""
64
-
65
- version = None
66
- if self .api :
67
- version = self .api .apiinfo .version ()
68
- self .assertEqual (
69
- version , str (self .api .api_version ()), "Request apiinfo.version was going wrong" )
70
-
71
- def test_check_auth (self ):
72
- """Tests checking authentication state works properly"""
73
-
74
- resp = None
75
- if self .api :
76
- if self .api ._ZabbixAPI__session_id == self .api ._ZabbixAPI__token :
77
- resp = self .api .user .checkAuthentication (token = self .api ._ZabbixAPI__session_id )
78
- else :
79
- resp = self .api .user .checkAuthentication (sessionid = self .api ._ZabbixAPI__session_id )
80
- self .assertEqual (
81
- type (resp ), dict , "Request user.checkAuthentication was going wrong" )
82
-
83
- def test_user_get (self ):
84
- """Tests getting users info works properly"""
85
-
86
- users = None
87
- if self .api :
88
- users = self .api .user .get (
89
- output = ['userid' , 'name' ]
90
- )
91
- self .assertEqual (type (users ), list , "Request user.get was going wrong" )
92
20
93
21
94
22
class CustomCertAPITest (unittest .TestCase ):
@@ -154,80 +82,6 @@ def test_user_get(self):
154
82
self .assertEqual (type (users ), list , "Request user.get was going wrong" )
155
83
156
84
157
- class IntegrationAsyncAPITest (unittest .IsolatedAsyncioTestCase ):
158
- """Test working with a real Zabbix API instance asynchronously"""
159
-
160
- async def asyncSetUp (self ):
161
- self .user = ZABBIX_USER
162
- self .password = ZABBIX_PASSWORD
163
- self .url = ZABBIX_URL + '/http_auth/'
164
- self .api = AsyncZabbixAPI (
165
- url = self .url ,
166
- skip_version_check = True ,
167
- validate_certs = False ,
168
- http_user = HTTP_USER ,
169
- http_password = HTTP_PASSWORD
170
- )
171
- await self .api .login (
172
- user = self .user ,
173
- password = self .password
174
- )
175
-
176
- async def asyncTearDown (self ):
177
- if self .api :
178
- await self .api .logout ()
179
-
180
- async def test_login (self ):
181
- """Tests login function works properly"""
182
-
183
- self .assertEqual (
184
- type (self .api ), AsyncZabbixAPI , "Login was going wrong" )
185
- self .assertEqual (
186
- type (self .api .api_version ()), APIVersion , "Version getting was going wrong" )
187
-
188
- async def test_basic_auth (self ):
189
- """Tests __basic_auth function works properly"""
190
-
191
- basic_auth = self .api .client_session ._default_auth
192
-
193
- self .assertEqual (
194
- base64 .b64encode (f"{ basic_auth .login } :{ basic_auth .password } " .encode ()).decode (),
195
- base64 .b64encode (f"{ HTTP_USER } :{ HTTP_PASSWORD } " .encode ()).decode (),
196
- "Basic auth credentials generation was going wrong"
197
- )
198
-
199
- async def test_version_get (self ):
200
- """Tests getting version info works properly"""
201
-
202
- version = None
203
- if self .api :
204
- version = await self .api .apiinfo .version ()
205
- self .assertEqual (
206
- version , str (self .api .api_version ()), "Request apiinfo.version was going wrong" )
207
-
208
- async def test_check_auth (self ):
209
- """Tests checking authentication state works properly"""
210
-
211
- resp = None
212
- if self .api :
213
- if self .api ._AsyncZabbixAPI__session_id == self .api ._AsyncZabbixAPI__token :
214
- resp = await self .api .user .checkAuthentication (token = (self .api ._AsyncZabbixAPI__session_id or '' ))
215
- else :
216
- resp = await self .api .user .checkAuthentication (sessionid = (self .api ._AsyncZabbixAPI__session_id or '' ))
217
- self .assertEqual (
218
- type (resp ), dict , "Request user.checkAuthentication was going wrong" )
219
-
220
- async def test_user_get (self ):
221
- """Tests getting users info works properly"""
222
-
223
- users = None
224
- if self .api :
225
- users = await self .api .user .get (
226
- output = ['userid' , 'name' ]
227
- )
228
- self .assertEqual (type (users ), list , "Request user.get was going wrong" )
229
-
230
-
231
85
class CustomCertAsyncAPITest (unittest .IsolatedAsyncioTestCase ):
232
86
"""Test working with a real Zabbix API instance asynchronously"""
233
87
@@ -238,14 +92,14 @@ async def asyncSetUp(self):
238
92
239
93
context = ssl .create_default_context ()
240
94
context .load_verify_locations ('/etc/nginx/ssl/nginx.crt' )
241
- session = ClientSession (
95
+ self . session = ClientSession (
242
96
connector = TCPConnector (ssl = context )
243
97
)
244
98
245
99
self .api = AsyncZabbixAPI (
246
100
url = self .url ,
247
101
skip_version_check = True ,
248
- client_session = session
102
+ client_session = self . session
249
103
)
250
104
await self .api .login (
251
105
user = self .user ,
@@ -255,6 +109,8 @@ async def asyncSetUp(self):
255
109
async def asyncTearDown (self ):
256
110
if self .api :
257
111
await self .api .logout ()
112
+ if not self .session .closed :
113
+ await self .session .close ()
258
114
259
115
async def test_login (self ):
260
116
"""Tests login function works properly"""
0 commit comments