From 0e0d39d40be762a26319d65e3458cf8640aa49ce Mon Sep 17 00:00:00 2001 From: Yuxing wang Date: Wed, 29 Apr 2015 00:44:07 -0700 Subject: [PATCH] Password is not strong enough failures Add a method to generate password with 1: use of both upper- and lower-case letters (case sensitivity) 2: inclusion of one or more numerical digits 3: inclusion of special characters, e.g. @, #, $ etc. 4: at least 15 characters in length Closes-Bug: 1448217 Change-Id: I86bf157f1bdb44f5fc579dc5317784fe31df8521 --- tempest_lib/common/utils/data_utils.py | 23 +++++++++++++++++++ .../tests/common/utils/test_data_utils.py | 14 +++++++++++ 2 files changed, 37 insertions(+) diff --git a/tempest_lib/common/utils/data_utils.py b/tempest_lib/common/utils/data_utils.py index c900d4d..046dd6c 100644 --- a/tempest_lib/common/utils/data_utils.py +++ b/tempest_lib/common/utils/data_utils.py @@ -16,6 +16,7 @@ import itertools import netaddr import random +import string import uuid @@ -56,6 +57,28 @@ def rand_name(name='', prefix=None): return rand_name +def rand_password(length=15): + """Generate a random password + + :param int length: The length of password that you expect to set + :return: a random password. The format is + '-- + -' + (e.g. 'G2*ac8&lKFFgh%2') + :rtype: string + """ + upper = random.choice(string.ascii_uppercase) + ascii_char = string.ascii_letters + digits = string.digits + digit = random.choice(string.digits) + puncs = '~!@#$%^&*_=+' + punc = random.choice(puncs) + seed = ascii_char + digits + puncs + pre = upper + digit + punc + password = pre + ''.join(random.choice(seed) for x in range(length - 3)) + return password + + def rand_url(): """Generate a random url that inclues a random number diff --git a/tempest_lib/tests/common/utils/test_data_utils.py b/tempest_lib/tests/common/utils/test_data_utils.py index e8a7c33..81dee57 100644 --- a/tempest_lib/tests/common/utils/test_data_utils.py +++ b/tempest_lib/tests/common/utils/test_data_utils.py @@ -56,6 +56,20 @@ class TestDataUtils(base.TestCase): actual2 = data_utils.rand_name(prefix='prefix-str') self.assertNotEqual(actual, actual2) + def test_rand_password(self): + actual = data_utils.rand_password() + self.assertIsInstance(actual, str) + self.assertRegexpMatches(actual, "[A-Za-z0-9~!@#$%^&*_=+]{15,}") + actual2 = data_utils.rand_password() + self.assertNotEqual(actual, actual2) + + def test_rand_password_with_len(self): + actual = data_utils.rand_password(8) + self.assertIsInstance(actual, str) + self.assertRegexpMatches(actual, "[A-Za-z0-9~!@#$%^&*_=+]{8}") + actual2 = data_utils.rand_password(8) + self.assertNotEqual(actual, actual2) + def test_rand_url(self): actual = data_utils.rand_url() self.assertIsInstance(actual, str)