Skip to content

Commit c6284e3

Browse files
authored
Merge pull request #301 from silversword411/tests_improvevisualizationonerrors
Refactor test_json.py to improve script loading and validation logic
2 parents 177500b + 7a2d471 commit c6284e3

2 files changed

Lines changed: 152 additions & 134 deletions

File tree

.vscode/settings.json

Lines changed: 82 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,83 @@
11
{
2-
"python.defaultInterpreterPath": "env/bin/python",
3-
"python.languageServer": "Pylance",
4-
"python.analysis.extraPaths": [
5-
"env"
6-
],
7-
"python.testing.pytestEnabled": true,
8-
"python.analysis.diagnosticSeverityOverrides": {
9-
"reportUnusedImport": "error",
10-
"reportDuplicateImport": "error",
11-
},
12-
"python.analysis.typeCheckingMode": "basic",
13-
"python.formatting.provider": "black",
14-
"editor.formatOnSave": true,
15-
"files.watcherExclude": {
16-
"files.watcherExclude": {
17-
"**/.git/objects/**": true,
18-
"**/.git/subtree-cache/**": true,
19-
"**/node_modules/": true,
20-
"/node_modules/**": true,
21-
"**/env/": true,
22-
"/env/**": true,
23-
"**/__pycache__": true,
24-
"/__pycache__/**": true,
25-
"**/.cache": true,
26-
"**/.eggs": true,
27-
"**/.ipynb_checkpoints": true,
28-
"**/.mypy_cache": true,
29-
"**/.pytest_cache": true,
30-
"**/*.egg-info": true,
31-
"**/*.feather": true,
32-
"**/*.parquet*": true,
33-
"**/*.pyc": true,
34-
"**/*.zip": true
35-
},
36-
},
37-
"cSpell.words": [
38-
"ADDC",
39-
"agentname",
40-
"Antispyware",
41-
"Antiviruses",
42-
"Anynet",
43-
"apitw",
44-
"Autorun",
45-
"bdexe",
46-
"bdfb",
47-
"bdurl",
48-
"Bitdefender",
49-
"Bitlocker",
50-
"Bluescreen",
51-
"BSOD",
52-
"Clearand",
53-
"clientname",
54-
"Cortana",
55-
"customidtw",
56-
"Debugmode",
57-
"DIMM's",
58-
"Duplicati",
59-
"ESET",
60-
"Faststartup",
61-
"fromaddress",
62-
"fullname",
63-
"Hudu",
64-
"iperf",
65-
"LAPSID",
66-
"localadmin",
67-
"MSITW",
68-
"netsh",
69-
"procname",
70-
"Restartor",
71-
"Screenconnect",
72-
"Securepoint",
73-
"sitename",
74-
"smtpserver",
75-
"Sophos",
76-
"Speedtest",
77-
"Splashtop",
78-
"SSID",
79-
"SUUID",
80-
"Teamviewer",
81-
"toaddress",
82-
"TRMM",
83-
"urlmsitw",
84-
"warnwhenovermemsize",
85-
"Winget"
86-
]
87-
}
2+
"python.defaultInterpreterPath": "env/bin/python",
3+
"python.languageServer": "Pylance",
4+
"python.analysis.extraPaths": ["env"],
5+
"python.testing.pytestEnabled": true,
6+
"python.analysis.diagnosticSeverityOverrides": {
7+
"reportUnusedImport": "error",
8+
"reportDuplicateImport": "error"
9+
},
10+
"python.analysis.typeCheckingMode": "basic",
11+
"python.formatting.provider": "black",
12+
"editor.formatOnSave": true,
13+
"files.watcherExclude": {
14+
"**/.git/objects/**": true,
15+
"**/.git/subtree-cache/**": true,
16+
"**/node_modules/": true,
17+
"/node_modules/**": true,
18+
"**/env/": true,
19+
"/env/**": true,
20+
"**/__pycache__": true,
21+
"/__pycache__/**": true,
22+
"**/.cache": true,
23+
"**/.eggs": true,
24+
"**/.ipynb_checkpoints": true,
25+
"**/.mypy_cache": true,
26+
"**/.pytest_cache": true,
27+
"**/*.egg-info": true,
28+
"**/*.feather": true,
29+
"**/*.parquet*": true,
30+
"**/*.pyc": true,
31+
"**/*.zip": true
32+
},
33+
"cSpell.words": [
34+
"ADDC",
35+
"agentname",
36+
"Antispyware",
37+
"Antiviruses",
38+
"Anynet",
39+
"apitw",
40+
"Autorun",
41+
"bdexe",
42+
"bdfb",
43+
"bdurl",
44+
"Bitdefender",
45+
"Bitlocker",
46+
"Bluescreen",
47+
"BSOD",
48+
"Clearand",
49+
"clientname",
50+
"Cortana",
51+
"customidtw",
52+
"Debugmode",
53+
"DIMM's",
54+
"Duplicati",
55+
"ESET",
56+
"Faststartup",
57+
"fromaddress",
58+
"fullname",
59+
"Hudu",
60+
"iperf",
61+
"LAPSID",
62+
"localadmin",
63+
"MSITW",
64+
"netsh",
65+
"procname",
66+
"Restartor",
67+
"Screenconnect",
68+
"Securepoint",
69+
"sitename",
70+
"smtpserver",
71+
"Sophos",
72+
"Speedtest",
73+
"Splashtop",
74+
"SSID",
75+
"SUUID",
76+
"Teamviewer",
77+
"toaddress",
78+
"TRMM",
79+
"urlmsitw",
80+
"warnwhenovermemsize",
81+
"Winget"
82+
]
83+
}

