Skip to content

Commit

Permalink
chore: move FakeKeyring fixture to conftest
Browse files Browse the repository at this point in the history
All unit test should use the mocked keyring.

Signed-off-by: Sergio Schvezov <sergio.schvezov@canonical.com>
  • Loading branch information
sergiusens committed Nov 21, 2023
1 parent ac1ff0a commit 93eb2e8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
59 changes: 59 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,70 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import datetime
from typing import Any, List, Optional, Tuple
from unittest.mock import patch

import pytest


class FakeKeyring:
"""Fake Keyring Backend implementation for tests."""

name = "Fake Keyring"

def __init__(self) -> None:
self.set_password_calls: List[Tuple[Any, ...]] = []
self.get_password_calls: List[Tuple[Any, ...]] = []
self.delete_password_calls: List[Tuple[Any, ...]] = []
self.password = None
self.delete_error: Optional[Exception] = None

def set_password(self, *args) -> None:
"""Set the service password for username in memory."""
self.set_password_calls.append(args)
self.password = args[2]

def get_password(self, *args):
"""Get the service password for username from memory."""
self.get_password_calls.append(args)
return self.password

def delete_password(self, *args):
"""Delete the service password for username from memory."""
self.delete_password_calls.append(args)
if self.delete_error is not None:
# https://www.logilab.org/ticket/3207
raise self.delete_error # pylint: disable=raising-bad-type


@pytest.fixture()
def keyring_set_keyring_mock():
"""Mock setting the keyring."""

patched_keyring = patch("keyring.set_keyring", autospec=True)
mocked_keyring = patched_keyring.start()
yield mocked_keyring
patched_keyring.stop()


@pytest.fixture()
def fake_keyring():
return FakeKeyring()


@pytest.fixture(autouse=True)
def fake_keyring_get(fake_keyring, request):
"""Mock keyring and return a FakeKeyring."""
if "disable_fake_keyring" in request.keywords:
yield
else:
patched_keyring = patch("keyring.get_keyring")
mocked_keyring = patched_keyring.start()
mocked_keyring.return_value = fake_keyring
yield mocked_keyring
patched_keyring.stop()


@pytest.fixture()
def expires():
"""Mocks/freezes utcnow() in craft_store.endpoints module.
Expand Down
61 changes: 1 addition & 60 deletions tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import logging
from typing import Any, List, Optional, Tuple
from unittest.mock import ANY, patch
from unittest.mock import ANY

import keyring
import keyring.backends.fail
Expand All @@ -26,64 +25,6 @@
from craft_store.auth import Auth, MemoryKeyring


class FakeKeyring:
"""Fake Keyring Backend implementation for tests."""

name = "Fake Keyring"

def __init__(self) -> None:
self.set_password_calls: List[Tuple[Any, ...]] = []
self.get_password_calls: List[Tuple[Any, ...]] = []
self.delete_password_calls: List[Tuple[Any, ...]] = []
self.password = None
self.delete_error: Optional[Exception] = None

def set_password(self, *args) -> None:
"""Set the service password for username in memory."""
self.set_password_calls.append(args)
self.password = args[2]

def get_password(self, *args):
"""Get the service password for username from memory."""
self.get_password_calls.append(args)
return self.password

def delete_password(self, *args):
"""Delete the service password for username from memory."""
self.delete_password_calls.append(args)
if self.delete_error is not None:
# https://www.logilab.org/ticket/3207
raise self.delete_error # pylint: disable=raising-bad-type


@pytest.fixture()
def keyring_set_keyring_mock():
"""Mock setting the keyring."""

patched_keyring = patch("keyring.set_keyring", autospec=True)
mocked_keyring = patched_keyring.start()
yield mocked_keyring
patched_keyring.stop()


@pytest.fixture()
def fake_keyring():
return FakeKeyring()


@pytest.fixture(autouse=True)
def fake_keyring_get(fake_keyring, request):
"""Mock keyring and return a FakeKeyring."""
if "disable_fake_keyring" in request.keywords:
yield
else:
patched_keyring = patch("keyring.get_keyring")
mocked_keyring = patched_keyring.start()
mocked_keyring.return_value = fake_keyring
yield mocked_keyring
patched_keyring.stop()


def test_set_credentials(caplog, fake_keyring):
auth = Auth("fakeclient", "fakestore.com")

Expand Down

0 comments on commit 93eb2e8

Please sign in to comment.