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] 2d6179c819

Change-Id: I3e5e12265d0bc7b753bbe1f57acdd663b9dd3587
This commit is contained in:
Akihiro Motoki 2021-02-24 23:32:07 +09:00 committed by Vishal Manchanda
parent bbaf21c4df
commit 9526289b76
3 changed files with 17 additions and 7 deletions

View File

@ -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(

View File

@ -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')

View File

@ -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.