def test_viewDetails_success(self): """An authenticated user can view their user account details""" expected_response_code = 200 displayName, email, password, level = ("John Doe", "*****@*****.**", "12345678", "Investor") number_of_accounts, number_of_watchlists = (1, 1) registration_response = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) viewdetails_response = ApiFacade.view_details(authentication_response.get_token()) deletion_response = ApiFacade.delete_user(authentication_response.get_token()) self.assertEqual(viewdetails_response.get_http_status(), expected_response_code, msg = "Expected HTTP{0}; got HTTP{1}".format(expected_response_code, viewdetails_response.get_http_status())) self.assertEqual(viewdetails_response.get_displayName(), displayName, msg = "Expected displayName = {0}; got displayName = {1}".format(displayName, viewdetails_response.get_displayName())) self.assertEqual(viewdetails_response.get_email(), email, msg = "Expected email = {0}; got email = {1}".format(email, viewdetails_response.get_email())) self.assertEqual(viewdetails_response.get_level(), level, msg = "Expected level = {0}; got level = {1}".format(level, viewdetails_response.get_level())) self.assertEqual(len(viewdetails_response.get_accounts()), number_of_accounts, msg = "Expected len(accounts) = {0}; got len(accounts) = {1}" .format(number_of_accounts, len(viewdetails_response.get_accounts()))) self.assertEqual(len(viewdetails_response.get_watchlists()), number_of_watchlists, msg = "Expected len(watchlists) = {0}; got len(watchlists) = {1}" .format(number_of_watchlists, len(viewdetails_response.get_watchlists())))
def test_buy_shares_success(self): """A user can buy shares if balance is sufficient""" expected_response_code = 200 symbol_to_buy = "ANZ" quantity = 100 displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) token = authentication_response.get_token() # get account id viewdetails_response = ApiFacade.view_details(token) account_id = viewdetails_response.get_main_account_id() buyshare_response = ApiFacade.buy_share(token, account_id, symbol_to_buy, quantity) deletion_response = ApiFacade.delete_user(token) self.assertEqual(buyshare_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, buyshare_response.get_http_status()))
def test_get_watchlist_success(self): expected_response_code = 200 expected_keys_in_share = [ "symbol", "name", "lastPrice", "change", "changePercent" ] displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) token = authentication_response.get_token() # get watchlist id viewdetails_response = ApiFacade.view_details(token) watchlist_id = viewdetails_response.get_main_watchlist_id() # buy shares first viewwatchlist_response = ApiFacade.get_watchlist(token, watchlist_id) deletion_response = ApiFacade.delete_user(token) self.assertEqual(viewwatchlist_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, viewwatchlist_response.get_http_status())) for share in viewwatchlist_response.get_all_shares(): for k in expected_keys_in_share: self.assertIsNotNone( share[k], msg="Expected value for key [{0}]; got [{1}]".format( str(k), str(None)))
def test_sell_shares_success(self): """An authenticated user can sell shares that they own""" expected_response_code = 200 symbol = "DDD" quantity = 100 displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) token = authentication_response.get_token() # get account id viewdetails_response = ApiFacade.view_details(token) account_id = viewdetails_response.get_main_account_id() # buy shares first buyshare_response = ApiFacade.buy_share(token, account_id, symbol, int(quantity)) # sell the shares sellshare_response = ApiFacade.sell_share(token, account_id, symbol, int(quantity / 3)) deletion_response = ApiFacade.delete_user(token) self.assertEqual(sellshare_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, sellshare_response.get_http_status()))
def test_view_portfolio_success(self): """An authenticated user can sell shares that they own""" expected_response_code = 200 expected_keys = ["id", "name", "balance", "positions"] symbol = "DDD" quantity = 100 displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) token = authentication_response.get_token() # get account id viewdetails_response = ApiFacade.view_details(token) account_id = viewdetails_response.get_main_account_id() # buy shares first portfolio_response = ApiFacade.get_portfolio(token, account_id) deletion_response = ApiFacade.delete_user(token) self.assertEqual(portfolio_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, portfolio_response.get_http_status())) for k in expected_keys: self.assertEqual(k in portfolio_response.get_json_body(), True, msg="Expected key [{0}]; got [{1}]".format( k, str(None)))
def test_view_transactions_success(self): """An authenticated user can sell shares that they own""" expected_response_code = 200 expected_keys = [ "items", "pageNumber", "pageSize", "totalPageCount", "totalRowCount" ] symbol = "DDD" quantity = 100 displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) token = authentication_response.get_token() # get account id viewdetails_response = ApiFacade.view_details(token) account_id = viewdetails_response.get_main_account_id() # buy a share buyshare_response = ApiFacade.buy_share(token, account_id, symbol, quantity) # buy shares first transactions_response = ApiFacade.get_transactions(token, account_id, page_number=None, page_size=None, start_date=None, end_date=None) deletion_response = ApiFacade.delete_user(token) self.assertEqual(transactions_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, transactions_response.get_http_status())) for k in expected_keys: self.assertEqual(k in transactions_response.get_json_body(), True, msg="Expected key [{0}]; got [{1}]".format( k, str(None))) #pprint(transactions_response.get_items()) self.assertEqual( len(transactions_response.get_items()), 4, msg="Expected {0} item in response; got {1} instead".format( 1, len(transactions_response.get_items())))
def test_deletion_userIsNotAuthenticated(self): """An unauthenticated user cannot delete their user account""" expected_response_code = 401 displayName, email, password = (None, None, None) registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) deletion_response = ApiFacade.delete_user( authentication_response.get_token()) self.assertEqual(deletion_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, deletion_response.get_http_status()))
def test_deletion_success(self): """An authenticated user can delete their user account""" expected_response_code = 204 displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) deletion_response = ApiFacade.delete_user( authentication_response.get_token()) self.assertEqual(deletion_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, deletion_response.get_http_status()))
def test_get_historical_prices_success(self, symbol): expected_response_code = 200 expected_keys = [ "marketCap", "previousClose", "exDividendDate", "bookValue", "priceBook", "peRatio", "earningsShare", "trailingEps", "forwardEps", "beta", "change52Weeks", "targetHighPrice", "targetLowPrice", "targetMeanPrice", "targetMedianPrice", "analystRecommendation", "numberOfAnalystOpinions", "totalRevenue", "earningsGrowth", "revenueGrowth", "low52Weeks", "high52Weeks", "movingAverage200Days", "movingAverage50Days", "averageDailyVolume", "symbol", "name", "industry" ] displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) fundamentals_response = ApiFacade.get_fundamentals(authentication_response.get_token(), symbol) deletion_response = ApiFacade.delete_user(authentication_response.get_token()) self.assertEqual(fundamentals_response.get_http_status(), expected_response_code, msg = "Expected HTTP{0}; got HTTP{1}".format(expected_response_code, fundamentals_response.get_http_status())) for k in fundamentals_response.get_json_body(): self.assertEqual(k in expected_keys, True, msg = "Expected key [{0}]; got [{1}]".format(k, str(None)))
def test_registration_displayNameIsNotAlphaNumeric(self, displayName, email, password): """A new user cannot register with a non-alphanumeric displayName""" expected_messages = ["""The field DisplayName must match the regular expression '^[^~`^$#@%!'*\\(\\)<>=.;:]+$'."""] expected_response_code = 400 response_wrapper = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) deletion_response = ApiFacade.delete_user(authentication_response.get_token()) response_status_match = response_wrapper.get_http_status() == expected_response_code response_body_match = response_wrapper.get_message() in expected_messages self.assertEqual(response_status_match, True, msg = "Expected HTTP{0}; got HTTP{1}; on data [{2}][{3}][{4}]" .format(expected_response_code, response_wrapper.get_http_status(), displayName, email, password)) self.assertEqual(response_body_match, True, msg = "Expected [{0}]; got [{1}]".format(expected_messages, response_wrapper.get_message()))
def test_registration_success(self, displayName, email, password): """A new user can register with valid details""" expected_messages = [None] expected_response_code = 201 response_wrapper = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) deletion_response = ApiFacade.delete_user(authentication_response.get_token()) response_status_match = response_wrapper.get_http_status() == expected_response_code response_body_match = response_wrapper.get_message() in expected_messages self.assertEqual(response_status_match, True, msg = "Expected HTTP{0}; got HTTP{1}; on data [{2}][{3}][{4}]" .format(expected_response_code, response_wrapper.get_http_status(), displayName, email, password)) self.assertEqual(response_body_match, True, msg = "Expected [{0}]; got [{1}]".format(expected_messages, response_wrapper.get_message()))
def test_registration_passwordIsEmpty(self, displayName, email, password): """A new user cannot register with an empty password""" expected_messages = ["The Password field is required."] expected_response_code = 400 response_wrapper = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) deletion_response = ApiFacade.delete_user(authentication_response.get_token()) response_status_match = response_wrapper.get_http_status() == expected_response_code response_body_match = response_wrapper.get_message() in expected_messages self.assertEqual(response_status_match, True, msg = "Expected HTTP{0}; got HTTP{1}; on data [{2}][{3}][{4}]" .format(expected_response_code, response_wrapper.get_http_status(), displayName, email, password)) self.assertEqual(response_body_match, True, msg = "Expected [{0}]; got [{1}]".format(expected_messages, response_wrapper.get_message()))
def test_get_historical_prices_success(self, symbol): expected_response_code = 200 displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) dividends_response = ApiFacade.get_dividends( authentication_response.get_token(), symbol) deletion_response = ApiFacade.delete_user( authentication_response.get_token()) self.assertEqual(dividends_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, dividends_response.get_http_status()))
def test_view_leaderboard_success(self): expected_response_code = 200 expected_keys_in_leaderboard = [ "items", "pageNumber", "pageSize", "totalPageCount", "totalRowCount" ] expected_keys_in_leaderboard_items = [ "gravatarUrl", "rank", "displayName", "totalAccountValue", "profit", "profitPercent", "isCurrentUser" ] displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) leaderboard_response = ApiFacade.get_leaderboard( authentication_response.get_token()) deletion_response = ApiFacade.delete_user( authentication_response.get_token()) self.assertEqual(leaderboard_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, leaderboard_response.get_http_status())) for k in expected_keys_in_leaderboard: exists = k in leaderboard_response.get_json_body() self.assertEqual( exists, True, msg="Expected value for key [{0}]; got [{1}]".format(k, None)) for item in leaderboard_response.get_all_items(): for k in expected_keys_in_leaderboard_items: exists = k in item self.assertEqual( exists, True, msg="Expected value for key [{0}]; got [{1}]".format( k, None))
def test_registration_passwordIsTooShort(self, displayName, email, password): """A new user cannot register with a password of less than 8 characters""" expected_messages = [ "The field Password must be a string or array type with a minimum length of '8'.", "The Password field is required." ] expected_response_code = 400 response_wrapper = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) deletion_response = ApiFacade.delete_user(authentication_response.get_token()) response_status_match = response_wrapper.get_http_status() == expected_response_code response_body_match = response_wrapper.get_message() in expected_messages self.assertEqual(response_status_match, True, msg = "Expected HTTP{0}; got HTTP{1}; on data [{2}][{3}][{4}]" .format(expected_response_code, response_wrapper.get_http_status(), displayName, email, password)) self.assertEqual(response_body_match, True, msg = "Expected [{0}]; got [{1}]".format(expected_messages, response_wrapper.get_message()))
def test_registration_emailIsTooLong(self, displayName, email, password): """A new user cannot register with an email of more than 100 characters""" expected_messages = [ "The field Email must be a string or array type with a maximum length of '100'.", "The email address is invalid." ] expected_response_code = 400 response_wrapper = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) deletion_response = ApiFacade.delete_user(authentication_response.get_token()) response_status_match = response_wrapper.get_http_status() == expected_response_code response_body_match = response_wrapper.get_message() in expected_messages self.assertEqual(response_status_match, True, msg = "Expected HTTP{0}; got HTTP{1}; on data [{2}][{3}][{4}]" .format(expected_response_code, response_wrapper.get_http_status(), displayName, email, password)) self.assertEqual(response_body_match, True, msg = "Expected [{0}]; got [{1}]".format(expected_messages, response_wrapper.get_message()))
def test_edit_user_success(self): """A user can edit their profile with valid details""" expected_messages = [None] expected_response_code = 204 displayName, email, password = ("John Doe", "*****@*****.**", "12345678") new_displayName, new_email = ("Edited John", "*****@*****.**") register_response = ApiFacade.register_user(displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) token = authentication_response.get_token() edituser_response = ApiFacade.edit_user(token, new_displayName, new_email) viewdetails_response = ApiFacade.view_details(token) deletion_response = ApiFacade.delete_user( authentication_response.get_token()) response_status_match = edituser_response.get_http_status( ) == expected_response_code self.assertEqual( response_status_match, True, msg="Expected HTTP{0}; got HTTP{1}; on data [{2}][{3}][{4}]". format(expected_response_code, edituser_response.get_http_status(), displayName, email, password)) self.assertEqual( viewdetails_response.get_email(), new_email, msg="Email not updated; expected {{0}}; got {{1}}".format( new_email, viewdetails_response.get_email())) self.assertEqual( viewdetails_response.get_displayName(), new_displayName, msg="Email not updated; expected {{0}}; got {{1}}".format( new_displayName, viewdetails_response.get_displayName()))
def test_get_multiple_quotes_success(self, symbols): expected_response_code = 200 no_of_quotes = len(symbols) expected_keys_in_quote = [ "symbol", "ask", "bid", "last", "lastSize", "change", "changePercent", "dayLow", "dayHigh" ] displayName, email, password = ("John Doe", "*****@*****.**", "12345678") registration_response = ApiFacade.register_user( displayName, email, password) authentication_response = ApiFacade.authenticate_user(email, password) currentquotes_response = ApiFacade.get_current_quotes( authentication_response.get_token(), symbols) deletion_response = ApiFacade.delete_user( authentication_response.get_token()) self.assertEqual(currentquotes_response.get_http_status(), expected_response_code, msg="Expected HTTP{0}; got HTTP{1}".format( expected_response_code, currentquotes_response.get_http_status())) self.assertEqual(len(currentquotes_response.get_all_quotes()), no_of_quotes, msg="Expected {0} quotes; got {1} quotes".format( no_of_quotes, len(currentquotes_response.get_all_quotes()))) for quote in currentquotes_response.get_all_quotes(): for k in expected_keys_in_quote: self.assertIsNotNone( quote[k], msg="Expected value for key [{0}]; got [{1}]".format( k, None))
def tearDownClass(cls): # delete test user deletion_response = ApiFacade.delete_user(cls.token)