Coverage for tests / test_security_http_digest_description.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import FastAPI, Security 1abcd

2from fastapi.security import HTTPAuthorizationCredentials, HTTPDigest 1abcd

3from fastapi.testclient import TestClient 1abcd

4from inline_snapshot import snapshot 1abcd

5 

6app = FastAPI() 1abcd

7 

8security = HTTPDigest(description="HTTPDigest scheme") 1abcd

9 

10 

11@app.get("/users/me") 1abcd

12def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)): 1abcd

13 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1efg

14 

15 

16client = TestClient(app) 1abcd

17 

18 

19def test_security_http_digest(): 1abcd

20 response = client.get("/users/me", headers={"Authorization": "Digest foobar"}) 1efg

21 assert response.status_code == 200, response.text 1efg

22 assert response.json() == {"scheme": "Digest", "credentials": "foobar"} 1efg

23 

24 

25def test_security_http_digest_no_credentials(): 1abcd

26 response = client.get("/users/me") 1hij

27 assert response.status_code == 401, response.text 1hij

28 assert response.json() == {"detail": "Not authenticated"} 1hij

29 assert response.headers["WWW-Authenticate"] == "Digest" 1hij

30 

31 

32def test_security_http_digest_incorrect_scheme_credentials(): 1abcd

33 response = client.get( 1klm

34 "/users/me", headers={"Authorization": "Other invalidauthorization"} 

35 ) 

36 assert response.status_code == 401, response.text 1klm

37 assert response.json() == {"detail": "Not authenticated"} 1klm

38 assert response.headers["WWW-Authenticate"] == "Digest" 1klm

39 

40 

41def test_openapi_schema(): 1abcd

42 response = client.get("/openapi.json") 1nop

43 assert response.status_code == 200, response.text 1nop

44 assert response.json() == snapshot( 1nop

45 { 

46 "openapi": "3.1.0", 

47 "info": {"title": "FastAPI", "version": "0.1.0"}, 

48 "paths": { 

49 "/users/me": { 

50 "get": { 

51 "responses": { 

52 "200": { 

53 "description": "Successful Response", 

54 "content": {"application/json": {"schema": {}}}, 

55 } 

56 }, 

57 "summary": "Read Current User", 

58 "operationId": "read_current_user_users_me_get", 

59 "security": [{"HTTPDigest": []}], 

60 } 

61 } 

62 }, 

63 "components": { 

64 "securitySchemes": { 

65 "HTTPDigest": { 

66 "type": "http", 

67 "scheme": "digest", 

68 "description": "HTTPDigest scheme", 

69 } 

70 } 

71 }, 

72 } 

73 )