Files
CTF-tool/tests/test_utils.py
2026-07-13 21:10:31 -05:00

49 lines
1.6 KiB
Python

from pathlib import Path
import toml
from ctf.utils import load_config, active_categories, active_competitions
# Define the persistent test environment path
TEST_ENV = Path("tests/env")
def test_load_config_static():
"""
Verifies load_config using the persistent test file.
Demonstrates: Reading from a specific relative Path.
"""
config_file = TEST_ENV / "config.toml"
test_data = {
"Competition": {"name": "PersistentComp"},
"Enviroment": {"data_dir": str(TEST_ENV.absolute())}
}
with open(config_file, "w") as f:
toml.dump(test_data, f)
loaded_data = load_config(str(config_file))
assert loaded_data["Competition"]["name"] == "PersistentComp"
def test_active_categories_static():
"""
Verifies active_categories using the pre-created 'comp1' folder.
Demonstrates: Path.iterdir() behavior on a real directory.
"""
comp_dir = TEST_ENV / "comp1"
cats = active_categories(comp_dir)
cat_names = [c.name for c in cats]
# We expect 'web' and 'pwn' which were created in the setup step
assert "web" in cat_names
assert "pwn" in cat_names
def test_active_competitions_static():
"""
Verifies active_competitions skips the 'tools' folder in TEST_ENV.
Demonstrates: Guard clauses and directory filtering.
"""
# Note: active_competitions takes a string path
comps = active_competitions(str(TEST_ENV))
comp_names = [p.name for p in comps.keys()]
assert "comp1" in comp_names
assert "comp2" in comp_names
assert "tools" not in comp_names # The 'tools' folder exists but should be ignored