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

1import pytest 

2from requests.exceptions import ConnectionError 

3from zenossapi.apiclient import ZenossAPIClientAuthenticationError, ZenossAPIClientError 

4from zenossapi.routers import ZenossRouter 

5 

6pytest_plugins = "pytest-responses" 

7url = 'https://zenoss/zport/dmd' 

8headers = dict( 

9 ContentType='application/json' 

10) 

11 

12 

13class TestBaseRouter(object): 

14 

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' 

22 

23 def test_routers_make_request_data(self): 

24 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter') 

25 

26 expected = dict( 

27 action='TestRouter', 

28 method='TestMethod', 

29 data=[dict()], 

30 tid=1, 

31 ) 

32 

33 request_data = br._make_request_data( 

34 'TestMethod', 

35 dict(), 

36 ) 

37 

38 assert request_data == expected 

39 

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 ) 

46 

47 br = ZenossRouter(url, headers, True, 'test_router', 'TestRouter') 

48 

49 resp = br._router_request({'data': dict()}) 

50 assert resp['success'] 

51 

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 ) 

64 

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()}) 

68 

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 ) 

75 

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()}) 

79 

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 ) 

86 

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()}) 

90 

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 ) 

97 

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()}) 

101 

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()})