From f61f68a5c2f790b5740c52483345866679293b30 Mon Sep 17 00:00:00 2001 From: Kevin Fronczak Date: Thu, 18 Oct 2018 12:23:23 -0400 Subject: [PATCH] Fix reauthorization bug --- .gitignore | 1 + blinkpy/blinkpy.py | 5 ++--- blinkpy/helpers/util.py | 25 ++++++++++++------------- tests/test_blinkpy.py | 7 ++++++- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 0bd45aa..9a182cc 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ dist/* .sh build/* docs/_build +*.log diff --git a/blinkpy/blinkpy.py b/blinkpy/blinkpy.py index e5fe32c..7124f1a 100644 --- a/blinkpy/blinkpy.py +++ b/blinkpy/blinkpy.py @@ -21,7 +21,7 @@ from blinkpy.helpers.util import ( create_session, BlinkURLHandler, BlinkAuthenticationException) from blinkpy.helpers.constants import ( - BLINK_URL, LOGIN_URL, LOGIN_BACKUP_URL) + BLINK_URL, LOGIN_URL, LOGIN_BACKUP_URL, PROJECT_URL) REFRESH_RATE = 30 @@ -146,8 +146,7 @@ class Blink(): if all_networks: _LOGGER.error(("More than one unboarded network. " "Platform may not work as intended. " - "Please open an issue on " - "https://github.com/fronzbot/blinkpy.")) + "Please open an issue on %s"), PROJECT_URL) def refresh(self, force_cache=False): """ diff --git a/blinkpy/helpers/util.py b/blinkpy/helpers/util.py index 23fb58d..6a8af07 100644 --- a/blinkpy/helpers/util.py +++ b/blinkpy/helpers/util.py @@ -2,7 +2,7 @@ import logging from requests import Request, Session, exceptions -from blinkpy.helpers.constants import BLINK_URL +from blinkpy.helpers.constants import BLINK_URL, PROJECT_URL import blinkpy.helpers.errors as ERROR @@ -17,7 +17,7 @@ def create_session(): def attempt_reauthorization(blink): """Attempt to refresh auth token and links.""" - _LOGGER.debug("Auth token expired, attempting reauthorization.") + _LOGGER.info("Auth token expired, attempting reauthorization.") headers = blink.get_auth_token() return headers @@ -48,21 +48,20 @@ def http_req(blink, url='http://example.com', data=None, headers=None, try: response = blink.session.send(prepped, stream=stream) + if json_resp and 'code' in response.json(): + if is_retry: + _LOGGER.error(("Cannot obtain new token for server auth. " + "Please report this issue on %s"), PROJECT_URL) + return None + else: + headers = attempt_reauthorization(blink) + return http_req(blink, url=url, data=data, headers=headers, + reqtype=reqtype, stream=stream, + json_resp=json_resp, is_retry=True) except (exceptions.ConnectionError, exceptions.Timeout): _LOGGER.error("Cannot connect to server. Possible outage.") return None - if json_resp and 'code' in response.json(): - if is_retry: - _LOGGER.error("Cannot authenticate with server.") - raise BlinkAuthenticationException( - (response.json()['code'], response.json()['message'])) - else: - headers = attempt_reauthorization(blink) - return http_req(blink, url=url, data=data, headers=headers, - reqtype=reqtype, stream=stream, - json_resp=json_resp, is_retry=True) - if json_resp: return response.json() diff --git a/tests/test_blinkpy.py b/tests/test_blinkpy.py index 0f5dadd..577d177 100644 --- a/tests/test_blinkpy.py +++ b/tests/test_blinkpy.py @@ -14,6 +14,7 @@ from blinkpy.sync_module import BlinkSyncModule from blinkpy.helpers.util import ( http_req, create_session, BlinkAuthenticationException, BlinkException, BlinkURLHandler) +from blinkpy.helpers.constants import PROJECT_URL import tests.mock_responses as mresp USERNAME = 'foobar' @@ -77,11 +78,15 @@ class TestBlinkSetup(unittest.TestCase): def test_bad_request(self, mock_sess): """Check that we raise an Exception with a bad request.""" self.blink.session = create_session() + explog = ("ERROR:blinkpy.helpers.util:" + "Cannot obtain new token for server auth. " + "Please report this issue on {}").format(PROJECT_URL) with self.assertRaises(BlinkException): http_req(self.blink, reqtype='bad') - with self.assertRaises(BlinkAuthenticationException): + with self.assertLogs() as logrecord: http_req(self.blink, reqtype='post', is_retry=True) + self.assertEqual(logrecord.output, [explog]) def test_authentication(self, mock_sess): """Check that we can authenticate Blink up properly."""