Coverage for tests/test_tutorial/test_authentication_error_status_code/test_tutorial001.py: 100%

22 statements  

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

1import importlib 1abcdefg

2 

3import pytest 1abcdefg

4from fastapi.testclient import TestClient 1abcdefg

5from inline_snapshot import snapshot 1abcdefg

6 

7from ...utils import needs_py39 1abcdefg

8 

9 

10@pytest.fixture( 1abcdefg

11 name="client", 

12 params=[ 

13 "tutorial001_an", 

14 pytest.param("tutorial001_an_py39", marks=needs_py39), 

15 ], 

16) 

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

18 mod = importlib.import_module( 1abcdefg

19 f"docs_src.authentication_error_status_code.{request.param}" 

20 ) 

21 

22 client = TestClient(mod.app) 1abcdefg

23 return client 1abcdefg

24 

25 

26def test_get_me(client: TestClient): 1abcdefg

27 response = client.get("/me", headers={"Authorization": "Bearer secrettoken"}) 1hijklmn

28 assert response.status_code == 200 1hijklmn

29 assert response.json() == { 1hijklmn

30 "message": "You are authenticated", 

31 "token": "secrettoken", 

32 } 

33 

34 

35def test_get_me_no_credentials(client: TestClient): 1abcdefg

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

37 assert response.status_code == 403 1opqrstu

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

39 

40 

41def test_openapi_schema(client: TestClient): 1abcdefg

42 response = client.get("/openapi.json") 1vwxyzAB

43 assert response.status_code == 200, response.text 1vwxyzAB

44 assert response.json() == snapshot( 1vwxyzAB

45 { 

46 "openapi": "3.1.0", 

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

48 "paths": { 

49 "/me": { 

50 "get": { 

51 "summary": "Read Me", 

52 "operationId": "read_me_me_get", 

53 "responses": { 

54 "200": { 

55 "description": "Successful Response", 

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

57 } 

58 }, 

59 "security": [{"HTTPBearer403": []}], 

60 } 

61 } 

62 }, 

63 "components": { 

64 "securitySchemes": { 

65 "HTTPBearer403": {"type": "http", "scheme": "bearer"} 

66 } 

67 }, 

68 } 

69 )