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
build/*
docs/_build
*.log
+2 -3
View File
@@ -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):
"""
+8 -9
View File
@@ -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,20 +48,19 @@ def http_req(blink, url='http://example.com', data=None, headers=None,
try:
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 is_retry:
_LOGGER.error("Cannot authenticate with server.")
raise BlinkAuthenticationException(
(response.json()['code'], response.json()['message']))
_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:
return response.json()
+6 -1
View File
@@ -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."""