Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pypi token keyrinerror (#5189) #5911

Merged
merged 20 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/poetry/utils/password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,22 @@ def set_pypi_token(self, name: str, token: str) -> None:
else:
self.keyring.set_password(name, "__token__", token)

def get_pypi_token(self, name: str) -> str | None:
if not self.keyring.is_available():
token: str | None = self._config.get(f"pypi-token.{name}")
return token

return self.keyring.get_password(name, "__token__")
def get_pypi_token(self, repo_name: str) -> str | None:
"""Get PyPi token.

First checks the environment variables for a token,
then the configured username/password and the
available keyring.

:param repo_name: Name of repository.
:return: Returns a token as a string if found, otherwise None.
"""
token = self._config.get(f"pypi-token.{repo_name}")
John15321 marked this conversation as resolved.
Show resolved Hide resolved
return (
John15321 marked this conversation as resolved.
Show resolved Hide resolved
token
if token is not None
else self.keyring.get_password(repo_name, "__token__")
)

def delete_pypi_token(self, name: str) -> None:
if not self.keyring.is_available():
Expand Down
47 changes: 47 additions & 0 deletions tests/utils/test_password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

from typing import TYPE_CHECKING
from unittest import mock
John15321 marked this conversation as resolved.
Show resolved Hide resolved

import pytest

Expand Down Expand Up @@ -231,3 +232,49 @@ def test_get_http_auth_from_environment_variables(

assert auth["username"] == "bar"
assert auth["password"] == "baz"


def test_get_pypi_token_with_env_var_positive(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
):
sample_token = "sampletoken-1234"
repo_name = "foo"
manager = PasswordManager(config)

with mock.patch.dict(
os.environ,
{f"POETRY-PYPI-TOKEN-{repo_name}".upper().replace("-", "_"): sample_token},
John15321 marked this conversation as resolved.
Show resolved Hide resolved
):
result_token = manager.get_pypi_token(repo_name)

assert result_token == sample_token


def test_get_pypi_token_with_env_var_negative(
neersighted marked this conversation as resolved.
Show resolved Hide resolved
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
):
sample_token = "sampletoken-1234"
repo_name = "foo"
manager = PasswordManager(config)

with mock.patch.dict(
os.environ,
{
"POETRY-PYPI-TOKEN-{repo_name}".upper().replace("-", "_"): sample_token
+ "somestuff"
},
):
result_token = manager.get_pypi_token(repo_name)

assert result_token != sample_token


def test_get_pypi_token_with_env_var_not_available(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
):
repo_name = "foo"
manager = PasswordManager(config)

result_token = manager.get_pypi_token(repo_name)

assert result_token is None