test_json.py

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import pytest
34

45

56
def _check_for_duplicate_keys(items):
@@ -12,64 +13,85 @@ def _check_for_duplicate_keys(items):
1213
return tmp
1314

1415

15-
def test_community_script_json_file():
16+
def _load_scripts():
17+
with open("community_scripts.json") as f:
18+
return json.load(f, object_pairs_hook=_check_for_duplicate_keys)
19+
20+
21+
@pytest.mark.parametrize(
22+
"script",
23+
_load_scripts(),
24+
ids=lambda script: script["filename"]
25+
)
26+
def test_community_script_json_file(script):
1627
valid_shells = ["powershell", "python", "cmd", "shell"]
1728
valid_os = ["windows", "linux", "darwin"]
1829

19-
with open("community_scripts.json") as f:
20-
info = json.load(f, object_pairs_hook=_check_for_duplicate_keys)
21-
22-
guids = []
23-
for script in info:
24-
fn: str = script["filename"]
25-
assert os.path.exists(os.path.join("scripts", fn))
26-
assert script["filename"]
27-
assert script["name"]
28-
assert script["description"]
29-
assert script["shell"]
30-
assert script["shell"] in valid_shells
31-
32-
if fn.endswith(".ps1"):
33-
assert script["shell"] == "powershell"
34-
elif fn.endswith(".bat"):
35-
assert script["shell"] == "cmd"
36-
elif fn.endswith(".py"):
37-
assert script["shell"] == "python"
38-
39-
if "args" in script.keys():
40-
assert isinstance(script["args"], list)
41-
42-
# allows strings as long as they can be type casted to int
43-
if "default_timeout" in script.keys():
44-
assert isinstance(int(script["default_timeout"]), int)
45-
46-
# check supported platforms
47-
if "supported_platforms" in script.keys():
48-
assert isinstance(script["supported_platforms"], list)
49-
for i in script["supported_platforms"]:
50-
assert i in valid_os
51-
52-
assert "guid" in script.keys()
53-
guids.append(script["guid"])
54-
55-
# check guids are unique
30+
fn: str = script["filename"]
31+
assert os.path.exists(os.path.join("scripts", fn))
32+
assert script["filename"]
33+
assert script["name"]
34+
assert script["description"]
35+
assert script["shell"]
36+
assert script["shell"] in valid_shells
37+
38+
if fn.endswith(".ps1"):
39+
assert script["shell"] == "powershell"
40+
elif fn.endswith(".bat"):
41+
assert script["shell"] == "cmd"
42+
elif fn.endswith(".py"):
43+
assert script["shell"] == "python"
44+
45+
if "args" in script.keys():
46+
assert isinstance(script["args"], list)
47+
48+
# allows strings as long as they can be type casted to int
49+
if "default_timeout" in script.keys():
50+
assert isinstance(int(script["default_timeout"]), int)
51+
52+
# check supported platforms
53+
if "supported_platforms" in script.keys():
54+
assert isinstance(script["supported_platforms"], list)
55+
for i in script["supported_platforms"]:
56+
assert i in valid_os
57+
58+
assert "guid" in script.keys()
59+
60+
61+
def test_guids_are_unique():
62+
"""Test that all script GUIDs are unique"""
63+
scripts = _load_scripts()
64+
guids = [script["guid"] for script in scripts]
5665
assert len(guids) == len(set(guids))
5766

5867

59-
def test_community_script_has_jsonfile_entry():
60-
with open(os.path.join("community_scripts.json")) as f:
61-
info = json.load(f)
62-
63-
filenames = [i["filename"] for i in info]
64-
68+
def _get_script_files():
69+
"""Get all script files from the scripts directory"""
70+
files = []
6571
with os.scandir("scripts") as it:
6672
for f in it:
6773
if not f.name.startswith(".") and f.is_file():
68-
assert f.name in filenames
74+
files.append(f.name)
75+
return files
6976

7077

71-
def test_script_filenames_do_not_contain_spaces():
78+
@pytest.mark.parametrize(
79+
"filename",
80+
_get_script_files(),
81+
ids=lambda filename: filename
82+
)
83+
def test_community_script_has_jsonfile_entry(filename):
7284
with open(os.path.join("community_scripts.json")) as f:
7385
info = json.load(f)
74-
for script in info:
75-
assert " " not in script["filename"]
86+
87+
filenames = [i["filename"] for i in info]
88+
assert filename in filenames
89+
90+
91+
@pytest.mark.parametrize(
92+
"script",
93+
_load_scripts(),
94+
ids=lambda script: script["filename"]
95+
)
96+
def test_script_filenames_do_not_contain_spaces(script):
97+
assert " " not in script["filename"]

0 commit comments

Comments
 (0)