def test_generate(self): """ Basic password generation tests. """ pw = pwtools.generate_password(255) self.assertEquals(255, len(pw)) self.assertTrue(all([a in string.printable for a in pw])) pw = pwtools.generate_password(length=12, ascii_lower=True, ascii_upper=False, punctuation=False, digits=False) print pw self.assertTrue(all([a.islower() for a in pw])) self.assertTrue(all([a.isalpha() for a in pw])) pw = pwtools.generate_password(length=12, ascii_lower=True, ascii_upper=False, punctuation=False, digits=False) print pw self.assertTrue(all([a.islower() for a in pw])) self.assertTrue(all([a.isalpha() for a in pw])) pw = pwtools.generate_password(length=12, ascii_lower=False, ascii_upper=False, punctuation=False, digits=True) print pw self.assertTrue(all([a.isdigit() for a in pw])) pw = pwtools.generate_password(length=12, ascii_lower=True, ascii_upper=True, punctuation=False, digits=False) self.assertTrue(all([a.isalpha() for a in pw])) pw = pwtools.generate_password(length=12, ascii_lower=False, ascii_upper=False, punctuation=True, digits=False) self.assertTrue(all([not a.isalpha() for a in pw])) self.assertTrue(all([not a.isdigit() for a in pw])) with self.assertRaises(ValueError): pw = pwtools.generate_password(length=12, ascii_lower=False, ascii_upper=False, punctuation=False, digits=False) # Strip ambiguous pw = pwtools.generate_password(length=200, strip_ambiguous=True) self.assertFalse(any(c in pw for c in "LlOo0iI1"), "Did not expect to find any ambiguous characters in password.")
def generatePassword(self, length=12, ascii_lower=True, ascii_upper=True, punctuation=True, digits=True, strip_ambiguous=True, strip_dangerous=True): """ Generates a random password and returns this to the client. (No database information is updated or revealed by this method.) :param length: Length of generated password. :type length: int :param ascii_lower: Whether to include ASCII lowercase chars. :type ascii_lower: bool :param ascii_upper: Whether to include ASCII uppercase chars. :type ascii_upper: bool :param punctuation: Whether to include punctuation. :type punctuation: bool :param strip_ambiguous: Whether to remove easily-confused (LlOo0iI1) chars from the password. :type strip_ambiguous: bool :param strip_dangerous: Whethr to remove some of the more 'dangerous' punctuation (e.g. quotes) from the generated password. :type strip_dangerous: bool :returns: The generated password. :rtype: str """ return generate_password(length=length, ascii_lower=ascii_lower, ascii_upper=ascii_upper, punctuation=punctuation, digits=digits, strip_ambiguous=strip_ambiguous, strip_dangerous=strip_dangerous)
def generate(self, digits, lower, upper, nonalpha, length, nonambig, _): """ (AJAX) Generate a password and return the text of the new password. (jquery will pass an additional _ arg for no-caching purposes.) """ return pwtools.generate_password(length=int(length), ascii_lower=int(lower), ascii_upper=int(upper), punctuation=int(nonalpha), digits=int(digits), strip_ambiguous=int(nonambig))