Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Oct 3, 2023
1 parent 2a53eee commit 7175b88
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
61 changes: 34 additions & 27 deletions tests/utils/test_authenticator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import base64
import logging
import re
import uuid

Expand All @@ -20,6 +21,7 @@


if TYPE_CHECKING:
from _pytest.logging import LogCaptureFixture
from _pytest.monkeypatch import MonkeyPatch
from pytest_mock import MockerFixture

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 23 additions & 2 deletions tests/utils/test_password_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os

from typing import TYPE_CHECKING
Expand All @@ -13,6 +14,7 @@


if TYPE_CHECKING:
from _pytest.logging import LogCaptureFixture
from pytest_mock import MockerFixture

from tests.conftest import Config
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 7175b88

Please sign in to comment.