From 9526289b76d9c5d5bd38a64317f2f8c9ad79cfd4 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Wed, 24 Feb 2021 23:32:07 +0900 Subject: [PATCH] Support Django 3.2 support (2) In Django 3.2 CookieStorage stores messages in the RFC 6265 compliant format [1][2]. This means that horizon messages pass through cookies are encoded in a different way that messages are encrypted. Previously horizon UT interpretes messages in cookies literally in its own way, but the change on CookieStorage in Django 3.2 broke it. Horizon should not depend on its own way. The suggested way I believe is to use a method defined in django.contrib.messages.storage.cookie.CookieStorage. [1] https://docs.djangoproject.com/en/3.2/releases/3.2/#miscellaneous [2] https://github.com/django/django/commit/2d6179c819010f6a9d00835d5893c4593c0b85a0 Change-Id: I3e5e12265d0bc7b753bbe1f57acdd663b9dd3587 --- .../dashboards/project/vg_snapshots/tests.py | 9 ++++----- .../dashboards/project/volume_groups/tests.py | 3 +-- openstack_dashboard/test/helpers.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/openstack_dashboard/dashboards/project/vg_snapshots/tests.py b/openstack_dashboard/dashboards/project/vg_snapshots/tests.py index d66185335c..d14709792b 100644 --- a/openstack_dashboard/dashboards/project/vg_snapshots/tests.py +++ b/openstack_dashboard/dashboards/project/vg_snapshots/tests.py @@ -71,11 +71,10 @@ class GroupSnapshotTests(test.TestCase): args=[vg_snapshot.id]) res = self.client.post(url, formData) self.assertNoFormErrors(res) - # There are a bunch of backslashes for formatting in the message from - # the response, so remove them when validating the error message. - self.assertIn('Unable to create group "%s" from snapshot.' - % new_cg_name, - res.cookies.output().replace('\\', '')) + self.assertCookieMessage( + res, + 'Unable to create group "%s" from snapshot.' % new_cg_name, + 'Expected failure.') self.assertRedirectsNoFollow(res, INDEX_URL) mock_group_snapshot_get.assert_called_once_with( diff --git a/openstack_dashboard/dashboards/project/volume_groups/tests.py b/openstack_dashboard/dashboards/project/volume_groups/tests.py index e5caffd291..d80b4fb42a 100644 --- a/openstack_dashboard/dashboards/project/volume_groups/tests.py +++ b/openstack_dashboard/dashboards/project/volume_groups/tests.py @@ -108,8 +108,7 @@ class VolumeGroupTests(test.TestCase): res = self.client.post(url, formData) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, INDEX_URL) - self.assertIn("Unable to create group.", - res.cookies.output()) + self.assertCookieMessage(res, "Unable to create group.") self.mock_extension_supported.assert_called_once_with( test.IsHttpRequest(), 'AvailabilityZones') diff --git a/openstack_dashboard/test/helpers.py b/openstack_dashboard/test/helpers.py index 6d2292673e..19afd7c02d 100644 --- a/openstack_dashboard/test/helpers.py +++ b/openstack_dashboard/test/helpers.py @@ -24,6 +24,7 @@ import traceback from unittest import mock from django.conf import settings +from django.contrib.messages.storage import cookie as cookie_storage from django.contrib.messages.storage import default_storage from django.core.handlers import wsgi from django.test.client import RequestFactory @@ -38,6 +39,7 @@ from requests.packages.urllib3.connection import HTTPConnection from horizon import base from horizon import conf +from horizon import exceptions from horizon.test import helpers as horizon_helpers from openstack_dashboard import api from openstack_dashboard import context_processors @@ -438,6 +440,16 @@ class TestCase(horizon_helpers.TestCase): len(errors), 0, "No errors were found on the workflow") + def assertCookieMessage(self, response, expected_msg, detail_msg=None): + data = response.cookies["messages"] + storage = cookie_storage.CookieStorage(None) + messages = [m.message for m in storage._decode(data.value)] + if detail_msg is not None: + _expected = exceptions._append_detail(expected_msg, detail_msg) + else: + _expected = expected_msg + self.assertIn(_expected, messages) + class BaseAdminViewTests(TestCase): """Sets an active user with the "admin" role.