16
16
from warehouse.api import integrity
17
17
18
18
19
- def test_select_content_type(db_request):
20
- db_request.accept = "application/json"
19
+ @pytest.mark.parametrize(
20
+ ("accept", "expected"),
21
+ [
22
+ # Simple cases
23
+ (
24
+ "application/vnd.pypi.integrity.v1+json",
25
+ integrity.MIME_PYPI_INTEGRITY_V1_JSON,
26
+ ),
27
+ ("application/json", integrity.MIME_APPLICATION_JSON),
28
+ # No accept header means we give the user our first offer
29
+ (None, integrity.MIME_PYPI_INTEGRITY_V1_JSON),
30
+ # Accept header contains only things we don't offer
31
+ ("text/xml", None),
32
+ ("application/octet-stream", None),
33
+ ("text/xml, application/octet-stream", None),
34
+ # Accept header contains both things we offer and things we don't;
35
+ # we pick our matching offer even if the q-value is lower
36
+ (
37
+ "text/xml, application/vnd.pypi.integrity.v1+json",
38
+ integrity.MIME_PYPI_INTEGRITY_V1_JSON,
39
+ ),
40
+ (
41
+ "application/vnd.pypi.integrity.v1+json; q=0.1, text/xml",
42
+ integrity.MIME_PYPI_INTEGRITY_V1_JSON,
43
+ ),
44
+ # Accept header contains multiple things we offer with the same q-value;
45
+ # we pick our preferred offer
46
+ (
47
+ "application/json, application/vnd.pypi.integrity.v1+json",
48
+ integrity.MIME_PYPI_INTEGRITY_V1_JSON,
49
+ ),
50
+ (
51
+ "application/vnd.pypi.integrity.v1+json; q=0.5, application/json; q=0.5",
52
+ integrity.MIME_PYPI_INTEGRITY_V1_JSON,
53
+ ),
54
+ # Accept header contains multiple things we offer; we pick our
55
+ # offer based on the q-value
56
+ (
57
+ "application/vnd.pypi.integrity.v1+json; q=0.1, application/json",
58
+ integrity.MIME_APPLICATION_JSON,
59
+ ),
60
+ ],
61
+ )
62
+ def test_select_content_type(db_request, accept, expected):
63
+ db_request.accept = accept
21
64
22
- assert (
23
- integrity._select_content_type(db_request)
24
- == integrity.MIME_PYPI_INTEGRITY_V1_JSON
25
- )
65
+ assert integrity._select_content_type(db_request) == expected
26
66
27
67
28
68
# Backstop; can be removed/changed once this view supports HTML.
29
69
@pytest.mark.parametrize(
30
70
"content_type",
31
- [integrity.MIME_TEXT_HTML, integrity.MIME_PYPI_INTEGRITY_V1_HTML ],
71
+ ["text/html", "application/vnd.pypi. integrity.v1+html" ],
32
72
)
33
73
def test_provenance_for_file_bad_accept(db_request, content_type):
34
74
db_request.accept = content_type
@@ -37,6 +77,15 @@ def test_provenance_for_file_bad_accept(db_request, content_type):
37
77
assert response.json == {"message": "Request not acceptable"}
38
78
39
79
80
+ def test_provenance_for_file_accept_multiple(db_request, monkeypatch):
81
+ db_request.accept = "text/html, application/vnd.pypi.integrity.v1+json; q=0.9"
82
+ file = pretend.stub(provenance=None, filename="fake-1.2.3.tar.gz")
83
+
84
+ response = integrity.provenance_for_file(file, db_request)
85
+ assert response.status_code == 404
86
+ assert response.json == {"message": "No provenance available for fake-1.2.3.tar.gz"}
87
+
88
+
40
89
def test_provenance_for_file_not_enabled(db_request, monkeypatch):
41
90
monkeypatch.setattr(db_request, "flags", pretend.stub(enabled=lambda *a: True))
42
91
0 commit comments