def test_update_client(self): self.client_dao.add_client(Client('username', 'password')) self.assertEqual(self.client_dao.get_clients()[0].get_username(), 'username') self.assertEqual(self.client_dao.get_clients()[0].get_password(), 'password') self.client_dao.update_client(Client('username', 'PASSWORD')) self.assertEqual(self.client_dao.get_clients()[0].get_username(), 'username') self.assertEqual(self.client_dao.get_clients()[0].get_password(), 'PASSWORD')
def logout_session(self): """ Logout of current session to a blank client """ log.log_debug("logged out from current session to default") self.client = Client()
def __init__(self, client_dao, client=Client()): # set ClientDAO for session self.client_dao = client_dao # set up BankSession with starting Client # default is blank session self.client = client log.log_debug("instantiated BankSession object")
def test_logout(self): self.bank_session.register(Client('username', 'password')) self.bank_session.logout_session() self.assertEqual(self.bank_session.current_session(), '')
def test_login_fail(self): self.client_dao.add_client(Client('username', 'password')) with self.assertRaises(LoginError): self.bank_session.login(Client('username1', 'password1'))
def test_username_init_space(self): with self.assertRaises(InvalidCharacterError): client = Client(username="******")
def test_delete_client(self): self.client_dao.add_client(Client('username', 'password')) self.assertEqual(self.client_dao.get_clients()[0].get_username(), 'username') self.assertEqual(self.client_dao.get_clients()[0].get_password(), 'password') self.client_dao.delete_client(Client('username')) self.assertEqual(self.client_dao.get_clients(), [])
def test_add_client_client_setup(self): with self.assertRaises(ClientSetupError): self.client_dao.add_client(Client("zamerman", "password")) self.client_dao.add_client(Client("zamerman", "password"))
def test_inequality(self): client1 = Client('username') client2 = Client('zamerman') self.assertNotEqual(client1, client2)
def test_password_init_space(self): with self.assertRaises(InvalidCharacterError): client = Client(password="******")
def test_withdraw_overdraw_error(self): self.bank_session.register(Client('username', 'password', 10000)) with self.assertRaises(OverdrawError): self.bank_session.withdraw(50000)
class BankSession(): """ Our service layer program which implements all the functionality we need for a session at the bank. """ def __init__(self, client_dao, client=Client()): # set ClientDAO for session self.client_dao = client_dao # set up BankSession with starting Client # default is blank session self.client = client log.log_debug("instantiated BankSession object") def current_session(self): """ Returns the username of the current session """ log.log_debug("pulled current session username") return self.client.get_username() @requires_client def register(self, client): """ Registers a new client and logs in as that client """ # Create transaction entry for account creation record = datetime.now().strftime(datetime_format) record += ": Created account with balance of: $" record += str(client.get_balance()) client.set_transactions([record]) # Instruct ClientDAO to add client self.client_dao.add_client(client) # Use login method to login as new client self.login(client) log.log_debug("registed new client and logged in") @requires_client def login(self, client): """ Login as a client Can raise LoginError if username or password is wrong """ # Get a list of clients from the ClientDAO clients = self.client_dao.get_clients() # Check if the client we are logging in as is in the list if client in clients: # Pull the client and check our password pulled_client = clients[clients.index(client)] if client.get_password() == pulled_client.get_password(): self.client = pulled_client log.log_debug("logged in as: " + str(pulled_client)) # if password is wrong raise LoginError else: log.log_error("LoginError: Wrong username or password") raise LoginError("Wrong username or password") # if client is not in ClientDAO raise LoginError else: log.log_error("LoginError: Wrong username or password") raise LoginError("Wrong username or password") def logout_session(self): """ Logout of current session to a blank client """ log.log_debug("logged out from current session to default") self.client = Client() def view_balance(self): """ Check balance of current login session May raise SessionError if you are not logged in or registered with bank """ # Check if we are logged in if self.client in self.client_dao.get_clients(): log.log_debug("viewed client balance") return self.client.get_balance() # if client is not part of bank raise SessionError else: log.log_error("SessionError: Not logged in or registered with bank") raise SessionError('Not logged in or registered with bank') @requires_int @requires_positive def deposit(self, cash): """ Deposit cash into balance May raise SessionError if you are not logged in or registered with bank """ # Checks if we are logged into an account in the vault if self.client in self.client_dao.get_clients(): # Sets the clients balance self.client.set_balance(self.client.get_balance() + cash) # Adds to the clients transactions record = datetime.now().strftime(datetime_format) record += ": Deposit: $" + str(cash) self.client.get_transactions().append(record) # Updates client in vault self.client_dao.update_client(self.client) log.log_debug("${0} deposited".format(str(cash))) # If we are not logged into a vault account raises a SessionError else: log.log_error("SessionError: Not logged in or registered with bank") raise SessionError('Not logged in or registered with bank') @requires_int @requires_positive def withdraw(self, cash): """ Withdraw cash from balance May raise SessionError if you are not logged in or registered with bank May raise OverdrawError if attempt is made to withdraw more money than is in the account """ # Checks if we are logged into an account in the vault if self.client in self.client_dao.get_clients(): # Checks and raises a OverdrawError if client attempts to withdraw # more money than is in the account if cash > self.client.get_balance(): log.log_error("OverdrawError: Attempted to withdraw more than user's balance") raise OverdrawError("Attempted to withdraw more than user's balance") # Performs withdraw else: # Withdraws from the client account and sets it self.client.set_balance(self.client.get_balance() - cash) # Adds to transaction history record = datetime.now().strftime(datetime_format) record += ": Withdrawal: $" + str(cash) self.client.get_transactions().append(record) # Updates the client self.client_dao.update_client(self.client) log.log_debug("${0} withdrawn".format(str(cash))) # If we are not logged into a vault account raises a SessionError else: log.log_error("SessionError: Not logged in or registered with bank") raise SessionError('Not logged in or registered with bank') def view_transactions(self): """ Pulls up client transaction history May raise SessionError if we are not logged in """ # Checks that we are logged in if self.client in self.client_dao.get_clients(): log.log_debug("Pulled client transactions") return self.client.get_transactions() # Raises error if we are not logged in else: log.log_error("SessionError: Not logged in or registered with bank") raise SessionError('Not logged in or registered with bank')
def test_balance_init_negative(self): with self.assertRaises(NegativeIntegerError): client = Client(balance=-1000)
def test_balance_init_not_int(self): with self.assertRaises(IntegerRequiredError): client = Client(balance="1000")
def test_password_init_length(self): with self.assertRaises(SecureStringError): client = Client(password="******")
def test_deposit(self): self.bank_session.register(Client('username', 'password', 10000)) self.bank_session.deposit(5000) self.assertEqual(self.bank_session.view_balance(), 15000)
def setUp(self): self.client = Client(username="******", password="******", balance=1000, transactions=['transaction'])
def test_withdraw(self): self.bank_session.register(Client('username', 'password', 10000)) self.bank_session.withdraw(5000) self.assertEqual(self.bank_session.view_balance(), 5000)
def test_username_init_length(self): with self.assertRaises(SecureStringError): client = Client(username="******")
class TestClientMethods(unittest.TestCase): def setUp(self): self.client = Client(username="******", password="******", balance=1000, transactions=['transaction']) def test_get_username(self): self.assertEqual(self.client.get_username(), 'username') def test_get_password(self): self.assertEqual(self.client.get_password(), "password") def test_get_balance(self): self.assertEqual(self.client.get_balance(), 1000) def test_get_transactions(self): self.assertEqual(self.client.get_transactions(), ['transaction']) def test_set_username(self): self.client.set_username("emanresu") self.assertEqual(self.client._Client__username, 'emanresu') def test_set_username_space(self): with self.assertRaises(InvalidCharacterError): self.client.set_username('user name') def test_set_username_length(self): with self.assertRaises(SecureStringError): self.client.set_username('user') def test_set_password(self): self.client.set_password("drowssap") self.assertEqual(self.client._Client__password, "drowssap") def test_set_password_space(self): with self.assertRaises(InvalidCharacterError): self.client.set_password('pass word') def test_set_password_length(self): with self.assertRaises(SecureStringError): self.client.set_password('pass') def test_set_balance(self): self.client.set_balance(2000) self.assertEqual(self.client._Client__balance, 2000) def test_set_balance_not_int(self): with self.assertRaises(IntegerRequiredError): self.client.set_balance('1000') def test_set_balance_negative(self): with self.assertRaises(NegativeIntegerError): self.client.set_balance(-1000) def test_set_transactions(self): self.client.set_transactions(['transaction', 'noitcasnart']) self.assertEqual(self.client._Client__transactions, ['transaction', 'noitcasnart']) def test_equality(self): client1 = Client('username') client2 = Client('username', 'password') self.assertEqual(client1, client2) def test_inequality(self): client1 = Client('username') client2 = Client('zamerman') self.assertNotEqual(client1, client2)
def test_view_transactions(self): self.bank_session.register(Client('username', 'password', 10000)) self.assertEqual(self.bank_session.view_transactions(), self.bank_session.client.get_transactions())
def test_equality(self): client1 = Client('username') client2 = Client('username', 'password') self.assertEqual(client1, client2)
def test_init(self): self.assertEqual(self.bank_session.client, Client()) self.assertEqual(self.bank_session.client_dao, self.client_dao)
def test_add_client(self): self.client_dao.add_client(Client()) self.assertEqual(self.client_dao.get_clients(), [Client()])
def test_register(self): self.bank_session.register(Client()) self.assertEqual(self.bank_session.client, Client()) self.assertEqual(self.client_dao.get_clients(), [Client()])
def test_register_fail(self): self.bank_session.register(Client()) with self.assertRaises(ClientSetupError): self.bank_session.register(Client())
def test_login(self): self.client_dao.add_client(Client('username', 'password')) self.bank_session.login(Client('username', 'password')) self.assertEqual(self.bank_session.current_session(), 'username')
def test_clear_clients(self): self.client_dao.add_client(Client('username', 'password')) self.client_dao.add_client(Client('username1', 'password')) self.client_dao.clear_clients() self.assertEqual(self.client_dao.get_clients(), [])
def setUp(self): self.client = Client()