Browse Source

Fixing unicode issue when to_dict is called on py2.7 env

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 4d71926b3a)
tags/9.0.5^2
Seyeong Kim 1 year ago
parent
commit
c4968824db
2 changed files with 48 additions and 2 deletions
  1. 2
    2
      heat/common/context.py
  2. 46
    0
      heat/tests/test_common_context.py

+ 2
- 2
heat/common/context.py View File

@@ -156,8 +156,8 @@ class RequestContext(context.RequestContext):
156 156
         return self._clients
157 157
 
158 158
     def to_dict(self):
159
-        user_idt = '{user} {tenant}'.format(user=self.user_id or '-',
160
-                                            tenant=self.tenant_id or '-')
159
+        user_idt = u'{user} {tenant}'.format(user=self.user_id or '-',
160
+                                             tenant=self.tenant_id or '-')
161 161
 
162 162
         return {'auth_token': self.auth_token,
163 163
                 'username': self.username,

+ 46
- 0
heat/tests/test_common_context.py View File

@@ -1,3 +1,4 @@
1
+# -*- coding: utf-8 -*-
1 2
 #
2 3
 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
3 4
 #    not use this file except in compliance with the License. You may obtain
@@ -78,6 +79,51 @@ class TestRequestContext(common.HeatTestCase):
78 79
         del(ctx_dict['request_id'])
79 80
         self.assertEqual(self.ctx, ctx_dict)
80 81
 
82
+    def test_request_context_to_dict_unicode(self):
83
+
84
+        ctx_origin = {'username': 'mick',
85
+                      'trustor_user_id': None,
86
+                      'auth_token': '123',
87
+                      'auth_token_info': {'123info': 'woop'},
88
+                      'is_admin': False,
89
+                      'user': 'mick',
90
+                      'password': 'foo',
91
+                      'trust_id': None,
92
+                      'global_request_id': None,
93
+                      'show_deleted': False,
94
+                      'roles': ['arole', 'notadmin'],
95
+                      'tenant_id': '456tenant',
96
+                      'user_id': u'Gāo',
97
+                      'tenant': u'\u5218\u80dc',
98
+                      'auth_url': 'http://xyz',
99
+                      'aws_creds': 'blah',
100
+                      'region_name': 'RegionOne',
101
+                      'user_identity': u'Gāo 456tenant',
102
+                      'user_domain': None,
103
+                      'project_domain': None}
104
+
105
+        ctx = context.RequestContext(
106
+            auth_token=ctx_origin.get('auth_token'),
107
+            username=ctx_origin.get('username'),
108
+            password=ctx_origin.get('password'),
109
+            aws_creds=ctx_origin.get('aws_creds'),
110
+            project_name=ctx_origin.get('tenant'),
111
+            tenant=ctx_origin.get('tenant_id'),
112
+            user=ctx_origin.get('user_id'),
113
+            auth_url=ctx_origin.get('auth_url'),
114
+            roles=ctx_origin.get('roles'),
115
+            show_deleted=ctx_origin.get('show_deleted'),
116
+            is_admin=ctx_origin.get('is_admin'),
117
+            auth_token_info=ctx_origin.get('auth_token_info'),
118
+            trustor_user_id=ctx_origin.get('trustor_user_id'),
119
+            trust_id=ctx_origin.get('trust_id'),
120
+            region_name=ctx_origin.get('region_name'),
121
+            user_domain_id=ctx_origin.get('user_domain'),
122
+            project_domain_id=ctx_origin.get('project_domain'))
123
+        ctx_dict = ctx.to_dict()
124
+        del(ctx_dict['request_id'])
125
+        self.assertEqual(ctx_origin, ctx_dict)
126
+
81 127
     def test_request_context_from_dict(self):
82 128
         ctx = context.RequestContext.from_dict(self.ctx)
83 129
         ctx_dict = ctx.to_dict()

Loading…
Cancel
Save