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

35 statements  

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

1import importlib 1abcd

2from base64 import b64encode 1abcd

3 

4import pytest 1abcd

5from fastapi.testclient import TestClient 1abcd

6from inline_snapshot import snapshot 1abcd

7 

8 

9@pytest.fixture( 1abcd

10 name="client", 

11 params=[ 

12 pytest.param("tutorial006_py310"), 

13 pytest.param("tutorial006_an_py310"), 

14 ], 

15) 

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

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

18 

19 client = TestClient(mod.app) 1BCDEFGHIJKLMNOPQRSTUVWXYZ0123456

20 return client 1BCDEFGHIJKLMNOPQRSTUVWXYZ0123456

21 

22 

23def test_security_http_basic(client: TestClient): 1abcd

24 response = client.get("/users/me", auth=("john", "secret")) 1789!#$%

25 assert response.status_code == 200, response.text 1789!#$%

26 assert response.json() == {"username": "john", "password": "secret"} 1789!#$%

27 

28 

29def test_security_http_basic_no_credentials(client: TestClient): 1abcd

30 response = client.get("/users/me") 1lmnopqrs

31 assert response.json() == {"detail": "Not authenticated"} 1lmnopqrs

32 assert response.status_code == 401, response.text 1lmnopqrs

33 assert response.headers["WWW-Authenticate"] == "Basic" 1lmnopqrs

34 

35 

36def test_security_http_basic_invalid_credentials(client: TestClient): 1abcd

37 response = client.get( 1tuvwxyzA

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

39 ) 

40 assert response.status_code == 401, response.text 1tuvwxyzA

41 assert response.headers["WWW-Authenticate"] == "Basic" 1tuvwxyzA

42 assert response.json() == {"detail": "Not authenticated"} 1tuvwxyzA

43 

44 

45def test_security_http_basic_non_basic_credentials(client: TestClient): 1abcd

46 payload = b64encode(b"johnsecret").decode("ascii") 1efghijk

47 auth_header = f"Basic {payload}" 1efghijk

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

49 assert response.status_code == 401, response.text 1efghijk

50 assert response.headers["WWW-Authenticate"] == "Basic" 1efghijk

51 assert response.json() == {"detail": "Not authenticated"} 1efghijk

52 

53 

54def test_openapi_schema(client: TestClient): 1abcd

55 response = client.get("/openapi.json") 1'()*+,-

56 assert response.status_code == 200, response.text 1'()*+,-

57 assert response.json() == snapshot( 1'()*+,-

58 { 

59 "openapi": "3.1.0", 

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

61 "paths": { 

62 "/users/me": { 

63 "get": { 

64 "responses": { 

65 "200": { 

66 "description": "Successful Response", 

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

68 } 

69 }, 

70 "summary": "Read Current User", 

71 "operationId": "read_current_user_users_me_get", 

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

73 } 

74 } 

75 }, 

76 "components": { 

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

78 }, 

79 } 

80 )