From 09ba305b367d94bfa6c73a18dd2df88553b574c6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 4 May 2016 15:44:02 +0200 Subject: [PATCH] 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 --- barbican/api/controllers/__init__.py | 5 ++++- barbican/tests/api/test_resources.py | 12 ++++++------ tests-py3-blacklist.txt | 1 - 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/barbican/api/controllers/__init__.py b/barbican/api/controllers/__init__.py index e4af34b5e..f48b2be73 100644 --- a/barbican/api/controllers/__init__.py +++ b/barbican/api/controllers/__init__.py @@ -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) diff --git a/barbican/tests/api/test_resources.py b/barbican/tests/api/test_resources.py index ef7385e2b..7c6a12024 100644 --- a/barbican/tests/api/test_resources.py +++ b/barbican/tests/api/test_resources.py @@ -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, diff --git a/tests-py3-blacklist.txt b/tests-py3-blacklist.txt index 351a38b04..7613390c2 100644 --- a/tests-py3-blacklist.txt +++ b/tests-py3-blacklist.txt @@ -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