def positions_sub_menu(pk): retrieve_bal = Account(pk=pk) views.generic_msg("Your current balance = {}".format( retrieve_bal.get_account().balance)) while True: position_choice = views.position_menu() if position_choice is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option") elif position_choice == 3: #Exit break elif position_choice == 1: #Retrieve and display a given position ticker = views.get_input("Please enter a Ticker Symbol") user_position = Account(pk=pk) position = user_position.get_position_for(ticker) valuation = Position() getval = valuation.current_value(ticker, position.shares) views.show_positions(position, getval) elif position_choice == 2: #Retrieve and display all positions user_positions = Account(pk=pk) positions = user_positions.get_positions() for position in positions: valuation = Position() getval = valuation.current_value(position.ticker, position.shares) views.show_positions(position, getval)
def test_add_account(self): bank = Bank() account_1 = Account("001", 50) account_2 = Account("002", 100) bank.add_account(account_1) bank.add_account(account_2) self.assertEqual(len(bank.accounts), 2)
def setUp(self): if os.path.exists(Account.filepath): os.unlink(Account.filepath) greg = Account("Greg") greg.balance = 10.50 greg.pin = "1234" greg.save()
def welcome_homepage(): while True: selection = view.welcome_screen() if selection not in ["1","2","3"]: view.improper_selection() continue if selection == "1": username, balance, password, confirm_password = view.get_username(), view.add_balance(), view.get_password(), view.confirm_password() if password != confirm_password: view.improper_password() continue if not balance.isdigit() or int(balance) < 0: view.improper_balance() continue account = Account(username = username, balance = balance) hashed_pw = Account.set_password(account, password) account.save() logged_in_homepage(account) return elif selection == "2": username, password = view.get_username(), view.get_password() logged_in_account = Account.login(username=username, password=password) if logged_in_account: logged_in_homepage(logged_in_account) return else: print("Invalid credentials supplied") continue elif selection == "3": view.goodbye() return
def test_account_conn_exception(self): mock_data_interface = Mock() mock_data_interface.get.side_effect = ConnectionError() # mock_data_interface.wibble_list = ['wibble', 'wobble'] account = Account(mock_data_interface) self.assertEqual("Connection error occurred. Try Again.", account.get_account(1))
def login_menu(): while True: try: user_input = view.login_menu() if int(user_input) == 3: view.program_end() sys.exit() elif int(user_input) == 2: username = view.username_inpt() pwd = util.hash_pass(view.password_inpt()) user = Account.login(username, pwd) if user == None: view.invalid_info() return login_menu() return user elif int(user_input) == 1: user = Account() user.username = view.username_inpt() user.set_password(util.hash_pass(view.set_password_inpt())) user.balance = view.deposit_inpt() user.save() view.acc_created(user.username) return user except ValueError: view.choose_valid()
def test_init_account(self): # Given # When account = Account() # Then assert account.balance == Decimal(0)
def createaccount(name, password): new_account = Account(username=name) new_account.set_password(password) ak = new_account.create_api_key() new_account.save() msg = "Account successfully created, API api_key = {}".format(ak) return jsonify({'message':msg})
def test_init_account_with_balance(self): # Given balance = Decimal(10) # When account = Account(balance) # Then assert account.balance == Decimal(10)
def setUp(self): """ Hook method for setting up the test fixture before exercising it. """ self.account_data = {"id": "1", "name": "test"} mock_data_interface = Mock() mock_data_interface.get_id.return_value = '1' mock_data_interface.get_name.return_value = 'test' mock_data_interface.get.side_effect = AccountException() self.account = Account(mock_data_interface)
def viewapikey(name, password): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk retrieve_ak = Account(pk=pk) msg = "Your API Key = {}".format(retrieve_ak.get_account().api_key) return jsonify({'message': msg})
def test_admin_fld(self): tester = Account(first='Test', last='Ad', username='******', password_hash='temp', balance=0, email='*****@*****.**') second = Account(first='Test', last='Ad', username='******', password_hash='temp', balance=0, email='*****@*****.**') tester.save() self.assertEqual(tester.admin, 1, "username admin should make admin field 1 for true") self.assertEqual(second.admin, 0, "this should not be admin")
def balance(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: pk = Account.api_authenticate(api_key).pk retrieve_bal = Account(pk=pk) msg = "Your current balance = {}".format(retrieve_bal.get_account().balance) return jsonify({'message':msg})
def test_get_account_balance(self): bank = Bank() account_1 = Account("001", 50) bank.add_account(account_1) self.assertEqual(bank.get_account_balance("001"), 50)
def test_client_with_account(self): # Given name = "John Doe" account = Account(Decimal(10)) # When client = Client(name, account) # Then assert client.account is account
def test_withdraw(self): # Given account = Account(Decimal(100)) amount = Decimal(10) # When account.withdraw(amount) # Then assert account.balance == Decimal(90)
def buy(key, ticker, shares): if Account.api_authenticate(key) == None: msg = "Invalid login credentials, pls retry" else: #pk = Account.login(name, password).pk pk = Account.api_authenticate(key).pk buy_txn = Account(pk=pk) msg = buy_txn.buy(ticker, shares) return jsonify({'message': msg})
def viewbalance(name, password): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk retrieve_bal = Account(pk=pk) msg = "Your current balance = {}".format( retrieve_bal.get_account().balance) return jsonify({'message': msg})
def create(): if not request.json or 'username' not in request.json or 'password_hash' not in request.json: return jsonify(BAD_REQUEST), 401 account = Account(username=request.json['username'], password_hash=request.json['password_hash']) account.save() token = encodeAuthToken(account.pk) return jsonify({'status': 'success', 'auth_token': str(token)})
def test_current_balance(self, mock_requests): # mock_the thing we patched mock_response = Mock() mock_response.status_code = 200 mock_response.text = 'any old text' mock_requests.get.return_value = mock_response account = Account(Mock()) self.assertEqual({ 'status': 200, 'data': 'any old text' }, account.get_current_balance('1'))
def trades_sub_menu(pk): while True: trade_choice = views.trades_menu() if trade_choice is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option") elif trade_choice == 3: #Exit break elif trade_choice == 1: #Retrieve and display trades re a given ticker ticker = views.get_input("Please enter a Ticker Symbol") user_trades = Account(pk=pk) trades = user_trades.get_trades_for(ticker) for trade in trades: views.show_trades(trade) elif trade_choice == 2: #Retrieve and display all trades user_trades = Account(pk=pk) trades = user_trades.get_trades() for trade in trades: views.show_trades(trade)
def sell(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: if not request.json or 'ticker' not in request.json or 'volume' not in request.json: return jsonify({"error": "bad request"}), 400 pk = Account.api_authenticate(api_key).pk sell_txn = Account(pk=pk) msg = sell_txn.sell(request.json['ticker'], request.json['volume']) return jsonify({'message':msg})
def test_get_current_balance_returns_data_correctly(self, mock_requests): mock_response = Mock() mock_response.status_code = 200 mock_response.text = 'Some text data' mock_requests.get.return_value = mock_response account = Account(Mock()) self.assertEqual({ 'status': 200, 'data': 'Some text data' }, account.get_current_balance('1'))
def sell(name, password, ticker, shares): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk sell_txn = Account(pk=pk) # sell_txn.sell(ticker, shares) # msg = "Sell transaction complete" msg = sell_txn.sell(ticker, shares) return jsonify({'message': msg})
def buy(name, password, ticker, shares): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk buy_txn = Account(pk=pk) # buy_txn.buy(ticker, shares) # msg = "Buy transaction complete" msg = buy_txn.buy(ticker, shares) return jsonify({'message': msg})
def deposit(name, password, amount): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk account_deposit = Account(pk=pk) new_bal = account_deposit.deposit(float(amount)) account_deposit.save() msg = "New Balance = {}".format(new_bal) return jsonify({'message': msg})
def positions(api_key, ticker): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: pk = Account.api_authenticate(api_key).pk user_position = Account(pk=pk) position = user_position.get_position_for(ticker) valuation = Position() getval = valuation.current_value(ticker, position.shares) msg = "Ticker Symbol: {}, Shares: {}, Valuation: ${}".format(position.ticker, position.shares, getval) return jsonify({'message':msg})
def account_create(): username, password, f_name, l_name, deposit = view.create_account( ) #takes all return values from create_account crypted_password = crypt_password(password) new_account = Account(username=username, crypted_password=crypted_password, f_name=f_name, l_name=l_name, balance=deposit) new_account.save()
def test_user_can_withdraw_amount_not_in_account(self): # Arrange account = Account("Alan", "33") account.balance = 100 expected_result = "Sorry insufficent funds" # Act observed = account.withdraw(101) # Assert self.assertEqual(observed, expected_result)
def test_user_can_deposit(self): # Arrange account = Account("Alan", "33") expected_result = 100 # Act account.deposit(100) observed = account.balance # Assert self.assertEqual(observed, expected_result)