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

34 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="client") 1eabcd

9def get_client(): 1eabcd

10 from docs_src.dependencies.tutorial006_an_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17def test_get_no_headers(client: TestClient): 1eabcd

18 response = client.get("/items/") 1abcd

19 assert response.status_code == 422, response.text 1abcd

20 assert response.json() == IsDict( 1abcd

21 { 

22 "detail": [ 

23 { 

24 "type": "missing", 

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

26 "msg": "Field required", 

27 "input": None, 

28 }, 

29 { 

30 "type": "missing", 

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

32 "msg": "Field required", 

33 "input": None, 

34 }, 

35 ] 

36 } 

37 ) | IsDict( 

38 # TODO: remove when deprecating Pydantic v1 

39 { 

40 "detail": [ 

41 { 

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

43 "msg": "field required", 

44 "type": "value_error.missing", 

45 }, 

46 { 

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

48 "msg": "field required", 

49 "type": "value_error.missing", 

50 }, 

51 ] 

52 } 

53 ) 

54 

55 

56@needs_py39 1eabcd

57def test_get_invalid_one_header(client: TestClient): 1eabcd

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

59 assert response.status_code == 400, response.text 1abcd

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

61 

62 

63@needs_py39 1eabcd

64def test_get_invalid_second_header(client: TestClient): 1eabcd

65 response = client.get( 1abcd

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

67 ) 

68 assert response.status_code == 400, response.text 1abcd

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

70 

71 

72@needs_py39 1eabcd

73def test_get_valid_headers(client: TestClient): 1eabcd

74 response = client.get( 1abcd

75 "/items/", 

76 headers={ 

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

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

79 }, 

80 ) 

81 assert response.status_code == 200, response.text 1abcd

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

83 

84 

85@needs_py39 1eabcd

86def test_openapi_schema(client: TestClient): 1eabcd

87 response = client.get("/openapi.json") 1abcd

88 assert response.status_code == 200, response.text 1abcd

89 assert response.json() == { 1abcd

90 "openapi": "3.1.0", 

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

92 "paths": { 

93 "/items/": { 

94 "get": { 

95 "responses": { 

96 "200": { 

97 "description": "Successful Response", 

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

99 }, 

100 "422": { 

101 "description": "Validation Error", 

102 "content": { 

103 "application/json": { 

104 "schema": { 

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

106 } 

107 } 

108 }, 

109 }, 

110 }, 

111 "summary": "Read Items", 

112 "operationId": "read_items_items__get", 

113 "parameters": [ 

114 { 

115 "required": True, 

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

117 "name": "x-token", 

118 "in": "header", 

119 }, 

120 { 

121 "required": True, 

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

123 "name": "x-key", 

124 "in": "header", 

125 }, 

126 ], 

127 } 

128 } 

129 }, 

130 "components": { 

131 "schemas": { 

132 "ValidationError": { 

133 "title": "ValidationError", 

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

135 "type": "object", 

136 "properties": { 

137 "loc": { 

138 "title": "Location", 

139 "type": "array", 

140 "items": { 

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

142 }, 

143 }, 

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

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

146 }, 

147 }, 

148 "HTTPValidationError": { 

149 "title": "HTTPValidationError", 

150 "type": "object", 

151 "properties": { 

152 "detail": { 

153 "title": "Detail", 

154 "type": "array", 

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

156 } 

157 }, 

158 }, 

159 } 

160 }, 

161 }