From d661585238ecc409765215d2a56e6e2b5a17028c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Voron?= Date: Mon, 25 Jul 2022 10:23:55 +0200 Subject: [PATCH] Revert "Cookie transport must return empty json and not `null` in `response.data` on login (#1037)" This reverts commit 77d0077503d9d6b4dd206e3fc643d96bc3c5834c. --- fastapi_users/authentication/transport/cookie.py | 8 +++++--- tests/test_authentication_transport_cookie.py | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/fastapi_users/authentication/transport/cookie.py b/fastapi_users/authentication/transport/cookie.py index 372a5eb5..6fa8e198 100644 --- a/fastapi_users/authentication/transport/cookie.py +++ b/fastapi_users/authentication/transport/cookie.py @@ -30,7 +30,6 @@ class CookieTransport(Transport): self.scheme = APIKeyCookie(name=self.cookie_name, auto_error=False) async def get_login_response(self, token: str, response: Response) -> Any: - response = Response(status_code=status.HTTP_204_NO_CONTENT) response.set_cookie( self.cookie_name, token, @@ -41,7 +40,10 @@ class CookieTransport(Transport): httponly=self.cookie_httponly, samesite=self.cookie_samesite, ) - return response + + # We shouldn't return directly the response + # so that FastAPI can terminate it properly + return None async def get_logout_response(self, response: Response) -> Any: response.set_cookie( @@ -57,7 +59,7 @@ class CookieTransport(Transport): @staticmethod def get_openapi_login_responses_success() -> OpenAPIResponseType: - return {status.HTTP_204_NO_CONTENT: {"model": None}} + return {status.HTTP_200_OK: {"model": None}} @staticmethod def get_openapi_logout_responses_success() -> OpenAPIResponseType: diff --git a/tests/test_authentication_transport_cookie.py b/tests/test_authentication_transport_cookie.py index 7c90808f..ca99f9ca 100644 --- a/tests/test_authentication_transport_cookie.py +++ b/tests/test_authentication_transport_cookie.py @@ -38,10 +38,10 @@ async def test_get_login_response(cookie_transport: CookieTransport): secure = cookie_transport.cookie_secure httponly = cookie_transport.cookie_httponly - response = await cookie_transport.get_login_response("TOKEN", Response()) + response = Response() + login_response = await cookie_transport.get_login_response("TOKEN", response) - assert isinstance(response, Response) - assert response.status_code == status.HTTP_204_NO_CONTENT + assert login_response is None cookies = [header for header in response.raw_headers if header[0] == b"set-cookie"] assert len(cookies) == 1 @@ -96,7 +96,7 @@ async def test_get_logout_response(cookie_transport: CookieTransport): @pytest.mark.openapi def test_get_openapi_login_responses_success(cookie_transport: CookieTransport): assert cookie_transport.get_openapi_login_responses_success() == { - status.HTTP_204_NO_CONTENT: {"model": None} + status.HTTP_200_OK: {"model": None} }