|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 | import unittest |
15 | | -from mock import Mock |
| 15 | + |
| 16 | +import ubersmith_client |
| 17 | +from mock import Mock, patch |
16 | 18 |
|
17 | 19 | from hamcrest import assert_that, raises, calling |
| 20 | +from requests.exceptions import ConnectionError, Timeout |
18 | 21 |
|
19 | | -from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized |
| 22 | +from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, UbersmithConnectionError, \ |
| 23 | + UbersmithTimeout |
20 | 24 | from tests.ubersmith_json.response_data_structure import a_response_data |
21 | 25 | from ubersmith_client.ubersmith_request import UbersmithRequest |
22 | 26 |
|
23 | 27 |
|
24 | 28 | class UbersmithRequestTest(unittest.TestCase): |
| 29 | + def setUp(self): |
| 30 | + self.url = 'http://ubersmith.example.com/' |
| 31 | + self.username = 'admin' |
| 32 | + self.password = 'test' |
| 33 | + |
25 | 34 | def test_process_ubersmith_response(self): |
26 | 35 | response = Mock() |
27 | 36 | response.status_code = 200 |
@@ -57,3 +66,17 @@ def test_process_ubersmith_response_raise_exception(self): |
57 | 66 | response.json = Mock(return_value={'status': False, 'error_code': 42, 'error_message': 'come and watch tv'}) |
58 | 67 | assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), |
59 | 68 | raises(UbersmithException, "Error code 42 - message: come and watch tv")) |
| 69 | + |
| 70 | + @patch('ubersmith_client.ubersmith_request_post.requests') |
| 71 | + def test_api_method_returns_handle_connection_error_exception(self, requests_mock): |
| 72 | + ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password) |
| 73 | + requests_mock.post = Mock(side_effect=ConnectionError()) |
| 74 | + |
| 75 | + assert_that(calling(ubersmith_api.client.list), raises(UbersmithConnectionError)) |
| 76 | + |
| 77 | + @patch('ubersmith_client.ubersmith_request_post.requests') |
| 78 | + def test_api_method_returns_handle_timeout_exception(self, requests_mock): |
| 79 | + ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password) |
| 80 | + requests_mock.post = Mock(side_effect=Timeout()) |
| 81 | + |
| 82 | + assert_that(calling(ubersmith_api.client.list), raises(UbersmithTimeout)) |
0 commit comments