def post(self, request): username = str(request.data.get('username')) password = str(request.data.get('password')) try: # start and acquire leap card session session = LeapSession() session.try_login(username, password) # obtain json response from leapcard.ie content = card_info(session) except Exception as e: logger.error('exception in leap_card view LeapCardGetUserInfo object') logger.exception(e) return Response({"Something went wrong! Enter valid credentials for active student leap card account"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Create JSON response payload = [content] # print(payload) try: return JsonResponse(payload, safe=False) except Exception as e: logger.error('exception in leap_card view while returning JSON response') logger.exception(e) return Response({"Something went wrong! Please try again later"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_handle_login_response_good_response_returns_true(self): session = LeapSession() with (open(sampledatadir + "login_result.html", "rb")) as f: good_login = f.read() loginOk = session._LeapSession__handle_login_response( good_login, 'usernam') self.assertTrue(loginOk)
def get_leap_balance(username, password): try: session = LeapSession() session.try_login(username, password) overview = session.get_card_overview() leap_balance = overview.balance # print(leap_balance) return leap_balance except IOError: return False
def test_handle_login_response_good_response_returns_true(self): session = LeapSession() with (open(pickledir + "login_result.dat", "rb")) as f: good_login = pickle.load(f) loginOk = session._LeapSession__handle_login_response( good_login, 'usernam') self.assertTrue(loginOk)
def test_handle_login_response_wrong_credentials_throws(self): session = LeapSession() with (open(sampledatadir + "failed_login_result.html", "rb")) as f: failed_login = f.read() with self.assertRaises(IOError) as context: session._LeapSession__handle_login_response( failed_login, 'usernam') self.assertTrue( 'Your credentials are incorrect' in str(context.exception))
def test_calls_overview_returns_expected(self): session = LeapSession() with (open(sampledatadir + "overview_page.html", "r")) as f: page = f.read() result = session._LeapSession__handle_card_overview_response(page) expected = CardOverview(u"1000000000", u"User's Card", 25.02, u"Adult", u"Unblocked", u"Unblocked", u"Not Enabled", u"02/04/2012 12:00:00 AM", u"09/12/2023 12:00:00 AM") self.assertEqual(result.__dict__, expected.__dict__)
def run(): login_ok = False login_error = "" session = LeapSession() try: login_ok = session.try_login(leap_user, leap_pass) except Exception: login_error = sys.exc_info()[0] state = StateMgmt() formatter = ResultsFormatter() if login_ok: card = session.get_card_overview() events = session.get_events() state.dump_state(card, events) formatter.print_output(card, events, True, session.login_url()) else: loaded_state = state.load_state() if loaded_state is not None: formatter.print_output(loaded_state[0], loaded_state[1], False, session.login_url()) else: formatter.print_error_message(login_error, session.login_url())
def test_calls_events_system_error_throws(self): session = LeapSession() with (open(sampledatadir + "journeys_page_invalid_account.html", "r")) as f: page = f.read() with self.assertRaises(Exception) as context: session._LeapSession__handle_events_response(page) expected = Exception( u"System Error", u"The application experienced unexpected problems completing your request (Code E002). We're sorry for the inconvenience. Please try again later." ) self.assertEqual(str(context.exception), str(expected))
def run(): login_ok= False login_error = "" session = LeapSession() try: login_ok = session.try_login(leap_user, leap_pass) except Exception: login_error = sys.exc_info()[0] state = StateMgmt() formatter = ResultsFormatter() if login_ok: card = session.get_card_overview() events = session.get_events() state.dump_state(card,events) formatter.print_output(card,events,True, session.login_url()) else: loaded_state = state.load_state() if loaded_state is not None: formatter.print_output(loaded_state[0],loaded_state[1],False, session.login_url()) else: formatter.print_error_message(login_error, session.login_url())
def test_calls_events_returns_expected(self): session = LeapSession() with (open(sampledatadir + "journeys_page.html", "r")) as f: page = f.read() result = session._LeapSession__handle_events_response(page) resultStr = "" for item in result: resultStr += str(item.__dict__) expected = "" if sys.version_info[0] < 3: expected = "{'event_type': u'Travel Credit Deduction', 'price': -1.96, 'time': u'6:02 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'11/02/2020'}{'event_type': u'Travel Credit Deduction', 'price': -1.96, 'time': u'8:30 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'08/02/2020'}{'event_type': u'Travel Credit Top-Up', 'price': 20.0, 'time': u'12:50 PM', 'was_topup': True, 'provider': u'Leap Top-Up App', 'date': u'08/02/2020'}{'event_type': u'Travel Credit Deduction', 'price': -1.96, 'time': u'6:22 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'05/07/2019'}" else: expected = "{'date': '11/02/2020', 'time': '6:02 PM', 'provider': 'Bus Eireann', 'price': -1.96, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '08/02/2020', 'time': '8:30 PM', 'provider': 'Bus Eireann', 'price': -1.96, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '08/02/2020', 'time': '12:50 PM', 'provider': 'Leap Top-Up App', 'price': 20.0, 'event_type': 'Travel Credit Top-Up', 'was_topup': True}{'date': '05/07/2019', 'time': '6:22 PM', 'provider': 'Bus Eireann', 'price': -1.96, 'event_type': 'Travel Credit Deduction', 'was_topup': False}" self.assertEqual(resultStr, expected)
def test_calls_events_returns_expected(self): session = LeapSession() with (open(pickledir + "journeys_page.dat", "rb")) as f: page = pickle.load(f) result = session._LeapSession__handle_events_response(page) resultStr = "" for item in result: resultStr += str(item.__dict__) expected = "" if sys.version_info[0] < 3: expected = "{'event_type': u'Travel Credit Deduction', 'price': -2.64, 'time': u'9:11 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'17/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -2.64, 'time': u'5:37 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'17/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -3.52, 'time': u'10:28 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'15/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -3.52, 'time': u'12:46 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'15/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -3.52, 'time': u'6:47 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'09/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -3.52, 'time': u'5:40 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'08/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -3.52, 'time': u'4:18 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'06/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -3.52, 'time': u'11:45 AM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'06/09/2017'}{'event_type': u'Travel Credit Top-Up', 'price': 50.0, 'time': u'10:00 AM', 'was_topup': True, 'provider': u'Leap Top-Up App', 'date': u'06/09/2017'}{'event_type': u'Travel Credit Deduction', 'price': -3.52, 'time': u'12:16 PM', 'was_topup': False, 'provider': u'Bus Eireann', 'date': u'02/09/2017'}" else: expected = "{'date': '17/09/2017', 'time': '9:11 PM', 'provider': 'Bus Eireann', 'price': -2.64, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '17/09/2017', 'time': '5:37 PM', 'provider': 'Bus Eireann', 'price': -2.64, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '15/09/2017', 'time': '10:28 PM', 'provider': 'Bus Eireann', 'price': -3.52, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '15/09/2017', 'time': '12:46 PM', 'provider': 'Bus Eireann', 'price': -3.52, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '09/09/2017', 'time': '6:47 PM', 'provider': 'Bus Eireann', 'price': -3.52, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '08/09/2017', 'time': '5:40 PM', 'provider': 'Bus Eireann', 'price': -3.52, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '06/09/2017', 'time': '4:18 PM', 'provider': 'Bus Eireann', 'price': -3.52, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '06/09/2017', 'time': '11:45 AM', 'provider': 'Bus Eireann', 'price': -3.52, 'event_type': 'Travel Credit Deduction', 'was_topup': False}{'date': '06/09/2017', 'time': '10:00 AM', 'provider': 'Leap Top-Up App', 'price': 50.0, 'event_type': 'Travel Credit Top-Up', 'was_topup': True}{'date': '02/09/2017', 'time': '12:16 PM', 'provider': 'Bus Eireann', 'price': -3.52, 'event_type': 'Travel Credit Deduction', 'was_topup': False}" self.assertEqual(resultStr, expected)