|
|
@@ -0,0 +1,257 @@
|
|
|
+from datetime import datetime
|
|
|
+import json
|
|
|
+import pytest
|
|
|
+from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
+from fastapi.testclient import TestClient
|
|
|
+
|
|
|
+
|
|
|
+from app.core.auth import get_current_user, require_min_role, RoleLevel
|
|
|
+from app.core.database import get_db
|
|
|
+from app.models.user import VasUser
|
|
|
+from app.models.node_configuration import NodeConfiguration, NodeConfigAuditLog
|
|
|
+from app.services.node_configuration_service import (
|
|
|
+ NodeConfigurationService,
|
|
|
+ calculate_config_hash,
|
|
|
+)
|
|
|
+from app.schemas.node_configuration import (
|
|
|
+ NodeConfigurationSaveRequest,
|
|
|
+ NodeConfigurationToggleRequest,
|
|
|
+ NodeConfigurationCloneRequest,
|
|
|
+ NodeConfigurationDeleteRequest,
|
|
|
+)
|
|
|
+from app.core.biz_exception import NotFoundError
|
|
|
+from app.main import app
|
|
|
+
|
|
|
+
|
|
|
+class DummyAdminUser:
|
|
|
+ id = "admin_01"
|
|
|
+ role = "admin"
|
|
|
+ email = "admin@example.com"
|
|
|
+ nickname = "SuperAdmin"
|
|
|
+
|
|
|
+
|
|
|
+def test_calculate_config_hash_idempotency():
|
|
|
+ """
|
|
|
+ 测试不同键顺序与格式下,Canonical Hash 的幂等一致性
|
|
|
+ """
|
|
|
+ content_a = {
|
|
|
+ "identifier": "vfs.ie.nl",
|
|
|
+ "debug": False,
|
|
|
+ "sentinel": {"target_instances": 1, "account_pool_id": "ie.nl"},
|
|
|
+ }
|
|
|
+ content_b = {
|
|
|
+ "debug": False,
|
|
|
+ "sentinel": {"account_pool_id": "ie.nl", "target_instances": 1},
|
|
|
+ "identifier": "vfs.ie.nl",
|
|
|
+ }
|
|
|
+ hash_a = calculate_config_hash(content_a)
|
|
|
+ hash_b = calculate_config_hash(content_b)
|
|
|
+ assert hash_a == hash_b
|
|
|
+ assert len(hash_a) == 32
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.anyio
|
|
|
+async def test_node_configuration_service_unit():
|
|
|
+ mock_db = AsyncMock()
|
|
|
+
|
|
|
+ # 1. Test save_config (Create)
|
|
|
+ mock_exec_res = MagicMock()
|
|
|
+ mock_exec_res.scalar_one_or_none.return_value = None
|
|
|
+ mock_db.execute.return_value = mock_exec_res
|
|
|
+
|
|
|
+ payload = NodeConfigurationSaveRequest(
|
|
|
+ role="sentinel",
|
|
|
+ node_id="node01",
|
|
|
+ config_content=[{"identifier": "vfs.ie.nl", "enable": True}],
|
|
|
+ is_enabled=1,
|
|
|
+ description="Sentinel test node",
|
|
|
+ operator="admin",
|
|
|
+ remark="Initial create",
|
|
|
+ )
|
|
|
+
|
|
|
+ created = await NodeConfigurationService.save_config(mock_db, payload, operator="admin")
|
|
|
+ assert created.role == "sentinel"
|
|
|
+ assert created.node_id == "node01"
|
|
|
+ assert len(created.config_hash) == 32
|
|
|
+ assert mock_db.add.call_count == 2 # config + audit_log
|
|
|
+ assert mock_db.commit.called
|
|
|
+
|
|
|
+ # 2. Test save_config (Update)
|
|
|
+ existing_obj = NodeConfiguration(
|
|
|
+ id=1,
|
|
|
+ role="sentinel",
|
|
|
+ node_id="node01",
|
|
|
+ config_content=[{"identifier": "vfs.ie.nl", "enable": True}],
|
|
|
+ config_hash="old_hash_1234567890123456789012",
|
|
|
+ is_enabled=1,
|
|
|
+ description="Old description",
|
|
|
+ )
|
|
|
+ mock_exec_res.scalar_one_or_none.return_value = existing_obj
|
|
|
+
|
|
|
+ update_payload = NodeConfigurationSaveRequest(
|
|
|
+ role="sentinel",
|
|
|
+ node_id="node01",
|
|
|
+ config_content=[{"identifier": "vfs.ie.nl", "enable": False}],
|
|
|
+ is_enabled=1,
|
|
|
+ description="Updated description",
|
|
|
+ operator="admin",
|
|
|
+ remark="Update config",
|
|
|
+ )
|
|
|
+
|
|
|
+ updated = await NodeConfigurationService.save_config(mock_db, update_payload, operator="admin")
|
|
|
+ assert updated.id == 1
|
|
|
+ assert updated.description == "Updated description"
|
|
|
+ assert updated.config_hash != "old_hash_1234567890123456789012"
|
|
|
+
|
|
|
+ # 3. Test toggle_config
|
|
|
+ toggled = await NodeConfigurationService.toggle_config(
|
|
|
+ mock_db,
|
|
|
+ NodeConfigurationToggleRequest(role="sentinel", node_id="node01", is_enabled=0, remark="disable"),
|
|
|
+ operator="admin",
|
|
|
+ )
|
|
|
+ assert toggled.is_enabled == 0
|
|
|
+
|
|
|
+ # 4. Test delete_config
|
|
|
+ deleted = await NodeConfigurationService.delete_config(
|
|
|
+ mock_db,
|
|
|
+ NodeConfigurationDeleteRequest(role="sentinel", node_id="node01", remark="delete"),
|
|
|
+ operator="admin",
|
|
|
+ )
|
|
|
+ assert deleted is True
|
|
|
+
|
|
|
+ # 5. Test get_detail not found
|
|
|
+ mock_exec_res.scalar_one_or_none.return_value = None
|
|
|
+ with pytest.raises(NotFoundError):
|
|
|
+ await NodeConfigurationService.get_detail(mock_db, role="sentinel", node_id="not_exist")
|
|
|
+
|
|
|
+
|
|
|
+def test_node_configuration_api_endpoints():
|
|
|
+ mock_db = AsyncMock()
|
|
|
+ mock_user = DummyAdminUser()
|
|
|
+
|
|
|
+ async def override_get_db():
|
|
|
+ yield mock_db
|
|
|
+
|
|
|
+ async def override_current_user():
|
|
|
+ return mock_user
|
|
|
+
|
|
|
+ app.dependency_overrides[get_db] = override_get_db
|
|
|
+ app.dependency_overrides[get_current_user] = override_current_user
|
|
|
+
|
|
|
+ # Mock service methods
|
|
|
+ sample_node = NodeConfiguration(
|
|
|
+ id=10,
|
|
|
+ role="booker",
|
|
|
+ node_id="worker01",
|
|
|
+ config_content=[{"identifier": "tls.uk.fr", "enable": True}],
|
|
|
+ config_hash="abc12345678901234567890123456789",
|
|
|
+ is_enabled=1,
|
|
|
+ description="Test node",
|
|
|
+ created_by="admin",
|
|
|
+ updated_by="admin",
|
|
|
+ created_at=datetime.utcnow(),
|
|
|
+ updated_at=datetime.utcnow(),
|
|
|
+ )
|
|
|
+
|
|
|
+ sample_log = NodeConfigAuditLog(
|
|
|
+ id=1,
|
|
|
+ role="booker",
|
|
|
+ node_id="worker01",
|
|
|
+ action="CREATE",
|
|
|
+ before_hash=None,
|
|
|
+ after_hash="abc12345678901234567890123456789",
|
|
|
+ operator="admin",
|
|
|
+ remark="Test creation",
|
|
|
+ created_at=datetime.utcnow(),
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+ with patch.object(NodeConfigurationService, "save_config", return_value=sample_node), \
|
|
|
+ patch.object(NodeConfigurationService, "get_detail", return_value=sample_node), \
|
|
|
+ patch.object(NodeConfigurationService, "list_configs", return_value=(1, [sample_node])), \
|
|
|
+ patch.object(NodeConfigurationService, "toggle_config", return_value=sample_node), \
|
|
|
+ patch.object(NodeConfigurationService, "clone_config", return_value=sample_node), \
|
|
|
+ patch.object(NodeConfigurationService, "delete_config", return_value=True), \
|
|
|
+ patch.object(NodeConfigurationService, "get_audit_logs", return_value=(1, [sample_log])):
|
|
|
+
|
|
|
+ with TestClient(app) as client:
|
|
|
+ headers = {"Authorization": "Bearer test_token"}
|
|
|
+
|
|
|
+ # 1. Save
|
|
|
+ res_save = client.post(
|
|
|
+ "/api/node_configurations/save",
|
|
|
+ json={
|
|
|
+ "role": "booker",
|
|
|
+ "node_id": "worker01",
|
|
|
+ "config_content": [{"identifier": "tls.uk.fr", "enable": True}],
|
|
|
+ "is_enabled": 1,
|
|
|
+ "description": "Test node",
|
|
|
+ },
|
|
|
+ headers=headers,
|
|
|
+ )
|
|
|
+ assert res_save.status_code == 200
|
|
|
+ assert res_save.json()["code"] == 0
|
|
|
+ assert res_save.json()["data"]["role"] == "booker"
|
|
|
+ assert res_save.json()["data"]["config_hash"] == "abc12345678901234567890123456789"
|
|
|
+
|
|
|
+ # 2. Detail
|
|
|
+ res_detail = client.get(
|
|
|
+ "/api/node_configurations/detail?role=booker&node_id=worker01",
|
|
|
+ headers=headers,
|
|
|
+ )
|
|
|
+ assert res_detail.status_code == 200
|
|
|
+ assert res_detail.json()["code"] == 0
|
|
|
+ assert res_detail.json()["data"]["node_id"] == "worker01"
|
|
|
+
|
|
|
+ # 3. List
|
|
|
+ res_list = client.get(
|
|
|
+ "/api/node_configurations/list?role=booker&page=1&page_size=10",
|
|
|
+ headers=headers,
|
|
|
+ )
|
|
|
+ assert res_list.status_code == 200
|
|
|
+ assert res_list.json()["code"] == 0
|
|
|
+ assert res_list.json()["data"]["total"] == 1
|
|
|
+ assert len(res_list.json()["data"]["items"]) == 1
|
|
|
+
|
|
|
+ # 4. Toggle
|
|
|
+ res_toggle = client.post(
|
|
|
+ "/api/node_configurations/toggle",
|
|
|
+ json={"role": "booker", "node_id": "worker01", "is_enabled": 0},
|
|
|
+ headers=headers,
|
|
|
+ )
|
|
|
+ assert res_toggle.status_code == 200
|
|
|
+ assert res_toggle.json()["code"] == 0
|
|
|
+
|
|
|
+ # 5. Clone
|
|
|
+ res_clone = client.post(
|
|
|
+ "/api/node_configurations/clone",
|
|
|
+ json={
|
|
|
+ "source_role": "booker",
|
|
|
+ "source_node_id": "worker01",
|
|
|
+ "target_role": "booker",
|
|
|
+ "target_node_id": "worker02",
|
|
|
+ },
|
|
|
+ headers=headers,
|
|
|
+ )
|
|
|
+ assert res_clone.status_code == 200
|
|
|
+ assert res_clone.json()["code"] == 0
|
|
|
+
|
|
|
+ # 6. Audit Logs
|
|
|
+ res_logs = client.get(
|
|
|
+ "/api/node_configurations/audit-logs?role=booker&node_id=worker01",
|
|
|
+ headers=headers,
|
|
|
+ )
|
|
|
+ assert res_logs.status_code == 200
|
|
|
+ assert res_logs.json()["code"] == 0
|
|
|
+ assert res_logs.json()["data"]["total"] == 1
|
|
|
+
|
|
|
+ # 7. Delete
|
|
|
+ res_del = client.delete(
|
|
|
+ "/api/node_configurations/delete?role=booker&node_id=worker01",
|
|
|
+ headers=headers,
|
|
|
+ )
|
|
|
+ assert res_del.status_code == 200
|
|
|
+ assert res_del.json()["code"] == 0
|
|
|
+ assert res_del.json()["data"]["deleted"] is True
|
|
|
+
|
|
|
+ app.dependency_overrides.clear()
|