From 7175b88f44ca421b25da321ad6443a6f202a36dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:08:16 +0200 Subject: [PATCH] improve tests --- tests/utils/test_authenticator.py | 61 ++++++++++++++++------------ tests/utils/test_password_manager.py | 25 +++++++++++- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/tests/utils/test_authenticator.py b/tests/utils/test_authenticator.py index cda447fa479..485982f8ca9 100644 --- a/tests/utils/test_authenticator.py +++ b/tests/utils/test_authenticator.py @@ -1,6 +1,7 @@ from __future__ import annotations import base64 +import logging import re import uuid @@ -20,6 +21,7 @@ if TYPE_CHECKING: + from _pytest.logging import LogCaptureFixture from _pytest.monkeypatch import MonkeyPatch from pytest_mock import MockerFixture @@ -65,8 +67,8 @@ def test_authenticator_uses_url_provided_credentials( authenticator.request("get", "https://foo001:bar002@foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic Zm9vMDAxOmJhcjAwMg==" + basic_auth = base64.b64encode(b"foo001:bar002").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" def test_authenticator_uses_credentials_from_config_if_not_provided( @@ -76,8 +78,8 @@ def test_authenticator_uses_credentials_from_config_if_not_provided( authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic YmFyOmJheg==" + basic_auth = base64.b64encode(b"bar:baz").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" def test_authenticator_uses_username_only_credentials( @@ -90,34 +92,38 @@ def test_authenticator_uses_username_only_credentials( authenticator.request("get", "https://foo001@foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic Zm9vMDAxOg==" + basic_auth = base64.b64encode(b"foo001:").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" def test_authenticator_ignores_locked_keyring( - mock_config: Config, mock_remote: None, http: type[httpretty.httpretty], with_locked_keyring: None, + caplog: LogCaptureFixture, ) -> None: - authenticator = Authenticator(mock_config, NullIO()) - authenticator.request("get", "https://foo001@foo.bar/files/foo-0.1.0.tar.gz") - request = http.last_request() + caplog.set_level(logging.DEBUG, logger="poetry.utils.password_manager") + authenticator = Authenticator() + authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") - assert request.headers["Authorization"] == "Basic Zm9vMDAxOg==" + request = http.last_request() + assert request.headers["Authorization"] is None + assert "Keyring foo.bar is locked" in caplog.messages def test_authenticator_ignores_failing_keyring( - mock_config: Config, mock_remote: None, http: type[httpretty.httpretty], with_erroneous_keyring: None, + caplog: LogCaptureFixture, ) -> None: - authenticator = Authenticator(mock_config, NullIO()) - authenticator.request("get", "https://foo001@foo.bar/files/foo-0.1.0.tar.gz") - request = http.last_request() + caplog.set_level(logging.DEBUG, logger="poetry.utils.password_manager") + authenticator = Authenticator() + authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") - assert request.headers["Authorization"] == "Basic Zm9vMDAxOg==" + request = http.last_request() + assert request.headers["Authorization"] is None + assert "Accessing keyring foo.bar failed" in caplog.messages def test_authenticator_uses_password_only_credentials( @@ -127,8 +133,8 @@ def test_authenticator_uses_password_only_credentials( authenticator.request("get", "https://:bar002@foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic OmJhcjAwMg==" + basic_auth = base64.b64encode(b":bar002").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" def test_authenticator_uses_empty_strings_as_default_password( @@ -149,8 +155,8 @@ def test_authenticator_uses_empty_strings_as_default_password( authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic YmFyOg==" + basic_auth = base64.b64encode(b"bar:").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" def test_authenticator_uses_empty_strings_as_default_username( @@ -170,8 +176,8 @@ def test_authenticator_uses_empty_strings_as_default_username( authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic OmJhcg==" + basic_auth = base64.b64encode(b":bar").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" def test_authenticator_falls_back_to_keyring_url( @@ -196,8 +202,8 @@ def test_authenticator_falls_back_to_keyring_url( authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic Zm9vOmJhcg==" + basic_auth = base64.b64encode(b"foo:bar").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" def test_authenticator_falls_back_to_keyring_netloc( @@ -220,8 +226,8 @@ def test_authenticator_falls_back_to_keyring_netloc( authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() - - assert request.headers["Authorization"] == "Basic Zm9vOmJhcg==" + basic_auth = base64.b64encode(b"foo:bar").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" @pytest.mark.filterwarnings("ignore::pytest.PytestUnhandledThreadExceptionWarning") @@ -356,7 +362,8 @@ def test_authenticator_uses_env_provided_credentials( request = http.last_request() - assert request.headers["Authorization"] == "Basic YmFyOmJheg==" + basic_auth = base64.b64encode(b"bar:baz").decode() + assert request.headers["Authorization"] == f"Basic {basic_auth}" @pytest.mark.parametrize( diff --git a/tests/utils/test_password_manager.py b/tests/utils/test_password_manager.py index ac634000a03..5efcf7f721b 100644 --- a/tests/utils/test_password_manager.py +++ b/tests/utils/test_password_manager.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging import os from typing import TYPE_CHECKING @@ -13,6 +14,7 @@ if TYPE_CHECKING: + from _pytest.logging import LogCaptureFixture from pytest_mock import MockerFixture from tests.conftest import Config @@ -190,11 +192,30 @@ def test_keyring_raises_errors_on_keyring_errors( key_ring.delete_password("foo", "bar") -def test_keyring_returns_none_on_locked_keyring(with_locked_keyring: None) -> None: +def test_keyring_returns_none_on_locked_keyring( + with_locked_keyring: None, + caplog: LogCaptureFixture, +) -> None: + caplog.set_level(logging.DEBUG, logger="poetry.utils.password_manager") key_ring = PoetryKeyring("poetry") - cred = key_ring.get_credential("any password", "any name") + cred = key_ring.get_credential("foo") + + assert cred.password is None + assert "Keyring foo is locked" in caplog.messages + + +def test_keyring_returns_none_on_erroneous_keyring( + with_erroneous_keyring: None, + caplog: LogCaptureFixture, +) -> None: + caplog.set_level(logging.DEBUG, logger="poetry.utils.password_manager") + key_ring = PoetryKeyring("poetry") + + cred = key_ring.get_credential("foo") + assert cred.password is None + assert "Accessing keyring foo failed" in caplog.messages def test_keyring_with_chainer_backend_and_fail_keyring_should_be_unavailable(