Coverage for tests/test_security_http_digest_optional.py: 100%
28 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 typing import Optional 1abcde
3from fastapi import FastAPI, Security 1abcde
4from fastapi.security import HTTPAuthorizationCredentials, HTTPDigest 1abcde
5from fastapi.testclient import TestClient 1abcde
7app = FastAPI() 1abcde
9security = HTTPDigest(auto_error=False) 1abcde
12@app.get("/users/me") 1abcde
13def read_current_user( 1abcde
14 credentials: Optional[HTTPAuthorizationCredentials] = Security(security),
15):
16 if credentials is None: 1abcde
17 return {"msg": "Create an account first"} 1abcde
18 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1abcde
21client = TestClient(app) 1abcde
24def test_security_http_digest(): 1abcde
25 response = client.get("/users/me", headers={"Authorization": "Digest foobar"}) 1abcde
26 assert response.status_code == 200, response.text 1abcde
27 assert response.json() == {"scheme": "Digest", "credentials": "foobar"} 1abcde
30def test_security_http_digest_no_credentials(): 1abcde
31 response = client.get("/users/me") 1abcde
32 assert response.status_code == 200, response.text 1abcde
33 assert response.json() == {"msg": "Create an account first"} 1abcde
36def test_security_http_digest_incorrect_scheme_credentials(): 1abcde
37 response = client.get( 1abcde
38 "/users/me", headers={"Authorization": "Other invalidauthorization"}
39 )
40 assert response.status_code == 403, response.text 1abcde
41 assert response.json() == {"detail": "Invalid authentication credentials"} 1abcde
44def test_openapi_schema(): 1abcde
45 response = client.get("/openapi.json") 1abcde
46 assert response.status_code == 200, response.text 1abcde
47 assert response.json() == { 1abcde
48 "openapi": "3.1.0",
49 "info": {"title": "FastAPI", "version": "0.1.0"},
50 "paths": {
51 "/users/me": {
52 "get": {
53 "responses": {
54 "200": {
55 "description": "Successful Response",
56 "content": {"application/json": {"schema": {}}},
57 }
58 },
59 "summary": "Read Current User",
60 "operationId": "read_current_user_users_me_get",
61 "security": [{"HTTPDigest": []}],
62 }
63 }
64 },
65 "components": {
66 "securitySchemes": {"HTTPDigest": {"type": "http", "scheme": "digest"}}
67 },
68 }