Coverage for tests/test_security_http_basic_realm_description.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1from base64 import b64encode 1abcdef

2 

3from fastapi import FastAPI, Security 1abcdef

4from fastapi.security import HTTPBasic, HTTPBasicCredentials 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7app = FastAPI() 1abcdef

8 

9security = HTTPBasic(realm="simple", description="HTTPBasic scheme") 1abcdef

10 

11 

12@app.get("/users/me") 1abcdef

13def read_current_user(credentials: HTTPBasicCredentials = Security(security)): 1abcdef

14 return {"username": credentials.username, "password": credentials.password} 1mnopqr

15 

16 

17client = TestClient(app) 1abcdef

18 

19 

20def test_security_http_basic(): 1abcdef

21 response = client.get("/users/me", auth=("john", "secret")) 1mnopqr

22 assert response.status_code == 200, response.text 1mnopqr

23 assert response.json() == {"username": "john", "password": "secret"} 1mnopqr

24 

25 

26def test_security_http_basic_no_credentials(): 1abcdef

27 response = client.get("/users/me") 1stuvwx

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

29 assert response.status_code == 401, response.text 1stuvwx

30 assert response.headers["WWW-Authenticate"] == 'Basic realm="simple"' 1stuvwx

31 

32 

33def test_security_http_basic_invalid_credentials(): 1abcdef

34 response = client.get( 1yzABCD

35 "/users/me", headers={"Authorization": "Basic notabase64token"} 

36 ) 

37 assert response.status_code == 401, response.text 1yzABCD

38 assert response.headers["WWW-Authenticate"] == 'Basic realm="simple"' 1yzABCD

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

40 

41 

42def test_security_http_basic_non_basic_credentials(): 1abcdef

43 payload = b64encode(b"johnsecret").decode("ascii") 1ghijkl

44 auth_header = f"Basic {payload}" 1ghijkl

45 response = client.get("/users/me", headers={"Authorization": auth_header}) 1ghijkl

46 assert response.status_code == 401, response.text 1ghijkl

47 assert response.headers["WWW-Authenticate"] == 'Basic realm="simple"' 1ghijkl

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

49 

50 

51def test_openapi_schema(): 1abcdef

52 response = client.get("/openapi.json") 1EFGHIJ

53 assert response.status_code == 200, response.text 1EFGHIJ

54 assert response.json() == { 1EFGHIJ

55 "openapi": "3.1.0", 

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

57 "paths": { 

58 "/users/me": { 

59 "get": { 

60 "responses": { 

61 "200": { 

62 "description": "Successful Response", 

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

64 } 

65 }, 

66 "summary": "Read Current User", 

67 "operationId": "read_current_user_users_me_get", 

68 "security": [{"HTTPBasic": []}], 

69 } 

70 } 

71 }, 

72 "components": { 

73 "securitySchemes": { 

74 "HTTPBasic": { 

75 "type": "http", 

76 "scheme": "basic", 

77 "description": "HTTPBasic scheme", 

78 } 

79 } 

80 }, 

81 }