Coverage for tests/test_security_http_digest_description.py: 100%
25 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1from fastapi import FastAPI, Security 1abcdef
2from fastapi.security import HTTPAuthorizationCredentials, HTTPDigest 1abcdef
3from fastapi.testclient import TestClient 1abcdef
5app = FastAPI() 1abcdef
7security = HTTPDigest(description="HTTPDigest scheme") 1abcdef
10@app.get("/users/me") 1abcdef
11def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)): 1abcdef
12 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1ghijkl
15client = TestClient(app) 1abcdef
18def test_security_http_digest(): 1abcdef
19 response = client.get("/users/me", headers={"Authorization": "Digest foobar"}) 1ghijkl
20 assert response.status_code == 200, response.text 1ghijkl
21 assert response.json() == {"scheme": "Digest", "credentials": "foobar"} 1ghijkl
24def test_security_http_digest_no_credentials(): 1abcdef
25 response = client.get("/users/me") 1mnopqr
26 assert response.status_code == 403, response.text 1mnopqr
27 assert response.json() == {"detail": "Not authenticated"} 1mnopqr
30def test_security_http_digest_incorrect_scheme_credentials(): 1abcdef
31 response = client.get( 1stuvwx
32 "/users/me", headers={"Authorization": "Other invalidauthorization"}
33 )
34 assert response.status_code == 403, response.text 1stuvwx
35 assert response.json() == {"detail": "Invalid authentication credentials"} 1stuvwx
38def test_openapi_schema(): 1abcdef
39 response = client.get("/openapi.json") 1yzABCD
40 assert response.status_code == 200, response.text 1yzABCD
41 assert response.json() == { 1yzABCD
42 "openapi": "3.1.0",
43 "info": {"title": "FastAPI", "version": "0.1.0"},
44 "paths": {
45 "/users/me": {
46 "get": {
47 "responses": {
48 "200": {
49 "description": "Successful Response",
50 "content": {"application/json": {"schema": {}}},
51 }
52 },
53 "summary": "Read Current User",
54 "operationId": "read_current_user_users_me_get",
55 "security": [{"HTTPDigest": []}],
56 }
57 }
58 },
59 "components": {
60 "securitySchemes": {
61 "HTTPDigest": {
62 "type": "http",
63 "scheme": "digest",
64 "description": "HTTPDigest scheme",
65 }
66 }
67 },
68 }