Coverage for tests/test_tutorial/test_dependencies/test_tutorial006_an.py: 100%
24 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from dirty_equals import IsDict 1abcde
2from fastapi.testclient import TestClient 1abcde
4from docs_src.dependencies.tutorial006_an import app 1abcde
6client = TestClient(app) 1abcde
9def test_get_no_headers(): 1abcde
10 response = client.get("/items/") 1abcde
11 assert response.status_code == 422, response.text 1abcde
12 assert response.json() == IsDict( 1abcde
13 {
14 "detail": [
15 {
16 "type": "missing",
17 "loc": ["header", "x-token"],
18 "msg": "Field required",
19 "input": None,
20 },
21 {
22 "type": "missing",
23 "loc": ["header", "x-key"],
24 "msg": "Field required",
25 "input": None,
26 },
27 ]
28 }
29 ) | IsDict(
30 # TODO: remove when deprecating Pydantic v1
31 {
32 "detail": [
33 {
34 "loc": ["header", "x-token"],
35 "msg": "field required",
36 "type": "value_error.missing",
37 },
38 {
39 "loc": ["header", "x-key"],
40 "msg": "field required",
41 "type": "value_error.missing",
42 },
43 ]
44 }
45 )
48def test_get_invalid_one_header(): 1abcde
49 response = client.get("/items/", headers={"X-Token": "invalid"}) 1abcde
50 assert response.status_code == 400, response.text 1abcde
51 assert response.json() == {"detail": "X-Token header invalid"} 1abcde
54def test_get_invalid_second_header(): 1abcde
55 response = client.get( 1abcde
56 "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
57 )
58 assert response.status_code == 400, response.text 1abcde
59 assert response.json() == {"detail": "X-Key header invalid"} 1abcde
62def test_get_valid_headers(): 1abcde
63 response = client.get( 1abcde
64 "/items/",
65 headers={
66 "X-Token": "fake-super-secret-token",
67 "X-Key": "fake-super-secret-key",
68 },
69 )
70 assert response.status_code == 200, response.text 1abcde
71 assert response.json() == [{"item": "Foo"}, {"item": "Bar"}] 1abcde
74def test_openapi_schema(): 1abcde
75 response = client.get("/openapi.json") 1abcde
76 assert response.status_code == 200, response.text 1abcde
77 assert response.json() == { 1abcde
78 "openapi": "3.1.0",
79 "info": {"title": "FastAPI", "version": "0.1.0"},
80 "paths": {
81 "/items/": {
82 "get": {
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {"application/json": {"schema": {}}},
87 },
88 "422": {
89 "description": "Validation Error",
90 "content": {
91 "application/json": {
92 "schema": {
93 "$ref": "#/components/schemas/HTTPValidationError"
94 }
95 }
96 },
97 },
98 },
99 "summary": "Read Items",
100 "operationId": "read_items_items__get",
101 "parameters": [
102 {
103 "required": True,
104 "schema": {"title": "X-Token", "type": "string"},
105 "name": "x-token",
106 "in": "header",
107 },
108 {
109 "required": True,
110 "schema": {"title": "X-Key", "type": "string"},
111 "name": "x-key",
112 "in": "header",
113 },
114 ],
115 }
116 }
117 },
118 "components": {
119 "schemas": {
120 "ValidationError": {
121 "title": "ValidationError",
122 "required": ["loc", "msg", "type"],
123 "type": "object",
124 "properties": {
125 "loc": {
126 "title": "Location",
127 "type": "array",
128 "items": {
129 "anyOf": [{"type": "string"}, {"type": "integer"}]
130 },
131 },
132 "msg": {"title": "Message", "type": "string"},
133 "type": {"title": "Error Type", "type": "string"},
134 },
135 },
136 "HTTPValidationError": {
137 "title": "HTTPValidationError",
138 "type": "object",
139 "properties": {
140 "detail": {
141 "title": "Detail",
142 "type": "array",
143 "items": {"$ref": "#/components/schemas/ValidationError"},
144 }
145 },
146 },
147 }
148 },
149 }