Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

# Copyright 2016 Internap
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nose==1.2.1
pyhamcrest==1.8.1
mock==1.3.0
mock==1.3.0
flake8
17 changes: 15 additions & 2 deletions tests/http_utils_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from ubersmith_client import _http_utils


Expand Down Expand Up @@ -51,5 +66,3 @@ def test_with_bools(self):
'true': True,
'false': False
}, result)


14 changes: 14 additions & 0 deletions tests/ubersmith_request_form_encoding_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from mock import sentinel, patch, MagicMock
Expand Down
2 changes: 2 additions & 0 deletions tests/ubersmith_request_get_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from hamcrest import assert_that, equal_to
from mock import patch, MagicMock
import ubersmith_client

from tests.ubersmith_json.response_data_structure import a_response_data


Expand Down
2 changes: 2 additions & 0 deletions tests/ubersmith_request_post_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from hamcrest import assert_that, equal_to, calling, raises
from mock import patch, MagicMock
import ubersmith_client

from tests.ubersmith_json.response_data_structure import a_response_data


Expand Down
30 changes: 18 additions & 12 deletions tests/ubersmith_request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import ubersmith_client
from mock import Mock, patch
from hamcrest import assert_that, raises, calling, equal_to
from requests.exceptions import ConnectionError, Timeout
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, \
UbersmithConnectionError, \
UbersmithTimeout
from tests.ubersmith_json.response_data_structure import a_response_data

from ubersmith_client import exceptions
from ubersmith_client.ubersmith_request import UbersmithRequest

from tests.ubersmith_json.response_data_structure import a_response_data


class UbersmithRequestTest(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -50,35 +51,40 @@ def test_process_ubersmith_response_not_application_json(self):

def test_process_ubersmith_response_raise_exception(self):
response = Mock(status_code=400, headers={'content-type': 'application/json'})
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(BadRequest))
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
raises(exceptions.BadRequest))

response.status_code = 401
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(Unauthorized))
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
raises(exceptions.Unauthorized))

response.status_code = 403
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(Forbidden))
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
raises(exceptions.Forbidden))

response.status_code = 404
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(NotFound))
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
raises(exceptions.NotFound))

response.status_code = 500
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(UnknownError))
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
raises(exceptions.UnknownError))

response.status_code = 200
response.json = Mock(return_value={'status': False, 'error_code': 42, 'error_message': 'come and watch tv'})
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
raises(UbersmithException, 'Error code 42 - message: come and watch tv'))
raises(exceptions.UbersmithException, 'Error code 42 - message: come and watch tv'))

@patch('ubersmith_client.ubersmith_request_post.requests')
def test_api_method_returns_handle_connection_error_exception(self, requests_mock):
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
requests_mock.post = Mock(side_effect=ConnectionError())

assert_that(calling(ubersmith_api.client.list), raises(UbersmithConnectionError))
assert_that(calling(ubersmith_api.client.list), raises(exceptions.UbersmithConnectionError))

@patch('ubersmith_client.ubersmith_request_post.requests')
def test_api_method_returns_handle_timeout_exception(self, requests_mock):
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
requests_mock.post = Mock(side_effect=Timeout())

assert_that(calling(ubersmith_api.client.list), raises(UbersmithTimeout))
assert_that(calling(ubersmith_api.client.list), raises(exceptions.UbersmithTimeout))
11 changes: 10 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
[tox]
envlist = py34,py27
envlist = py34,py27,pep8

[testenv]
deps = -r{toxinidir}/test-requirements.txt
commands =
nosetests

[testenv:pep8]
deps = -r{toxinidir}/test-requirements.txt
commands = flake8 {posargs}

[flake8]
show-source = True
max-line-length = 120
exclude = .venv,.git,.tox,dist,doc,*egg,build,ubersmith_client/__init__.py
15 changes: 15 additions & 0 deletions ubersmith_client/_http_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def form_encode(data):
exploded_data = {}
for k, v in data.items():
Expand Down
2 changes: 2 additions & 0 deletions ubersmith_client/ubersmith_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -10,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ubersmith_client.ubersmith_request_get import UbersmithRequestGet
from ubersmith_client.ubersmith_request_post import UbersmithRequestPost

Expand Down
18 changes: 8 additions & 10 deletions ubersmith_client/ubersmith_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -10,11 +11,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import abstractmethod
from requests import Timeout, ConnectionError

from ubersmith_client.exceptions import get_exception_for, UbersmithException, UbersmithConnectionError, \
UbersmithTimeout
from ubersmith_client import exceptions


class UbersmithRequest(object):
Expand All @@ -39,9 +40,9 @@ def _process_request(self, method, **kwargs):
return method(**kwargs)

except ConnectionError:
raise UbersmithConnectionError(self.url)
raise exceptions.UbersmithConnectionError(self.url)
except Timeout:
raise UbersmithTimeout(self.url, self.timeout)
raise exceptions.UbersmithTimeout(self.url, self.timeout)

def _build_request_params(self, kwargs):
_methods = '.'.join(self.methods)
Expand All @@ -50,16 +51,13 @@ def _build_request_params(self, kwargs):
@staticmethod
def process_ubersmith_response(response):
if response.status_code < 200 or response.status_code >= 400:
raise get_exception_for(status_code=response.status_code)
raise exceptions.get_exception_for(status_code=response.status_code)

if response.headers['content-type'] == 'application/json':
response_json = response.json()
if not response_json['status']:
raise UbersmithException(
response_json['error_code'],
response_json['error_message']
)

raise exceptions.UbersmithException(response_json['error_code'],
response_json['error_message'])
return response_json['data']

return response.content
2 changes: 2 additions & 0 deletions ubersmith_client/ubersmith_request_get.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -10,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests

from ubersmith_client import _http_utils
Expand Down
2 changes: 2 additions & 0 deletions ubersmith_client/ubersmith_request_post.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright 2017 Internap.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -10,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests

from ubersmith_client import _http_utils
Expand Down