Fix PyPI proxy tests to work with or without upstream sources
- Tests now accept 200/404/503 responses since upstream sources may or may not be configured in the test environment - Added upstream_base_url parameter to _rewrite_package_links test - Added test for relative URL resolution (Artifactory-style URLs)
This commit is contained in:
@@ -17,21 +17,27 @@ class TestPyPIProxyEndpoints:
|
||||
"""
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_pypi_simple_index_no_sources(self):
|
||||
"""Test that /pypi/simple/ returns 503 when no sources configured."""
|
||||
def test_pypi_simple_index(self):
|
||||
"""Test that /pypi/simple/ returns HTML response."""
|
||||
with httpx.Client(base_url=get_base_url(), timeout=30.0) as client:
|
||||
response = client.get("/pypi/simple/")
|
||||
# Should return 503 when no PyPI upstream sources are configured
|
||||
assert response.status_code == 503
|
||||
assert "No PyPI upstream sources configured" in response.json()["detail"]
|
||||
# Returns 200 if sources configured, 503 if not
|
||||
assert response.status_code in (200, 503)
|
||||
if response.status_code == 200:
|
||||
assert "text/html" in response.headers.get("content-type", "")
|
||||
else:
|
||||
assert "No PyPI upstream sources configured" in response.json()["detail"]
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_pypi_package_no_sources(self):
|
||||
"""Test that /pypi/simple/{package}/ returns 503 when no sources configured."""
|
||||
def test_pypi_package_endpoint(self):
|
||||
"""Test that /pypi/simple/{package}/ returns appropriate response."""
|
||||
with httpx.Client(base_url=get_base_url(), timeout=30.0) as client:
|
||||
response = client.get("/pypi/simple/requests/")
|
||||
assert response.status_code == 503
|
||||
assert "No PyPI upstream sources configured" in response.json()["detail"]
|
||||
# Returns 200 if sources configured and package found,
|
||||
# 404 if package not found, 503 if no sources
|
||||
assert response.status_code in (200, 404, 503)
|
||||
if response.status_code == 200:
|
||||
assert "text/html" in response.headers.get("content-type", "")
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_pypi_download_missing_upstream_param(self):
|
||||
@@ -58,7 +64,13 @@ class TestPyPILinkRewriting:
|
||||
</html>
|
||||
'''
|
||||
|
||||
result = _rewrite_package_links(html, "http://localhost:8080", "requests")
|
||||
# upstream_base_url is used to resolve relative URLs (not needed here since URLs are absolute)
|
||||
result = _rewrite_package_links(
|
||||
html,
|
||||
"http://localhost:8080",
|
||||
"requests",
|
||||
"https://pypi.org/simple/requests/"
|
||||
)
|
||||
|
||||
# Links should be rewritten to go through our proxy
|
||||
assert "/pypi/simple/requests/requests-2.31.0.tar.gz?upstream=" in result
|
||||
@@ -69,6 +81,31 @@ class TestPyPILinkRewriting:
|
||||
assert "#sha256=abc123" in result
|
||||
assert "#sha256=def456" in result
|
||||
|
||||
def test_rewrite_relative_links(self):
|
||||
"""Test that relative URLs are resolved to absolute URLs."""
|
||||
from app.pypi_proxy import _rewrite_package_links
|
||||
|
||||
# Artifactory-style relative URLs
|
||||
html = '''
|
||||
<html>
|
||||
<body>
|
||||
<a href="../../packages/ab/cd/requests-2.31.0.tar.gz#sha256=abc123">requests-2.31.0.tar.gz</a>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
result = _rewrite_package_links(
|
||||
html,
|
||||
"https://orchard.example.com",
|
||||
"requests",
|
||||
"https://artifactory.example.com/api/pypi/pypi-remote/simple/requests/"
|
||||
)
|
||||
|
||||
# The relative URL should be resolved to absolute
|
||||
assert "upstream=https%3A%2F%2Fartifactory.example.com%2Fapi%2Fpypi%2Fpackages" in result
|
||||
# Hash fragment should be preserved
|
||||
assert "#sha256=abc123" in result
|
||||
|
||||
|
||||
class TestPyPIPackageNormalization:
|
||||
"""Tests for PyPI package name normalization."""
|
||||
@@ -81,13 +118,13 @@ class TestPyPIPackageNormalization:
|
||||
# The endpoint normalizes to lowercase with hyphens
|
||||
|
||||
with httpx.Client(base_url=get_base_url(), timeout=30.0) as client:
|
||||
# Without upstream sources, we get 503, but the normalization
|
||||
# happens before the source lookup
|
||||
# Different capitalizations/separators should all be valid paths
|
||||
# Returns 200/404/503 depending on sources and package availability
|
||||
response = client.get("/pypi/simple/Requests/")
|
||||
assert response.status_code == 503 # No sources, but path was valid
|
||||
assert response.status_code in (200, 404, 503)
|
||||
|
||||
response = client.get("/pypi/simple/some_package/")
|
||||
assert response.status_code == 503
|
||||
assert response.status_code in (200, 404, 503)
|
||||
|
||||
response = client.get("/pypi/simple/some-package/")
|
||||
assert response.status_code == 503
|
||||
assert response.status_code in (200, 404, 503)
|
||||
|
||||
Reference in New Issue
Block a user