66
77import sys
88import ssl
9- import base64
109import unittest
1110from aiohttp import ClientSession , TCPConnector
1211
1817ZABBIX_URL = 'https://127.0.0.1:443'
1918ZABBIX_USER = 'Admin'
2019ZABBIX_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" )
9220
9321
9422class CustomCertAPITest (unittest .TestCase ):
@@ -154,80 +82,6 @@ def test_user_get(self):
15482 self .assertEqual (type (users ), list , "Request user.get was going wrong" )
15583
15684
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-
23185class CustomCertAsyncAPITest (unittest .IsolatedAsyncioTestCase ):
23286 """Test working with a real Zabbix API instance asynchronously"""
23387
@@ -238,14 +92,14 @@ async def asyncSetUp(self):
23892
23993 context = ssl .create_default_context ()
24094 context .load_verify_locations ('/etc/nginx/ssl/nginx.crt' )
241- session = ClientSession (
95+ self . session = ClientSession (
24296 connector = TCPConnector (ssl = context )
24397 )
24498
24599 self .api = AsyncZabbixAPI (
246100 url = self .url ,
247101 skip_version_check = True ,
248- client_session = session
102+ client_session = self . session
249103 )
250104 await self .api .login (
251105 user = self .user ,
@@ -255,6 +109,8 @@ async def asyncSetUp(self):
255109 async def asyncTearDown (self ):
256110 if self .api :
257111 await self .api .logout ()
112+ if not self .session .closed :
113+ await self .session .close ()
258114
259115 async def test_login (self ):
260116 """Tests login function works properly"""
0 commit comments