Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling errors 401, 403 when looking for repositories #3337

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,18 @@ def _get(self, endpoint: str) -> Optional[Page]:
url = self._url + endpoint
try:
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:
return
response.raise_for_status()
except requests.HTTPError as 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:
self._log(
"Response URL {response_url} differs from request URL {url}".format(
Expand Down
15 changes: 7 additions & 8 deletions tests/repositories/test_legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,19 +310,18 @@ def test_get_200_returns_page(http):
assert repo._get("/foo")


def test_get_404_returns_none(http):
repo = MockHttpRepository({"/foo": 404}, http)
@pytest.mark.parametrize("status_code", [401, 403, 404])
def test_get_40x_and_returns_none(http, status_code):
repo = MockHttpRepository({"/foo": status_code}, http)

assert repo._get("/foo") is None


def test_get_4xx_and_5xx_raises(http):
endpoints = {"/{}".format(code): code for code in {401, 403, 500}}
repo = MockHttpRepository(endpoints, http)
def test_get_5xx_raises(http):
repo = MockHttpRepository({"/foo": 500}, http)

for endpoint in endpoints:
with pytest.raises(RepositoryError):
repo._get(endpoint)
with pytest.raises(RepositoryError):
repo._get("/foo")


def test_get_redirected_response_url(http, monkeypatch):
Expand Down