From 6cb7d0ee0af0127b9b699760bd394fa2de24e8e2 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Fri, 22 Mar 2024 22:17:01 +0100 Subject: [PATCH] utils: ensure Downloader defaults to Authenticator --- src/poetry/utils/helpers.py | 9 +++------ tests/utils/test_helpers.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/poetry/utils/helpers.py b/src/poetry/utils/helpers.py index fbc29e5b4dd..221c94d3426 100644 --- a/src/poetry/utils/helpers.py +++ b/src/poetry/utils/helpers.py @@ -20,10 +20,9 @@ from typing import Any from typing import overload -import requests - from requests.utils import atomic_open +from poetry.utils.authenticator import Authenticator from poetry.utils.constants import REQUESTS_TIMEOUT @@ -35,8 +34,6 @@ from poetry.core.packages.package import Package from requests import Session - from poetry.utils.authenticator import Authenticator - logger = logging.getLogger(__name__) prioritised_hash_types: tuple[str, ...] = tuple( t @@ -171,10 +168,10 @@ def __init__( ): self._dest = dest - get = requests.get if not session else session.get + session = session or Authenticator() headers = {"Accept-Encoding": "Identity"} - self._response = get( + self._response = session.get( url, stream=True, headers=headers, timeout=REQUESTS_TIMEOUT ) self._response.raise_for_status() diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py index 2399e298552..8361bbf3571 100644 --- a/tests/utils/test_helpers.py +++ b/tests/utils/test_helpers.py @@ -1,5 +1,8 @@ from __future__ import annotations +import base64 +import re + from typing import TYPE_CHECKING from typing import Any @@ -7,6 +10,7 @@ from poetry.core.utils.helpers import parse_requires +from poetry.utils.helpers import Downloader from poetry.utils.helpers import HTTPRangeRequestSupported from poetry.utils.helpers import download_file from poetry.utils.helpers import get_file_hash @@ -19,6 +23,7 @@ from httpretty import httpretty from httpretty.core import HTTPrettyRequest + from tests.conftest import Config from tests.types import FixtureDirGetter @@ -188,3 +193,30 @@ def handle_request( else: download_file(url, dest, raise_accepts_ranges=raise_accepts_ranges) assert dest.is_file() + + +def test_downloader_uses_authenticator_by_default( + config: Config, + http: type[httpretty], + tmp_working_directory: Path, +) -> None: + http.register_uri( + http.GET, + re.compile("^https?://foo.bar/(.+?)$"), + ) + + config.merge( + { + "repositories": {"foo": {"url": "https://foo.bar/simple/"}}, + "http-basic": {"foo": {"username": "bar", "password": "baz"}}, + } + ) + + Downloader( + "https://foo.bar/files/foo-0.1.0.tar.gz", + tmp_working_directory / "foo-0.1.0.tar.gz", + ) + + request = http.last_request() + basic_auth = base64.b64encode(b"bar:baz").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}"