From edecc79a6a6239b53aec45b7b1106e59456c5735 Mon Sep 17 00:00:00 2001 From: deepaerial Date: Sun, 24 Mar 2024 22:07:48 +0100 Subject: [PATCH] Remove example code --- tests/endpoints/test_get_downloads.py | 10 ++---- ytdl_api/config/settings.py | 2 -- ytdl_api/enpoints_403.py | 52 --------------------------- 3 files changed, 3 insertions(+), 61 deletions(-) delete mode 100644 ytdl_api/enpoints_403.py diff --git a/tests/endpoints/test_get_downloads.py b/tests/endpoints/test_get_downloads.py index eab7b53..4de66eb 100644 --- a/tests/endpoints/test_get_downloads.py +++ b/tests/endpoints/test_get_downloads.py @@ -21,14 +21,10 @@ def test_get_downloads(uid: str, app_client: TestClient, mock_persisted_download ) -@pytest.mark.skip("Fails in Github Actions for unknown reason") +@pytest.mark.skip( + "Fails in Github Actions for unknown reason due to bug https://github.com/tiangolo/fastapi/discussions/11341" +) def test_get_downloads_no_cookie(app_client: TestClient): response = app_client.get("/api/downloads") assert response.status_code == 403 assert response.json()["detail"] == "No cookie provided :(" - - -def test_example_no_cookie(app_client: TestClient): - response = app_client.get("/example") - assert response.status_code == 403 - assert response.json()["detail"] == "No cookie provided :(" diff --git a/ytdl_api/config/settings.py b/ytdl_api/config/settings.py index ce0b5f2..ae10ec6 100644 --- a/ytdl_api/config/settings.py +++ b/ytdl_api/config/settings.py @@ -94,10 +94,8 @@ def init_app(__pydantic_self__) -> FastAPI: def __setup_endpoints(__pydantic_self__, app: FastAPI): from ..endpoints import router - from ..enpoints_403 import router as router_403 app.include_router(router, prefix="/api") - app.include_router(router_403, prefix="") def __setup_exception_handlers(__pydantic_self__, app: FastAPI): from ..exceptions import ERROR_HANDLERS diff --git a/ytdl_api/enpoints_403.py b/ytdl_api/enpoints_403.py deleted file mode 100644 index 5b96b85..0000000 --- a/ytdl_api/enpoints_403.py +++ /dev/null @@ -1,52 +0,0 @@ -import secrets - -from fastapi import APIRouter, Cookie, Depends, FastAPI, HTTPException, Response -from starlette import status - - -def get_uid_dependency_factory(raise_error_on_empty: bool = False): - """ - Factory function fore returning dependency that fetches client ID. - """ - - def get_uid( - response: Response, - uid: str | None = Cookie(None), - ): - """ - Dependency for fetchng user ID from cookie or setting it in cookie if absent. - """ - if uid is None and raise_error_on_empty: - raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="No cookie provided :(") - elif uid is None and not raise_error_on_empty: - uid = secrets.token_hex(16) - response.set_cookie( - key="uid", - value=uid, - samesite="None", - secure=True, - httponly=True, - ) - return uid - - return get_uid - - -# .... -router = APIRouter(tags=["example"]) - - -@router.get( - "/example", - status_code=status.HTTP_200_OK, -) -async def get_api_version( - uid: str = Depends(get_uid_dependency_factory(raise_error_on_empty=True)), -): - ... - return status.HTTP_200_OK - - -if __name__ == "__main__": - fastapi_app = FastAPI() - fastapi_app.add_router(router)