Coverage for tests/test_security_http_digest_description.py: 100%

25 statements  

« 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 import HTTPAuthorizationCredentials, HTTPDigest 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5app = FastAPI() 1abcde

6 

7security = HTTPDigest(description="HTTPDigest scheme") 1abcde

8 

9 

10@app.get("/users/me") 1abcde

11def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)): 1abcde

12 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1abcde

13 

14 

15client = TestClient(app) 1abcde

16 

17 

18def test_security_http_digest(): 1abcde

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

20 assert response.status_code == 200, response.text 1abcde

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

22 

23 

24def test_security_http_digest_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

28 

29 

30def test_security_http_digest_incorrect_scheme_credentials(): 1abcde

31 response = client.get( 1abcde

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

33 ) 

34 assert response.status_code == 403, response.text 1abcde

35 assert response.json() == {"detail": "Invalid authentication credentials"} 1abcde

36 

37 

38def test_openapi_schema(): 1abcde

39 response = client.get("/openapi.json") 1abcde

40 assert response.status_code == 200, response.text 1abcde

41 assert response.json() == { 1abcde

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 }