Port API test_resources to Python 3

* _do_enforce_content_types(): only decode content type if it's a
  byte string (don't decode on Python 3)
* Decode bytes HTTP body to compare it to text string. On Python 3,
  bytes == str is always false (or emit/raise a BytesWarning).
* Use byte strings rather than text strings for literal HTTP body:
  HTTP body type is bytes.
* Remove test_resources from tests-py3-blacklist.py to run it

Partially implements: blueprint barbican-py3
Change-Id: I6d830ea620ac5a237b5e2929077eb2f4504b5e59
This commit is contained in:
Victor Stinner
2016-05-04 15:44:02 +02:00
parent a0ca5c0e5e
commit 09ba305b36
3 changed files with 10 additions and 8 deletions

View File

@@ -132,11 +132,14 @@ def _do_enforce_content_types(pecan_req, valid_content_types):
types passed in by our caller.
"""
if pecan_req.content_type not in valid_content_types:
content_type = pecan_req.content_type
if isinstance(content_type, bytes):
content_type = content_type.decode('utf-8')
m = u._(
"Unexpected content type: {type}. Expected content types "
"are: {expected}"
).format(
type=pecan_req.content_type.decode('utf-8'),
type=content_type,
expected=valid_content_types
)
pecan.abort(415, m)

View File

@@ -401,7 +401,7 @@ class WhenGettingPuttingOrDeletingSecretUsingSecretResource(FunctionalTest):
suppress_exception=True)
self.assertEqual(200, resp.status_int)
self.assertEqual(data, resp.body)
self.assertEqual(data, resp.body.decode())
mock_get_secret.assert_called_once_with(
'text/plain',
self.secret,
@@ -428,7 +428,7 @@ class WhenGettingPuttingOrDeletingSecretUsingSecretResource(FunctionalTest):
suppress_exception=True)
self.assertEqual(200, resp.status_int)
self.assertEqual(data, resp.body)
self.assertEqual(data, resp.body.decode())
mock_get_secret.assert_called_once_with(
'text/plain',
self.secret,
@@ -457,7 +457,7 @@ class WhenGettingPuttingOrDeletingSecretUsingSecretResource(FunctionalTest):
suppress_exception=True)
self.assertEqual(200, resp.status_int)
self.assertEqual(data, resp.body)
self.assertEqual(data, resp.body.decode())
mock_get_secret.assert_called_once_with(
'text/plain',
self.secret,
@@ -580,7 +580,7 @@ class WhenGettingPuttingOrDeletingSecretUsingSecretResource(FunctionalTest):
}
)
self.assertEqual(data, resp.body)
self.assertEqual(data, resp.body.decode())
mock_get_secret.assert_called_once_with(
'application/octet-stream',
@@ -605,7 +605,7 @@ class WhenGettingPuttingOrDeletingSecretUsingSecretResource(FunctionalTest):
self.assertEqual(204, resp.status_int)
mock_store_secret.assert_called_once_with(
unencrypted_raw='plain text',
unencrypted_raw=b'plain text',
content_type_raw='text/plain',
content_encoding=None,
secret_model=self.secret,
@@ -631,7 +631,7 @@ class WhenGettingPuttingOrDeletingSecretUsingSecretResource(FunctionalTest):
self.assertEqual(204, resp.status_int)
mock_store_secret.assert_called_once_with(
unencrypted_raw='plain text',
unencrypted_raw=b'plain text',
content_type_raw='application/octet-stream',
content_encoding=None,
secret_model=self.secret,

View File

@@ -2,7 +2,6 @@ barbican.tests.api.controllers.test_containers
barbican.tests.api.controllers.test_orders
barbican.tests.api.controllers.test_quotas
barbican.tests.api.controllers.test_secrets
barbican.tests.api.test_resources
barbican.tests.api.test_transport_keys_resource
barbican.tests.cmd.test_barbican_manage
barbican.tests.cmd.test_db_cleanup