From c4968824dbeaad849d16482cc7a6234e862714c6 Mon Sep 17 00:00:00 2001 From: Seyeong Kim Date: Thu, 5 Apr 2018 15:10:01 -0700 Subject: [PATCH] Fixing unicode issue when to_dict is called on py2.7 env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using non-unicode old style user id such as Gāo Unicode error popup on py2.7 environment Fixing it on common/context.py Change-Id: I95e49f359410049ff5b254cd1b8ee16402c8719d Closes-Bug: #1761629 (cherry picked from commit 4d71926b3afc50c3f16378de260b86a85e8d721d) --- heat/common/context.py | 4 +-- heat/tests/test_common_context.py | 46 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/heat/common/context.py b/heat/common/context.py index 974ee815fd..ad89c83f65 100644 --- a/heat/common/context.py +++ b/heat/common/context.py @@ -156,8 +156,8 @@ class RequestContext(context.RequestContext): return self._clients def to_dict(self): - user_idt = '{user} {tenant}'.format(user=self.user_id or '-', - tenant=self.tenant_id or '-') + user_idt = u'{user} {tenant}'.format(user=self.user_id or '-', + tenant=self.tenant_id or '-') return {'auth_token': self.auth_token, 'username': self.username, diff --git a/heat/tests/test_common_context.py b/heat/tests/test_common_context.py index 9f66d9e0df..0fb8f9d4e4 100644 --- a/heat/tests/test_common_context.py +++ b/heat/tests/test_common_context.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -78,6 +79,51 @@ class TestRequestContext(common.HeatTestCase): del(ctx_dict['request_id']) self.assertEqual(self.ctx, ctx_dict) + def test_request_context_to_dict_unicode(self): + + ctx_origin = {'username': 'mick', + 'trustor_user_id': None, + 'auth_token': '123', + 'auth_token_info': {'123info': 'woop'}, + 'is_admin': False, + 'user': 'mick', + 'password': 'foo', + 'trust_id': None, + 'global_request_id': None, + 'show_deleted': False, + 'roles': ['arole', 'notadmin'], + 'tenant_id': '456tenant', + 'user_id': u'Gāo', + 'tenant': u'\u5218\u80dc', + 'auth_url': 'http://xyz', + 'aws_creds': 'blah', + 'region_name': 'RegionOne', + 'user_identity': u'Gāo 456tenant', + 'user_domain': None, + 'project_domain': None} + + ctx = context.RequestContext( + auth_token=ctx_origin.get('auth_token'), + username=ctx_origin.get('username'), + password=ctx_origin.get('password'), + aws_creds=ctx_origin.get('aws_creds'), + project_name=ctx_origin.get('tenant'), + tenant=ctx_origin.get('tenant_id'), + user=ctx_origin.get('user_id'), + auth_url=ctx_origin.get('auth_url'), + roles=ctx_origin.get('roles'), + show_deleted=ctx_origin.get('show_deleted'), + is_admin=ctx_origin.get('is_admin'), + auth_token_info=ctx_origin.get('auth_token_info'), + trustor_user_id=ctx_origin.get('trustor_user_id'), + trust_id=ctx_origin.get('trust_id'), + region_name=ctx_origin.get('region_name'), + user_domain_id=ctx_origin.get('user_domain'), + project_domain_id=ctx_origin.get('project_domain')) + ctx_dict = ctx.to_dict() + del(ctx_dict['request_id']) + self.assertEqual(ctx_origin, ctx_dict) + def test_request_context_from_dict(self): ctx = context.RequestContext.from_dict(self.ctx) ctx_dict = ctx.to_dict()