"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "keystone/tests/unit/test_exception.py" between
keystone-16.0.1.tar.gz and keystone-17.0.0.tar.gz

About: OpenStack Keystone (Core Service: Identity) provides an authentication and authorization service for other OpenStack services. Provides a catalog of endpoints for all OpenStack services.
The "Ussuri" series (latest release).

test_exception.py  (keystone-16.0.1):test_exception.py  (keystone-17.0.0)
skipping to change at line 24 skipping to change at line 24
# NOTE(morgan): These test cases are used for AuthContextMiddleware exception # NOTE(morgan): These test cases are used for AuthContextMiddleware exception
# rendering. # rendering.
import uuid import uuid
import fixtures import fixtures
from oslo_config import fixture as config_fixture from oslo_config import fixture as config_fixture
from oslo_log import log from oslo_log import log
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import six
import keystone.conf import keystone.conf
from keystone import exception from keystone import exception
from keystone.server.flask.request_processing.middleware import auth_context from keystone.server.flask.request_processing.middleware import auth_context
from keystone.tests import unit from keystone.tests import unit
CONF = keystone.conf.CONF CONF = keystone.conf.CONF
class ExceptionTestCase(unit.BaseTestCase): class ExceptionTestCase(unit.BaseTestCase):
def assertValidJsonRendering(self, e): def assertValidJsonRendering(self, e):
skipping to change at line 67 skipping to change at line 66
""" """
for cls in [x for x in exception.__dict__.values() if callable(x)]: for cls in [x for x in exception.__dict__.values() if callable(x)]:
if cls is not exception.Error and isinstance(cls, exception.Error): if cls is not exception.Error and isinstance(cls, exception.Error):
self.assertValidJsonRendering(cls(message='Overridden.')) self.assertValidJsonRendering(cls(message='Overridden.'))
def test_validation_error(self): def test_validation_error(self):
target = uuid.uuid4().hex target = uuid.uuid4().hex
attribute = uuid.uuid4().hex attribute = uuid.uuid4().hex
e = exception.ValidationError(target=target, attribute=attribute) e = exception.ValidationError(target=target, attribute=attribute)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertIn(target, six.text_type(e)) self.assertIn(target, str(e))
self.assertIn(attribute, six.text_type(e)) self.assertIn(attribute, str(e))
def test_not_found(self): def test_not_found(self):
target = uuid.uuid4().hex target = uuid.uuid4().hex
e = exception.NotFound(target=target) e = exception.NotFound(target=target)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertIn(target, six.text_type(e)) self.assertIn(target, str(e))
def test_forbidden_title(self): def test_forbidden_title(self):
e = exception.Forbidden() e = exception.Forbidden()
resp = auth_context.render_exception(e) resp = auth_context.render_exception(e)
j = jsonutils.loads(resp.body) j = jsonutils.loads(resp.body)
self.assertEqual('Forbidden', e.title) self.assertEqual('Forbidden', e.title)
self.assertEqual('Forbidden', j['error'].get('title')) self.assertEqual('Forbidden', j['error'].get('title'))
def test_unicode_message(self): def test_unicode_message(self):
message = u'Comment \xe7a va' message = u'Comment \xe7a va'
e = exception.Error(message) e = exception.Error(message)
try: try:
self.assertEqual(message, six.text_type(e)) self.assertEqual(message, str(e))
except UnicodeEncodeError: except UnicodeEncodeError:
self.fail("unicode error message not supported") self.fail("unicode error message not supported")
def test_unicode_string(self): def test_unicode_string(self):
e = exception.ValidationError(attribute='xx', e = exception.ValidationError(attribute='xx',
target='Long \xe2\x80\x93 Dash') target='Long \xe2\x80\x93 Dash')
self.assertIn('Long \xe2\x80\x93 Dash', str(e))
if six.PY2:
self.assertIn(u'\u2013', six.text_type(e))
else:
self.assertIn('Long \xe2\x80\x93 Dash', six.text_type(e))
def test_invalid_unicode_string(self): def test_invalid_unicode_string(self):
# NOTE(jamielennox): This is a complete failure case so what is # NOTE(jamielennox): This is a complete failure case so what is
# returned in the exception message is not that important so long # returned in the exception message is not that important so long
# as there is an error with a message # as there is an error with a message
e = exception.ValidationError(attribute='xx', e = exception.ValidationError(attribute='xx',
target='\xe7a va') target='\xe7a va')
self.assertIn('\xe7a va', str(e))
if six.PY2:
self.assertIn('%(attribute)', six.text_type(e))
else:
# There's no UnicodeDecodeError on python 3.
self.assertIn('\xe7a va', six.text_type(e))
class UnexpectedExceptionTestCase(ExceptionTestCase): class UnexpectedExceptionTestCase(ExceptionTestCase):
"""Test if internal info is exposed to the API user on UnexpectedError.""" """Test if internal info is exposed to the API user on UnexpectedError."""
class SubClassExc(exception.UnexpectedError): class SubClassExc(exception.UnexpectedError):
debug_message_format = 'Debug Message: %(debug_info)s' debug_message_format = 'Debug Message: %(debug_info)s'
def setUp(self): def setUp(self):
super(UnexpectedExceptionTestCase, self).setUp() super(UnexpectedExceptionTestCase, self).setUp()
self.exc_str = uuid.uuid4().hex self.exc_str = uuid.uuid4().hex
self.config_fixture = self.useFixture(config_fixture.Config(CONF)) self.config_fixture = self.useFixture(config_fixture.Config(CONF))
def test_unexpected_error_no_debug(self): def test_unexpected_error_no_debug(self):
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
e = exception.UnexpectedError(exception=self.exc_str) e = exception.UnexpectedError(exception=self.exc_str)
self.assertNotIn(self.exc_str, six.text_type(e)) self.assertNotIn(self.exc_str, str(e))
def test_unexpected_error_debug(self): def test_unexpected_error_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
e = exception.UnexpectedError(exception=self.exc_str) e = exception.UnexpectedError(exception=self.exc_str)
self.assertIn(self.exc_str, six.text_type(e)) self.assertIn(self.exc_str, str(e))
def test_unexpected_error_subclass_no_debug(self): def test_unexpected_error_subclass_no_debug(self):
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
e = UnexpectedExceptionTestCase.SubClassExc( e = UnexpectedExceptionTestCase.SubClassExc(
debug_info=self.exc_str) debug_info=self.exc_str)
self.assertEqual(exception.UnexpectedError.message_format, self.assertEqual(exception.UnexpectedError.message_format,
six.text_type(e)) str(e))
def test_unexpected_error_subclass_debug(self): def test_unexpected_error_subclass_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
subclass = self.SubClassExc subclass = self.SubClassExc
e = subclass(debug_info=self.exc_str) e = subclass(debug_info=self.exc_str)
expected = subclass.debug_message_format % {'debug_info': self.exc_str} expected = subclass.debug_message_format % {'debug_info': self.exc_str}
self.assertEqual( self.assertEqual(
'%s %s' % (expected, exception.SecurityError.amendment), '%s %s' % (expected, exception.SecurityError.amendment),
six.text_type(e)) str(e))
def test_unexpected_error_custom_message_no_debug(self): def test_unexpected_error_custom_message_no_debug(self):
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
e = exception.UnexpectedError(self.exc_str) e = exception.UnexpectedError(self.exc_str)
self.assertEqual(exception.UnexpectedError.message_format, self.assertEqual(exception.UnexpectedError.message_format,
six.text_type(e)) str(e))
def test_unexpected_error_custom_message_debug(self): def test_unexpected_error_custom_message_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
e = exception.UnexpectedError(self.exc_str) e = exception.UnexpectedError(self.exc_str)
self.assertEqual( self.assertEqual(
'%s %s' % (self.exc_str, exception.SecurityError.amendment), '%s %s' % (self.exc_str, exception.SecurityError.amendment),
six.text_type(e)) str(e))
def test_unexpected_error_custom_message_exception_debug(self): def test_unexpected_error_custom_message_exception_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
orig_e = exception.NotFound(target=uuid.uuid4().hex) orig_e = exception.NotFound(target=uuid.uuid4().hex)
e = exception.UnexpectedError(orig_e) e = exception.UnexpectedError(orig_e)
self.assertEqual( self.assertEqual(
'%s %s' % (six.text_type(orig_e), '%s %s' % (str(orig_e),
exception.SecurityError.amendment), exception.SecurityError.amendment),
six.text_type(e)) str(e))
def test_unexpected_error_custom_message_binary_debug(self): def test_unexpected_error_custom_message_binary_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
binary_msg = b'something' binary_msg = b'something'
e = exception.UnexpectedError(binary_msg) e = exception.UnexpectedError(binary_msg)
self.assertEqual( self.assertEqual(
'%s %s' % (six.text_type(binary_msg), '%s %s' % (str(binary_msg),
exception.SecurityError.amendment), exception.SecurityError.amendment),
six.text_type(e)) str(e))
class SecurityErrorTestCase(ExceptionTestCase): class SecurityErrorTestCase(ExceptionTestCase):
"""Test whether security-related info is exposed to the API user.""" """Test whether security-related info is exposed to the API user."""
def setUp(self): def setUp(self):
super(SecurityErrorTestCase, self).setUp() super(SecurityErrorTestCase, self).setUp()
self.config_fixture = self.useFixture(config_fixture.Config(CONF)) self.config_fixture = self.useFixture(config_fixture.Config(CONF))
def test_unauthorized_exposure(self): def test_unauthorized_exposure(self):
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
risky_info = uuid.uuid4().hex risky_info = uuid.uuid4().hex
e = exception.Unauthorized(message=risky_info) e = exception.Unauthorized(message=risky_info)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertNotIn(risky_info, six.text_type(e)) self.assertNotIn(risky_info, str(e))
def test_unauthorized_exposure_in_debug(self): def test_unauthorized_exposure_in_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
risky_info = uuid.uuid4().hex risky_info = uuid.uuid4().hex
e = exception.Unauthorized(message=risky_info) e = exception.Unauthorized(message=risky_info)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertIn(risky_info, six.text_type(e)) self.assertIn(risky_info, str(e))
def test_forbidden_exposure(self): def test_forbidden_exposure(self):
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
risky_info = uuid.uuid4().hex risky_info = uuid.uuid4().hex
e = exception.Forbidden(message=risky_info) e = exception.Forbidden(message=risky_info)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertNotIn(risky_info, six.text_type(e)) self.assertNotIn(risky_info, str(e))
def test_forbidden_exposure_in_debug(self): def test_forbidden_exposure_in_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
risky_info = uuid.uuid4().hex risky_info = uuid.uuid4().hex
e = exception.Forbidden(message=risky_info) e = exception.Forbidden(message=risky_info)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertIn(risky_info, six.text_type(e)) self.assertIn(risky_info, str(e))
def test_forbidden_action_exposure(self): def test_forbidden_action_exposure(self):
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
risky_info = uuid.uuid4().hex risky_info = uuid.uuid4().hex
action = uuid.uuid4().hex action = uuid.uuid4().hex
e = exception.ForbiddenAction(message=risky_info, action=action) e = exception.ForbiddenAction(message=risky_info, action=action)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertNotIn(risky_info, six.text_type(e)) self.assertNotIn(risky_info, str(e))
self.assertIn(action, six.text_type(e)) self.assertIn(action, str(e))
self.assertNotIn(exception.SecurityError.amendment, six.text_type(e)) self.assertNotIn(exception.SecurityError.amendment, str(e))
e = exception.ForbiddenAction(action=action) e = exception.ForbiddenAction(action=action)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertIn(action, six.text_type(e)) self.assertIn(action, str(e))
self.assertNotIn(exception.SecurityError.amendment, six.text_type(e)) self.assertNotIn(exception.SecurityError.amendment, str(e))
def test_forbidden_action_exposure_in_debug(self): def test_forbidden_action_exposure_in_debug(self):
self.config_fixture.config(debug=True, insecure_debug=True) self.config_fixture.config(debug=True, insecure_debug=True)
risky_info = uuid.uuid4().hex risky_info = uuid.uuid4().hex
action = uuid.uuid4().hex action = uuid.uuid4().hex
e = exception.ForbiddenAction(message=risky_info, action=action) e = exception.ForbiddenAction(message=risky_info, action=action)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertIn(risky_info, six.text_type(e)) self.assertIn(risky_info, str(e))
self.assertIn(exception.SecurityError.amendment, six.text_type(e)) self.assertIn(exception.SecurityError.amendment, str(e))
e = exception.ForbiddenAction(action=action) e = exception.ForbiddenAction(action=action)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertIn(action, six.text_type(e)) self.assertIn(action, str(e))
self.assertNotIn(exception.SecurityError.amendment, six.text_type(e)) self.assertNotIn(exception.SecurityError.amendment, str(e))
def test_forbidden_action_no_message(self): def test_forbidden_action_no_message(self):
# When no custom message is given when the ForbiddenAction (or other # When no custom message is given when the ForbiddenAction (or other
# SecurityError subclass) is created the exposed message is the same # SecurityError subclass) is created the exposed message is the same
# whether debug is enabled or not. # whether debug is enabled or not.
action = uuid.uuid4().hex action = uuid.uuid4().hex
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
e = exception.ForbiddenAction(action=action) e = exception.ForbiddenAction(action=action)
exposed_message = six.text_type(e) exposed_message = str(e)
self.assertIn(action, exposed_message) self.assertIn(action, exposed_message)
self.assertNotIn(exception.SecurityError.amendment, six.text_type(e)) self.assertNotIn(exception.SecurityError.amendment, str(e))
self.config_fixture.config(debug=True) self.config_fixture.config(debug=True)
e = exception.ForbiddenAction(action=action) e = exception.ForbiddenAction(action=action)
self.assertEqual(exposed_message, six.text_type(e)) self.assertEqual(exposed_message, str(e))
def test_unicode_argument_message(self): def test_unicode_argument_message(self):
self.config_fixture.config(debug=False) self.config_fixture.config(debug=False)
risky_info = u'\u7ee7\u7eed\u884c\u7f29\u8fdb\u6216' risky_info = u'\u7ee7\u7eed\u884c\u7f29\u8fdb\u6216'
e = exception.Forbidden(message=risky_info) e = exception.Forbidden(message=risky_info)
self.assertValidJsonRendering(e) self.assertValidJsonRendering(e)
self.assertNotIn(risky_info, six.text_type(e)) self.assertNotIn(risky_info, str(e))
class TestSecurityErrorTranslation(unit.BaseTestCase): class TestSecurityErrorTranslation(unit.BaseTestCase):
"""Test i18n for SecurityError exceptions.""" """Test i18n for SecurityError exceptions."""
def setUp(self): def setUp(self):
super(TestSecurityErrorTranslation, self).setUp() super(TestSecurityErrorTranslation, self).setUp()
self.config_fixture = self.useFixture(config_fixture.Config(CONF)) self.config_fixture = self.useFixture(config_fixture.Config(CONF))
self.config_fixture.config(insecure_debug=False) self.config_fixture.config(insecure_debug=False)
self.warning_log = self.useFixture(fixtures.FakeLogger(level=log.WARN)) self.warning_log = self.useFixture(fixtures.FakeLogger(level=log.WARN))
 End of changes. 28 change blocks. 
43 lines changed or deleted 33 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)