Skip to content

Commit

Permalink
fix: linting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Aug 31, 2024
1 parent 6449af9 commit b89d944
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 49 deletions.
12 changes: 5 additions & 7 deletions httpx_oauth/integrations/litestar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pylint: disable=[invalid-name,import-outside-toplevel]
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, List, TypeAlias, Union # noqa: UP035
from typing import TYPE_CHECKING, Any, Dict, List, TypeAlias, Union

from litestar import status_codes as status
from litestar.exceptions import HTTPException
Expand Down Expand Up @@ -32,14 +32,12 @@ def __init__(
self,
status_code: int,
detail: Any = None,
headers: Union[Dict[str, str], None] = None, # noqa: UP007, UP006
response: Union[httpx.Response, None] = None, # noqa: UP007
extra: Union[Dict[str, Any], List[Any]] | None = None, # noqa: UP007, UP006
headers: Union[Dict[str, str], None] = None,
response: Union[httpx.Response, None] = None,
extra: Union[Dict[str, Any], List[Any]] | None = None,
) -> None:
super().__init__(message=detail)
HTTPException.__init__(
self, detail=detail, status_code=status_code, extra=extra, headers=headers
)
HTTPException.__init__(self, detail=detail, status_code=status_code, extra=extra, headers=headers)
self.response = response


Expand Down
56 changes: 14 additions & 42 deletions tests/test_integrations_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
ROUTE_NAME = "callback"

client = OAuth2(CLIENT_ID, CLIENT_SECRET, AUTHORIZE_ENDPOINT, ACCESS_TOKEN_ENDPOINT)
oauth2_authorize_callback_route_name = OAuth2AuthorizeCallback(
client, route_name=ROUTE_NAME
)
oauth2_authorize_callback_redirect_url = OAuth2AuthorizeCallback(
client, redirect_url=REDIRECT_URL
)
oauth2_authorize_callback_route_name = OAuth2AuthorizeCallback(client, route_name=ROUTE_NAME)
oauth2_authorize_callback_redirect_url = OAuth2AuthorizeCallback(client, redirect_url=REDIRECT_URL)


@get(
Expand All @@ -37,9 +33,7 @@ async def authorize_route_name(

@get(
"/authorize-redirect-url",
dependencies={
"access_token_state": Provide(oauth2_authorize_callback_redirect_url)
},
dependencies={"access_token_state": Provide(oauth2_authorize_callback_redirect_url)},
)
async def authorize_redirect_url(
access_token_state: AccessTokenState = Dependency(skip_validation=True),
Expand Down Expand Up @@ -74,18 +68,14 @@ def test_oauth2_authorize_error(self, route, expected_redirect_url):
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json() == {"status_code": 400, "detail": "access_denied"}

def test_oauth2_authorize_get_access_token_error(
self, mocker: MockerFixture, route, expected_redirect_url
):
def test_oauth2_authorize_get_access_token_error(self, mocker: MockerFixture, route, expected_redirect_url):
get_access_token_mock = mocker.patch.object(
client, "get_access_token", side_effect=GetAccessTokenError("ERROR")
)

response = test_client.get(route, params={"code": "CODE"})

get_access_token_mock.assert_called_once_with(
"CODE", expected_redirect_url, None
)
get_access_token_mock.assert_called_once_with("CODE", expected_redirect_url, None)
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
# by default, litestar will only return `Internal Server Error` as the detail on a response.
# we are adding the ERROR to the `extra` payload
Expand All @@ -95,53 +85,37 @@ def test_oauth2_authorize_get_access_token_error(
"extra": {"message": "ERROR"},
}

def test_oauth2_authorize_without_state(
self, patch_async_method, route, expected_redirect_url
):
def test_oauth2_authorize_without_state(self, patch_async_method, route, expected_redirect_url):
patch_async_method(client, "get_access_token", return_value="ACCESS_TOKEN")

response = test_client.get(route, params={"code": "CODE"})

client.get_access_token.assert_called()
client.get_access_token.assert_called_once_with(
"CODE", expected_redirect_url, None
)
client.get_access_token.assert_called_once_with("CODE", expected_redirect_url, None)
assert response.status_code == status.HTTP_200_OK
assert response.json() == ["ACCESS_TOKEN", None]

def test_oauth2_authorize_code_verifier_without_state(
self, patch_async_method, route, expected_redirect_url
):
def test_oauth2_authorize_code_verifier_without_state(self, patch_async_method, route, expected_redirect_url):
patch_async_method(client, "get_access_token", return_value="ACCESS_TOKEN")

response = test_client.get(
route, params={"code": "CODE", "code_verifier": "CODE_VERIFIER"}
)
response = test_client.get(route, params={"code": "CODE", "code_verifier": "CODE_VERIFIER"})

client.get_access_token.assert_called()
client.get_access_token.assert_called_once_with(
"CODE", expected_redirect_url, "CODE_VERIFIER"
)
client.get_access_token.assert_called_once_with("CODE", expected_redirect_url, "CODE_VERIFIER")
assert response.status_code == status.HTTP_200_OK
assert response.json() == ["ACCESS_TOKEN", None]

def test_oauth2_authorize_with_state(
self, patch_async_method, route, expected_redirect_url
):
def test_oauth2_authorize_with_state(self, patch_async_method, route, expected_redirect_url):
patch_async_method(client, "get_access_token", return_value="ACCESS_TOKEN")

response = test_client.get(route, params={"code": "CODE", "state": "STATE"})

client.get_access_token.assert_called()
client.get_access_token.assert_called_once_with(
"CODE", expected_redirect_url, None
)
client.get_access_token.assert_called_once_with("CODE", expected_redirect_url, None)
assert response.status_code == status.HTTP_200_OK
assert response.json() == ["ACCESS_TOKEN", "STATE"]

def test_oauth2_authorize_with_state_and_code_verifier(
self, patch_async_method, route, expected_redirect_url
):
def test_oauth2_authorize_with_state_and_code_verifier(self, patch_async_method, route, expected_redirect_url):
patch_async_method(client, "get_access_token", return_value="ACCESS_TOKEN")

response = test_client.get(
Expand All @@ -150,8 +124,6 @@ def test_oauth2_authorize_with_state_and_code_verifier(
)

client.get_access_token.assert_called()
client.get_access_token.assert_called_once_with(
"CODE", expected_redirect_url, "CODE_VERIFIER"
)
client.get_access_token.assert_called_once_with("CODE", expected_redirect_url, "CODE_VERIFIER")
assert response.status_code == status.HTTP_200_OK
assert response.json() == ["ACCESS_TOKEN", "STATE"]

0 comments on commit b89d944

Please sign in to comment.