From c63d7a6390dbbccac1c34a12719d0eef735cd089 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 28 Aug 2015 16:56:03 +0200 Subject: [PATCH] Port password tests to Python 3 * load_template_source(): use io.open() with an encoding instead of open(), to have the same behaviour on Python 2 and Python 3 * assertRedirectsNoFollow(): remove an useless assertion on the response status code which is already tested a few lines below. The assertion used code/100 which returns a float on Python 3, whereas an int was expected. * add_logout_reason(): don't encode reason to UTF-8 on Python 3 * tox.ini: add openstack_dashboard.dashboards.settings.password to Python 3.4 Partial-Implements: blueprint porting-python3 Change-Id: Iff8997917b5d812952150b2abe89ddd12deab0c0 --- horizon/loaders.py | 5 +++-- horizon/utils/functions.py | 4 +++- openstack_dashboard/test/helpers.py | 2 -- tox.ini | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/horizon/loaders.py b/horizon/loaders.py index 3544f6bcf9..9a7a133c34 100644 --- a/horizon/loaders.py +++ b/horizon/loaders.py @@ -14,6 +14,7 @@ Wrapper for loading templates from "templates" directories in panel modules. """ +import io import os import django @@ -54,8 +55,8 @@ class TemplateLoader(tLoaderCls): def load_template_source(self, template_name, template_dirs=None): for path in self.get_template_sources(template_name): try: - with open(path) as file: - return (file.read().decode(settings.FILE_CHARSET), path) + with io.open(path, encoding=settings.FILE_CHARSET) as file: + return (file.read(), path) except IOError: pass raise TemplateDoesNotExist(template_name) diff --git a/horizon/utils/functions.py b/horizon/utils/functions.py index a8afb4f2a6..2d2abec9b7 100644 --- a/horizon/utils/functions.py +++ b/horizon/utils/functions.py @@ -42,7 +42,9 @@ def add_logout_reason(request, response, reason): # Store the translated string in the cookie lang = translation.get_language_from_request(request) with translation.override(lang): - reason = six.text_type(reason).encode('utf-8') + reason = six.text_type(reason) + if six.PY2: + reason = reason.encode('utf-8') response.set_cookie('logout_reason', reason, max_age=10) diff --git a/openstack_dashboard/test/helpers.py b/openstack_dashboard/test/helpers.py index f8d831d0e6..9e4267412f 100644 --- a/openstack_dashboard/test/helpers.py +++ b/openstack_dashboard/test/helpers.py @@ -227,8 +227,6 @@ class TestCase(horizon_helpers.TestCase): Asserts that the given response issued a 302 redirect without processing the view which is redirected to. """ - assert (response.status_code / 100 == 3), \ - "The response did not return a redirect." self.assertEqual(response._headers.get('location', None), ('Location', settings.TESTSERVER + expected_url)) self.assertEqual(response.status_code, 302) diff --git a/tox.ini b/tox.ini index 66b13a3c61..839c916ed3 100644 --- a/tox.ini +++ b/tox.ini @@ -39,6 +39,7 @@ commands = openstack_dashboard.dashboards.project.images.images.tests.CreateImageFormTests \ openstack_dashboard.dashboards.project.images.tests.ImagesAndSnapshotsUtilsTests \ openstack_dashboard.dashboards.project.stacks.tests.TemplateFormTests \ + openstack_dashboard.dashboards.settings.password \ openstack_dashboard.test.api_tests.base_tests.APIDictWrapperTests \ openstack_dashboard.test.api_tests.base_tests.APIResourceWrapperTests \ openstack_dashboard.test.api_tests.base_tests.ApiHelperTests \