Skip to content

Commit

Permalink
Fix pypi token keyrinerror (#5189) (#5911)
Browse files Browse the repository at this point in the history
* add checking for env variable in pypi token getter

* add unit tests for env pypi token

* add checking for env variable in pypi token getter

* add unit tests for env pypi token

* add info to the changelog

* refactor the structure

* fix unit tests

* undo changelog addition

* change docstring format to sphinx

* delete uncrsy unit test

* Delete unscry funtion call

Co-authored-by: Bjorn Neergaard <bjorn@neersighted.com>

* Update src/poetry/utils/password_manager.py

Co-authored-by: Bartosz Sokorski <b.sokorski@gmail.com>

* fix unit tests

* lol

* fix type hints

Co-authored-by: Bjorn Neergaard <bjorn@neersighted.com>
Co-authored-by: Bartosz Sokorski <b.sokorski@gmail.com>
  • Loading branch information
3 people authored Aug 22, 2022
1 parent c37a38e commit 386cf9c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
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

0 comments on commit 386cf9c

Please sign in to comment.