Coverage for tests/routers/test_routers.py: 100%
46 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-25 05:47 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-25 05:47 +0000
1import pytest
2from requests.exceptions import ConnectionError
3from zenossapi.apiclient import ZenossAPIClientAuthenticationError, ZenossAPIClientError
4from zenossapi.routers import ZenossRouter
6pytest_plugins = "pytest-responses"
7url = 'https://zenoss/zport/dmd'
8headers = dict(
9 ContentType='application/json'
10)
13class TestBaseRouter(object):
15 def test_routers_init(self):
16 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
17 assert br.api_url == url
18 assert br.api_headers == headers
19 assert br.ssl_verify
20 assert br.api_endpoint == 'test_router'
21 assert br.api_action == 'TestRouter'
23 def test_routers_make_request_data(self):
24 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
26 expected = dict(
27 action='TestRouter',
28 method='TestMethod',
29 data=[dict()],
30 tid=1,
31 )
33 request_data = br._make_request_data(
34 'TestMethod',
35 dict(),
36 )
38 assert request_data == expected
40 def test_routers_router_request(self, responses):
41 responses.add(
42 responses.POST,
43 '{0}/test_router'.format(url),
44 json={'result': {'success': True}}
45 )
47 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
49 resp = br._router_request({'data': dict()})
50 assert resp['success']
52 def test_routers_router_request_login_failure(self, responses):
53 responses.add(
54 responses.POST,
55 '{0}/test_router'.format(url),
56 headers={'location': '{0}/login_form'.format(url)},
57 status=302,
58 )
59 responses.add(
60 responses.GET,
61 '{0}/login_form'.format(url),
62 body='',
63 )
65 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
66 with pytest.raises(ZenossAPIClientAuthenticationError, match='API Login Failed'):
67 resp = br._router_request({'data': dict()})
69 def test_routers_router_request_failure(self, responses):
70 responses.add(
71 responses.POST,
72 '{0}/test_router'.format(url),
73 json={'result': {'success': False, 'msg': 'Test failure'}}
74 )
76 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
77 with pytest.raises(ZenossAPIClientError, match='Request failed: Test failure'):
78 resp = br._router_request({'data': dict()})
80 def test_routers_router_request_no_data(self, responses):
81 responses.add(
82 responses.POST,
83 '{0}/test_router'.format(url),
84 json=dict()
85 )
87 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
88 with pytest.raises(ZenossAPIClientError, match='Request failed, no response data returned'):
89 resp = br._router_request({'data': dict()})
91 def test_routers_router_request_not_ok_status(self, responses):
92 responses.add(
93 responses.POST,
94 '{0}/test_router'.format(url),
95 status=404
96 )
98 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
99 with pytest.raises(ZenossAPIClientError, match='Request failed: 404 '):
100 resp = br._router_request({'data': dict()})
102 # TODO: This test no longer works with the retry on failure method as implemented. Should revisit eventually.
103 # def test_routers_router_request_bad_host(self, responses):
104 # responses.add(
105 # responses.POST,
106 # '{0}/test_router'.format(url),
107 # body=ConnectionError('Failed to establish a new connection: [Errno 110] Connection timed out',),
108 # )
109 #
110 # br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter')
111 # with pytest.raises(ZenossAPIClientError, match='Unable to connect to Zenoss server {0}'.format(url)):
112 # resp = br._router_request({'data': dict()})