All checks were successful
Build and push image / build-containers (push) Successful in 10m14s
79 lines
2.5 KiB
Python
79 lines
2.5 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 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.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)
|
|
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
|