Coverage for tests/test_security_http_base.py: 100%
21 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi import FastAPI, Security 1abcde
2from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase 1abcde
3from fastapi.testclient import TestClient 1abcde
5app = FastAPI() 1abcde
7security = HTTPBase(scheme="Other") 1abcde
10@app.get("/users/me") 1abcde
11def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)): 1abcde
12 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1abcde
15client = TestClient(app) 1abcde
18def test_security_http_base(): 1abcde
19 response = client.get("/users/me", headers={"Authorization": "Other foobar"}) 1abcde
20 assert response.status_code == 200, response.text 1abcde
21 assert response.json() == {"scheme": "Other", "credentials": "foobar"} 1abcde
24def test_security_http_base_no_credentials(): 1abcde
25 response = client.get("/users/me") 1abcde
26 assert response.status_code == 403, response.text 1abcde
27 assert response.json() == {"detail": "Not authenticated"} 1abcde
30def test_openapi_schema(): 1abcde
31 response = client.get("/openapi.json") 1abcde
32 assert response.status_code == 200, response.text 1abcde
33 assert response.json() == { 1abcde
34 "openapi": "3.1.0",
35 "info": {"title": "FastAPI", "version": "0.1.0"},
36 "paths": {
37 "/users/me": {
38 "get": {
39 "responses": {
40 "200": {
41 "description": "Successful Response",
42 "content": {"application/json": {"schema": {}}},
43 }
44 },
45 "summary": "Read Current User",
46 "operationId": "read_current_user_users_me_get",
47 "security": [{"HTTPBase": []}],
48 }
49 }
50 },
51 "components": {
52 "securitySchemes": {"HTTPBase": {"type": "http", "scheme": "Other"}}
53 },
54 }