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 all 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
17 changes: 13 additions & 4 deletions src/poetry/utils/password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,21 @@ 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}")
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: str | None = self._config.get(f"pypi-token.{repo_name}")
if token:
return token

return self.keyring.get_password(name, "__token__")
return self.keyring.get_password(repo_name, "__token__")

def delete_pypi_token(self, name: str) -> None:
if not self.keyring.is_available():
Expand Down
28 changes: 28 additions & 0 deletions tests/utils/test_password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,31 @@ 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(
mocker: MockerFixture,
config: Config,
with_simple_keyring: None,
dummy_keyring: DummyBackend,
):
sample_token = "sampletoken-1234"
repo_name = "foo"
manager = PasswordManager(config)
mocker.patch.dict(
os.environ,
{f"POETRY_PYPI_TOKEN_{repo_name.upper()}": sample_token},
)

assert manager.get_pypi_token(repo_name) == 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