def main(): conn = sqlite3.connect(DB_NAME) conn.row_factory = sqlite3.Row bank_manager = BankDatabaseManager(conn) interface = CliInterface(bank_manager) interface.start()
def __init__(self): self.__manager = BankDatabaseManager() self.WrongPasswordAttempts = 2
class CliInterface(): def __init__(self): self.__manager = BankDatabaseManager() self.WrongPasswordAttempts = 2 def __command_dispatcher(self, command): if command == "register": self.register() elif command == "login": self.login() elif command == "info": self.info() elif command == "exit": self.exit_program() elif command == "reset": self.reset_password() else: self.not_valid() def reset_password(self): email = input("Enter your e-mail for password reset:") newPassword = Client.generate_random_password() hashNewPassword = self.__manager.hash_password(newPassword) print(hashNewPassword) if (self.__manager.update_hash_password(email, hashNewPassword)): print("Password request has been made.") def not_valid(self): print("Not a valid command.\nTry again.") def exit_program(self): print("Thank you for using the bank app!") exit() def start(self): print( """Welcome to our bank service. You are not logged in. \n Please register or login""") while True: command = input("Enter a command:") self.__command_dispatcher(command) def info(self): print("login - for logging in!") print("register - for creating new account!") print("exit - for closing program!") def login(self): username = input("Enter your username: "******"Enter your password: "******"Login failed") if (self.WrongPasswordAttempts > 1): self.WrongPasswordAttempts -= 1 print("You have {} tries left".format(self.WrongPasswordAttempts)) else: print("Are you human or are you robot? :)") print("Database is locked for 10 seconds. Enjoy your day!") self.__manager.lock_database() def register(self): username = input("Enter your username: "******"Enter your password: "******"Enter your mail: ") if (self.__manager.register(username, password, mail)): print("Registration Successfull") else: print("Registration failed.") def __logged_dispatcher(self, command, logged_user): if command == 'info': self.logged_info(logged_user) elif command == 'changepass': self.logged_changepass(logged_user) elif command == 'change-message': self.logged_change_message(logged_user) elif command == 'show-message': self.logged_show_message(logged_user) elif command == 'help': self.logged_help() elif command == 'close': self.exit_program() else: self.not_valid() def logged_info(self, logged_user): print("You are: " + logged_user.get_username()) print("Your id is: " + str(logged_user.get_id())) print( "Your balance is:" + str(logged_user.get_balance()) + '$') def logged_changepass(self, logged_user): new_pass = getpass(prompt="Enter your new password: "******"Enter your new message: ") self.__manager.change_message(new_message, logged_user) def logged_show_message(self, logged_user): print(logged_user.get_message()) def logged_help(self): print("info - for showing account info") print("changepass - for changing passowrd") print("change-message - for changing users message") print("show-message - for showing users message") def logged_menu(self, logged_user): print("Welcome you are logged in as: " + logged_user.get_username()) while True: command = input("Logged>>\n Now enter a command:") self.__logged_dispatcher(command, logged_user)
def setUp(self): create_db.generate_tables() self.newDB = BankDatabaseManager() self.newDB.register("Tester", "123BatMAN!1", "*****@*****.**")
class SqlManagerTests(unittest.TestCase): def setUp(self): create_db.generate_tables() self.newDB = BankDatabaseManager() self.newDB.register("Tester", "123BatMAN!1", "*****@*****.**") def tearDown(self): self.newDB.cursor.execute('DROP TABLE clients') @classmethod def tearDownClass(cls): os.remove(DB_NAME) def test_mail(self): self.newDB.cursor.execute( """SELECT mail FROM clients where username = (?); """, ("Tester", ) ) user_result = self.newDB.cursor.fetchone() self.assertEqual(user_result[0], "*****@*****.**") # self.newDB. def test_register(self): password = "******" self.newDB.register("Dinko", password, "*****@*****.**") password = self.newDB.hash_password(password) self.newDB.cursor.execute( """SELECT Count(*) FROM clients WHERE username = (?) AND password = (?)""", ('Dinko', password)) users_count = self.newDB.cursor.fetchone() self.assertEqual(users_count[0], 1) def test_name(self): self.newDB.register( 'Peter', 'harrypotteralealeBatMAN!1', "*****@*****.**") self.newDB.cursor.execute( """SELECT username, password, balance, message FROM clients""") user_info = self.newDB.cursor.fetchall() self.assertEqual(user_info[1][0], "Peter") self.assertEqual(user_info[0][0], "Tester") def test_login(self): logged_user = self.newDB.login('Tester', '123BatMAN!1') self.assertEqual(logged_user.get_username(), 'Tester') def test_login_wrong_password(self): logged_user = self.newDB.login('Tester', '123567BatMAN!1') self.assertFalse(logged_user) def test_change_message(self): logged_user = self.newDB.login( "Tester", "123BatMAN!1") new_message = "podaivinototam" self.newDB.change_message(new_message, logged_user) self.assertEqual(logged_user.get_message(), new_message) def test_change_password(self): logged_user = self.newDB.login('Tester', '123BatMAN!1') new_password = "******" # new_password = self.newDB.hash_password(new_password) self.newDB.change_pass(new_password, logged_user) logged_user_new_password = self.newDB.login('Tester', new_password) self.assertEqual(logged_user_new_password.get_username(), 'Tester') def test_check_password_stability_1(self): self.assertFalse( self.newDB.check_password("Vitomir1!2", username="******")) def test_check_password_stability_2(self): self.assertFalse( self.newDB.check_password("vitomir12", username="******")) def test_check_password_stability_3(self): self.assertTrue( self.newDB.check_password("VitoshASDmy!com@1123", username="******")) def test_check_password_stability_4(self): self.assertFalse( self.newDB.check_password("Vitoshacademy!com!1", username="******")) def test_check_password_stability_5(self): self.assertFalse( self.newDB.check_password("@DFfdgfdgfds", username="******")) def test_check_password_stability_6(self): self.assertTrue( self.newDB.check_password("@DFfdgfdgfds123", username="******")) def test_check_password_stability_7(self): self.assertFalse( self.newDB.check_password("DFfdgfdgfds123123123", username="******"))
def __init__(self): self.manager = BankDatabaseManager() self.WrongPasswordAttempts = LOGIN_ATTEMPTS
class CliInterface(): def __init__(self): self.manager = BankDatabaseManager() self.WrongPasswordAttempts = LOGIN_ATTEMPTS def command_dispatcher(self, command): if command == "register": self.register() elif command == "login": self.login() elif command == "info": self.info() elif command == "exit": self.exit_program() elif command == "reset": self.reset_password() else: self.not_valid() def reset_password(self): email = input("Enter your e-mail for password reset:") newPassword = Client.generate_random_password() hashNewPassword = self.manager.hash_password(newPassword) if (self.manager.update_hash_password(email, hashNewPassword)): # for testing purposes, the only e-mail that may receive mails is my e-mail, # which is hidden in the settings folder ... # You may change it by adding "email" at the end of the send_mail() # parameters. username = self.manager.get_username_from_email(email) send_mail(username, hashNewPassword) print("Password request has been made.") hash_code = input( "Wait a little to receive the mail from us and enter the code here:" ) if (hash_code == hashNewPassword): new_password = getpass(prompt="Enter your new password here: ", stream=None) self.manager.set_pass_from_reset(username, new_password) print("Your new password has been generated.") else: print("Try again to reset!") def not_valid(self): print("Not a valid command.\nTry again.") def exit_program(self): print("Thank you for using the bank app!") exit() def start(self): print( """\n\n\n****BANK INTERNATIONAL****\n\n\nWelcome to our bank service. You are not logged in. \nPlease register or login:""" ) while True: command = input("Enter a command:") self.command_dispatcher(command) def info(self): print("login - for logging in!") print("register - for creating new account!") print("exit - for closing program!") def login(self): username = input("Enter your username: "******"Enter your password: "******"Login failed") if (self.WrongPasswordAttempts > 1): self.WrongPasswordAttempts -= 1 print("You have {} tries left".format( self.WrongPasswordAttempts)) else: print("Are you human or are you robot? :)") print("Database is locked for 10 seconds. Enjoy your day!") self.manager.lock_database() def register(self): username = input("Enter your username: "******"Enter your password: "******"Enter your mail: ") if (self.manager.register(username, password, mail)): print("Registration Successfull") else: print("Registration failed.") def logged_dispatcher(self, command, logged_user): if command == 'info': self.logged_info(logged_user) elif command == 'changepass': self.logged_changepass(logged_user) elif command == 'change-message': self.logged_change_message(logged_user) elif command == 'show-message': self.logged_show_message(logged_user) elif command == 'help': self.logged_help() elif command == 'close': self.exit_program() elif command == "deposit": self.logged_deposit(logged_user) elif command == "withdraw": pass elif command == "display": pass else: self.not_valid() def logged_deposit(self, logged_user): sum = float(input("Enter integer money to deposit.")) self.manager.deposit(logged_user, sum) print("The sum {} has been deposited.".format(sum)) def logged_info(self, logged_user): print("You are: " + logged_user.get_username()) print("Your id is: " + str(logged_user.get_id())) print("Your balance is:" + str(logged_user.get_balance()) + '$') def logged_changepass(self, logged_user): new_pass = getpass(prompt="Enter your new password: "******"Enter your new message: ") self.manager.change_message(new_message, logged_user) def logged_show_message(self, logged_user): print(logged_user.get_message()) def logged_help(self): print("info - for showing account info") print("changepass - for changing passowrd") print("change-message - for changing users message") print("show-message - for showing users message") def logged_menu(self, logged_user): print("Welcome you are logged in as: " + logged_user.get_username()) while True: command = input("Logged>>\n Now enter a command:") self.logged_dispatcher(command, logged_user)