Handle non ASCII tenant names gracefully when downloading RC file

Co-Authored-By: Paul Karikh <pkarikh@mirantis.com>
Change-Id: I2ab75b46d69579c6516be52cdc1498c0f598eb10
Closes-Bug: #1545008
This commit is contained in:
Timur Sufiev 2016-03-15 16:38:17 +03:00
parent caa5e91059
commit 3850196a17
3 changed files with 84 additions and 12 deletions

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from django.core.urlresolvers import reverse
from django.http import HttpRequest # noqa
from django.test.utils import override_settings # noqa
@ -135,3 +137,68 @@ class APIAccessTests(test.TestCase):
def test_recreate_user_credentials_with_no_existing_creds(self):
self._test_recreate_user_credentials(exists_credentials=False)
class ASCIITenantNameRCTests(test.TestCase):
TENANT_NAME = 'tenant'
def _setup_user(self, **kwargs):
super(ASCIITenantNameRCTests, self)._setup_user(
tenant_name=self.TENANT_NAME)
def test_openrcv2_credentials_filename(self):
expected = 'attachment; filename="%s-openrc.sh"' % self.TENANT_NAME
res = self.client.get(OPENRCV2_URL)
self.assertEqual(res.status_code, 200)
self.assertEqual(expected, res['content-disposition'])
@override_settings(OPENSTACK_API_VERSIONS={"identity": 3})
def test_openrc_credentials_filename(self):
expected = 'attachment; filename="%s-openrc.sh"' % self.TENANT_NAME
res = self.client.get(OPENRC_URL)
self.assertEqual(res.status_code, 200)
self.assertEqual(expected, res['content-disposition'])
class UnicodeTenantNameRCTests(test.TestCase):
TENANT_NAME = u'\u043f\u0440\u043e\u0435\u043a\u0442'
def _setup_user(self, **kwargs):
super(UnicodeTenantNameRCTests, self)._setup_user(
tenant_name=self.TENANT_NAME)
def test_openrcv2_credentials_filename(self):
expected = ('attachment; filename="%s-openrc.sh"' %
self.TENANT_NAME).encode('utf-8')
res = self.client.get(OPENRCV2_URL)
self.assertEqual(res.status_code, 200)
result_content_disposition = res['content-disposition']
# we need to encode('latin-1') because django response object
# has custom setter which encodes all values to latin-1 for Python3.
# https://github.com/django/django/blob/1.9.6/django/http/response.py#L142
# see _convert_to_charset() method for details.
if six.PY3:
result_content_disposition = result_content_disposition.\
encode('latin-1')
self.assertEqual(expected,
result_content_disposition)
@override_settings(OPENSTACK_API_VERSIONS={"identity": 3})
def test_openrc_credentials_filename(self):
expected = ('attachment; filename="%s-openrc.sh"' %
self.TENANT_NAME).encode('utf-8')
res = self.client.get(OPENRC_URL)
self.assertEqual(res.status_code, 200)
result_content_disposition = res['content-disposition']
if six.PY3:
result_content_disposition = result_content_disposition.\
encode('latin-1')
self.assertEqual(expected,
result_content_disposition)

View File

@ -139,9 +139,9 @@ def _download_rc_file_for_template(request, context, template):
template,
context,
content_type="text/plain")
response['Content-Disposition'] = ('attachment; '
'filename="%s-openrc.sh"'
% context['tenant_name'])
tenant_name = context['tenant_name']
disposition = 'attachment; filename="%s-openrc.sh"' % tenant_name
response['Content-Disposition'] = disposition.encode('utf-8')
response['Content-Length'] = str(len(response.content))
return response

View File

@ -185,17 +185,21 @@ class TestCase(horizon_helpers.TestCase):
# For some magical reason we need a copy of this here.
self.factory = RequestFactoryWithMessages()
def _setup_user(self):
def _setup_user(self, **kwargs):
self._real_get_user = utils.get_user
tenants = self.context['authorized_tenants']
self.setActiveUser(id=self.user.id,
token=self.token,
username=self.user.name,
domain_id=self.domain.id,
user_domain_name=self.domain.name,
tenant_id=self.tenant.id,
service_catalog=self.service_catalog,
authorized_tenants=tenants)
base_kwargs = {
'id': self.user.id,
'token': self.token,
'username': self.user.name,
'domain_id': self.domain.id,
'user_domain_name': self.domain.name,
'tenant_id': self.tenant.id,
'service_catalog': self.service_catalog,
'authorized_tenants': tenants
}
base_kwargs.update(kwargs)
self.setActiveUser(**base_kwargs)
def _setup_request(self):
super(TestCase, self)._setup_request()
@ -231,6 +235,7 @@ class TestCase(horizon_helpers.TestCase):
domain_id=domain_id,
user_domain_name=user_domain_name,
tenant_id=tenant_id,
tenant_name=tenant_name,
service_catalog=service_catalog,
roles=roles,
enabled=enabled,