Skip to content

Commit

Permalink
add unit tests for env pypi token
Browse files Browse the repository at this point in the history
  • Loading branch information
John15321 committed Jun 25, 2022
1 parent 28bf29a commit e688067
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/poetry/utils/password_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ 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
----------
name
PYPI username.
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")

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

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(
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

0 comments on commit e688067

Please sign in to comment.