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 6 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
- Improved documentation of the `readme` option, including multiple files and additional formats ([#5158](https://github.com/python-poetry/poetry/pull/5158))
- Improved contributing documentation ([#5708](https://github.com/python-poetry/poetry/pull/5708))
- Remove all references to `--dev-only` option ([#5771](https://github.com/python-poetry/poetry/pull/5771))
- Added environment variable checking for PyPi token ([#5911](https://github.com/python-poetry/poetry/pull/5911))
mkniewallner marked this conversation as resolved.
Show resolved Hide resolved


## [1.2.0b1] - 2022-03-17

Expand Down
20 changes: 20 additions & 0 deletions src/poetry/utils/password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dataclasses
import logging
import os

from contextlib import suppress
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -173,6 +174,25 @@ def set_pypi_token(self, name: str, token: str) -> None:
self.keyring.set_password(name, "__token__", token)

def get_pypi_token(self, name: str) -> str | None:
"""Get PYPI token.

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


Parameters
mkniewallner marked this conversation as resolved.
Show resolved Hide resolved
----------
name
PYPI username.
neersighted marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Returns a token as a string if found, otherwise None.
"""
if os.getenv("POETRY_PYPI_TOKEN_PYPI"):
return os.getenv("POETRY_PYPI_TOKEN_PYPI")
neersighted marked this conversation as resolved.
Show resolved Hide resolved

if not self.keyring.is_available():
token: str | None = self._config.get(f"pypi-token.{name}")
return token
Expand Down
37 changes: 37 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,39 @@ 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"
manager = PasswordManager(config)

with mock.patch.dict(os.environ, {"POETRY_PYPI_TOKEN_PYPI": sample_token}):
result_token = manager.get_pypi_token("foo")

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"
manager = PasswordManager(config)

with mock.patch.dict(
os.environ, {"POETRY_PYPI_TOKEN_PYPI": sample_token + "somestuff"}
):
result_token = manager.get_pypi_token("foo")

assert result_token != sample_token


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

result_token = manager.get_pypi_token("foo")

assert result_token is None