test_credential.py work with python34

test_credential.py was failing on python34 with two different errors.

1. Should encode unicode into byte before hashing on python3

...
    hash_.update(access)
TypeError: Unicode-objects must be encoded before hashing

2. Request body should be always bytes on python3, use `dump_as_bytes`
to covert the body to bytes.

...
  File "/opt/stack/keystone/.tox/py34/lib/python3.4/site-packages/
  webob/request.py",
  line 710, in _body__set % type(value))
TypeError: You can only set Request.body to bytes (not <class 'str'>)

bp python3
Change-Id: I82e731ea5e517e030db55cf01cc86db9684f4622
This commit is contained in:
Dave Chen 2016-01-25 17:14:54 +08:00
parent fa1befe785
commit aeaf8592d6
3 changed files with 8 additions and 1 deletions
keystone
common
tests/unit
tox.ini

@ -135,6 +135,8 @@ def verify_length_and_trunc_password(password):
def hash_access_key(access):
hash_ = hashlib.sha256()
if not isinstance(access, six.binary_type):
access = access.encode('utf-8')
hash_.update(access)
return hash_.hexdigest()

@ -152,7 +152,11 @@ class RestfulTestCase(unit.TestCase):
headers['Accept'] = 'application/json'
if body:
headers['Content-Type'] = 'application/json'
return jsonutils.dumps(body)
# NOTE(davechen):dump the body to bytes since WSGI requires
# the body of the response to be `Bytestrings`.
# see pep-3333:
# https://www.python.org/dev/peps/pep-3333/#a-note-on-string-types
return jsonutils.dump_as_bytes(body)
def _from_content_type(self, response, content_type=None):
"""Attempt to decode JSON and XML automatically, if detected."""

@ -43,6 +43,7 @@ commands =
keystone/tests/unit/test_cli.py \
keystone/tests/unit/test_config.py \
keystone/tests/unit/test_contrib_s3_core.py \
keystone/tests/unit/test_credential.py \
keystone/tests/unit/test_driver_hints.py \
keystone/tests/unit/test_exception.py \
keystone/tests/unit/test_ipv6.py \