Skip to content

Commit

Permalink
password_manager: do not try to use keyring when disabled ("null" key…
Browse files Browse the repository at this point in the history
…ring) (#5251)

* when disabled, keyring uses a "null" keyring
* Fix the mock for the chainer tests
  • Loading branch information
lakinwecker authored Apr 12, 2022
1 parent a9d05f5 commit f462b7f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/poetry/utils/password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _check(self) -> None:

backend = keyring.get_keyring()
name = backend.name.split(" ")[0]
if name == "fail":
if name in ("fail", "null"):
logger.debug("No suitable keyring backend found")
self._is_available = False
elif "plaintext" in backend.name.lower():
Expand All @@ -107,7 +107,7 @@ def _check(self) -> None:
backends = keyring.backend.get_all_keyring()

self._is_available = any(
b.name.split(" ")[0] not in ["chainer", "fail"]
b.name.split(" ")[0] not in ["chainer", "fail", "null"]
and "plaintext" not in b.name.lower()
for b in backends
)
Expand Down
25 changes: 23 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,31 @@ def with_fail_keyring() -> None:


@pytest.fixture()
def with_chained_keyring(mocker: MockerFixture) -> None:
def with_null_keyring() -> None:
import keyring

from keyring.backends.null import Keyring

keyring.set_keyring(Keyring())


@pytest.fixture()
def with_chained_fail_keyring(mocker: MockerFixture) -> None:
from keyring.backends.fail import Keyring

mocker.patch("keyring.backend.get_all_keyring", [Keyring()])
mocker.patch("keyring.backend.get_all_keyring", lambda: [Keyring()])
import keyring

from keyring.backends.chainer import ChainerBackend

keyring.set_keyring(ChainerBackend())


@pytest.fixture()
def with_chained_null_keyring(mocker: MockerFixture) -> None:
from keyring.backends.null import Keyring

mocker.patch("keyring.backend.get_all_keyring", lambda: [Keyring()])
import keyring

from keyring.backends.chainer import ChainerBackend
Expand Down
28 changes: 26 additions & 2 deletions tests/utils/test_password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,32 @@ def test_keyring_raises_errors_on_keyring_errors(
key_ring.delete_password("foo", "bar")


def test_keyring_with_chainer_backend_and_not_compatible_only_should_be_unavailable(
with_chained_keyring: None,
def test_keyring_with_chainer_backend_and_fail_keyring_should_be_unavailable(
with_chained_fail_keyring: None,
):
key_ring = KeyRing("poetry")

assert not key_ring.is_available()


def test_keyring_with_chainer_backend_and_null_keyring_should_be_unavailable(
with_chained_null_keyring: None,
):
key_ring = KeyRing("poetry")

assert not key_ring.is_available()


def test_null_keyring_should_be_unavailable(
with_null_keyring: None,
):
key_ring = KeyRing("poetry")

assert not key_ring.is_available()


def test_fail_keyring_should_be_unavailable(
with_fail_keyring: None,
):
key_ring = KeyRing("poetry")

Expand Down

0 comments on commit f462b7f

Please sign in to comment.