From 7a2d4711afd64d1354193be48173080ad5b9a747 Mon Sep 17 00:00:00 2001 From: silversword411 Date: Sat, 8 Nov 2025 05:31:06 -0500 Subject: [PATCH] Refactor test_json.py to improve script loading and validation logic --- .vscode/settings.json | 168 +++++++++++++++++++++--------------------- test_json.py | 118 +++++++++++++++++------------ 2 files changed, 152 insertions(+), 134 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fdd938a9..cbc13412 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,87 +1,83 @@ { - "python.defaultInterpreterPath": "env/bin/python", - "python.languageServer": "Pylance", - "python.analysis.extraPaths": [ - "env" - ], - "python.testing.pytestEnabled": true, - "python.analysis.diagnosticSeverityOverrides": { - "reportUnusedImport": "error", - "reportDuplicateImport": "error", - }, - "python.analysis.typeCheckingMode": "basic", - "python.formatting.provider": "black", - "editor.formatOnSave": true, - "files.watcherExclude": { - "files.watcherExclude": { - "**/.git/objects/**": true, - "**/.git/subtree-cache/**": true, - "**/node_modules/": true, - "/node_modules/**": true, - "**/env/": true, - "/env/**": true, - "**/__pycache__": true, - "/__pycache__/**": true, - "**/.cache": true, - "**/.eggs": true, - "**/.ipynb_checkpoints": true, - "**/.mypy_cache": true, - "**/.pytest_cache": true, - "**/*.egg-info": true, - "**/*.feather": true, - "**/*.parquet*": true, - "**/*.pyc": true, - "**/*.zip": true - }, - }, - "cSpell.words": [ - "ADDC", - "agentname", - "Antispyware", - "Antiviruses", - "Anynet", - "apitw", - "Autorun", - "bdexe", - "bdfb", - "bdurl", - "Bitdefender", - "Bitlocker", - "Bluescreen", - "BSOD", - "Clearand", - "clientname", - "Cortana", - "customidtw", - "Debugmode", - "DIMM's", - "Duplicati", - "ESET", - "Faststartup", - "fromaddress", - "fullname", - "Hudu", - "iperf", - "LAPSID", - "localadmin", - "MSITW", - "netsh", - "procname", - "Restartor", - "Screenconnect", - "Securepoint", - "sitename", - "smtpserver", - "Sophos", - "Speedtest", - "Splashtop", - "SSID", - "SUUID", - "Teamviewer", - "toaddress", - "TRMM", - "urlmsitw", - "warnwhenovermemsize", - "Winget" - ] -} \ No newline at end of file + "python.defaultInterpreterPath": "env/bin/python", + "python.languageServer": "Pylance", + "python.analysis.extraPaths": ["env"], + "python.testing.pytestEnabled": true, + "python.analysis.diagnosticSeverityOverrides": { + "reportUnusedImport": "error", + "reportDuplicateImport": "error" + }, + "python.analysis.typeCheckingMode": "basic", + "python.formatting.provider": "black", + "editor.formatOnSave": true, + "files.watcherExclude": { + "**/.git/objects/**": true, + "**/.git/subtree-cache/**": true, + "**/node_modules/": true, + "/node_modules/**": true, + "**/env/": true, + "/env/**": true, + "**/__pycache__": true, + "/__pycache__/**": true, + "**/.cache": true, + "**/.eggs": true, + "**/.ipynb_checkpoints": true, + "**/.mypy_cache": true, + "**/.pytest_cache": true, + "**/*.egg-info": true, + "**/*.feather": true, + "**/*.parquet*": true, + "**/*.pyc": true, + "**/*.zip": true + }, + "cSpell.words": [ + "ADDC", + "agentname", + "Antispyware", + "Antiviruses", + "Anynet", + "apitw", + "Autorun", + "bdexe", + "bdfb", + "bdurl", + "Bitdefender", + "Bitlocker", + "Bluescreen", + "BSOD", + "Clearand", + "clientname", + "Cortana", + "customidtw", + "Debugmode", + "DIMM's", + "Duplicati", + "ESET", + "Faststartup", + "fromaddress", + "fullname", + "Hudu", + "iperf", + "LAPSID", + "localadmin", + "MSITW", + "netsh", + "procname", + "Restartor", + "Screenconnect", + "Securepoint", + "sitename", + "smtpserver", + "Sophos", + "Speedtest", + "Splashtop", + "SSID", + "SUUID", + "Teamviewer", + "toaddress", + "TRMM", + "urlmsitw", + "warnwhenovermemsize", + "Winget" + ] +} diff --git a/test_json.py b/test_json.py index 7867114a..fce7a6ce 100644 --- a/test_json.py +++ b/test_json.py @@ -1,5 +1,6 @@ import json import os +import pytest def _check_for_duplicate_keys(items): @@ -12,64 +13,85 @@ def _check_for_duplicate_keys(items): return tmp -def test_community_script_json_file(): +def _load_scripts(): + with open("community_scripts.json") as f: + return json.load(f, object_pairs_hook=_check_for_duplicate_keys) + + +@pytest.mark.parametrize( + "script", + _load_scripts(), + ids=lambda script: script["filename"] +) +def test_community_script_json_file(script): valid_shells = ["powershell", "python", "cmd", "shell"] valid_os = ["windows", "linux", "darwin"] - with open("community_scripts.json") as f: - info = json.load(f, object_pairs_hook=_check_for_duplicate_keys) - - guids = [] - for script in info: - fn: str = script["filename"] - assert os.path.exists(os.path.join("scripts", fn)) - assert script["filename"] - assert script["name"] - assert script["description"] - assert script["shell"] - assert script["shell"] in valid_shells - - if fn.endswith(".ps1"): - assert script["shell"] == "powershell" - elif fn.endswith(".bat"): - assert script["shell"] == "cmd" - elif fn.endswith(".py"): - assert script["shell"] == "python" - - if "args" in script.keys(): - assert isinstance(script["args"], list) - - # allows strings as long as they can be type casted to int - if "default_timeout" in script.keys(): - assert isinstance(int(script["default_timeout"]), int) - - # check supported platforms - if "supported_platforms" in script.keys(): - assert isinstance(script["supported_platforms"], list) - for i in script["supported_platforms"]: - assert i in valid_os - - assert "guid" in script.keys() - guids.append(script["guid"]) - - # check guids are unique + fn: str = script["filename"] + assert os.path.exists(os.path.join("scripts", fn)) + assert script["filename"] + assert script["name"] + assert script["description"] + assert script["shell"] + assert script["shell"] in valid_shells + + if fn.endswith(".ps1"): + assert script["shell"] == "powershell" + elif fn.endswith(".bat"): + assert script["shell"] == "cmd" + elif fn.endswith(".py"): + assert script["shell"] == "python" + + if "args" in script.keys(): + assert isinstance(script["args"], list) + + # allows strings as long as they can be type casted to int + if "default_timeout" in script.keys(): + assert isinstance(int(script["default_timeout"]), int) + + # check supported platforms + if "supported_platforms" in script.keys(): + assert isinstance(script["supported_platforms"], list) + for i in script["supported_platforms"]: + assert i in valid_os + + assert "guid" in script.keys() + + +def test_guids_are_unique(): + """Test that all script GUIDs are unique""" + scripts = _load_scripts() + guids = [script["guid"] for script in scripts] assert len(guids) == len(set(guids)) -def test_community_script_has_jsonfile_entry(): - with open(os.path.join("community_scripts.json")) as f: - info = json.load(f) - - filenames = [i["filename"] for i in info] - +def _get_script_files(): + """Get all script files from the scripts directory""" + files = [] with os.scandir("scripts") as it: for f in it: if not f.name.startswith(".") and f.is_file(): - assert f.name in filenames + files.append(f.name) + return files -def test_script_filenames_do_not_contain_spaces(): +@pytest.mark.parametrize( + "filename", + _get_script_files(), + ids=lambda filename: filename +) +def test_community_script_has_jsonfile_entry(filename): with open(os.path.join("community_scripts.json")) as f: info = json.load(f) - for script in info: - assert " " not in script["filename"] + + filenames = [i["filename"] for i in info] + assert filename in filenames + + +@pytest.mark.parametrize( + "script", + _load_scripts(), + ids=lambda script: script["filename"] +) +def test_script_filenames_do_not_contain_spaces(script): + assert " " not in script["filename"]