|
| 1 | +"""Content-app JSON via Accept: application/json (PythonDistribution.content_handler_json). |
| 2 | +
|
| 3 | +These tests hit the content app, not the DRF ``/pypi/`` API. They require pulpcore |
| 4 | +Phase 1 (issue 7887): ``Distribution.content_handler_json`` plus Handler Accept |
| 5 | +negotiation. When CI pulpcore is unpatched, skip — unit tests still cover the |
| 6 | +handler return values. |
| 7 | +""" |
| 8 | + |
| 9 | +from importlib.util import find_spec |
| 10 | +from pathlib import Path |
| 11 | +from urllib.parse import urljoin |
| 12 | + |
| 13 | +import pytest |
| 14 | +import requests |
| 15 | + |
| 16 | +from pulp_python.tests.functional.constants import ( |
| 17 | + PYPI_SIMPLE_V1_JSON, |
| 18 | + PYTHON_WHEEL_FILENAME, |
| 19 | + PYTHON_XS_PROJECT_SPECIFIER, |
| 20 | +) |
| 21 | + |
| 22 | +JSON_ACCEPT = {"Accept": "application/json"} |
| 23 | + |
| 24 | + |
| 25 | +def _pulpcore_content_app_json_enabled(): |
| 26 | + """True when pulpcore will invoke Distribution.content_handler_json on JSON Accept. |
| 27 | +
|
| 28 | + Inspect source files instead of importing Django models, which are not ready |
| 29 | + during pytest collection. Use find_spec so editable installs are resolved. |
| 30 | + """ |
| 31 | + handler = find_spec("pulpcore.content.handler") |
| 32 | + if not handler or not handler.origin: |
| 33 | + return False |
| 34 | + handler_path = Path(handler.origin) |
| 35 | + publication_path = handler_path.resolve().parents[1] / "app" / "models" / "publication.py" |
| 36 | + try: |
| 37 | + handler_src = handler_path.read_text() |
| 38 | + publication_src = publication_path.read_text() |
| 39 | + except OSError: |
| 40 | + return False |
| 41 | + return "def negotiate_json" in handler_src and "def content_handler_json" in publication_src |
| 42 | + |
| 43 | + |
| 44 | +pytestmark = pytest.mark.skipif( |
| 45 | + not _pulpcore_content_app_json_enabled(), |
| 46 | + reason=( |
| 47 | + "Requires pulpcore content-app Accept negotiation (Handler.negotiate_json and " |
| 48 | + "Distribution.content_handler_json from pulpcore issue 7887). Unit tests cover " |
| 49 | + "PythonDistribution.content_handler_json without that pulpcore change." |
| 50 | + ), |
| 51 | +) |
| 52 | + |
| 53 | + |
| 54 | +def _content_url(pulp_content_url, distro, rel=""): |
| 55 | + return urljoin(pulp_content_url, f"{distro.base_path}/{rel}") |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parallel |
| 59 | +def test_content_app_simple_index_and_detail_json( |
| 60 | + python_remote_factory, python_repo_with_sync, python_distribution_factory, pulp_content_url |
| 61 | +): |
| 62 | + """``.../simple/`` and ``.../simple/<name>/`` return PEP 691 Simple JSON.""" |
| 63 | + remote = python_remote_factory(includes=PYTHON_XS_PROJECT_SPECIFIER) |
| 64 | + repo = python_repo_with_sync(remote) |
| 65 | + distro = python_distribution_factory(repository=repo) |
| 66 | + |
| 67 | + index = requests.get(_content_url(pulp_content_url, distro, "simple/"), headers=JSON_ACCEPT) |
| 68 | + assert index.status_code == 200 |
| 69 | + assert PYPI_SIMPLE_V1_JSON in index.headers["Content-Type"] |
| 70 | + index_body = index.json() |
| 71 | + assert index_body["meta"]["api-version"] == "1.1" |
| 72 | + names = [project["name"] for project in index_body["projects"]] |
| 73 | + assert any("shelf" in name.lower() for name in names) |
| 74 | + |
| 75 | + detail = requests.get( |
| 76 | + _content_url(pulp_content_url, distro, "simple/shelf-reader/"), headers=JSON_ACCEPT |
| 77 | + ) |
| 78 | + assert detail.status_code == 200 |
| 79 | + assert PYPI_SIMPLE_V1_JSON in detail.headers["Content-Type"] |
| 80 | + detail_body = detail.json() |
| 81 | + assert detail_body["name"] == "shelf-reader" |
| 82 | + assert detail_body["files"] |
| 83 | + assert detail_body["versions"] |
| 84 | + filenames = [f["filename"] for f in detail_body["files"]] |
| 85 | + assert PYTHON_WHEEL_FILENAME in filenames or any("shelf" in f for f in filenames) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parallel |
| 89 | +def test_content_app_pypi_json_without_json_suffix( |
| 90 | + python_remote_factory, python_repo_with_sync, python_distribution_factory, pulp_content_url |
| 91 | +): |
| 92 | + """``.../pypi/<name>/`` returns PyPI JSON without the ``/json`` URL convention.""" |
| 93 | + remote = python_remote_factory(includes=PYTHON_XS_PROJECT_SPECIFIER) |
| 94 | + repo = python_repo_with_sync(remote) |
| 95 | + distro = python_distribution_factory(repository=repo) |
| 96 | + |
| 97 | + response = requests.get( |
| 98 | + _content_url(pulp_content_url, distro, "pypi/shelf-reader/"), headers=JSON_ACCEPT |
| 99 | + ) |
| 100 | + assert response.status_code == 200 |
| 101 | + assert "application/json" in response.headers["Content-Type"] |
| 102 | + body = response.json() |
| 103 | + assert "info" in body |
| 104 | + assert "releases" in body |
| 105 | + assert "urls" in body |
| 106 | + assert "shelf" in body["info"]["name"].lower() |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parallel |
| 110 | +def test_content_app_distro_root_is_generic_listing( |
| 111 | + python_remote_factory, python_repo_with_sync, python_distribution_factory, pulp_content_url |
| 112 | +): |
| 113 | + """Distribution root still uses pulpcore's generic JSON listing, not PyPI/Simple JSON.""" |
| 114 | + remote = python_remote_factory(includes=PYTHON_XS_PROJECT_SPECIFIER) |
| 115 | + repo = python_repo_with_sync(remote) |
| 116 | + distro = python_distribution_factory(repository=repo) |
| 117 | + |
| 118 | + response = requests.get(_content_url(pulp_content_url, distro), headers=JSON_ACCEPT) |
| 119 | + assert response.status_code == 200 |
| 120 | + assert "application/json" in response.headers["Content-Type"] |
| 121 | + body = response.json() |
| 122 | + assert "packages" in body |
| 123 | + assert "projects" not in body |
| 124 | + assert "info" not in body |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parallel |
| 128 | +def test_content_app_artifact_stays_binary( |
| 129 | + python_remote_factory, python_repo_with_sync, python_distribution_factory, pulp_content_url |
| 130 | +): |
| 131 | + """Concrete package artifacts stay binary even when Accept prefers JSON.""" |
| 132 | + remote = python_remote_factory(includes=PYTHON_XS_PROJECT_SPECIFIER, policy="immediate") |
| 133 | + repo = python_repo_with_sync(remote) |
| 134 | + distro = python_distribution_factory(repository=repo) |
| 135 | + |
| 136 | + response = requests.get( |
| 137 | + _content_url(pulp_content_url, distro, PYTHON_WHEEL_FILENAME), headers=JSON_ACCEPT |
| 138 | + ) |
| 139 | + assert response.status_code == 200 |
| 140 | + assert "application/json" not in response.headers.get("Content-Type", "") |
| 141 | + assert response.content[:2] == b"PK" # zip/wheel local file header |
0 commit comments