Files
sshecret/tests/integration/test_backend.py
2025-05-11 11:22:00 +02:00

59 lines
1.7 KiB
Python

"""Test backend.
These tests just ensure that the backend works well enough for us to run the
rest of the tests.
"""
import pytest
import httpx
from sshecret.backend import SshecretBackend
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
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 "test" in secret_to_client.clients
secret = await backend_api.get_client_secret("test", "mysecret")
assert secret is not None
assert secret == "encrypted_secret"