-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathtest_dependencies.py
More file actions
36 lines (27 loc) · 1.19 KB
/
test_dependencies.py
File metadata and controls
36 lines (27 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pytest
from fastapi import FastAPI, Depends
from fastapi.testclient import TestClient
from src.ContentProcessorAPI.app.dependencies import get_token_header, get_query_token
# from starlette.status import HTTP_400_BAD_REQUEST
@pytest.fixture
def test_app():
app = FastAPI()
@app.get("/header-protected")
async def protected_route_header(dep=Depends(get_token_header)):
return {"message": "Success"}
@app.get("/query-protected")
async def protected_route_query(dep=Depends(get_query_token)):
return {"message": "Success"}
return app
def test_get_token_header_fails(test_app):
client = TestClient(test_app)
# Provide the required header so FastAPI doesn't return 422
response = client.get("/header-protected", headers={"x-token": "fake"})
assert response.status_code == 400
assert response.json() == {"detail": "X-Token header invalid"}
def test_get_query_token_fails(test_app):
client = TestClient(test_app)
# Provide the required query param so FastAPI doesn't return 422
response = client.get("/query-protected?token=fake")
assert response.status_code == 400
assert response.json() == {"detail": "No ... token provided"}