Skip to content

Commit f24bc64

Browse files
Create test_dependencies.py
1 parent 45e6f85 commit f24bc64

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
from fastapi import FastAPI, Depends
3+
from fastapi.testclient import TestClient
4+
from src.ContentProcessorAPI.app.dependencies import get_token_header, get_query_token
5+
# from starlette.status import HTTP_400_BAD_REQUEST
6+
7+
8+
@pytest.fixture
9+
def test_app():
10+
app = FastAPI()
11+
12+
@app.get("/header-protected")
13+
async def protected_route_header(dep=Depends(get_token_header)):
14+
return {"message": "Success"}
15+
16+
@app.get("/query-protected")
17+
async def protected_route_query(dep=Depends(get_query_token)):
18+
return {"message": "Success"}
19+
20+
return app
21+
22+
23+
def test_get_token_header_fails(test_app):
24+
client = TestClient(test_app)
25+
# Provide the required header so FastAPI doesn't return 422
26+
response = client.get("/header-protected", headers={"x-token": "fake"})
27+
assert response.status_code == 400
28+
assert response.json() == {"detail": "X-Token header invalid"}
29+
30+
31+
def test_get_query_token_fails(test_app):
32+
client = TestClient(test_app)
33+
# Provide the required query param so FastAPI doesn't return 422
34+
response = client.get("/query-protected?token=fake")
35+
assert response.status_code == 400
36+
assert response.json() == {"detail": "No ... token provided"}

0 commit comments

Comments
 (0)