""" LinkSyncServer - Auth API Tests (extended) """ import uuid import pytest from fastapi.testclient import TestClient class TestAuthExtended: def test_list_api_keys(self, client: TestClient, admin_token: str): response = client.get( "/api/auth/api-keys", headers={"Authorization": f"Bearer {admin_token}"}, ) assert response.status_code == 200 assert isinstance(response.json(), list) def test_list_api_keys_empty(self, client: TestClient, admin_token: str): response = client.get( "/api/auth/api-keys", headers={"Authorization": f"Bearer {admin_token}"}, ) assert response.status_code == 200 assert isinstance(response.json(), list) def test_create_and_list_api_key(self, client: TestClient, admin_token: str): import uuid key_name = f"test-key-{uuid.uuid4().hex[:8]}" client.post( "/api/auth/api-key", params={"name": key_name}, headers={"Authorization": f"Bearer {admin_token}"}, ) response = client.get( "/api/auth/api-keys", headers={"Authorization": f"Bearer {admin_token}"}, ) assert response.status_code == 200 keys = response.json() assert any(k["name"] == key_name for k in keys) def test_get_api_key(self, client: TestClient, admin_token: str): import uuid key_name = f"get-key-{uuid.uuid4().hex[:8]}" create_resp = client.post( "/api/auth/api-key", params={"name": key_name}, headers={"Authorization": f"Bearer {admin_token}"}, ) key_id = create_resp.json()["key_id"] response = client.get( f"/api/auth/api-key/{key_id}", headers={"Authorization": f"Bearer {admin_token}"}, ) assert response.status_code == 200 data = response.json() assert data["name"] == key_name def test_get_api_key_not_found(self, client: TestClient, admin_token: str): response = client.get( "/api/auth/api-key/00000000-0000-0000-0000-000000000000", headers={"Authorization": f"Bearer {admin_token}"}, ) assert response.status_code == 404 def test_delete_api_key(self, client: TestClient, admin_token: str): import uuid key_name = f"del-key-{uuid.uuid4().hex[:8]}" create_resp = client.post( "/api/auth/api-key", params={"name": key_name}, headers={"Authorization": f"Bearer {admin_token}"}, ) key_id = create_resp.json()["key_id"] response = client.delete( f"/api/auth/api-key/{key_id}", headers={"Authorization": f"Bearer {admin_token}"}, ) assert response.status_code == 200 def test_delete_api_key_not_found(self, client: TestClient, admin_token: str): response = client.delete( "/api/auth/api-key/00000000-0000-0000-0000-000000000000", headers={"Authorization": f"Bearer {admin_token}"}, ) assert response.status_code == 404 def test_api_key_requires_auth(self, client: TestClient): response = client.get("/api/auth/api-keys") assert response.status_code == 401 def test_create_api_key_requires_auth(self, client: TestClient): response = client.post("/api/auth/api-key", params={"name": "test"}) assert response.status_code == 401