From d5ec39113e60ff325b09710df9d9741ce689172d Mon Sep 17 00:00:00 2001 From: Kevin Fronczak Date: Wed, 27 May 2020 23:18:06 +0000 Subject: [PATCH] Add is_errored property to Auth class --- blinkpy/auth.py | 6 ++++++ tests/test_auth.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/blinkpy/auth.py b/blinkpy/auth.py index b379406..2cf0010 100644 --- a/blinkpy/auth.py +++ b/blinkpy/auth.py @@ -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( diff --git a/tests/test_auth.py b/tests/test_auth.py index 8ee18ce..e1fae96 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -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."""