Coverage for tests / test_security_http_basic_realm_description.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from base64 import b64encode 1abcd

2 

3from fastapi import FastAPI, Security 1abcd

4from fastapi.security import HTTPBasic, HTTPBasicCredentials 1abcd

5from fastapi.testclient import TestClient 1abcd

6from inline_snapshot import snapshot 1abcd

7 

8app = FastAPI() 1abcd

9 

10security = HTTPBasic(realm="simple", description="HTTPBasic scheme") 1abcd

11 

12 

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

14def read_current_user(credentials: HTTPBasicCredentials = Security(security)): 1abcd

15 return {"username": credentials.username, "password": credentials.password} 1ijkl

16 

17 

18client = TestClient(app) 1abcd

19 

20 

21def test_security_http_basic(): 1abcd

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

23 assert response.status_code == 200, response.text 1ijkl

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

25 

26 

27def test_security_http_basic_no_credentials(): 1abcd

28 response = client.get("/users/me") 1mnop

29 assert response.json() == {"detail": "Not authenticated"} 1mnop

30 assert response.status_code == 401, response.text 1mnop

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

32 

33 

34def test_security_http_basic_invalid_credentials(): 1abcd

35 response = client.get( 1qrst

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

37 ) 

38 assert response.status_code == 401, response.text 1qrst

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

40 assert response.json() == {"detail": "Not authenticated"} 1qrst

41 

42 

43def test_security_http_basic_non_basic_credentials(): 1abcd

44 payload = b64encode(b"johnsecret").decode("ascii") 1efgh

45 auth_header = f"Basic {payload}" 1efgh

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

47 assert response.status_code == 401, response.text 1efgh

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

49 assert response.json() == {"detail": "Not authenticated"} 1efgh

50 

51 

52def test_openapi_schema(): 1abcd

53 response = client.get("/openapi.json") 1uvwx

54 assert response.status_code == 200, response.text 1uvwx

55 assert response.json() == snapshot( 1uvwx

56 { 

57 "openapi": "3.1.0", 

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

59 "paths": { 

60 "/users/me": { 

61 "get": { 

62 "responses": { 

63 "200": { 

64 "description": "Successful Response", 

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

66 } 

67 }, 

68 "summary": "Read Current User", 

69 "operationId": "read_current_user_users_me_get", 

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

71 } 

72 } 

73 }, 

74 "components": { 

75 "securitySchemes": { 

76 "HTTPBasic": { 

77 "type": "http", 

78 "scheme": "basic", 

79 "description": "HTTPBasic scheme", 

80 } 

81 } 

82 }, 

83 } 

84 )