import json
import threading
import time
+import six
import bcrypt
def password_hash(password, salt_password=None):
if not password:
return None
+ if six.PY2:
+ password = unicode(password, 'utf-8') if isinstance(password, str) else password
if not salt_password:
salt_password = bcrypt.gensalt()
else:
import errno
import json
+import tempfile
import time
import unittest
def test_sanitize_password(self):
self.test_create_user()
password = 'myPass\\n\\r\\n'
- with open('/tmp/test_sanitize_password.txt', 'w+') as pwd_file:
+ with tempfile.TemporaryFile(mode='w+') as pwd_file:
# Add new line separators (like some text editors when a file is saved).
pwd_file.write('{}{}'.format(password, '\n\r\n\n'))
pwd_file.seek(0)
pass_hash = password_hash(password, user['password'])
self.assertEqual(user['password'], pass_hash)
+ 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)
+
def test_set_user_password_nonexistent_user(self):
with self.assertRaises(CmdException) as ctx:
self.exec_cmd('ac-user-set-password', username='admin',