| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture( |
| 9 |
name="client", |
| 10 |
params=[ |
| 11 |
pytest.param("tutorial012_py310"), |
| 12 |
pytest.param("tutorial012_an_py310"), |
| 13 |
], |
| 14 |
) |
| 15 |
def get_client(request: pytest.FixtureRequest): |
| 16 |
mod = importlib.import_module(f"docs_src.dependencies.{request.param}") |
| 17 |
|
| 18 |
client = TestClient(mod.app) |
| 19 |
return client |
| 20 |
|
| 21 |
|
| 22 |
def test_get_no_headers_items(client: TestClient): |
| 23 |
response = client.get("/items/") |
| 24 |
assert response.status_code == 422, response.text |
| 25 |
assert response.json() == { |
| 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 |
|
| 43 |
def test_get_no_headers_users(client: TestClient): |
| 44 |
response = client.get("/users/") |
| 45 |
assert response.status_code == 422, response.text |
| 46 |
assert response.json() == { |
| 47 |
"detail": [ |
| 48 |
{ |
| 49 |
"type": "missing", |
| 50 |
"loc": ["header", "x-token"], |
| 51 |
"msg": "Field required", |
| 52 |
"input": None, |
| 53 |
}, |
| 54 |
{ |
| 55 |
"type": "missing", |
| 56 |
"loc": ["header", "x-key"], |
| 57 |
"msg": "Field required", |
| 58 |
"input": None, |
| 59 |
}, |
| 60 |
] |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
def test_get_invalid_one_header_items(client: TestClient): |
| 65 |
response = client.get("/items/", headers={"X-Token": "invalid"}) |
| 66 |
assert response.status_code == 400, response.text |
| 67 |
assert response.json() == {"detail": "X-Token header invalid"} |
| 68 |
|
| 69 |
|
| 70 |
def test_get_invalid_one_users(client: TestClient): |
| 71 |
response = client.get("/users/", headers={"X-Token": "invalid"}) |
| 72 |
assert response.status_code == 400, response.text |
| 73 |
assert response.json() == {"detail": "X-Token header invalid"} |
| 74 |
|
| 75 |
|
| 76 |
def test_get_invalid_second_header_items(client: TestClient): |
| 77 |
response = client.get( |
| 78 |
"/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} |
| 79 |
) |
| 80 |
assert response.status_code == 400, response.text |
| 81 |
assert response.json() == {"detail": "X-Key header invalid"} |
| 82 |
|
| 83 |
|
| 84 |
def test_get_invalid_second_header_users(client: TestClient): |
| 85 |
response = client.get( |
| 86 |
"/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} |
| 87 |
) |
| 88 |
assert response.status_code == 400, response.text |
| 89 |
assert response.json() == {"detail": "X-Key header invalid"} |
| 90 |
|
| 91 |
|
| 92 |
def test_get_valid_headers_items(client: TestClient): |
| 93 |
response = client.get( |
| 94 |
"/items/", |
| 95 |
headers={ |
| 96 |
"X-Token": "fake-super-secret-token", |
| 97 |
"X-Key": "fake-super-secret-key", |
| 98 |
}, |
| 99 |
) |
| 100 |
assert response.status_code == 200, response.text |
| 101 |
assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}] |
| 102 |
|
| 103 |
|
| 104 |
def test_get_valid_headers_users(client: TestClient): |
| 105 |
response = client.get( |
| 106 |
"/users/", |
| 107 |
headers={ |
| 108 |
"X-Token": "fake-super-secret-token", |
| 109 |
"X-Key": "fake-super-secret-key", |
| 110 |
}, |
| 111 |
) |
| 112 |
assert response.status_code == 200, response.text |
| 113 |
assert response.json() == [{"username": "Rick"}, {"username": "Morty"}] |
| 114 |
|
| 115 |
|
| 116 |
def test_openapi_schema(client: TestClient): |
| 117 |
response = client.get("/openapi.json") |
| 118 |
assert response.status_code == 200, response.text |
| 119 |
assert response.json() == snapshot( |
| 120 |
{ |
| 121 |
"openapi": "3.1.0", |
| 122 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 123 |
"paths": { |
| 124 |
"/items/": { |
| 125 |
"get": { |
| 126 |
"summary": "Read Items", |
| 127 |
"operationId": "read_items_items__get", |
| 128 |
"parameters": [ |
| 129 |
{ |
| 130 |
"required": True, |
| 131 |
"schema": {"title": "X-Token", "type": "string"}, |
| 132 |
"name": "x-token", |
| 133 |
"in": "header", |
| 134 |
}, |
| 135 |
{ |
| 136 |
"required": True, |
| 137 |
"schema": {"title": "X-Key", "type": "string"}, |
| 138 |
"name": "x-key", |
| 139 |
"in": "header", |
| 140 |
}, |
| 141 |
], |
| 142 |
"responses": { |
| 143 |
"200": { |
| 144 |
"description": "Successful Response", |
| 145 |
"content": {"application/json": {"schema": {}}}, |
| 146 |
}, |
| 147 |
"422": { |
| 148 |
"description": "Validation Error", |
| 149 |
"content": { |
| 150 |
"application/json": { |
| 151 |
"schema": { |
| 152 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 153 |
} |
| 154 |
} |
| 155 |
}, |
| 156 |
}, |
| 157 |
}, |
| 158 |
} |
| 159 |
}, |
| 160 |
"/users/": { |
| 161 |
"get": { |
| 162 |
"summary": "Read Users", |
| 163 |
"operationId": "read_users_users__get", |
| 164 |
"parameters": [ |
| 165 |
{ |
| 166 |
"required": True, |
| 167 |
"schema": {"title": "X-Token", "type": "string"}, |
| 168 |
"name": "x-token", |
| 169 |
"in": "header", |
| 170 |
}, |
| 171 |
{ |
| 172 |
"required": True, |
| 173 |
"schema": {"title": "X-Key", "type": "string"}, |
| 174 |
"name": "x-key", |
| 175 |
"in": "header", |
| 176 |
}, |
| 177 |
], |
| 178 |
"responses": { |
| 179 |
"200": { |
| 180 |
"description": "Successful Response", |
| 181 |
"content": {"application/json": {"schema": {}}}, |
| 182 |
}, |
| 183 |
"422": { |
| 184 |
"description": "Validation Error", |
| 185 |
"content": { |
| 186 |
"application/json": { |
| 187 |
"schema": { |
| 188 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 189 |
} |
| 190 |
} |
| 191 |
}, |
| 192 |
}, |
| 193 |
}, |
| 194 |
} |
| 195 |
}, |
| 196 |
}, |
| 197 |
"components": { |
| 198 |
"schemas": { |
| 199 |
"HTTPValidationError": { |
| 200 |
"title": "HTTPValidationError", |
| 201 |
"type": "object", |
| 202 |
"properties": { |
| 203 |
"detail": { |
| 204 |
"title": "Detail", |
| 205 |
"type": "array", |
| 206 |
"items": { |
| 207 |
"$ref": "#/components/schemas/ValidationError" |
| 208 |
}, |
| 209 |
} |
| 210 |
}, |
| 211 |
}, |
| 212 |
"ValidationError": { |
| 213 |
"title": "ValidationError", |
| 214 |
"required": ["loc", "msg", "type"], |
| 215 |
"type": "object", |
| 216 |
"properties": { |
| 217 |
"loc": { |
| 218 |
"title": "Location", |
| 219 |
"type": "array", |
| 220 |
"items": { |
| 221 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 222 |
}, |
| 223 |
}, |
| 224 |
"msg": {"title": "Message", "type": "string"}, |
| 225 |
"type": {"title": "Error Type", "type": "string"}, |
| 226 |
"input": {"title": "Input"}, |
| 227 |
"ctx": {"title": "Context", "type": "object"}, |
| 228 |
}, |
| 229 |
}, |
| 230 |
} |
| 231 |
}, |
| 232 |
} |
| 233 |
) |