dont use raise_for_status

use the webob abort method for http errors

Change-Id: Ie56c8197cdee7b522da9d9f91e6314a5cbb30dc5
This commit is contained in:
Eyal 2017-10-16 10:49:09 +03:00
parent b5f3a6b2dd
commit fe6daf5312
2 changed files with 11 additions and 10 deletions

View File

@ -20,6 +20,7 @@ from oslo_config import cfg
from oslo_log import log as logging from oslo_log import log as logging
from oslo_middleware import base from oslo_middleware import base
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from pecan.core import abort
from six.moves import urllib from six.moves import urllib
from webob import exc from webob import exc
@ -108,7 +109,9 @@ class KeycloakAuth(base.ConfigurableMiddleware):
else None else None
resp = requests.get(endpoint, headers=headers, resp = requests.get(endpoint, headers=headers,
verify=verify, cert=cert) verify=verify, cert=cert)
resp.raise_for_status()
if not resp.ok:
abort(resp.status_code, resp.reason)
def _set_req_headers(self, req): def _set_req_headers(self, req):
req.headers['X-Identity-Status'] = 'Confirmed' req.headers['X-Identity-Status'] = 'Confirmed'

View File

@ -17,12 +17,10 @@
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
from datetime import datetime from datetime import datetime
from mock import mock from mock import mock
import requests
import requests_mock import requests_mock
from webtest import TestRequest
from vitrage.middleware.keycloak import KeycloakAuth from vitrage.middleware.keycloak import KeycloakAuth
from vitrage.tests.functional.api.v1 import FunctionalTest from vitrage.tests.functional.api.v1 import FunctionalTest
from webtest import TestRequest
TOKEN = { TOKEN = {
@ -112,13 +110,13 @@ class KeycloakTest(FunctionalTest):
reason='Access token is invalid' reason='Access token is invalid'
) )
self.assertRaises(requests.exceptions.HTTPError, resp = self.post_json('/topology/',
self.post_json,
'/topology/',
params=None, params=None,
headers=HEADERS, headers=HEADERS,
expect_errors=True) expect_errors=True)
self.assertEqual('401 Unauthorized', resp.status)
@mock.patch('jwt.decode', return_value=TOKEN) @mock.patch('jwt.decode', return_value=TOKEN)
@requests_mock.Mocker() @requests_mock.Mocker()
def test_in_keycloak_mode_auth_success(self, _, req_mock): def test_in_keycloak_mode_auth_success(self, _, req_mock):