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

21 statements  

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

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 "tutorial001_an_py310", 

12 ], 

13) 

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

15 mod = importlib.import_module( 1abc

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

17 ) 

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_get_me(client: TestClient): 1abdc

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

25 assert response.status_code == 200 1efg

26 assert response.json() == { 1efg

27 "message": "You are authenticated", 

28 "token": "secrettoken", 

29 } 

30 

31 

32def test_get_me_no_credentials(client: TestClient): 1abdc

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

34 assert response.status_code == 403 1hij

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

36 

37 

38def test_openapi_schema(client: TestClient): 1abdc

39 response = client.get("/openapi.json") 1klm

40 assert response.status_code == 200, response.text 1klm

41 assert response.json() == snapshot( 1klm

42 { 

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/me": { 

47 "get": { 

48 "summary": "Read Me", 

49 "operationId": "read_me_me_get", 

50 "responses": { 

51 "200": { 

52 "description": "Successful Response", 

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

54 } 

55 }, 

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

57 } 

58 } 

59 }, 

60 "components": { 

61 "securitySchemes": { 

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

63 } 

64 }, 

65 } 

66 )