Debian Codegraph @

tests/test_security_http_base_description.py

raw
1 from fastapi import FastAPI, Security
2 from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
3 from fastapi.testclient import TestClient
4 from inline_snapshot import snapshot
5
6 app = FastAPI()
7
8 security = HTTPBase(scheme="Other", description="Other Security Scheme")
9
10
11 @app.get("/users/me")
12 def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)):
13 return {"scheme": credentials.scheme, "credentials": credentials.credentials}
14
15
16 client = TestClient(app)
17
18
19 def test_security_http_base():
20 response = client.get("/users/me", headers={"Authorization": "Other foobar"})
21 assert response.status_code == 200, response.text
22 assert response.json() == {"scheme": "Other", "credentials": "foobar"}
23
24
25 def test_security_http_base_no_credentials():
26 response = client.get("/users/me")
27 assert response.status_code == 401, response.text
28 assert response.json() == {"detail": "Not authenticated"}
29 assert response.headers["WWW-Authenticate"] == "Other"
30
31
32 def test_openapi_schema():
33 response = client.get("/openapi.json")
34 assert response.status_code == 200, response.text
35 assert response.json() == snapshot(
36 {
37 "openapi": "3.1.0",
38 "info": {"title": "FastAPI", "version": "0.1.0"},
39 "paths": {
40 "/users/me": {
41 "get": {
42 "responses": {
43 "200": {
44 "description": "Successful Response",
45 "content": {"application/json": {"schema": {}}},
46 }
47 },
48 "summary": "Read Current User",
49 "operationId": "read_current_user_users_me_get",
50 "security": [{"HTTPBase": []}],
51 }
52 }
53 },
54 "components": {
55 "securitySchemes": {
56 "HTTPBase": {
57 "type": "http",
58 "scheme": "Other",
59 "description": "Other Security Scheme",
60 }
61 }
62 },
63 }
64 )