18
18
from pyramid .testing import DummyRequest
19
19
20
20
from warehouse .api import simple
21
- from warehouse .packaging .utils import API_VERSION
21
+ from warehouse .packaging .utils import API_VERSION , _valid_simple_detail_context
22
22
23
23
from ...common .db .accounts import UserFactory
24
24
from ...common .db .packaging import (
25
+ AlternateRepositoryFactory ,
25
26
FileFactory ,
26
27
JournalEntryFactory ,
27
28
ProjectFactory ,
@@ -48,29 +49,30 @@ def test_defaults_text_html(self, header):
48
49
default to text/html.
49
50
"""
50
51
request = DummyRequest (accept = header )
51
- assert simple ._select_content_type (request ) == "text/html"
52
+ assert simple ._select_content_type (request ) == simple . MIME_TEXT_HTML
52
53
53
54
@pytest .mark .parametrize (
54
55
("header" , "expected" ),
55
56
[
56
- ("text/html" , "text/html" ),
57
+ (simple . MIME_TEXT_HTML , simple . MIME_TEXT_HTML ),
57
58
(
58
- "application/vnd.pypi. simple.v1+html" ,
59
- "application/vnd.pypi. simple.v1+html" ,
59
+ simple .MIME_PYPI_SIMPLE_V1_HTML ,
60
+ simple .MIME_PYPI_SIMPLE_V1_HTML ,
60
61
),
61
62
(
62
- "application/vnd.pypi. simple.v1+json" ,
63
- "application/vnd.pypi. simple.v1+json" ,
63
+ simple .MIME_PYPI_SIMPLE_V1_JSON ,
64
+ simple .MIME_PYPI_SIMPLE_V1_JSON ,
64
65
),
65
66
(
66
- "text/html, application/vnd.pypi. simple.v1+html , "
67
- "application/vnd.pypi. simple.v1+json " ,
68
- "text/html" ,
67
+ f" { simple . MIME_TEXT_HTML } , { simple .MIME_PYPI_SIMPLE_V1_HTML } , "
68
+ f" { simple .MIME_PYPI_SIMPLE_V1_JSON } " ,
69
+ simple . MIME_TEXT_HTML ,
69
70
),
70
71
(
71
- "text/html;q=0.01, application/vnd.pypi.simple.v1+html;q=0.2, "
72
- "application/vnd.pypi.simple.v1+json" ,
73
- "application/vnd.pypi.simple.v1+json" ,
72
+ f"{ simple .MIME_TEXT_HTML } ;q=0.01, "
73
+ f"{ simple .MIME_PYPI_SIMPLE_V1_HTML } ;q=0.2, "
74
+ f"{ simple .MIME_PYPI_SIMPLE_V1_JSON } " ,
75
+ simple .MIME_PYPI_SIMPLE_V1_JSON ,
74
76
),
75
77
],
76
78
)
@@ -80,9 +82,9 @@ def test_selects(self, header, expected):
80
82
81
83
82
84
CONTENT_TYPE_PARAMS = [
83
- ("text/html" , None ),
84
- ("application/vnd.pypi. simple.v1+html" , None ),
85
- ("application/vnd.pypi. simple.v1+json" , "json" ),
85
+ (simple . MIME_TEXT_HTML , None ),
86
+ (simple .MIME_PYPI_SIMPLE_V1_HTML , None ),
87
+ (simple .MIME_PYPI_SIMPLE_V1_JSON , "json" ),
86
88
]
87
89
88
90
@@ -211,12 +213,15 @@ def test_no_files_no_serial(self, db_request, content_type, renderer_override):
211
213
user = UserFactory .create ()
212
214
JournalEntryFactory .create (submitted_by = user )
213
215
214
- assert simple . simple_detail ( project , db_request ) = = {
216
+ context = {
215
217
"meta" : {"_last-serial" : 0 , "api-version" : API_VERSION },
216
218
"name" : project .normalized_name ,
217
219
"files" : [],
218
220
"versions" : [],
221
+ "alternate-locations" : [],
219
222
}
223
+ context = _update_context (context , content_type , renderer_override )
224
+ assert simple .simple_detail (project , db_request ) == context
220
225
221
226
assert db_request .response .headers ["X-PyPI-Last-Serial" ] == "0"
222
227
assert db_request .response .content_type == content_type
@@ -235,13 +240,20 @@ def test_no_files_with_serial(self, db_request, content_type, renderer_override)
235
240
db_request .matchdict ["name" ] = project .normalized_name
236
241
user = UserFactory .create ()
237
242
je = JournalEntryFactory .create (name = project .name , submitted_by = user )
243
+ als = [
244
+ AlternateRepositoryFactory .create (project = project ),
245
+ AlternateRepositoryFactory .create (project = project ),
246
+ ]
238
247
239
- assert simple . simple_detail ( project , db_request ) = = {
248
+ context = {
240
249
"meta" : {"_last-serial" : je .id , "api-version" : API_VERSION },
241
250
"name" : project .normalized_name ,
242
251
"files" : [],
243
252
"versions" : [],
253
+ "alternate-locations" : sorted (al .url for al in als ),
244
254
}
255
+ context = _update_context (context , content_type , renderer_override )
256
+ assert simple .simple_detail (project , db_request ) == context
245
257
246
258
assert db_request .response .headers ["X-PyPI-Last-Serial" ] == str (je .id )
247
259
assert db_request .response .content_type == content_type
@@ -271,7 +283,7 @@ def test_with_files_no_serial(self, db_request, content_type, renderer_override)
271
283
user = UserFactory .create ()
272
284
JournalEntryFactory .create (submitted_by = user )
273
285
274
- assert simple . simple_detail ( project , db_request ) = = {
286
+ context = {
275
287
"meta" : {"_last-serial" : 0 , "api-version" : API_VERSION },
276
288
"name" : project .normalized_name ,
277
289
"versions" : release_versions ,
@@ -289,7 +301,10 @@ def test_with_files_no_serial(self, db_request, content_type, renderer_override)
289
301
}
290
302
for f in files
291
303
],
304
+ "alternate-locations" : [],
292
305
}
306
+ context = _update_context (context , content_type , renderer_override )
307
+ assert simple .simple_detail (project , db_request ) == context
293
308
294
309
assert db_request .response .headers ["X-PyPI-Last-Serial" ] == "0"
295
310
assert db_request .response .content_type == content_type
@@ -319,7 +334,7 @@ def test_with_files_with_serial(self, db_request, content_type, renderer_overrid
319
334
user = UserFactory .create ()
320
335
je = JournalEntryFactory .create (name = project .name , submitted_by = user )
321
336
322
- assert simple . simple_detail ( project , db_request ) = = {
337
+ context = {
323
338
"meta" : {"_last-serial" : je .id , "api-version" : API_VERSION },
324
339
"name" : project .normalized_name ,
325
340
"versions" : release_versions ,
@@ -337,7 +352,10 @@ def test_with_files_with_serial(self, db_request, content_type, renderer_overrid
337
352
}
338
353
for f in files
339
354
],
355
+ "alternate-locations" : [],
340
356
}
357
+ context = _update_context (context , content_type , renderer_override )
358
+ assert simple .simple_detail (project , db_request ) == context
341
359
342
360
assert db_request .response .headers ["X-PyPI-Last-Serial" ] == str (je .id )
343
361
assert db_request .response .content_type == content_type
@@ -404,7 +422,7 @@ def test_with_files_with_version_multi_digit(
404
422
user = UserFactory .create ()
405
423
je = JournalEntryFactory .create (name = project .name , submitted_by = user )
406
424
407
- assert simple . simple_detail ( project , db_request ) = = {
425
+ context = {
408
426
"meta" : {"_last-serial" : je .id , "api-version" : API_VERSION },
409
427
"name" : project .normalized_name ,
410
428
"versions" : release_versions ,
@@ -430,7 +448,10 @@ def test_with_files_with_version_multi_digit(
430
448
}
431
449
for f in files
432
450
],
451
+ "alternate-locations" : [],
433
452
}
453
+ context = _update_context (context , content_type , renderer_override )
454
+ assert simple .simple_detail (project , db_request ) == context
434
455
435
456
assert db_request .response .headers ["X-PyPI-Last-Serial" ] == str (je .id )
436
457
assert db_request .response .content_type == content_type
@@ -439,6 +460,15 @@ def test_with_files_with_version_multi_digit(
439
460
if renderer_override is not None :
440
461
assert db_request .override_renderer == renderer_override
441
462
463
+
464
+ def _update_context (context , content_type , renderer_override ):
465
+ if renderer_override != "json" or content_type in [
466
+ simple .MIME_TEXT_HTML ,
467
+ simple .MIME_PYPI_SIMPLE_V1_HTML ,
468
+ ]:
469
+ return _valid_simple_detail_context (context )
470
+ return context
471
+
442
472
def test_with_files_quarantined_omitted_from_index (self , db_request ):
443
473
db_request .accept = "text/html"
444
474
project = ProjectFactory .create (lifecycle_status = "quarantine-enter" )
0 commit comments