Write your code: Random Password#

Write a function that works as a Random Strong Password Generator.

Rules#

  1. Function returns a string

  2. Length between 8 and 24 characters

  3. At least one uppercase letter (A-Z)

  4. At least one lowercase letter (a-z)

  5. At least one digit (0-9)

  6. It contains at least one special character (!@#$%^&*()+=)

  7. Maximum of 2 repeated characters (regardless of order)

  8. Always return a different password

  9. You can use any random function from the random module

Function Definition#

from random import # add function

def password_generator():

    # write your code here
  File "/tmp/ipykernel_2426/4144797343.py", line 1
    from random import # add function
                                     ^
SyntaxError: invalid syntax

Testing#

Check if your function returns the expected value using the cell below.

import unittest

class UnitTests(unittest.TestCase):
    def setUp(self):
        self.password = password_generator()
        self.passwords = [password_generator() for _ in range(10)]

    def test_password_type(self):
        self.assertTrue(isinstance(self.password, str), 'The function should return a string')
    def test_password_min_size(self):
        self.assertGreater(len(self.password), 7, "The function should return a password with minimum length of 8 characters")
    def test_password_max_size(self):
        self.assertLess(len(self.password), 25, "The function should return a password with maximum length of 24 characters")
    def test_upper(self):
        self.assertTrue(any(char.isupper() for char in self.password), "The function should return a password with at least 1 upper-case letter")
    def test_lower(self):
        self.assertTrue(any(char.islower() for char in self.password), "The function should return a password with at least 1 lower-case letter")
    def test_numeric(self):
        self.assertTrue(any(char.isdigit() for char in self.password), "The function should return a password with at least 1 number")
    def test_special(self):
        self.assertTrue(any(char in "!@#$%^&*()+=" for char in self.password), "The function should return a password with at least 1 special character")     
    def test_repeated_char(self):
        self.assertTrue(all(self.password.count(char)<3 for char in self.password), "The function should return a password with a maximum of 2 repeated character")
    def test_unique_password(self):
        self.assertEqual(len(self.passwords), len(set(self.passwords)), "The function should return an unique password")

unittest.main(argv=[''], verbosity=2,exit=False)