Commit 386cf9c4 by Jan Bronicki Committed by GitHub

Fix pypi token keyrinerror (#5189) (#5911)

* 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>
parent c37a38e9
...@@ -170,12 +170,21 @@ class PasswordManager: ...@@ -170,12 +170,21 @@ class PasswordManager:
else: else:
self.keyring.set_password(name, "__token__", token) self.keyring.set_password(name, "__token__", token)
def get_pypi_token(self, name: str) -> str | None: def get_pypi_token(self, repo_name: str) -> str | None:
if not self.keyring.is_available(): """Get PyPi token.
token: str | None = self._config.get(f"pypi-token.{name}")
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 token
return self.keyring.get_password(name, "__token__") return self.keyring.get_password(repo_name, "__token__")
def delete_pypi_token(self, name: str) -> None: def delete_pypi_token(self, name: str) -> None:
if not self.keyring.is_available(): if not self.keyring.is_available():
......
...@@ -231,3 +231,31 @@ def test_get_http_auth_from_environment_variables( ...@@ -231,3 +231,31 @@ def test_get_http_auth_from_environment_variables(
assert auth["username"] == "bar" assert auth["username"] == "bar"
assert auth["password"] == "baz" 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment