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

35 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import importlib 1abcdefg

2from base64 import b64encode 1abcdefg

3 

4import pytest 1abcdefg

5from fastapi.testclient import TestClient 1abcdefg

6 

7from ...utils import needs_py39 1abcdefg

8 

9 

10@pytest.fixture( 1abcdefg

11 name="client", 

12 params=[ 

13 "tutorial006", 

14 "tutorial006_an", 

15 pytest.param("tutorial006_an_py39", marks=needs_py39), 

16 ], 

17) 

18def get_client(request: pytest.FixtureRequest): 1abcdefg

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

20 

21 client = TestClient(mod.app) 1abcdefg

22 return client 1abcdefg

23 

24 

25def test_security_http_basic(client: TestClient): 1abcdefg

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

27 assert response.status_code == 200, response.text 1CDEFGHI

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

29 

30 

31def test_security_http_basic_no_credentials(client: TestClient): 1abcdefg

32 response = client.get("/users/me") 1opqrstu

33 assert response.json() == {"detail": "Not authenticated"} 1opqrstu

34 assert response.status_code == 401, response.text 1opqrstu

35 assert response.headers["WWW-Authenticate"] == "Basic" 1opqrstu

36 

37 

38def test_security_http_basic_invalid_credentials(client: TestClient): 1abcdefg

39 response = client.get( 1vwxyzAB

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

41 ) 

42 assert response.status_code == 401, response.text 1vwxyzAB

43 assert response.headers["WWW-Authenticate"] == "Basic" 1vwxyzAB

44 assert response.json() == {"detail": "Not authenticated"} 1vwxyzAB

45 

46 

47def test_security_http_basic_non_basic_credentials(client: TestClient): 1abcdefg

48 payload = b64encode(b"johnsecret").decode("ascii") 1hijklmn

49 auth_header = f"Basic {payload}" 1hijklmn

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

51 assert response.status_code == 401, response.text 1hijklmn

52 assert response.headers["WWW-Authenticate"] == "Basic" 1hijklmn

53 assert response.json() == {"detail": "Not authenticated"} 1hijklmn

54 

55 

56def test_openapi_schema(client: TestClient): 1abcdefg

57 response = client.get("/openapi.json") 1JKLMNOP

58 assert response.status_code == 200, response.text 1JKLMNOP

59 assert response.json() == { 1JKLMNOP

60 "openapi": "3.1.0", 

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

62 "paths": { 

63 "/users/me": { 

64 "get": { 

65 "responses": { 

66 "200": { 

67 "description": "Successful Response", 

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

69 } 

70 }, 

71 "summary": "Read Current User", 

72 "operationId": "read_current_user_users_me_get", 

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

74 } 

75 } 

76 }, 

77 "components": { 

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

79 }, 

80 }