Commit 3c9ced2e by Cere Blanco Committed by GitHub

repositories: fix 401, 403 handling

Resolves: #3303
parent 931cf120
...@@ -423,19 +423,18 @@ class LegacyRepository(PyPiRepository): ...@@ -423,19 +423,18 @@ class LegacyRepository(PyPiRepository):
url = self._url + endpoint url = self._url + endpoint
try: try:
response = self.session.get(url) response = self.session.get(url)
if response.status_code in (401, 403):
self._log(
"Authorization error accessing {url}".format(url=url),
level="warning",
)
return
if response.status_code == 404: if response.status_code == 404:
return return
response.raise_for_status() response.raise_for_status()
except requests.HTTPError as e: except requests.HTTPError as e:
raise RepositoryError(e) raise RepositoryError(e)
if response.status_code in (401, 403):
self._log(
"Authorization error accessing {url}".format(url=response.url),
level="warn",
)
return
if response.url != url: if response.url != url:
self._log( self._log(
"Response URL {response_url} differs from request URL {url}".format( "Response URL {response_url} differs from request URL {url}".format(
......
...@@ -338,19 +338,18 @@ def test_get_200_returns_page(http): ...@@ -338,19 +338,18 @@ def test_get_200_returns_page(http):
assert repo._get("/foo") assert repo._get("/foo")
def test_get_404_returns_none(http): @pytest.mark.parametrize("status_code", [401, 403, 404])
repo = MockHttpRepository({"/foo": 404}, http) def test_get_40x_and_returns_none(http, status_code):
repo = MockHttpRepository({"/foo": status_code}, http)
assert repo._get("/foo") is None assert repo._get("/foo") is None
def test_get_4xx_and_5xx_raises(http): def test_get_5xx_raises(http):
endpoints = {"/{}".format(code): code for code in {401, 403, 500}} repo = MockHttpRepository({"/foo": 500}, http)
repo = MockHttpRepository(endpoints, http)
for endpoint in endpoints: with pytest.raises(RepositoryError):
with pytest.raises(RepositoryError): repo._get("/foo")
repo._get(endpoint)
def test_get_redirected_response_url(http, monkeypatch): def test_get_redirected_response_url(http, monkeypatch):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment