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

35 statements  

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

1import importlib 1abcdef

2from base64 import b64encode 1abcdef

3 

4import pytest 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

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): 1abcdef

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

20 

21 client = TestClient(mod.app) 1abcdef

22 return client 1abcdef

23 

24 

25def test_security_http_basic(client: TestClient): 1abcdef

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

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

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

29 

30 

31def test_security_http_basic_no_credentials(client: TestClient): 1abcdef

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

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

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

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

36 

37 

38def test_security_http_basic_invalid_credentials(client: TestClient): 1abcdef

39 response = client.get( 1stuvwx

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

41 ) 

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

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

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

45 

46 

47def test_security_http_basic_non_basic_credentials(client: TestClient): 1abcdef

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

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

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

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

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

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

54 

55 

56def test_openapi_schema(client: TestClient): 1abcdef

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

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

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

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 }