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
This commit is contained in:
Yuxing wang
2015-04-29 00:44:07 -07:00
parent c307ffc525
commit 0e0d39d40b
2 changed files with 37 additions and 0 deletions

View File

@@ -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
'<random upper letter>-<random number>-<random special character>
-<random ascii letters or digit characters or special symbols>'
(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

View File

@@ -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)