From 1f2205ff273834e060a0ae458e1423df62a71c84 Mon Sep 17 00:00:00 2001 From: "Jokob @NetAlertX" <96159884+jokob-sk@users.noreply.github.com> Date: Tue, 26 May 2026 21:59:11 +0000 Subject: [PATCH] Refactor sync endpoint tests to use monkeypatch for environment variables and remove unused imports --- test/api_endpoints/test_sync_endpoint.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/test/api_endpoints/test_sync_endpoint.py b/test/api_endpoints/test_sync_endpoint.py index da88b591..baf999d2 100644 --- a/test/api_endpoints/test_sync_endpoint.py +++ b/test/api_endpoints/test_sync_endpoint.py @@ -9,8 +9,6 @@ Covers: import os import sys -from unittest.mock import patch - import pytest INSTALL_PATH = os.getenv("NETALERTX_APP", "/app") @@ -18,7 +16,6 @@ sys.path.extend([f"{INSTALL_PATH}/front/plugins", f"{INSTALL_PATH}/server"]) from helper import get_setting_value # noqa: E402 from api_server.api_server_start import app # noqa: E402 -import api_server.sync_endpoint as sync_endpoint # noqa: E402 @pytest.fixture(scope="session") @@ -75,13 +72,13 @@ def test_sync_post_form_encoded_returns_415(client, api_token): assert resp.status_code == 415 -def test_sync_post_json_body_is_accepted(client, api_token, tmp_path): +def test_sync_post_json_body_is_accepted(client, api_token, tmp_path, monkeypatch): """JSON body must pass validation and return 200.""" plugins_dir = tmp_path / "log" / "plugins" plugins_dir.mkdir(parents=True) - with patch.object(sync_endpoint, "INSTALL_PATH", str(tmp_path)): - resp = client.post( + monkeypatch.setenv("NETALERTX_PLUGINS_LOG", str(plugins_dir)) + resp = client.post( "/sync", headers=auth_headers(api_token), json={"data": "test_payload", "plugin": "TESTPLUGIN", "node_name": "TestNode"}, @@ -93,13 +90,13 @@ def test_sync_post_json_body_is_accepted(client, api_token, tmp_path): assert "message" in data -def test_sync_post_json_body_writes_encoded_file(client, api_token, tmp_path): +def test_sync_post_json_body_writes_encoded_file(client, api_token, tmp_path, monkeypatch): """A successful POST must persist an encoded file in the plugins log dir.""" plugins_dir = tmp_path / "log" / "plugins" plugins_dir.mkdir(parents=True) - with patch.object(sync_endpoint, "INSTALL_PATH", str(tmp_path)): - client.post( + monkeypatch.setenv("NETALERTX_PLUGINS_LOG", str(plugins_dir)) + client.post( "/sync", headers=auth_headers(api_token), json={"data": "encrypted_blob", "plugin": "ARPSCAN", "node_name": "Node1"},