1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414import unittest
15- from mock import patch , MagicMock
15+ from hamcrest import assert_that , raises , calling
16+ from mock import Mock
17+ from requests .exceptions import ConnectionError , Timeout
1618
17- from hamcrest import assert_that , equal_to , raises , calling
18-
19- from tests .ubersmith_json .response_data_structure import a_response_data
2019import ubersmith_client
20+ from ubersmith_client .exceptions import UbersmithConnectionError , UbersmithTimeout
2121
2222
2323class ApiTest (unittest .TestCase ):
@@ -26,110 +26,14 @@ def setUp(self):
2626 self .username = 'admin'
2727 self .password = 'test'
2828
29- self .auth = (self .username , self .password )
30- self .timeout = 60
31-
32- @patch ('ubersmith_client.ubersmith_request_get.requests' )
33- def test_api_get_method_returns_without_arguments (self , requests_mock ):
34- json_data = {
35- 'company' : 'council of ricks'
36- }
37- expected_call = self .expect_a_ubersmith_call (requests_mock = requests_mock ,
38- method = 'client.list' ,
39- returning = a_response_data (data = json_data ))
40-
41- ubersmith_api = ubersmith_client .api .init (self .url , self .username , self .password , use_http_get = True )
42- response = ubersmith_api .client .list ()
43-
44- assert_that (response , equal_to (json_data ))
45-
46- expected_call ()
47-
48- @patch ('ubersmith_client.ubersmith_request_get.requests' )
49- def test_api_get_method_returns_with_arguments (self , request_mock ):
50- json_data = {
51- 'group_id' : '1' ,
52- 'client_id' : '30001' ,
53- 'assignment_count' : '1'
54- }
55- expected_call = self .expect_a_ubersmith_call (requests_mock = request_mock ,
56- method = 'device.ip_group_list' ,
57- fac_id = 1 ,
58- client_id = 30001 ,
59- returning = a_response_data (data = json_data ))
60-
61- ubersmith_api = ubersmith_client .api .init (self .url , self .username , self .password , use_http_get = True )
62- response = ubersmith_api .device .ip_group_list (fac_id = 1 , client_id = 30001 )
63-
64- assert_that (response , equal_to (json_data ))
65-
66- expected_call ()
67-
68- @patch ('ubersmith_client.ubersmith_request_post.requests' )
69- def test_api_post_method_returns_with_arguments (self , request_mock ):
70- json_data = {
71- 'group_id' : '1' ,
72- 'client_id' : '30001' ,
73- 'assignment_count' : '1'
74- }
75- expected_call = self .expect_a_ubersmith_call_post (requests_mock = request_mock ,
76- method = 'device.ip_group_list' ,
77- fac_id = 1 ,
78- client_id = 30001 ,
79- returning = a_response_data (data = json_data ))
80-
81- ubersmith_api = ubersmith_client .api .init (self .url , self .username , self .password , use_http_get = False )
82- response = ubersmith_api .device .ip_group_list (fac_id = 1 , client_id = 30001 )
83-
84- assert_that (response , equal_to (json_data ))
85-
86- expected_call ()
87-
88- @patch ('ubersmith_client.ubersmith_request_post.requests' )
89- def test_api_post_method_returns_without_arguments (self , requests_mock ):
90- json_data = {
91- 'company' : 'schwifty'
92- }
93- expected_call = self .expect_a_ubersmith_call_post (requests_mock = requests_mock ,
94- method = 'client.list' ,
95- returning = a_response_data (data = json_data ))
96-
97- ubersmith_api = ubersmith_client .api .init (self .url , self .username , self .password , use_http_get = False )
98- response = ubersmith_api .client .list ()
99-
100- assert_that (response , equal_to (json_data ))
101-
102- expected_call ()
103-
104- @patch ('ubersmith_client.ubersmith_request_post.requests' )
105- def test_api_raises_exception_with_if_data_status_is_false (self , requests_mock ):
106- data = a_response_data (status = False ,
107- error_code = 1 ,
108- error_message = 'invalid method specified: client.miss' ,
109- data = "schwifty" )
110- ubersmith_api = ubersmith_client .api .init (self .url , self .username , self .password , use_http_get = False )
111-
112- self .expect_a_ubersmith_call_post (requests_mock , method = 'client.miss' , returning = data )
113- assert_that (calling (ubersmith_api .client .miss ), raises (ubersmith_client .exceptions .UbersmithException ))
114-
115- def expect_a_ubersmith_call (self , requests_mock , returning = None , ** kwargs ):
116- response = MagicMock (status_code = 200 )
117- requests_mock .get = MagicMock (return_value = response )
118- response .json = MagicMock (return_value = returning )
119-
120- def assert_called_with ():
121- requests_mock .get .assert_called_with (auth = self .auth , params = kwargs , timeout = self .timeout , url = self .url )
122- response .json .assert_called_with ()
123-
124- return assert_called_with
29+ def test_api_method_returns_handle_connection_error_exception (self ):
30+ ubersmith_api = ubersmith_client .api .init (self .url , self .username , self .password )
31+ ubersmith_api .ubersmith_request = Mock (side_effect = ConnectionError ())
12532
126- def expect_a_ubersmith_call_post (self , requests_mock , returning = None , status_code = 200 , ** kwargs ):
127- response = MagicMock (status_code = status_code )
128- requests_mock .post = MagicMock (return_value = response )
129- response .json = MagicMock (return_value = returning )
33+ assert_that (calling (ubersmith_api .__getattr__ ).with_args ("client" ), raises (UbersmithConnectionError ))
13034
131- def assert_called_with ( ):
132- requests_mock . post . assert_called_with ( auth = self .auth , timeout = self .timeout , url = self .url , data = kwargs )
133- response . json . assert_called_with ( )
35+ def test_api_method_returns_handle_timeout_exception ( self ):
36+ ubersmith_api = ubersmith_client . api . init ( self .url , self .username , self .password )
37+ ubersmith_api . ubersmith_request = Mock ( side_effect = Timeout () )
13438
135- return assert_called_with
39+ assert_that ( calling ( ubersmith_api . __getattr__ ). with_args ( "client" ), raises ( UbersmithTimeout ))
0 commit comments