Coverage for tests / test_tutorial / test_security / test_tutorial007.py: 100%

44 statements  

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

1import importlib 1abdc

2from base64 import b64encode 1abdc

3 

4import pytest 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import snapshot 1abdc

7 

8 

9@pytest.fixture( 1abdc

10 name="client", 

11 params=[ 

12 pytest.param("tutorial007_py310"), 

13 pytest.param("tutorial007_an_py310"), 

14 ], 

15) 

16def get_client(request: pytest.FixtureRequest): 1abdc

17 mod = importlib.import_module(f"docs_src.security.{request.param}") 1abc

18 return TestClient(mod.app) 1abc

19 

20 

21def test_security_http_basic(client: TestClient): 1abdc

22 response = client.get("/users/me", auth=("stanleyjobson", "swordfish")) 1tuv

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

24 assert response.json() == {"username": "stanleyjobson"} 1tuv

25 

26 

27def test_security_http_basic_no_credentials(client: TestClient): 1abdc

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

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

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

31 assert response.headers["WWW-Authenticate"] == "Basic" 1hij

32 

33 

34def test_security_http_basic_invalid_credentials(client: TestClient): 1abdc

35 response = client.get( 1klm

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

37 ) 

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

39 assert response.headers["WWW-Authenticate"] == "Basic" 1klm

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

41 

42 

43def test_security_http_basic_non_basic_credentials(client: TestClient): 1abdc

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

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

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

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

48 assert response.headers["WWW-Authenticate"] == "Basic" 1efg

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

50 

51 

52def test_security_http_basic_invalid_username(client: TestClient): 1abdc

53 response = client.get("/users/me", auth=("alice", "swordfish")) 1nop

54 assert response.status_code == 401, response.text 1nop

55 assert response.json() == {"detail": "Incorrect username or password"} 1nop

56 assert response.headers["WWW-Authenticate"] == "Basic" 1nop

57 

58 

59def test_security_http_basic_invalid_password(client: TestClient): 1abdc

60 response = client.get("/users/me", auth=("stanleyjobson", "wrongpassword")) 1qrs

61 assert response.status_code == 401, response.text 1qrs

62 assert response.json() == {"detail": "Incorrect username or password"} 1qrs

63 assert response.headers["WWW-Authenticate"] == "Basic" 1qrs

64 

65 

66def test_openapi_schema(client: TestClient): 1abdc

67 response = client.get("/openapi.json") 1wxy

68 assert response.status_code == 200, response.text 1wxy

69 assert response.json() == snapshot( 1wxy

70 { 

71 "openapi": "3.1.0", 

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

73 "paths": { 

74 "/users/me": { 

75 "get": { 

76 "responses": { 

77 "200": { 

78 "description": "Successful Response", 

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

80 } 

81 }, 

82 "summary": "Read Current User", 

83 "operationId": "read_current_user_users_me_get", 

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

85 } 

86 } 

87 }, 

88 "components": { 

89 "securitySchemes": {"HTTPBasic": {"type": "http", "scheme": "basic"}} 

90 }, 

91 } 

92 )