From 9608be436518340a8c5dee26fe413ac56441e74f Mon Sep 17 00:00:00 2001 From: David Knowles Date: Sat, 9 Dec 2023 00:17:03 +0000 Subject: [PATCH] Only raise NotAuthorizedError from boto-level errors --- pyschlage/auth.py | 3 --- tests/test_auth.py | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pyschlage/auth.py b/pyschlage/auth.py index 8b94d65..d428aa4 100644 --- a/pyschlage/auth.py +++ b/pyschlage/auth.py @@ -64,9 +64,6 @@ def wrapper(*args, **kwargs): message = resp.json().get("message", resp.reason) except requests.JSONDecodeError: message = resp.reason - - if resp.status_code == 401: - raise NotAuthorizedError(message) from ex raise UnknownError(message) from ex return wrapper diff --git a/tests/test_auth.py b/tests/test_auth.py index 89276f4..92d30a1 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,5 +1,6 @@ from unittest import mock +from botocore.exceptions import ClientError import pytest import requests @@ -46,16 +47,19 @@ def test_request(mock_cognito, mock_srp_auth, mock_request): def test_request_not_authorized(mock_cognito, mock_srp_auth, mock_request): url = "https://api.allegion.yonomi.cloud/v1/foo/bar" auth = _auth.Auth("__username__", "__password__") - mock_resp = mock.create_autospec(requests.Response) - mock_resp.raise_for_status.side_effect = requests.HTTPError( - f"401 Client Error: Unauthorized for url: {url}" + mock_request.side_effect = ClientError( + { + "Error": { + "Code": "NotAuthorizedException", + "Message": f"Unauthorized for url: {url}", + } + }, + "foo-op", ) - mock_resp.status_code = 401 - mock_resp.reason = "Unauthorized" - mock_resp.json.side_effect = lambda: {"message": "Unauthorized"} - mock_request.return_value = mock_resp - with pytest.raises(pyschlage.exceptions.NotAuthorizedError): + with pytest.raises( + pyschlage.exceptions.NotAuthorizedError, match=f"Unauthorized for url: {url}" + ): auth.request("get", "/foo/bar", baz="bam") mock_request.assert_called_once_with(