Skip to content

Commit 0ab17ae

Browse files
authored
Merge pull request #103 from fronzbot/reauth-bug-fix
Reauth bug fix
2 parents 0781d79 + 1a12eb3 commit 0ab17ae

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist/*
1010
.sh
1111
build/*
1212
docs/_build
13+
*.log

blinkpy/blinkpy.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
create_session, BlinkURLHandler,
2222
BlinkAuthenticationException)
2323
from blinkpy.helpers.constants import (
24-
BLINK_URL, LOGIN_URL, LOGIN_BACKUP_URL)
24+
BLINK_URL, LOGIN_URL, LOGIN_BACKUP_URL, PROJECT_URL)
2525

2626
REFRESH_RATE = 30
2727

@@ -146,8 +146,7 @@ def get_ids(self):
146146
if all_networks:
147147
_LOGGER.error(("More than one unboarded network. "
148148
"Platform may not work as intended. "
149-
"Please open an issue on "
150-
"https://github.com/fronzbot/blinkpy."))
149+
"Please open an issue on %s"), PROJECT_URL)
151150

152151
def refresh(self, force_cache=False):
153152
"""

blinkpy/helpers/util.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44
from requests import Request, Session, exceptions
5-
from blinkpy.helpers.constants import BLINK_URL
5+
from blinkpy.helpers.constants import BLINK_URL, PROJECT_URL
66
import blinkpy.helpers.errors as ERROR
77

88

@@ -17,7 +17,7 @@ def create_session():
1717

1818
def attempt_reauthorization(blink):
1919
"""Attempt to refresh auth token and links."""
20-
_LOGGER.debug("Auth token expired, attempting reauthorization.")
20+
_LOGGER.info("Auth token expired, attempting reauthorization.")
2121
headers = blink.get_auth_token()
2222
return headers
2323

@@ -48,20 +48,25 @@ def http_req(blink, url='http://example.com', data=None, headers=None,
4848

4949
try:
5050
response = blink.session.send(prepped, stream=stream)
51+
if json_resp and 'code' in response.json():
52+
if is_retry:
53+
_LOGGER.error(("Cannot obtain new token for server auth. "
54+
"Please report this issue on %s"), PROJECT_URL)
55+
return None
56+
else:
57+
headers = attempt_reauthorization(blink)
58+
return http_req(blink, url=url, data=data, headers=headers,
59+
reqtype=reqtype, stream=stream,
60+
json_resp=json_resp, is_retry=True)
5161
except (exceptions.ConnectionError, exceptions.Timeout):
52-
_LOGGER.error("Cannot connect to server. Possible outage.")
53-
return None
54-
55-
if json_resp and 'code' in response.json():
56-
if is_retry:
57-
_LOGGER.error("Cannot authenticate with server.")
58-
raise BlinkAuthenticationException(
59-
(response.json()['code'], response.json()['message']))
60-
else:
62+
_LOGGER.error("Cannot connect to server with url %s.", url)
63+
if not is_retry:
6164
headers = attempt_reauthorization(blink)
6265
return http_req(blink, url=url, data=data, headers=headers,
6366
reqtype=reqtype, stream=stream,
6467
json_resp=json_resp, is_retry=True)
68+
_LOGGER.error("Possible issue with Blink servers.")
69+
return None
6570

6671
if json_resp:
6772
return response.json()

tests/test_api.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test various api functions."""
22

33
import unittest
4+
from unittest import mock
45
from blinkpy import api
56
from blinkpy.blinkpy import Blink
67
from blinkpy.helpers.util import create_session
@@ -20,13 +21,21 @@ def tearDown(self):
2021
"""Tear down blink module."""
2122
self.blink = None
2223

23-
def test_http_req_connect_error(self):
24+
@mock.patch('blinkpy.blinkpy.Blink.get_auth_token')
25+
def test_http_req_connect_error(self, mock_auth):
2426
"""Test http_get error condition."""
25-
expected = ("ERROR:blinkpy.helpers.util:"
26-
"Cannot connect to server. Possible outage.")
27+
mock_auth.return_value = {'foo': 'bar'}
28+
firstlog = ("ERROR:blinkpy.helpers.util:"
29+
"Cannot connect to server with url {}").format(
30+
'http://notreal.fake.')
31+
nextlog = ("INFO:blinkpy.helpers.util:"
32+
"Auth token expired, attempting reauthorization.")
33+
lastlog = ("ERROR:blinkpy.helpers.util:"
34+
"Possible issue with Blink servers.")
35+
expected = [firstlog, nextlog, firstlog, lastlog]
2736
with self.assertLogs() as getlog:
2837
api.http_get(self.blink, 'http://notreal.fake')
2938
with self.assertLogs() as postlog:
3039
api.http_post(self.blink, 'http://notreal.fake')
31-
self.assertEqual(getlog.output, [expected])
32-
self.assertEqual(postlog.output, [expected])
40+
self.assertEqual(getlog.output, expected)
41+
self.assertEqual(postlog.output, expected)

tests/test_blinkpy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from blinkpy.helpers.util import (
1515
http_req, create_session, BlinkAuthenticationException,
1616
BlinkException, BlinkURLHandler)
17+
from blinkpy.helpers.constants import PROJECT_URL
1718
import tests.mock_responses as mresp
1819

1920
USERNAME = 'foobar'
@@ -77,11 +78,15 @@ def test_manual_login(self, getpwd, mock_sess):
7778
def test_bad_request(self, mock_sess):
7879
"""Check that we raise an Exception with a bad request."""
7980
self.blink.session = create_session()
81+
explog = ("ERROR:blinkpy.helpers.util:"
82+
"Cannot obtain new token for server auth. "
83+
"Please report this issue on {}").format(PROJECT_URL)
8084
with self.assertRaises(BlinkException):
8185
http_req(self.blink, reqtype='bad')
8286

83-
with self.assertRaises(BlinkAuthenticationException):
87+
with self.assertLogs() as logrecord:
8488
http_req(self.blink, reqtype='post', is_retry=True)
89+
self.assertEqual(logrecord.output, [explog])
8590

8691
def test_authentication(self, mock_sess):
8792
"""Check that we can authenticate Blink up properly."""

0 commit comments

Comments
 (0)