Debian Codegraph @

tests/test_tutorial/test_cookie_param_models/test_tutorial001.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 from tests.utils import needs_py310
8
9
10 @pytest.fixture(
11 name="client",
12 params=[
13 pytest.param("tutorial001_py310", marks=needs_py310),
14 pytest.param("tutorial001_an_py310", marks=needs_py310),
15 ],
16 )
17 def get_client(request: pytest.FixtureRequest):
18 mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}")
19
20 client = TestClient(mod.app)
21 return client
22
23
24 def test_cookie_param_model(client: TestClient):
25 with client as c:
26 c.cookies.set("session_id", "123")
27 c.cookies.set("fatebook_tracker", "456")
28 c.cookies.set("googall_tracker", "789")
29 response = c.get("/items/")
30 assert response.status_code == 200
31 assert response.json() == {
32 "session_id": "123",
33 "fatebook_tracker": "456",
34 "googall_tracker": "789",
35 }
36
37
38 def test_cookie_param_model_defaults(client: TestClient):
39 with client as c:
40 c.cookies.set("session_id", "123")
41 response = c.get("/items/")
42 assert response.status_code == 200
43 assert response.json() == {
44 "session_id": "123",
45 "fatebook_tracker": None,
46 "googall_tracker": None,
47 }
48
49
50 def test_cookie_param_model_invalid(client: TestClient):
51 response = client.get("/items/")
52 assert response.status_code == 422
53 assert response.json() == snapshot(
54 {
55 "detail": [
56 {
57 "type": "missing",
58 "loc": ["cookie", "session_id"],
59 "msg": "Field required",
60 "input": {},
61 }
62 ]
63 }
64 )
65
66
67 def test_cookie_param_model_extra(client: TestClient):
68 with client as c:
69 c.cookies.set("session_id", "123")
70 c.cookies.set("extra", "track-me-here-too")
71 response = c.get("/items/")
72 assert response.status_code == 200
73 assert response.json() == snapshot(
74 {"session_id": "123", "fatebook_tracker": None, "googall_tracker": None}
75 )
76
77
78 def test_openapi_schema(client: TestClient):
79 response = client.get("/openapi.json")
80 assert response.status_code == 200, response.text
81 assert response.json() == snapshot(
82 {
83 "openapi": "3.1.0",
84 "info": {"title": "FastAPI", "version": "0.1.0"},
85 "paths": {
86 "/items/": {
87 "get": {
88 "summary": "Read Items",
89 "operationId": "read_items_items__get",
90 "parameters": [
91 {
92 "name": "session_id",
93 "in": "cookie",
94 "required": True,
95 "schema": {"type": "string", "title": "Session Id"},
96 },
97 {
98 "name": "fatebook_tracker",
99 "in": "cookie",
100 "required": False,
101 "schema": {
102 "anyOf": [{"type": "string"}, {"type": "null"}],
103 "title": "Fatebook Tracker",
104 },
105 },
106 {
107 "name": "googall_tracker",
108 "in": "cookie",
109 "required": False,
110 "schema": {
111 "anyOf": [{"type": "string"}, {"type": "null"}],
112 "title": "Googall Tracker",
113 },
114 },
115 ],
116 "responses": {
117 "200": {
118 "description": "Successful Response",
119 "content": {"application/json": {"schema": {}}},
120 },
121 "422": {
122 "description": "Validation Error",
123 "content": {
124 "application/json": {
125 "schema": {
126 "$ref": "#/components/schemas/HTTPValidationError"
127 }
128 }
129 },
130 },
131 },
132 }
133 }
134 },
135 "components": {
136 "schemas": {
137 "HTTPValidationError": {
138 "properties": {
139 "detail": {
140 "items": {
141 "$ref": "#/components/schemas/ValidationError"
142 },
143 "type": "array",
144 "title": "Detail",
145 }
146 },
147 "type": "object",
148 "title": "HTTPValidationError",
149 },
150 "ValidationError": {
151 "properties": {
152 "ctx": {"title": "Context", "type": "object"},
153 "input": {"title": "Input"},
154 "loc": {
155 "items": {
156 "anyOf": [{"type": "string"}, {"type": "integer"}]
157 },
158 "type": "array",
159 "title": "Location",
160 },
161 "msg": {"type": "string", "title": "Message"},
162 "type": {"type": "string", "title": "Error Type"},
163 },
164 "type": "object",
165 "required": ["loc", "msg", "type"],
166 "title": "ValidationError",
167 },
168 }
169 },
170 }
171 )