Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only raise NotAuthorizedError from boto-level errors #106

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions pyschlage/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import mock

from botocore.exceptions import ClientError
import pytest
import requests

Expand Down Expand Up @@ -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(
Expand Down