示例#1
0
class TestCredential(unittest.TestCase):
    def setUp(self):
        '''Setting up credentials that will be used in the tests.'''
        self.test_user_credential = Credential('*****@*****.**',
                                               'Twitter', '123@Iiht')
        self.test_other_user_credential = Credential(
            '*****@*****.**', 'Instagram', '123@Iiht')

    def tearDown(self):
        '''Clean up. Delete the credentials db after every test.'''
        if os.path.isfile(Credential.database):
            os.remove(Credential.database)

    def test_intialize_credential(self):
        '''Test that Credential instance is created successfully.'''

        self.assertEqual('*****@*****.**',
                         self.test_user_credential.get_email())
        self.assertEqual('Twitter', self.test_user_credential.get_account())
        self.assertEqual('123@Iiht', self.test_user_credential.get_password())

    def test_create_credentials_file(self):
        '''Test that the db is created successfully.'''

        with open(Credential.database, 'w+') as test_file:
            file_exists = os.path.isfile(Credential.database)
            self.assertTrue(file_exists)

    def test_record_a_credential(self):
        '''Test that a credential is always created and stored in the db successfully'''

        self.test_user_credential.create_credential()
        self.test_other_user_credential.create_credential()
        with open(Credential.database, 'r') as read_file:
            fields = ['email', 'account', 'password']
            data_read = csv.DictReader(read_file, fields, lineterminator='\n')
            self.add_success = False
            counter = 0
            for line in data_read:
                counter += 1
                if line['email'] == '*****@*****.**' and line[
                        'account'] == 'Twitter' and line[
                            'password'] == '123@Iiht':
                    self.add_success = True

            self.assertTrue(self.add_success)

    def test_check_account_exist(self):
        '''From the instance method. Search the DB and ensures the account exists'''
        self.test_user_credential.create_credential()

        account_exist = self.test_user_credential.check_account_exist()
        self.assertTrue(account_exist)

    def test_check_account_exist(self):
        '''From the class method. Search the DB and ensure an account exists'''
        self.test_user_credential.create_credential()

        account_exist = Credential.check_an_account_exist(
            self.test_user_credential.get_email(),
            self.test_user_credential.get_account())
        self.assertTrue(account_exist)

    def test_check_account_exist_without_db(self):
        '''Tests and ensures check_an_account_exist() does not return True if the DB is non-Existent.'''
        account_exist = Credential.check_an_account_exist(
            self.test_user_credential.get_email(),
            self.test_user_credential.get_account())
        self.assertFalse(account_exist)

    def test_randomizer(self):
        '''Tests and ensures the radomizer() generates the passwords'''
        random_password = Credential.randomizer()
        self.assertGreater(len(random_password), 7)
        self.assertLess(len(random_password), 9)

    def test_display_accounts(self):
        self.test_other_user_credential.create_credential()
        self.test_user_credential.create_credential()

        list_of_credentials = Credential.display_accounts(
            self.test_user_credential.get_email())
        for a_credential in list_of_credentials:
            a_credential_exist = Credential.check_an_account_exist(
                a_credential['email'], a_credential['account'])
            if not a_credential_exist:
                return False

        self.assertTrue(a_credential_exist)

    def test_delete_account(self):
        '''Tests and ensures deletes successfully.'''
        self.test_user_credential.create_credential()
        account_created = Credential.check_an_account_exist(
            self.test_user_credential.get_email(),
            self.test_user_credential.get_account())
        is_deleted = Credential.delete_account(
            self.test_other_user_credential.get_email(),
            self.test_other_user_credential.get_account())

        list_of_credentials = Credential.display_accounts(
            self.test_user_credential.get_email())

        account_exist = False
        for account in list_of_credentials:
            if account['account'] == self.test_user_credential.get_account():
                account_exist = False
            else:
                account_exist = True
        self.assertTrue(account_created)
        self.assertTrue(is_deleted)
        self.assertFalse(account_exist)
