"""Test backend. These tests just ensure that the backend works well enough for us to run the rest of the tests. """ import uuid import pytest import httpx from sshecret.backend import SshecretBackend from sshecret.backend.models import ClientFilter from .clients import create_test_client @pytest.mark.asyncio async def test_healthcheck(backend_client: httpx.AsyncClient) -> None: """Test healthcheck command.""" resp = await backend_client.get("/health") assert resp.status_code == 200 assert resp.json() == {"status": "LIVE"} @pytest.mark.asyncio async def test_create_client(backend_api: SshecretBackend) -> None: """Test creating a client.""" test_client = create_test_client("test") await backend_api.create_client("test", test_client.public_key, "A test client") # fetch the list of clients. clients = await backend_api.get_clients() assert clients is not None assert len(clients) == 1 assert clients[0].name == "test" assert clients[0].public_key == test_client.public_key @pytest.mark.asyncio async def test_create_secret(backend_api: SshecretBackend) -> None: """Test creating secrets.""" test_client = create_test_client("test") await backend_api.create_client("test", test_client.public_key, "A test client") await backend_api.create_client_secret("test", "mysecret", "encrypted_secret") secrets = await backend_api.get_secrets() assert len(secrets) == 1 assert secrets[0].name == "mysecret" secret_to_client = await backend_api.get_secret("mysecret") assert secret_to_client is not None assert secret_to_client.name == "mysecret" assert secret_to_client.clients[0].name == "test" secret = await backend_api.get_client_secret("test", "mysecret") assert secret is not None assert secret == "encrypted_secret" @pytest.mark.skip("This test is broken due to time precision issues") @pytest.mark.parametrize("offset,limit", [(0, 10), (0, 20), (10, 1)]) @pytest.mark.asyncio async def test_client_filtering(backend_api: SshecretBackend, offset: int, limit: int) -> None: """Test filtering on the backend API.""" # We need to create 100 test clients. for x in range(20): client_name = f"test-{x}" test_client = create_test_client(client_name) await backend_api.create_client(client_name, test_client.public_key) client_filter = ClientFilter(offset=offset, limit=limit, order_by="name") clients = await backend_api.get_clients(client_filter) assert len(clients) == limit first_client = clients[0] expected_name = f"test-{offset}" assert first_client.name == expected_name class TestClientDeletion: """Tests that ensure client deletion properly works.""" @pytest.fixture(autouse=True) @pytest.mark.asyncio async def create_client(self, backend_api: SshecretBackend) -> None: """Create initial client.""" test_client = create_test_client("testclient") await backend_api.create_client(name="testclient", public_key=test_client.public_key, description="Test Client") @pytest.mark.asyncio async def test_delete_client(self, backend_api: SshecretBackend) -> None: """Test deleting a client.""" client_name = "testclient" received_client = await backend_api.get_client(("name", client_name)) assert received_client is not None assert received_client.id is not None client_id = str(received_client.id) await backend_api.delete_client(("name", client_name)) received_by_name = await backend_api.get_client(("name", client_name)) received_by_id = await backend_api.get_client(("id", client_id)) assert received_by_name is None # Should this be None? assert received_by_id is None # Check if it's gone from all clients. all_clients = await backend_api.get_clients() assert len(all_clients) == 0 @pytest.mark.asyncio async def test_delete_and_recreate(self, backend_api: SshecretBackend) -> None: """Test deleting a client and creating it again.""" await backend_api.delete_client(("name", "testclient")) test_client = create_test_client("testclient") await backend_api.create_client(name="testclient", public_key=test_client.public_key, description="Test Client") new_client = await backend_api.get_client(("name", "testclient")) assert new_client is not None @pytest.mark.asyncio async def test_delete_with_secrets(self, backend_api: SshecretBackend) -> None: """Ensure that the client is gone properly.""" await backend_api.create_client_secret(("name", "testclient"), "testsecret", "test") await backend_api.delete_client(("name", "testclient")) secrets = await backend_api.get_secrets() # What do we actually expect to happen here? Should the secret be archived somehow? assert len(secrets) == 1 secret = secrets[0] assert len(secret.clients) == 0