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

30 statements  

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

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsDict 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.dependencies.{request.param}") 1abcdef

20 

21 client = TestClient(mod.app) 1abcdef

22 return client 1abcdef

23 

24 

25def test_get_no_headers(client: TestClient): 1abcdef

26 response = client.get("/items/") 1ghijkl

27 assert response.status_code == 422, response.text 1ghijkl

28 assert response.json() == IsDict( 1ghijkl

29 { 

30 "detail": [ 

31 { 

32 "type": "missing", 

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

34 "msg": "Field required", 

35 "input": None, 

36 }, 

37 { 

38 "type": "missing", 

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

40 "msg": "Field required", 

41 "input": None, 

42 }, 

43 ] 

44 } 

45 ) | IsDict( 

46 # TODO: remove when deprecating Pydantic v1 

47 { 

48 "detail": [ 

49 { 

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

51 "msg": "field required", 

52 "type": "value_error.missing", 

53 }, 

54 { 

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

56 "msg": "field required", 

57 "type": "value_error.missing", 

58 }, 

59 ] 

60 } 

61 ) 

62 

63 

64def test_get_invalid_one_header(client: TestClient): 1abcdef

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

66 assert response.status_code == 400, response.text 1mnopqr

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

68 

69 

70def test_get_invalid_second_header(client: TestClient): 1abcdef

71 response = client.get( 1stuvwx

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

73 ) 

74 assert response.status_code == 400, response.text 1stuvwx

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

76 

77 

78def test_get_valid_headers(client: TestClient): 1abcdef

79 response = client.get( 1yzABCD

80 "/items/", 

81 headers={ 

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

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

84 }, 

85 ) 

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

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

88 

89 

90def test_openapi_schema(client: TestClient): 1abcdef

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

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

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

94 "openapi": "3.1.0", 

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

96 "paths": { 

97 "/items/": { 

98 "get": { 

99 "responses": { 

100 "200": { 

101 "description": "Successful Response", 

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

103 }, 

104 "422": { 

105 "description": "Validation Error", 

106 "content": { 

107 "application/json": { 

108 "schema": { 

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

110 } 

111 } 

112 }, 

113 }, 

114 }, 

115 "summary": "Read Items", 

116 "operationId": "read_items_items__get", 

117 "parameters": [ 

118 { 

119 "required": True, 

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

121 "name": "x-token", 

122 "in": "header", 

123 }, 

124 { 

125 "required": True, 

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

127 "name": "x-key", 

128 "in": "header", 

129 }, 

130 ], 

131 } 

132 } 

133 }, 

134 "components": { 

135 "schemas": { 

136 "ValidationError": { 

137 "title": "ValidationError", 

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

139 "type": "object", 

140 "properties": { 

141 "loc": { 

142 "title": "Location", 

143 "type": "array", 

144 "items": { 

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

146 }, 

147 }, 

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

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

150 }, 

151 }, 

152 "HTTPValidationError": { 

153 "title": "HTTPValidationError", 

154 "type": "object", 

155 "properties": { 

156 "detail": { 

157 "title": "Detail", 

158 "type": "array", 

159 "items": {"$ref": "#/components/schemas/ValidationError"}, 

160 } 

161 }, 

162 }, 

163 } 

164 }, 

165 }