Refactor backend views

This commit is contained in:
2025-06-08 17:40:50 +02:00
parent aa6b55a911
commit 7ad41f43d8
25 changed files with 1382 additions and 452 deletions

View File

@ -11,7 +11,7 @@ from fastapi.testclient import TestClient
from sshecret.crypto import generate_private_key, generate_public_key_string
from sshecret_backend.app import create_backend_app
from sshecret_backend.testing import create_test_token
from sshecret_backend.view_models import AuditView
from sshecret_backend.api.audit.schemas import AuditView
from sshecret_backend.settings import BackendSettings
@ -167,8 +167,8 @@ def test_put_add_secret(test_client: TestClient) -> None:
response_model = response.json()
del response_model["created_at"]
del response_model["updated_at"]
assert response_model == data
for key, value in data.items():
assert response_model.get(key) == value
def test_put_update_secret(test_client: TestClient) -> None:
"""Test updating a client secret."""
@ -407,7 +407,7 @@ def test_get_secret_list(test_client: TestClient) -> None:
assert len(entry["clients"]) == 4
else:
assert len(entry["clients"]) == 1
assert entry["clients"][0] == entry["name"]
assert entry["clients"][0]["name"] == entry["name"]
def test_get_secret_clients(test_client: TestClient) -> None:
@ -428,8 +428,9 @@ def test_get_secret_clients(test_client: TestClient) -> None:
data = resp.json()
assert data["name"] == "commonsecret"
assert "client-0" in data["clients"]
assert "client-1" not in data["clients"]
client_names = [client["name"] for client in data["clients"]]
assert "client-0" in client_names
assert "client-1" not in client_names
assert len(data["clients"]) == 2
@ -473,7 +474,7 @@ def test_operations_with_id(test_client: TestClient) -> None:
data = resp.json()
client = data["clients"][0]
client_id = client["id"]
resp = test_client.get(f"/api/v1/clients/by-id/{client_id}")
resp = test_client.get(f"/api/v1/clients/id:{client_id}")
assert resp.status_code == 200
data = resp.json()
assert data["name"] == "test"
@ -559,3 +560,47 @@ def test_filter_audit_log(test_client: TestClient) -> None:
assert data["results"][0]["operation"] == "login"
assert data["results"][0]["message"] == "message1"
def test_secret_flexid(test_client: TestClient) -> None:
"""Test flexible IDs in the secret API."""
client_name = "test"
create_response = create_client(
test_client,
client_name,
)
assert create_response.status_code == 200
assert "id" in create_response.json()
client_id = create_response.json()["id"]
# Create a secret using the client name
secrets: dict[str, str] = {}
resp = test_client.put(
"/api/v1/clients/test/secrets/clientnamesecret",
json={"value": "secret"},
)
assert resp.status_code == 200
secret_data = resp.json()
assert "id" in secret_data
secrets["clientnamesecret"] = secret_data["id"]
# Create one using the client ID
resp = test_client.put(
f"/api/v1/clients/id:{client_id}/secrets/clientidsecret",
json={"value": "secret"},
)
assert resp.status_code == 200
secret_data = resp.json()
assert "id" in secret_data
secrets["clientidsecret"] = secret_data["id"]
# Let's try fetching the various permutations
for client_identifier in ("test", f"id:{client_id}"):
for secret_name, secret_id in secrets.items():
for secret_identifier in (secret_name, f"id:{secret_id}"):
resp = test_client.get(f"/api/v1/clients/{client_identifier}/secrets/{secret_identifier}")
assert resp.status_code == 200
resp_body = resp.json()
assert "id" in resp_body
assert resp_body["id"] == secret_id