Fix reauthorization bug

This commit is contained in:
Kevin Fronczak
2018-10-18 12:23:23 -04:00
parent 0781d79ca7
commit f61f68a5c2
4 changed files with 21 additions and 17 deletions
+1
View File
@@ -10,3 +10,4 @@ dist/*
.sh .sh
build/* build/*
docs/_build docs/_build
*.log
+2 -3
View File
@@ -21,7 +21,7 @@ from blinkpy.helpers.util import (
create_session, BlinkURLHandler, create_session, BlinkURLHandler,
BlinkAuthenticationException) BlinkAuthenticationException)
from blinkpy.helpers.constants import ( from blinkpy.helpers.constants import (
BLINK_URL, LOGIN_URL, LOGIN_BACKUP_URL) BLINK_URL, LOGIN_URL, LOGIN_BACKUP_URL, PROJECT_URL)
REFRESH_RATE = 30 REFRESH_RATE = 30
@@ -146,8 +146,7 @@ class Blink():
if all_networks: if all_networks:
_LOGGER.error(("More than one unboarded network. " _LOGGER.error(("More than one unboarded network. "
"Platform may not work as intended. " "Platform may not work as intended. "
"Please open an issue on " "Please open an issue on %s"), PROJECT_URL)
"https://github.com/fronzbot/blinkpy."))
def refresh(self, force_cache=False): def refresh(self, force_cache=False):
""" """
+8 -9
View File
@@ -2,7 +2,7 @@
import logging import logging
from requests import Request, Session, exceptions 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 import blinkpy.helpers.errors as ERROR
@@ -17,7 +17,7 @@ def create_session():
def attempt_reauthorization(blink): def attempt_reauthorization(blink):
"""Attempt to refresh auth token and links.""" """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() headers = blink.get_auth_token()
return headers return headers
@@ -48,20 +48,19 @@ def http_req(blink, url='http://example.com', data=None, headers=None,
try: try:
response = blink.session.send(prepped, stream=stream) response = blink.session.send(prepped, stream=stream)
except (exceptions.ConnectionError, exceptions.Timeout):
_LOGGER.error("Cannot connect to server. Possible outage.")
return None
if json_resp and 'code' in response.json(): if json_resp and 'code' in response.json():
if is_retry: if is_retry:
_LOGGER.error("Cannot authenticate with server.") _LOGGER.error(("Cannot obtain new token for server auth. "
raise BlinkAuthenticationException( "Please report this issue on %s"), PROJECT_URL)
(response.json()['code'], response.json()['message'])) return None
else: else:
headers = attempt_reauthorization(blink) headers = attempt_reauthorization(blink)
return http_req(blink, url=url, data=data, headers=headers, return http_req(blink, url=url, data=data, headers=headers,
reqtype=reqtype, stream=stream, reqtype=reqtype, stream=stream,
json_resp=json_resp, is_retry=True) 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: if json_resp:
return response.json() return response.json()
+6 -1
View File
@@ -14,6 +14,7 @@ from blinkpy.sync_module import BlinkSyncModule
from blinkpy.helpers.util import ( from blinkpy.helpers.util import (
http_req, create_session, BlinkAuthenticationException, http_req, create_session, BlinkAuthenticationException,
BlinkException, BlinkURLHandler) BlinkException, BlinkURLHandler)
from blinkpy.helpers.constants import PROJECT_URL
import tests.mock_responses as mresp import tests.mock_responses as mresp
USERNAME = 'foobar' USERNAME = 'foobar'
@@ -77,11 +78,15 @@ class TestBlinkSetup(unittest.TestCase):
def test_bad_request(self, mock_sess): def test_bad_request(self, mock_sess):
"""Check that we raise an Exception with a bad request.""" """Check that we raise an Exception with a bad request."""
self.blink.session = create_session() 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): with self.assertRaises(BlinkException):
http_req(self.blink, reqtype='bad') http_req(self.blink, reqtype='bad')
with self.assertRaises(BlinkAuthenticationException): with self.assertLogs() as logrecord:
http_req(self.blink, reqtype='post', is_retry=True) http_req(self.blink, reqtype='post', is_retry=True)
self.assertEqual(logrecord.output, [explog])
def test_authentication(self, mock_sess): def test_authentication(self, mock_sess):
"""Check that we can authenticate Blink up properly.""" """Check that we can authenticate Blink up properly."""