Debian Codegraph @

tests/test_links.py

raw
1 import pytest
2
3 from apispec.exceptions import APISpecError
4
5 from .utils import build_ref, get_paths
6
7
8 @pytest.mark.parametrize("spec", ("3.0.0",), indirect=True)
9 class TestLinksInPath:
10 def test_basic_two_element_tuple(self, spec):
11 spec.path(
12 path="/posts",
13 operations={
14 "post": {
15 "responses": {
16 "201": {
17 "description": "Created",
18 }
19 }
20 }
21 },
22 links={
23 "GetPost": ("/posts/{post_id}", "GET"),
24 },
25 )
26 paths = get_paths(spec)
27 post_response = paths["/posts"]["post"]["responses"]["201"]
28 assert "links" in post_response
29 assert "GetPost" in post_response["links"]
30 link = post_response["links"]["GetPost"]
31 assert link["operationRef"] == "#/paths/~1posts~1{post_id}/get"
32 assert "parameters" not in link
33
34 def test_three_element_tuple_with_parameters(self, spec):
35 spec.path(
36 path="/posts",
37 operations={
38 "post": {
39 "responses": {
40 "201": {
41 "description": "Created",
42 }
43 }
44 }
45 },
46 links={
47 "GetPost": (
48 "/posts/{post_id}",
49 "GET",
50 {"post_id": "$response.body#/postIdentifier"},
51 ),
52 },
53 )
54 paths = get_paths(spec)
55 post_response = paths["/posts"]["post"]["responses"]["201"]
56 link = post_response["links"]["GetPost"]
57 assert link["operationRef"] == "#/paths/~1posts~1{post_id}/get"
58 assert link["parameters"] == {"post_id": "$response.body#/postIdentifier"}
59
60 def test_multiple_links_on_single_endpoint(self, spec):
61 spec.path(
62 path="/users",
63 operations={
64 "post": {
65 "responses": {
66 "201": {
67 "description": "Created",
68 }
69 }
70 }
71 },
72 links={
73 "GetUser": ("/users/{user_id}", "GET"),
74 "UpdateUser": ("/users/{user_id}", "PUT"),
75 "DeleteUser": ("/users/{user_id}", "DELETE"),
76 },
77 )
78 paths = get_paths(spec)
79 post_response = paths["/users"]["post"]["responses"]["201"]
80 assert "links" in post_response
81 assert len(post_response["links"]) == 3
82 assert "GetUser" in post_response["links"]
83 assert "UpdateUser" in post_response["links"]
84 assert "DeleteUser" in post_response["links"]
85
86 def test_links_added_to_multiple_operations(self, spec):
87 spec.path(
88 path="/posts",
89 operations={
90 "get": {
91 "responses": {
92 "200": {"description": "Success"},
93 }
94 },
95 "post": {
96 "responses": {
97 "201": {"description": "Created"},
98 }
99 },
100 },
101 links={
102 "GetPost": ("/posts/{post_id}", "GET"),
103 },
104 )
105 paths = get_paths(spec)
106 # Both operations should have the link in their responses
107 assert "GetPost" in paths["/posts"]["get"]["responses"]["200"]["links"]
108 assert "GetPost" in paths["/posts"]["post"]["responses"]["201"]["links"]
109
110 def test_links_not_shared_between_paths(self, spec):
111 spec.path(
112 path="/users",
113 operations={"post": {"responses": {"201": {"description": "Created"}}}},
114 links={"GetUser": ("/users/{user_id}", "GET")},
115 )
116 spec.path(
117 path="/posts",
118 operations={"post": {"responses": {"201": {"description": "Created"}}}},
119 links={"GetPost": ("/posts/{post_id}", "GET")},
120 )
121 paths = get_paths(spec)
122 # Each path should only have its own links
123 assert "GetPost" not in paths["/users"]["post"]["responses"]["201"]["links"]
124 assert "GetUser" not in paths["/posts"]["post"]["responses"]["201"]["links"]
125
126 def test_path_escaping_for_json_pointer(self, spec):
127 spec.path(
128 path="/posts",
129 operations={
130 "post": {
131 "responses": {
132 "201": {"description": "Created"},
133 }
134 }
135 },
136 links={
137 "GetPost": ("/posts/{id}", "GET"),
138 },
139 )
140 paths = get_paths(spec)
141 link = paths["/posts"]["post"]["responses"]["201"]["links"]["GetPost"]
142 # Forward slashes should be escaped as ~1
143 assert link["operationRef"] == "#/paths/~1posts~1{id}/get"
144
145 def test_path_escaping_with_tilde(self, spec):
146 spec.path(
147 path="/posts",
148 operations={
149 "post": {
150 "responses": {
151 "201": {"description": "Created"},
152 }
153 }
154 },
155 links={
156 "GetItem": ("/items/~special", "GET"),
157 },
158 )
159 paths = get_paths(spec)
160 link = paths["/posts"]["post"]["responses"]["201"]["links"]["GetItem"]
161 # Tildes should be escaped as ~0, then slashes as ~1
162 assert link["operationRef"] == "#/paths/~1items~1~0special/get"
163
164 def test_prefers_operationId_when_target_registered(self, spec):
165 """If the target operation is already registered with an operationId,
166 the link should use `operationId` instead of `operationRef`.
167 """
168 # Register the target operation first
169 spec.path(
170 path="/posts/{post_id}",
171 operations={
172 "get": {
173 "operationId": "getPost",
174 "responses": {"200": {"description": "Success"}},
175 }
176 },
177 )
178
179 # Add a different path that links to the registered operation
180 spec.path(
181 path="/comments",
182 operations={"post": {"responses": {"201": {"description": "Created"}}}},
183 links={"GetPost": ("/posts/{post_id}", "GET")},
184 )
185
186 paths = get_paths(spec)
187 link = paths["/comments"]["post"]["responses"]["201"]["links"]["GetPost"]
188 assert link == {"operationId": "getPost"}
189
190
191 @pytest.mark.parametrize("spec", ("2.0",), indirect=True)
192 class TestLinksOpenAPI2:
193 def test_links_ignored_in_openapi_2(self, spec):
194 spec.path(
195 path="/posts",
196 operations={
197 "post": {
198 "responses": {
199 "201": {"description": "Created"},
200 }
201 }
202 },
203 links={
204 "GetPost": ("/posts/{post_id}", "GET"),
205 },
206 )
207 paths = get_paths(spec)
208 post_response = paths["/posts"]["post"]["responses"]["201"]
209 assert "links" not in post_response
210
211
212 @pytest.mark.parametrize("spec", ("3.0.0",), indirect=True)
213 class TestLinksErrorHandling:
214 @pytest.mark.parametrize(
215 "invalid_tuple",
216 [
217 ("/posts/{id}",),
218 ("/posts/{id}", "GET", {}, "extra"),
219 ],
220 )
221 def test_invalid_tuple_length(self, spec, invalid_tuple):
222 with pytest.raises(APISpecError, match="must be a tuple of"):
223 spec.path(
224 path="/posts",
225 operations={"post": {"responses": {"201": {"description": "Created"}}}},
226 links={
227 "GetPost": invalid_tuple,
228 },
229 )
230
231 def test_invalid_tuple_type(self, spec):
232 with pytest.raises(APISpecError, match="must be a tuple of"):
233 spec.path(
234 path="/posts",
235 operations={"post": {"responses": {"201": {"description": "Created"}}}},
236 links={
237 "GetPost": "/posts/{id}", # String instead of tuple
238 },
239 )
240
241 def test_invalid_route_type(self, spec):
242 with pytest.raises(APISpecError, match="route must be a string"):
243 spec.path(
244 path="/posts",
245 operations={"post": {"responses": {"201": {"description": "Created"}}}},
246 links={
247 "GetPost": (123, "GET"), # Route is not a string
248 },
249 )
250
251 def test_invalid_http_method(self, spec):
252 with pytest.raises(APISpecError, match="invalid HTTP method"):
253 spec.path(
254 path="/posts",
255 operations={"post": {"responses": {"201": {"description": "Created"}}}},
256 links={
257 "GetPost": ("/posts/{id}", "INVALID"),
258 },
259 )
260
261 def test_case_insensitive_method(self, spec):
262 spec.path(
263 path="/posts",
264 operations={"post": {"responses": {"201": {"description": "Created"}}}},
265 links={
266 "GetPost": ("/posts/{id}", "GeT"), # Mixed case
267 },
268 )
269 paths = get_paths(spec)
270 link = paths["/posts"]["post"]["responses"]["201"]["links"]["GetPost"]
271 # Method should be lowercased in the operationRef
272 assert link["operationRef"] == "#/paths/~1posts~1{id}/get"
273
274
275 @pytest.mark.parametrize("spec", ("3.0.0",), indirect=True)
276 class TestLinksWithReferences:
277 def test_link_component_reference_in_response(self, spec):
278 # Register a link component
279 link_def = {
280 "operationRef": "#/paths/~1users~1{user_id}/get",
281 "parameters": {"user_id": "$response.body#/id"},
282 }
283 spec.components.link("GetUserLink", link_def)
284
285 # Use the link as a reference in a response
286 spec.path(
287 path="/posts",
288 operations={
289 "post": {
290 "responses": {
291 "201": {
292 "description": "Created",
293 "links": {
294 "GetUser": "GetUserLink",
295 },
296 }
297 }
298 }
299 },
300 )
301
302 paths = get_paths(spec)
303 post_response = paths["/posts"]["post"]["responses"]["201"]
304 # The reference should be resolved to $ref
305 assert post_response["links"]["GetUser"] == build_ref(
306 spec, "link", "GetUserLink"
307 )
308
309 def test_mixed_direct_and_reference_links(self, spec):
310 # Register a link component
311 spec.components.link(
312 "GetUserLink", {"operationRef": "#/paths/~1users~1{user_id}/get"}
313 )
314
315 expected_operation_ref = "#/paths/~1posts~1{id}/get"
316 # Use both direct links and references
317 spec.path(
318 path="/posts",
319 operations={
320 "post": {
321 "responses": {
322 "201": {
323 "description": "Created",
324 "links": {
325 "GetUser": "GetUserLink", # Reference
326 "DirectLink": { # Direct object
327 "operationRef": expected_operation_ref
328 },
329 },
330 }
331 }
332 }
333 },
334 )
335
336 paths = get_paths(spec)
337 post_response = paths["/posts"]["post"]["responses"]["201"]
338 # Reference should be $ref
339 assert post_response["links"]["GetUser"] == build_ref(
340 spec, "link", "GetUserLink"
341 )
342 # Direct object should remain as is
343 assert (
344 post_response["links"]["DirectLink"]["operationRef"]
345 == expected_operation_ref
346 )