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

29 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 pytest.param("tutorial006_py310"), 

12 pytest.param("tutorial006_an_py310"), 

13 ], 

14) 

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

16 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abc

17 

18 client = TestClient(mod.app) 1abc

19 return client 1abc

20 

21 

22def test_get_no_headers(client: TestClient): 1abdc

23 response = client.get("/items/") 1efg

24 assert response.status_code == 422, response.text 1efg

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

26 "detail": [ 

27 { 

28 "type": "missing", 

29 "loc": ["header", "x-token"], 

30 "msg": "Field required", 

31 "input": None, 

32 }, 

33 { 

34 "type": "missing", 

35 "loc": ["header", "x-key"], 

36 "msg": "Field required", 

37 "input": None, 

38 }, 

39 ] 

40 } 

41 

42 

43def test_get_invalid_one_header(client: TestClient): 1abdc

44 response = client.get("/items/", headers={"X-Token": "invalid"}) 1hij

45 assert response.status_code == 400, response.text 1hij

46 assert response.json() == {"detail": "X-Token header invalid"} 1hij

47 

48 

49def test_get_invalid_second_header(client: TestClient): 1abdc

50 response = client.get( 1klm

51 "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} 

52 ) 

53 assert response.status_code == 400, response.text 1klm

54 assert response.json() == {"detail": "X-Key header invalid"} 1klm

55 

56 

57def test_get_valid_headers(client: TestClient): 1abdc

58 response = client.get( 1nop

59 "/items/", 

60 headers={ 

61 "X-Token": "fake-super-secret-token", 

62 "X-Key": "fake-super-secret-key", 

63 }, 

64 ) 

65 assert response.status_code == 200, response.text 1nop

66 assert response.json() == [{"item": "Foo"}, {"item": "Bar"}] 1nop

67 

68 

69def test_openapi_schema(client: TestClient): 1abdc

70 response = client.get("/openapi.json") 1qrs

71 assert response.status_code == 200, response.text 1qrs

72 assert response.json() == snapshot( 1qrs

73 { 

74 "openapi": "3.1.0", 

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

76 "paths": { 

77 "/items/": { 

78 "get": { 

79 "responses": { 

80 "200": { 

81 "description": "Successful Response", 

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

83 }, 

84 "422": { 

85 "description": "Validation Error", 

86 "content": { 

87 "application/json": { 

88 "schema": { 

89 "$ref": "#/components/schemas/HTTPValidationError" 

90 } 

91 } 

92 }, 

93 }, 

94 }, 

95 "summary": "Read Items", 

96 "operationId": "read_items_items__get", 

97 "parameters": [ 

98 { 

99 "required": True, 

100 "schema": {"title": "X-Token", "type": "string"}, 

101 "name": "x-token", 

102 "in": "header", 

103 }, 

104 { 

105 "required": True, 

106 "schema": {"title": "X-Key", "type": "string"}, 

107 "name": "x-key", 

108 "in": "header", 

109 }, 

110 ], 

111 } 

112 } 

113 }, 

114 "components": { 

115 "schemas": { 

116 "ValidationError": { 

117 "title": "ValidationError", 

118 "required": ["loc", "msg", "type"], 

119 "type": "object", 

120 "properties": { 

121 "loc": { 

122 "title": "Location", 

123 "type": "array", 

124 "items": { 

125 "anyOf": [{"type": "string"}, {"type": "integer"}] 

126 }, 

127 }, 

128 "msg": {"title": "Message", "type": "string"}, 

129 "type": {"title": "Error Type", "type": "string"}, 

130 "input": {"title": "Input"}, 

131 "ctx": {"title": "Context", "type": "object"}, 

132 }, 

133 }, 

134 "HTTPValidationError": { 

135 "title": "HTTPValidationError", 

136 "type": "object", 

137 "properties": { 

138 "detail": { 

139 "title": "Detail", 

140 "type": "array", 

141 "items": { 

142 "$ref": "#/components/schemas/ValidationError" 

143 }, 

144 } 

145 }, 

146 }, 

147 } 

148 }, 

149 } 

150 )