[GEMINI] Refactor tests to use Config class and autouse test isolation fixture
This commit is contained in:
27
tests/conftest.py
Normal file
27
tests/conftest.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# tests/conftest.py
|
||||||
|
import pytest
|
||||||
|
import toml
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def setup_test_config(tmp_path, monkeypatch):
|
||||||
|
"""Autouse fixture to isolate test runs from the real config.toml."""
|
||||||
|
test_config = tmp_path / "config.toml"
|
||||||
|
default_data = {
|
||||||
|
"Competition": {
|
||||||
|
"competition": "picoctf",
|
||||||
|
"catagory": "textCat",
|
||||||
|
"challenge": "testChal",
|
||||||
|
"flag_format": r"picoCTF\{.*\}",
|
||||||
|
"last_flag": "flag{my_flag_here}"
|
||||||
|
},
|
||||||
|
"Enviroment": {
|
||||||
|
"ctf_dir": "/home/venus/ctf"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
test_config.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with open(test_config, "w") as f:
|
||||||
|
toml.dump(default_data, f)
|
||||||
|
|
||||||
|
monkeypatch.setenv("CTF_CONFIG_PATH", str(test_config))
|
||||||
|
return test_config
|
||||||
@@ -4,7 +4,7 @@ import pytest
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from ctf.config import load_config, write_config
|
from ctf.config import Config
|
||||||
|
|
||||||
# {{{ test_flag_cmd_cli
|
# {{{ test_flag_cmd_cli
|
||||||
def test_flag_cmd_cli():
|
def test_flag_cmd_cli():
|
||||||
@@ -13,16 +13,9 @@ def test_flag_cmd_cli():
|
|||||||
"""
|
"""
|
||||||
from ctf.main import cli
|
from ctf.main import cli
|
||||||
|
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
cfg = Config()
|
||||||
original_config = load_config(str(config_file))
|
cfg.data["Competition"]["last_flag"] = "flag{test_cli_flag}"
|
||||||
|
cfg.save(cfg.data)
|
||||||
try:
|
|
||||||
# Pre-set last_flag in config
|
|
||||||
temp_config = load_config(str(config_file))
|
|
||||||
if "Competition" not in temp_config:
|
|
||||||
temp_config["Competition"] = {}
|
|
||||||
temp_config["Competition"]["last_flag"] = "flag{test_cli_flag}"
|
|
||||||
write_config(temp_config, str(config_file))
|
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
|
|
||||||
@@ -35,8 +28,6 @@ def test_flag_cmd_cli():
|
|||||||
result_plain = runner.invoke(cli, ["flag", "--plain"])
|
result_plain = runner.invoke(cli, ["flag", "--plain"])
|
||||||
assert result_plain.exit_code == 0
|
assert result_plain.exit_code == 0
|
||||||
assert result_plain.output.strip() == "flag{test_cli_flag}"
|
assert result_plain.output.strip() == "flag{test_cli_flag}"
|
||||||
finally:
|
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# {{{ test_flag_cmd_exemption
|
# {{{ test_flag_cmd_exemption
|
||||||
@@ -46,16 +37,10 @@ def test_flag_cmd_exemption(capsys):
|
|||||||
"""
|
"""
|
||||||
from ctf.main import main
|
from ctf.main import main
|
||||||
|
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
cfg = Config()
|
||||||
original_config = load_config(str(config_file))
|
cfg.data["Competition"]["flag_format"] = r"flag\{[a-z_]+\}"
|
||||||
|
cfg.data["Competition"]["last_flag"] = "flag{test_exempt_flag}"
|
||||||
try:
|
cfg.save(cfg.data)
|
||||||
temp_config = load_config(str(config_file))
|
|
||||||
if "Competition" not in temp_config:
|
|
||||||
temp_config["Competition"] = {}
|
|
||||||
temp_config["Competition"]["flag_format"] = r"flag\{[a-z_]+\}"
|
|
||||||
temp_config["Competition"]["last_flag"] = "flag{test_exempt_flag}"
|
|
||||||
write_config(temp_config, str(config_file))
|
|
||||||
|
|
||||||
with patch.object(sys, "argv", ["ctf", "flag"]):
|
with patch.object(sys, "argv", ["ctf", "flag"]):
|
||||||
with pytest.raises(SystemExit) as exc_info:
|
with pytest.raises(SystemExit) as exc_info:
|
||||||
@@ -67,8 +52,6 @@ def test_flag_cmd_exemption(capsys):
|
|||||||
assert "Potential flag(s) detected in command output" not in captured.out
|
assert "Potential flag(s) detected in command output" not in captured.out
|
||||||
# Verify that the actual flag info IS printed
|
# Verify that the actual flag info IS printed
|
||||||
assert "Last detected flag: flag{test_exempt_flag}" in captured.out
|
assert "Last detected flag: flag{test_exempt_flag}" in captured.out
|
||||||
finally:
|
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# {{{ test_flag_cmd_list_format
|
# {{{ test_flag_cmd_list_format
|
||||||
@@ -78,15 +61,9 @@ def test_flag_cmd_list_format():
|
|||||||
"""
|
"""
|
||||||
from ctf.main import cli
|
from ctf.main import cli
|
||||||
|
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
cfg = Config()
|
||||||
original_config = load_config(str(config_file))
|
cfg.data["Competition"]["flag_format"] = "TEST_FORMAT{[a-z]+}"
|
||||||
|
cfg.save(cfg.data)
|
||||||
try:
|
|
||||||
temp_config = load_config(str(config_file))
|
|
||||||
if "Competition" not in temp_config:
|
|
||||||
temp_config["Competition"] = {}
|
|
||||||
temp_config["Competition"]["flag_format"] = "TEST_FORMAT{[a-z]+}"
|
|
||||||
write_config(temp_config, str(config_file))
|
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
|
|
||||||
@@ -99,8 +76,6 @@ def test_flag_cmd_list_format():
|
|||||||
result_plain = runner.invoke(cli, ["flag", "-l", "-p"])
|
result_plain = runner.invoke(cli, ["flag", "-l", "-p"])
|
||||||
assert result_plain.exit_code == 0
|
assert result_plain.exit_code == 0
|
||||||
assert result_plain.output.strip() == "TEST_FORMAT{[a-z]+}"
|
assert result_plain.output.strip() == "TEST_FORMAT{[a-z]+}"
|
||||||
finally:
|
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# {{{ test_flag_cmd_set_format
|
# {{{ test_flag_cmd_set_format
|
||||||
@@ -110,18 +85,11 @@ def test_flag_cmd_set_format():
|
|||||||
"""
|
"""
|
||||||
from ctf.main import cli
|
from ctf.main import cli
|
||||||
|
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
|
||||||
original_config = load_config(str(config_file))
|
|
||||||
|
|
||||||
try:
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
result = runner.invoke(cli, ["flag", "-s", "NEW_FLAG_FORMAT{[0-9]+}"])
|
result = runner.invoke(cli, ["flag", "-s", "NEW_FLAG_FORMAT{[0-9]+}"])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "Flag format set to: NEW_FLAG_FORMAT{[0-9]+}" in result.output
|
assert "Flag format set to: NEW_FLAG_FORMAT{[0-9]+}" in result.output
|
||||||
|
|
||||||
updated_config = load_config(str(config_file))
|
updated_cfg = Config()
|
||||||
assert updated_config["Competition"]["flag_format"] == "NEW_FLAG_FORMAT{[0-9]+}"
|
assert updated_cfg.data["Competition"]["flag_format"] == "NEW_FLAG_FORMAT{[0-9]+}"
|
||||||
finally:
|
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
|
# tests/test_commands.py
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import toml
|
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
from ctf.commands import basic_group
|
from ctf.commands import basic_group
|
||||||
|
from ctf.config import Config
|
||||||
|
|
||||||
TEST_ENV = Path("tests/env")
|
# {{{ test_basic_test_cli
|
||||||
|
|
||||||
def test_basic_test_cli():
|
def test_basic_test_cli():
|
||||||
"""
|
"""
|
||||||
Verifies that 'basic test' runs successfully and outputs 'hello from test'.
|
Verifies that 'basic test' runs successfully and outputs 'hello from test'.
|
||||||
@@ -13,7 +13,9 @@ def test_basic_test_cli():
|
|||||||
result = runner.invoke(basic_group, ["test"])
|
result = runner.invoke(basic_group, ["test"])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "hello from test" in result.output
|
assert "hello from test" in result.output
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# {{{ test_basic_greet_cli
|
||||||
def test_basic_greet_cli():
|
def test_basic_greet_cli():
|
||||||
"""
|
"""
|
||||||
Verifies that 'basic greet' runs successfully and greets the name argument.
|
Verifies that 'basic greet' runs successfully and greets the name argument.
|
||||||
@@ -22,65 +24,44 @@ def test_basic_greet_cli():
|
|||||||
result = runner.invoke(basic_group, ["greet", "Alice"])
|
result = runner.invoke(basic_group, ["greet", "Alice"])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "hello Alice" in result.output
|
assert "hello Alice" in result.output
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# {{{ test_basic_set_flag_format_cli
|
||||||
def test_basic_set_flag_format_cli():
|
def test_basic_set_flag_format_cli():
|
||||||
"""
|
"""
|
||||||
Verifies that 'basic set-flag-format' CLI command writes the pattern to config.toml.
|
Verifies that 'basic set-flag-format' CLI command writes the pattern to config.toml.
|
||||||
"""
|
"""
|
||||||
from ctf.config import load_config
|
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
|
||||||
|
|
||||||
# Save the original config to restore later
|
cfg = Config()
|
||||||
original_config = load_config(str(config_file))
|
assert cfg.data["Competition"]["flag_format"] == r"picoCTF\{.*\}"
|
||||||
|
|
||||||
try:
|
|
||||||
result = runner.invoke(basic_group, ["set-flag-format", "TEST_FLAG{[a-z]+}"])
|
result = runner.invoke(basic_group, ["set-flag-format", "TEST_FLAG{[a-z]+}"])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "Flag format set to" in result.output
|
assert "Flag format set to" in result.output
|
||||||
|
|
||||||
# Verify it was written to config.toml
|
updated_cfg = Config()
|
||||||
updated_config = load_config(str(config_file))
|
assert updated_cfg.data["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
|
||||||
assert updated_config["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
|
# }}}
|
||||||
finally:
|
|
||||||
# Restore original config
|
|
||||||
from ctf.config import write_config
|
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
|
|
||||||
|
# {{{ test_basic_set_flag_format_original_option_cli
|
||||||
def test_basic_set_flag_format_original_option_cli():
|
def test_basic_set_flag_format_original_option_cli():
|
||||||
"""
|
"""
|
||||||
Verifies that 'basic set-flag-format' with --original/-o option prompts for selection and writes it.
|
Verifies that 'basic set-flag-format' with --original/-o option prompts for selection and writes it.
|
||||||
"""
|
"""
|
||||||
from ctf.config import load_config
|
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
|
||||||
|
|
||||||
# Save the original config to restore later
|
|
||||||
original_config = load_config(str(config_file))
|
|
||||||
|
|
||||||
try:
|
|
||||||
# We pass -o SKY-1111-000 and input "2" to choose the second suggested pattern
|
|
||||||
result = runner.invoke(basic_group, ["set-flag-format", "-o", "SKY-1111-000"], input="2\n")
|
result = runner.invoke(basic_group, ["set-flag-format", "-o", "SKY-1111-000"], input="2\n")
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
comp_name = original_config.get("Competition", {}).get("competition", "")
|
|
||||||
if comp_name:
|
|
||||||
assert f"Suggested regex patterns for competition {comp_name}:" in result.output
|
|
||||||
else:
|
|
||||||
assert "Suggested regex patterns:" in result.output
|
|
||||||
assert "Select a pattern index" in result.output
|
assert "Select a pattern index" in result.output
|
||||||
assert "Flag format set to" in result.output
|
assert "Flag format set to" in result.output
|
||||||
|
|
||||||
# Verify it was written to config.toml
|
updated_cfg = Config()
|
||||||
updated_config = load_config(str(config_file))
|
selected_pattern = updated_cfg.data["Competition"]["flag_format"]
|
||||||
selected_pattern = updated_config["Competition"]["flag_format"]
|
|
||||||
assert len(selected_pattern) > 0
|
assert len(selected_pattern) > 0
|
||||||
finally:
|
# }}}
|
||||||
# Restore original config
|
|
||||||
from ctf.config import write_config
|
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
|
|
||||||
|
# {{{ test_basic_set_flag_format_validation_cli
|
||||||
def test_basic_set_flag_format_validation_cli():
|
def test_basic_set_flag_format_validation_cli():
|
||||||
"""
|
"""
|
||||||
Verifies validation rules:
|
Verifies validation rules:
|
||||||
@@ -104,53 +85,36 @@ def test_basic_set_flag_format_validation_cli():
|
|||||||
result3 = runner.invoke(basic_group, ["set-flag-format", "-o", " "])
|
result3 = runner.invoke(basic_group, ["set-flag-format", "-o", " "])
|
||||||
assert result3.exit_code != 0
|
assert result3.exit_code != 0
|
||||||
assert "Original flag cannot be empty or whitespace only." in result3.output
|
assert "Original flag cannot be empty or whitespace only." in result3.output
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# {{{ test_basic_set_competition_cli
|
||||||
def test_basic_set_competition_cli():
|
def test_basic_set_competition_cli():
|
||||||
"""
|
"""
|
||||||
Verifies that 'basic set-competition' updates the competition name in config.toml.
|
Verifies that 'basic set-competition' updates the competition name in config.toml.
|
||||||
"""
|
"""
|
||||||
from ctf.config import load_config
|
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
|
||||||
original_config = load_config(str(config_file))
|
|
||||||
|
|
||||||
try:
|
|
||||||
result = runner.invoke(basic_group, ["set-competition", "CyberCTF2026"])
|
result = runner.invoke(basic_group, ["set-competition", "CyberCTF2026"])
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "Competition name set to: CyberCTF2026" in result.output
|
assert "Competition name set to: CyberCTF2026" in result.output
|
||||||
|
|
||||||
updated_config = load_config(str(config_file))
|
updated_cfg = Config()
|
||||||
assert updated_config["Competition"]["competition"] == "CyberCTF2026"
|
assert updated_cfg.data["Competition"]["competition"] == "CyberCTF2026"
|
||||||
finally:
|
# }}}
|
||||||
from ctf.config import write_config
|
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
|
|
||||||
|
# {{{ test_basic_set_flag_format_warning_cli
|
||||||
def test_basic_set_flag_format_warning_cli():
|
def test_basic_set_flag_format_warning_cli():
|
||||||
"""
|
"""
|
||||||
Verifies that if competition name is set and not present in the example flag,
|
Verifies that if competition name is set and not present in the example flag,
|
||||||
set-flag-format outputs a warning.
|
set-flag-format outputs a warning.
|
||||||
"""
|
"""
|
||||||
from ctf.config import load_config, write_config
|
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
|
||||||
original_config = load_config(str(config_file))
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Set competition name first
|
# Set competition name first
|
||||||
temp_config = load_config(str(config_file))
|
cfg = Config()
|
||||||
if "Competition" not in temp_config:
|
cfg.data["Competition"]["competition"] = "SECURE"
|
||||||
temp_config["Competition"] = {}
|
cfg.save(cfg.data)
|
||||||
temp_config["Competition"]["competition"] = "SECURE"
|
|
||||||
write_config(temp_config, str(config_file))
|
|
||||||
|
|
||||||
# Now run set-flag-format with an example flag that has no "SECURE" substring
|
|
||||||
# We also pass input "1" to satisfy the choice prompt
|
|
||||||
result = runner.invoke(basic_group, ["set-flag-format", "-o", "CTF{easy_flag_123}"], input="1\n")
|
result = runner.invoke(basic_group, ["set-flag-format", "-o", "CTF{easy_flag_123}"], input="1\n")
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "Warning: Current competition name 'SECURE' was not found in the example flag." in result.output
|
assert "Warning: Current competition name 'SECURE' was not found in the example flag." in result.output
|
||||||
finally:
|
# }}}
|
||||||
write_config(original_config, str(config_file))
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
# tests/test_config.py
|
# tests/test_config.py
|
||||||
import toml
|
import toml
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from ctf.config import load_config, write_config
|
from ctf.config import Config
|
||||||
|
|
||||||
TEST_ENV = Path("tests/env")
|
TEST_ENV = Path("tests/env")
|
||||||
|
|
||||||
# {{{ test_load_config_static
|
# {{{ test_load_config_static
|
||||||
def test_load_config_static():
|
def test_load_config_static():
|
||||||
"""Verifies load_config using the persistent test file."""
|
"""Verifies Config loading using a persistent test file."""
|
||||||
config_file = TEST_ENV / "config.toml"
|
config_file = TEST_ENV / "config.toml"
|
||||||
test_data = {
|
test_data = {
|
||||||
"Competition": {"name": "PersistentComp"},
|
"Competition": {"name": "PersistentComp"},
|
||||||
@@ -17,17 +17,19 @@ def test_load_config_static():
|
|||||||
with open(config_file, "w") as f:
|
with open(config_file, "w") as f:
|
||||||
toml.dump(test_data, f)
|
toml.dump(test_data, f)
|
||||||
|
|
||||||
loaded_data = load_config(str(config_file))
|
cfg = Config(config_file)
|
||||||
assert loaded_data["Competition"]["name"] == "PersistentComp"
|
assert cfg.data["Competition"]["name"] == "PersistentComp"
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# {{{ test_write_config_static
|
# {{{ test_write_config_static
|
||||||
def test_write_config_static():
|
def test_write_config_static():
|
||||||
"""Verifies write_config by writing and reloading."""
|
"""Verifies Config writing by saving and reloading."""
|
||||||
config_file = TEST_ENV / "test_write_config.toml"
|
config_file = TEST_ENV / "test_write_config.toml"
|
||||||
test_data = {"TestKey": "TestVal"}
|
test_data = {"TestKey": "TestVal"}
|
||||||
|
|
||||||
write_config(test_data, str(config_file))
|
cfg = Config(config_file)
|
||||||
loaded = load_config(str(config_file))
|
cfg.save(test_data)
|
||||||
assert loaded == test_data
|
|
||||||
|
reloaded = Config(config_file)
|
||||||
|
assert reloaded.data == test_data
|
||||||
# }}}
|
# }}}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import os
|
|||||||
import stat
|
import stat
|
||||||
import sys
|
import sys
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
from ctf.config import load_config
|
|
||||||
from ctf.utils import active_categories
|
from ctf.utils import active_categories
|
||||||
from ctf.forensics import get_metadata
|
from ctf.forensics import get_metadata
|
||||||
from ctf.cli_forensics import forensics_group, metadata
|
from ctf.cli_forensics import forensics_group, metadata
|
||||||
|
|||||||
Reference in New Issue
Block a user