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

33 statements  

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

1import importlib 1mnopqr

2 

3from fastapi.testclient import TestClient 1mnopqr

4 

5from ...utils import needs_pydanticv2 1mnopqr

6 

7 

8def get_client() -> TestClient: 1mnopqr

9 from docs_src.conditional_openapi import tutorial001 1gashbticujdvkewlfx

10 

11 importlib.reload(tutorial001) 1gashbticujdvkewlfx

12 

13 client = TestClient(tutorial001.app) 1gashbticujdvkewlfx

14 return client 1gashbticujdvkewlfx

15 

16 

17@needs_pydanticv2 1mnopqr

18def test_disable_openapi(monkeypatch): 1mnopqr

19 monkeypatch.setenv("OPENAPI_URL", "") 1abcdef

20 # Load the client after setting the env var 

21 client = get_client() 1abcdef

22 response = client.get("/openapi.json") 1abcdef

23 assert response.status_code == 404, response.text 1abcdef

24 response = client.get("/docs") 1abcdef

25 assert response.status_code == 404, response.text 1abcdef

26 response = client.get("/redoc") 1abcdef

27 assert response.status_code == 404, response.text 1abcdef

28 

29 

30@needs_pydanticv2 1mnopqr

31def test_root(): 1mnopqr

32 client = get_client() 1stuvwx

33 response = client.get("/") 1stuvwx

34 assert response.status_code == 200 1stuvwx

35 assert response.json() == {"message": "Hello World"} 1stuvwx

36 

37 

38@needs_pydanticv2 1mnopqr

39def test_default_openapi(): 1mnopqr

40 client = get_client() 1ghijkl

41 response = client.get("/docs") 1ghijkl

42 assert response.status_code == 200, response.text 1ghijkl

43 response = client.get("/redoc") 1ghijkl

44 assert response.status_code == 200, response.text 1ghijkl

45 response = client.get("/openapi.json") 1ghijkl

46 assert response.json() == { 1ghijkl

47 "openapi": "3.1.0", 

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

49 "paths": { 

50 "/": { 

51 "get": { 

52 "summary": "Root", 

53 "operationId": "root__get", 

54 "responses": { 

55 "200": { 

56 "description": "Successful Response", 

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

58 } 

59 }, 

60 } 

61 } 

62 }, 

63 }