mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-01-22 05:38:32 -05:00
New Features: - API endpoints now support comprehensive input validation with detailed error responses via Pydantic models. - OpenAPI specification endpoint (/openapi.json) and interactive Swagger UI documentation (/docs) now available for API discovery. - Enhanced MCP session lifecycle management with create, retrieve, and delete operations. - Network diagnostic tools: traceroute, nslookup, NMAP scanning, and network topology viewing exposed via API. - Device search, filtering by status (including 'offline'), and bulk operations (copy, delete, update). - Wake-on-LAN functionality for remote device management. - Added dynamic tool disablement and status reporting. Bug Fixes: - Fixed get_tools_status in registry to correctly return boolean values instead of None for enabled tools. - Improved error handling for invalid API inputs with standardized validation responses. - Fixed OPTIONS request handling for cross-origin requests. Refactoring: - Significant refactoring of api_server_start.py to use decorator-based validation (@validate_request).
123 lines
3.8 KiB
Python
123 lines
3.8 KiB
Python
import sys
|
|
import base64
|
|
import random
|
|
import os
|
|
import pytest
|
|
|
|
INSTALL_PATH = os.getenv('NETALERTX_APP', '/app')
|
|
sys.path.extend([f"{INSTALL_PATH}/front/plugins", f"{INSTALL_PATH}/server"])
|
|
|
|
from helper import get_setting_value # noqa: E402 [flake8 lint suppression]
|
|
from utils.datetime_utils import timeNowDB # noqa: E402 [flake8 lint suppression]
|
|
from api_server.api_server_start import app # noqa: E402 [flake8 lint suppression]
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def api_token():
|
|
return get_setting_value("API_TOKEN")
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
with app.test_client() as client:
|
|
yield client
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def test_mac():
|
|
# Generate a unique MAC for each test run
|
|
return "AA:BB:CC:" + ":".join(f"{random.randint(0, 255):02X}" for _ in range(3))
|
|
|
|
|
|
def auth_headers(token):
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
def b64(sql: str) -> str:
|
|
"""Helper to base64 encode SQL"""
|
|
return base64.b64encode(sql.encode("utf-8")).decode("utf-8")
|
|
|
|
|
|
# -----------------------------
|
|
# Device lifecycle via dbquery endpoints
|
|
# -----------------------------
|
|
def test_dbquery_create_device(client, api_token, test_mac):
|
|
|
|
now = timeNowDB()
|
|
|
|
sql = f"""
|
|
INSERT INTO Devices (devMac, devName, devVendor, devOwner, devFirstConnection, devLastConnection, devLastIP)
|
|
VALUES ('{test_mac}', 'UnitTestDevice', 'TestVendor', 'UnitTest', '{now}', '{now}', '192.168.100.22' )
|
|
"""
|
|
resp = client.post(
|
|
"/dbquery/write",
|
|
json={"rawSql": b64(sql), "confirm_dangerous_query": True},
|
|
headers=auth_headers(api_token)
|
|
)
|
|
print(resp.json)
|
|
print(resp)
|
|
assert resp.status_code == 200
|
|
assert resp.json.get("success") is True
|
|
assert resp.json.get("affected_rows") == 1
|
|
|
|
|
|
def test_dbquery_read_device(client, api_token, test_mac):
|
|
sql = f"SELECT * FROM Devices WHERE devMac = '{test_mac}'"
|
|
resp = client.post(
|
|
"/dbquery/read",
|
|
json={"rawSql": b64(sql), "confirm_dangerous_query": True},
|
|
headers=auth_headers(api_token)
|
|
)
|
|
assert resp.status_code == 200
|
|
assert resp.json.get("success") is True
|
|
results = resp.json.get("results")
|
|
assert any(row["devMac"] == test_mac for row in results)
|
|
|
|
|
|
def test_dbquery_update_device(client, api_token, test_mac):
|
|
sql = f"""
|
|
UPDATE Devices
|
|
SET devName = 'UnitTestDeviceRenamed'
|
|
WHERE devMac = '{test_mac}'
|
|
"""
|
|
resp = client.post(
|
|
"/dbquery/write",
|
|
json={"rawSql": b64(sql), "confirm_dangerous_query": True},
|
|
headers=auth_headers(api_token)
|
|
)
|
|
assert resp.status_code == 200
|
|
assert resp.json.get("success") is True
|
|
assert resp.json.get("affected_rows") == 1
|
|
|
|
# Verify update
|
|
sql_check = f"SELECT devName FROM Devices WHERE devMac = '{test_mac}'"
|
|
resp2 = client.post(
|
|
"/dbquery/read",
|
|
json={"rawSql": b64(sql_check), "confirm_dangerous_query": True},
|
|
headers=auth_headers(api_token)
|
|
)
|
|
assert resp2.status_code == 200
|
|
assert resp2.json.get("results")[0]["devName"] == "UnitTestDeviceRenamed"
|
|
|
|
|
|
def test_dbquery_delete_device(client, api_token, test_mac):
|
|
sql = f"DELETE FROM Devices WHERE devMac = '{test_mac}'"
|
|
resp = client.post(
|
|
"/dbquery/write",
|
|
json={"rawSql": b64(sql), "confirm_dangerous_query": True},
|
|
headers=auth_headers(api_token)
|
|
)
|
|
assert resp.status_code == 200
|
|
assert resp.json.get("success") is True
|
|
assert resp.json.get("affected_rows") == 1
|
|
|
|
# Verify deletion
|
|
sql_check = f"SELECT * FROM Devices WHERE devMac = '{test_mac}'"
|
|
resp2 = client.post(
|
|
"/dbquery/read",
|
|
json={"rawSql": b64(sql_check), "confirm_dangerous_query": True},
|
|
headers=auth_headers(api_token)
|
|
)
|
|
assert resp2.status_code == 200
|
|
assert resp2.json.get("results") == []
|