Skip to content

Commit

Permalink
Set password directly if keyring credentials are used
Browse files Browse the repository at this point in the history
  • Loading branch information
stollero committed Nov 18, 2024
1 parent 6689b9a commit 1056b7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/hatch/publish/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ def __get_password(self) -> str:
if password is not None:
return password

password = keyring.get_password(self._repo, self.username)
if password is not None:
return password

if self._options['no_prompt']:
self._app.abort('Missing required option: auth')

Expand Down Expand Up @@ -77,6 +73,8 @@ def _read_keyring(self) -> str | None:
creds = keyring.get_credential(self._repo, None)
if not creds:
return None
self.__password = creds.password
self.__password_was_read = True
return creds.username

def _read_previous_working_user_data(self) -> str | None:
Expand Down
32 changes: 17 additions & 15 deletions tests/utils/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import pytest

import hatch.publish.auth
from hatch.publish.auth import AuthenticationCredentials
from hatch.utils.fs import Path


@pytest.fixture(autouse=True)
def mock_keyring(monkeypatch):
class MockKeyring:
@staticmethod
def get_credential(*_):
class Credential:
username = 'gat'
password = 'guido'

return Credential()

monkeypatch.setattr(hatch.publish.auth, 'keyring', MockKeyring)


def test_pypirc(fs):
fs.create_file(
Path.home() / '.pypirc',
Expand Down Expand Up @@ -45,21 +61,7 @@ def test_pypirc(fs):
assert credentials.password == 'gat'


def test_keyring_credentials(monkeypatch):
class MockKeyring:
@staticmethod
def get_credential(*_):
class Credential:
username = 'gat'

return Credential()

@staticmethod
def get_password(*_):
return 'guido'

monkeypatch.setattr(hatch.publish.auth, 'keyring', MockKeyring)

def test_keyring_credentials():
credentials = AuthenticationCredentials(
app=None,
cache_dir=Path('/none'),
Expand Down

0 comments on commit 1056b7b

Please sign in to comment.