Skip to content

Commit

Permalink
Remove dummy secrets handler, just use mock
Browse files Browse the repository at this point in the history
  • Loading branch information
TheByronHimes committed Oct 30, 2024
1 parent 14d37ad commit 536198b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 37 deletions.
22 changes: 0 additions & 22 deletions tests/fixtures/dummies.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,6 @@
from sms.models import Criteria, UpsertionDetails
from sms.ports.inbound.docs_handler import DocsHandlerPort
from sms.ports.inbound.objects_handler import ObjectsHandlerPort, S3ObjectStoragesPort
from sms.ports.inbound.secrets_handler import SecretsHandlerPort


class DummySecretsHandler(SecretsHandlerPort):
"""Dummy SecretsHandler implementation for testing.
`secrets` is a dictionary that maps vault paths to lists of secrets.
"""

def __init__(
self,
secrets: dict[str, list[str]] | None = None,
):
self.secrets = secrets if secrets else {}

def get_secrets(self, vault_path: str) -> list[str]:
"""Get all secrets currently stored for the specified vault."""
return self.secrets.get(vault_path, [])

def delete_secrets(self, vault_path: str) -> None:
"""Delete all secrets stored in the vault."""
self.secrets.pop(vault_path, None)


@dataclass
Expand Down
22 changes: 7 additions & 15 deletions tests/unit/secrets/test_rest_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"""Unit tests for the Vault part of the SMS REST API."""

from typing import Any
from unittest.mock import Mock

import pytest

from tests.fixtures.config import DEFAULT_TEST_CONFIG
from tests.fixtures.dummies import DummySecretsHandler
from tests.fixtures.utils import VALID_BEARER_TOKEN, get_rest_client_with_mocks
from tests.fixtures.vault import DEFAULT_VAULT_PATH

Expand All @@ -44,33 +44,25 @@
)
async def test_get_secrets(stored_secrets: list[str]):
"""Test the GET secrets endpoint without errors."""
vault_path_and_secrets = {DEFAULT_VAULT_PATH: stored_secrets}
secrets_handler = DummySecretsHandler(secrets=vault_path_and_secrets)
secrets_handler = Mock()
secrets_handler.get_secrets.return_value = stored_secrets
async with get_rest_client_with_mocks(
config=DEFAULT_TEST_CONFIG, secrets_handler_override=secrets_handler
) as client:
response = await client.get(TEST_URL, headers=HEADERS)

assert response.status_code == 200
assert response.json() == stored_secrets
secrets_handler.get_secrets.assert_called_once_with(DEFAULT_VAULT_PATH)


@pytest.mark.parametrize(
"stored_secrets",
[
[],
["secret1", "secret2", "secret3"],
],
ids=["VaultIsEmpty", "VaultHasThreeSecrets"],
)
async def test_delete_secrets(stored_secrets: list[str]):
async def test_delete_secrets():
"""Test the DELETE secrets endpoint without errors."""
vault_path_and_secrets = {DEFAULT_VAULT_PATH: stored_secrets}
secrets_handler = DummySecretsHandler(secrets=vault_path_and_secrets)
secrets_handler = Mock()
async with get_rest_client_with_mocks(
config=DEFAULT_TEST_CONFIG, secrets_handler_override=secrets_handler
) as client:
response = await client.delete(TEST_URL, headers=HEADERS)

assert response.status_code == 204
assert secrets_handler.secrets.get(DEFAULT_VAULT_PATH, []) == []
secrets_handler.delete_secrets.assert_called_once_with(DEFAULT_VAULT_PATH)

0 comments on commit 536198b

Please sign in to comment.