From cdfaa6de94ca48b5b9a6dbe808230800ff258bfd Mon Sep 17 00:00:00 2001 From: ralbertazzi Date: Mon, 15 May 2023 11:30:37 +0200 Subject: [PATCH] feat: do not call keyring when username in config and password in env --- src/poetry/utils/password_manager.py | 17 +++++-------- tests/utils/test_password_manager.py | 38 +++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/poetry/utils/password_manager.py b/src/poetry/utils/password_manager.py index 53c42910ba7..620556043c0 100644 --- a/src/poetry/utils/password_manager.py +++ b/src/poetry/utils/password_manager.py @@ -193,16 +193,13 @@ def delete_pypi_token(self, name: str) -> None: self.keyring.delete_password(name, "__token__") def get_http_auth(self, name: str) -> dict[str, str | None] | None: - auth = self._config.get(f"http-basic.{name}") - if not auth: - username = self._config.get(f"http-basic.{name}.username") - password = self._config.get(f"http-basic.{name}.password") - if not username and not password: - return None - else: - username, password = auth["username"], auth.get("password") - if password is None: - password = self.keyring.get_password(name, username) + username = self._config.get(f"http-basic.{name}.username") + password = self._config.get(f"http-basic.{name}.password") + if not username and not password: + return None + + if not password: + password = self.keyring.get_password(name, username) return { "username": username, diff --git a/tests/utils/test_password_manager.py b/tests/utils/test_password_manager.py index d09f88b1fdf..f2cbb7b7bce 100644 --- a/tests/utils/test_password_manager.py +++ b/tests/utils/test_password_manager.py @@ -3,6 +3,7 @@ import os from typing import TYPE_CHECKING +from unittest.mock import MagicMock import pytest @@ -222,7 +223,7 @@ def test_fail_keyring_should_be_unavailable( def test_get_http_auth_from_environment_variables( - environ: None, config: Config, with_simple_keyring: None + environ: None, config: Config ) -> None: os.environ["POETRY_HTTP_BASIC_FOO_USERNAME"] = "bar" os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz" @@ -230,10 +231,39 @@ def test_get_http_auth_from_environment_variables( manager = PasswordManager(config) auth = manager.get_http_auth("foo") - assert auth is not None + assert auth == {"username": "bar", "password": "baz"} - assert auth["username"] == "bar" - assert auth["password"] == "baz" + +def test_get_http_auth_does_not_call_keyring_when_credentials_in_environment_variables( + environ: None, config: Config +) -> None: + os.environ["POETRY_HTTP_BASIC_FOO_USERNAME"] = "bar" + os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz" + + manager = PasswordManager(config) + manager._keyring = MagicMock() + + auth = manager.get_http_auth("foo") + assert auth == {"username": "bar", "password": "baz"} + manager._keyring.get_password.assert_not_called() + + +def test_get_http_auth_does_not_call_keyring_when_password_in_environment_variables( + environ: None, config: Config +) -> None: + config.merge( + { + "http-basic": {"foo": {"username": "bar"}}, + } + ) + os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz" + + manager = PasswordManager(config) + manager._keyring = MagicMock() + + auth = manager.get_http_auth("foo") + assert auth == {"username": "bar", "password": "baz"} + manager._keyring.get_password.assert_not_called() def test_get_pypi_token_with_env_var_positive(