Add is_errored property to Auth class

This commit is contained in:
Kevin Fronczak
2020-05-27 23:19:59 +00:00
parent 58117b3daf
commit d5ec39113e
2 changed files with 20 additions and 0 deletions
+6
View File
@@ -32,6 +32,7 @@ class Auth:
self.client_id = login_data.get("client_id", None)
self.account_id = login_data.get("account_id", None)
self.login_response = None
self.is_errored = False
self.no_prompt = no_prompt
self.session = self.create_session()
@@ -86,6 +87,7 @@ class Auth:
def refresh_token(self):
"""Refresh auth token."""
self.is_errored = True
try:
_LOGGER.info("Token expired, attempting automatic refresh.")
self.login_response = self.login()
@@ -94,6 +96,7 @@ class Auth:
self.token = self.login_response["authtoken"]["authtoken"]
self.client_id = self.login_response["client"]["id"]
self.account_id = self.login_response["account"]["id"]
self.is_errored = False
except LoginError:
_LOGGER.error("Login endpoint failed. Try again later.")
raise TokenRefreshFailed
@@ -111,7 +114,9 @@ class Auth:
def validate_response(self, response, json_resp):
"""Check for valid response."""
if not json_resp:
self.is_errored = False
return response
self.is_errored = True
try:
if response.status_code in [101, 401]:
raise UnauthorizedError
@@ -123,6 +128,7 @@ class Auth:
except (AttributeError, ValueError):
raise BlinkBadResponse
self.is_errored = False
return json_data
def query(
+14
View File
@@ -92,28 +92,38 @@ class TestAuth(unittest.TestCase):
def test_bad_response_code(self):
"""Check bad response code from server."""
self.auth.is_errored = False
fake_resp = mresp.MockResponse({"code": 404}, 404)
with self.assertRaises(exceptions.ConnectionError):
self.auth.validate_response(fake_resp, True)
self.assertTrue(self.auth.is_errored)
self.auth.is_errored = False
fake_resp = mresp.MockResponse({"code": 101}, 401)
with self.assertRaises(UnauthorizedError):
self.auth.validate_response(fake_resp, True)
self.assertTrue(self.auth.is_errored)
def test_good_response_code(self):
"""Check good response code from server."""
fake_resp = mresp.MockResponse({"foo": "bar"}, 200)
self.auth.is_errored = True
self.assertEqual(self.auth.validate_response(fake_resp, True), {"foo": "bar"})
self.assertFalse(self.auth.is_errored)
def test_response_not_json(self):
"""Check response when not json."""
fake_resp = "foobar"
self.auth.is_errored = True
self.assertEqual(self.auth.validate_response(fake_resp, False), "foobar")
self.assertFalse(self.auth.is_errored)
def test_response_bad_json(self):
"""Check response when not json but expecting json."""
self.auth.is_errored = False
with self.assertRaises(BlinkBadResponse):
self.auth.validate_response(None, True)
self.assertTrue(self.auth.is_errored)
def test_header(self):
"""Test header data."""
@@ -141,10 +151,12 @@ class TestAuth(unittest.TestCase):
"""Test login handling when bad response."""
fake_resp = mresp.MockResponse({"foo": "bar"}, 404)
mock_req.return_value = fake_resp
self.auth.is_errored = False
with self.assertRaises(LoginError):
self.auth.login()
with self.assertRaises(TokenRefreshFailed):
self.auth.refresh_token()
self.assertTrue(self.auth.is_errored)
@mock.patch("blinkpy.auth.Auth.login")
def test_refresh_token(self, mock_login):
@@ -165,8 +177,10 @@ class TestAuth(unittest.TestCase):
def test_refresh_token_failed(self, mock_login):
"""Test refresh token failed."""
mock_login.return_value = {}
self.auth.is_errored = False
with self.assertRaises(TokenRefreshFailed):
self.auth.refresh_token()
self.assertTrue(self.auth.is_errored)
def test_check_key_required(self):
"""Check key required method."""