示例#2
0
def main():
    home = True
             
    head = ' ***********************************************************************************************'
    while home:
        print(head)
        print('|\t\t\t\t\t\t\t\t\t\t\t\t| \n|\tHi There!.Welcome to the Password-Locker System.\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t\t\t***Have all your credentials from one point.***\t\t\t\t\t|\n|\t\t\t\t\t\t\t\t\t\t\t\t|\n|\tSelect a number to proceed...\t\t\t\t\t\t\t\t|\n|1. Create a Password Account\t\t\t\t\t\t\t\t\t\t|\n|2. Login with an existing account  credentials\t\t\t\t\t\t\t\t\t\t|\n|3. Exit Password-Locker\t\t\t\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t\t\t\t\t\t|')
        print(head)

        option = input()
        if option == '1':
            signup = False
            while not signup:
                print('--------------------------------------------------------')
                print('|\t\t\t\t\t\t\t|\n|\tHi! the system says- "Happy you are running me".\t\t\t|\n|\t\t\t\t\t\t\t|\n|Enter  Your first name\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t1. Cancel\t\t|')
                print('--------------------------------------------------------')
                first_name = input()
                if first_name == '1':
                    signup = not signup
                else: 
                    print('--------------------------------------------------------')
                    print('|\t\t\t\t\t\t\t|\n|\tHi! the system says- "Happy you are running me"\t\t\t|\n|\t\t\t\t\t\t\t|\n|Enter Your last name\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t1. Cancel\t\t|')
                    print('--------------------------------------------------------')
                    last_name = input()
                    if last_name == '1':
                        signup = not signup
                    else:
                        print('--------------------------------------------------------')
                        print('|\t\t\t\t\t\t\t|\n|\tHi! the system says- "Happy you are running me"\t\t\t|\n|\t\t\t\t\t\t\t|\n|Enter your email\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t1. Cancel\t\t|')
                        print('--------------------------------------------------------')
                        email = input()
                        if email == '1':
                            signup = not signup
                        else:
                            print('--------------------------------------------------------')
                            print('|\t\t\t\t\t\t\t|\n|\tHi! the system says- "Happy you are running me"\t\t\t|\n|\t\t\t\t\t\t\t|\n|Enter a password\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t1. Cancel\t\t|')
                            print('--------------------------------------------------------')
                            password = input()
                            if password == '1':
                                signup = not signup
                            else:
                                user_created = User(first_name, last_name, email, password)
                                if user_created.create_account():
                                    print('--------------------------------------------------------')
                                    print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\t Hello! User created successfully!\t\t|\n|\t\t\t\t\t\t\t|\n|\t\t\tEnter\t\t\t\t|')
                                    print('--------------------------------------------------------')
                                    status = input()
                                    if status == '1':
                                        signup = not signup
                                    else:
                                        signup = not signup
                                else:
                                    print('--------------------------------------------------------')
                                    print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tThe process was violated.Kindly repeat.\t\t|\n|\t\t\t\t\t\t\t|\n|\t\t\tPress Enter\t\t\t\t|')
                                    print('--------------------------------------------------------')
                                    status = input()
                                    if status == '1':
                                        signup = not signup
                                    else:
                                        signup = not signup

        if option =='2':
            login = False
            while not login:
                print('--------------------------------------------------------')
                print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Enter your email\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t1. Cancel\t\t|')
                print('--------------------------------------------------------')
                email = input()
                if email == '1':
                    login = not login
                else:
                    print('--------------------------------------------------------')
                    print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Enter your password\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t1. Cancel\t\t|')
                    print('--------------------------------------------------------')
                    password = input()
                    if password == '1':
                        login = not login
                    else:
                        account_status = User.check_account_exist(email, password)
                        if account_status:
                            print('--------------------------------------------------------')
                            print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tYou are now logged in \t\t\t\t|\n|\t\t\t\t\t\t\t|\n|1. View Password Accounts\t\t\t\t|\n|2. Create a new Password Account\t\t\t\t|\n|3. Delete an existing Account\t\t\t\t|\n\t\t\t\t\t\t\t|')
                            print('--------------------------------------------------------')
                            perform = input()
                            if perform == '1':
                                all_acounts = Credential.display_accounts(email)
                                print('--------------------------------------------------------')
                                print('|\t\t\t\t\t\t\t|\n|\tHolla!!!! See your accounts now.\t\t\t|')
                                if len(all_acounts) > 0:
                                    count = 1
                                    for account in all_acounts:
                                        print(f'\t\t\t\t\t\t\t|\n|{count} {account["account"]}\t{account["password"]}')
                                        count +=1
                                else:
                                    print('|\t\t\t\t\t\t\t|\n|\t!!!!!!!!!!!!!!!!!!!!\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Sorry! You do not have any accounts set up.\t\t|')
                                print('|\t\t\t\t\t\t\t|\n|\tEnter\t\t\t\t\t\t|')
                                print('--------------------------------------------------------')
                                input()
                                login = not login
                            elif perform == '2':
                                print('--------------------------------------------------------')
                                print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Which  account is this? i.e Instagram/Twitter ...\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t\t|')
                                print('--------------------------------------------------------')
                                account = input()
                                account_exists = Credential.check_an_account_exist(email, account)
                                if len(account) > 0 and not account_exists :
                                    print('--------------------------------------------------------')
                                    print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tYou are now logged in\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|1. Get a system generated password\t\t\t\t|\n|2. Generate your own password\t\t\t\t|\n|\t\t\t\t\t\t\t|')
                                    print('--------------------------------------------------------')
                                    selected_account = input()
                                    if selected_account == '1':
                                        generated_password = Credential.randomizer()
                                        user_created_account = Credential(email, account, generated_password)
                                                
                                        if user_created_account.create_credential():
                                            print('--------------------------------------------------------')
                                            print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Account created successfully.\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t\t|')
                                            print('--------------------------------------------------------')
                                            input()
                                        else:
                                            print('--------------------------------------------------------')
                                            print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Sorry! Please try again.\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t|')
                                            print('--------------------------------------------------------')
                                        
                                        login = not login
                                    else:
                                        print('--------------------------------------------------------')
                                        print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Enter a password to use.(At least more than 8 characters long) \t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t\t|')
                                        print('--------------------------------------------------------')
                                        account_password = input()
                                        ok_create = True
                                        while ok_create:
                                            if len(account_password) > 8:
                                                user_created_account = Credential(email, account, account_password)
                                                
                                                if user_created_account.create_credential():
                                                    print('--------------------------------------------------------')
                                                    print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Account Successfully created  .\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t\t|')
                                                    print('--------------------------------------------------------')
                                                    input()
                                                else:
                                                    print('--------------------------------------------------------')
                                                    print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Sorry! Please try again.\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t|')
                                                    print('--------------------------------------------------------')
                                                
                                                ok_create = not ok_create
                                                login = not login
                                            else:
                                                print('--------------------------------------------------------')
                                                print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Sorry the password  is too short-Pleaset try again.\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t|')
                                                print('--------------------------------------------------------')
                                                ok_create = not ok_create    
                                else:
                                    print('--------------------------------------------------------')
                                    print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|This Account already exists.\t|\n|\t\t\t\t\t\t\t|\n|\t\tEnter\t\t\t\t|')
                                    print('--------------------------------------------------------')
                                    input()  
                                    login = not login      
                            elif perform == '3':
                                all_acounts = Credential.display_accounts(email)
                                print('--------------------------------------------------------')
                                print('|\t\t\t\t\t\t\t|\n|\tSelect the account you want to delete.\t\t\t|')
                                if len(all_acounts) > 0:
                                    count = 1
                                    for account in all_acounts:
                                        print(f'\t\t\t\t\t\t\t|\n|{count} {account["account"]}\t{account["password"]}')
                                        count +=1     
                                    to_delete = int(input())      
                                    account_to_delete = all_acounts[to_delete - 1]
                                    print(account_to_delete)   
                                    print(account_to_delete['account'])
                                    is_deleted = Credential.delete_account(account_to_delete['email'], account_to_delete['account'])  
                                    if is_deleted:
                                        print('--------------------------------------------------------')
                                        print('|\t\t\t\t\t\t\t|\n|\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Account deleted successfully\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\t\tEnter\t\t\t\t|')
                                        print('--------------------------------------------------------')
                                        input()
                                    else:
                                        print('--------------------------------------------------------')
                                        print('|\t\t\t\t\t\t\t|\n|\!!!!!!!!!!!!!!!!!!!!!!!!!!t\t\t|\n|\t\t\t\t\t\t\t|\n|Sorry Something went wrong!\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\t\tEnter\t\t\t\t|')
                                        print('--------------------------------------------------------')
                            else:
                                login = not login
                        else:
                            print('--------------------------------------------------------')
                            print('|\t\t\t\t\t\t\t|\n|\t\t\t\t\t\t\t|\n|Account does not exist in the system. Please Sign Up\t\t\t|\n|\t\t\t\t\t\t\t|\n|\t\t\tEnter\t\t\t\t|')
                            print('--------------------------------------------------------')
                            error = input()
                            login = not login

        elif option == '3':
            home = not home