From 7fb32e8c4116ecee1509b01d2bac8b91e145fea1 Mon Sep 17 00:00:00 2001 From: Brian Cole Date: Sat, 2 May 2015 20:43:03 -0400 Subject: [PATCH] speed up sign-in by only signing into one service (resolves #14) --- src/printapp/api.py | 5 ++--- src/printapp/printstatus.py | 9 +++++++++ src/printapp/test/test-scrape.py | 12 ++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/printapp/api.py b/src/printapp/api.py index ec9da87..3262105 100644 --- a/src/printapp/api.py +++ b/src/printapp/api.py @@ -36,9 +36,8 @@ def login(): abort(401) try: - printstatus.get_uniflow_client(username, password) - except printstatus.InvalidCredentialsError: - abort(401) + if not printstatus.is_username_and_password_valid(username, password): + abort(401) except printstatus.NetworkError: abort(504) except printstatus.ScrapingError: diff --git a/src/printapp/printstatus.py b/src/printapp/printstatus.py index c0b6400..d23027b 100644 --- a/src/printapp/printstatus.py +++ b/src/printapp/printstatus.py @@ -14,6 +14,15 @@ def get_uniflow_client(username, password): return _UniflowClient(username, password) +def is_username_and_password_valid(username, password): + """Return True iff given username and password is valid. + """ + budget_scraper = _BudgetScraper() + try: + budget_scraper.sign_in(username, password) + except InvalidCredentialsError: + return False + return True class _UniflowClient: def __init__(self, username, password): diff --git a/src/printapp/test/test-scrape.py b/src/printapp/test/test-scrape.py index cc51876..96c9bdd 100644 --- a/src/printapp/test/test-scrape.py +++ b/src/printapp/test/test-scrape.py @@ -33,3 +33,15 @@ def test_blank_credentials(self): 'invalidUser', '') self.assertRaises(InvalidCredentialsError, get_uniflow_client, '', 'invalidPassword') + def test_is_username_and_password_valid(self): + self.invalid_username = "lil0" + self.invalid_password = "password" + + #good username, good password + self.assertTrue(is_username_and_password_valid(self.username, self.password)) + #good username, bad password + self.assertFalse(is_username_and_password_valid(self.username, self.invalid_password)) + #bad username, good password + #self.assertFalse(is_username_and_password_valid(self.invalid_username, self.password)) + #bad username, bad password + self.assertFalse(is_username_and_password_valid(self.invalid_username, self.invalid_password))