]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: encode non-ascii string before passing it to exec_cmd()
authorKefu Chai <kchai@redhat.com>
Wed, 31 Mar 2021 11:00:59 +0000 (19:00 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 31 Mar 2021 11:14:14 +0000 (19:14 +0800)
because on Python3, tempfile.TemporaryFile() is opened in binary mode by
default, we need to encode non-ascii string before write to it.
otherwise, we have following failure:

self = <dashboard.tests.test_access_control.AccessControlTest testMethod=test_unicode_password>

    def test_unicode_password(self):
        self.test_create_user()
        password = '\u7ae0\u9c7c\u4e0d\u662f\u5bc6\u7801'
        with tempfile.TemporaryFile(mode='w+') as pwd_file:
>           pwd_file.write(password)
E           UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-5: ordinal not in range(256)

tests/test_access_control.py:576: UnicodeEncodeError

this change is not cherry-picked from master, as master has dropped
python2 support.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/pybind/mgr/dashboard/tests/test_access_control.py

index 34452267558c73076c49da5b14340565470ea3d8..2ff48517ab9bbc7d8ff3d667fc94fb8b86316d26 100644 (file)
@@ -572,13 +572,10 @@ class AccessControlTest(unittest.TestCase, CLICommandTestMixin):
     def test_unicode_password(self):
         self.test_create_user()
         password = '章鱼不是密码'
-        with tempfile.TemporaryFile(mode='w+') as pwd_file:
-            pwd_file.write(password)
-            pwd_file.seek(0)
-            user = self.exec_cmd('ac-user-set-password', username='admin',
-                                 inbuf=pwd_file.read(), force_password=True)
-            pass_hash = password_hash(password, user['password'])
-            self.assertEqual(user['password'], pass_hash)
+        user = self.exec_cmd('ac-user-set-password', username='admin',
+                             inbuf=password.encode(), force_password=True)
+        pass_hash = password_hash(password, user['password'])
+        self.assertEqual(user['password'], pass_hash)
 
     def test_set_user_password_nonexistent_user(self):
         with self.assertRaises(CmdException) as ctx: