docs/test: add tests and documentation for competition-aware patterns and commands
This commit is contained in:
@@ -101,3 +101,52 @@ def test_basic_set_flag_format_validation_cli():
|
||||
assert result3.exit_code != 0
|
||||
assert "Original flag cannot be empty or whitespace only." in result3.output
|
||||
|
||||
def test_basic_set_competition_cli():
|
||||
"""
|
||||
Verifies that 'basic set-competition' updates the competition name in config.toml.
|
||||
"""
|
||||
from ctf.utils import load_config
|
||||
|
||||
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"])
|
||||
assert result.exit_code == 0
|
||||
assert "Competition name set to: CyberCTF2026" in result.output
|
||||
|
||||
updated_config = load_config(str(config_file))
|
||||
assert updated_config["Competition"]["competition"] == "CyberCTF2026"
|
||||
finally:
|
||||
from ctf.utils import write_config
|
||||
write_config(original_config, str(config_file))
|
||||
|
||||
def test_basic_set_flag_format_warning_cli():
|
||||
"""
|
||||
Verifies that if competition name is set and not present in the example flag,
|
||||
set-flag-format outputs a warning.
|
||||
"""
|
||||
from ctf.utils import load_config, write_config
|
||||
|
||||
runner = CliRunner()
|
||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
||||
original_config = load_config(str(config_file))
|
||||
|
||||
try:
|
||||
# Set competition name first
|
||||
temp_config = load_config(str(config_file))
|
||||
if "Competition" not in temp_config:
|
||||
temp_config["Competition"] = {}
|
||||
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")
|
||||
assert result.exit_code == 0
|
||||
assert "Warning: Current competition name 'SECURE' was not found in the example flag." in result.output
|
||||
finally:
|
||||
write_config(original_config, str(config_file))
|
||||
|
||||
|
||||
|
||||
@@ -98,3 +98,40 @@ def test_suggest_patterns_empty_and_whitespace():
|
||||
|
||||
with pytest.raises(ValueError, match="Input string cannot be empty or whitespace only."):
|
||||
suggest_patterns(" ")
|
||||
|
||||
def test_suggest_patterns_with_competition_name_braced():
|
||||
"""
|
||||
Verifies that when the competition name is matched in a braced flag,
|
||||
additional general wildcard patterns like '^CTF\\{.*\\}$' are generated.
|
||||
"""
|
||||
patterns = suggest_patterns("CTF{easy_flag_123}", comp_name="CTF")
|
||||
|
||||
# Check that competition prefix is kept literal in specific/variable patterns
|
||||
assert "^CTF\\{[a-z]{4}_[a-z]{4}_\\d{3}\\}$" in patterns
|
||||
assert "^CTF\\{[a-z]+_[a-z]+_\\d+\\}$" in patterns
|
||||
|
||||
# Check that braced wildcard options are suggested
|
||||
assert "^CTF\\{.*\\}$" in patterns
|
||||
assert "^CTF\\{[A-Za-z0-9_\\-]+\\}$" in patterns
|
||||
assert "^CTF\\{[a-z0-9_]+\\}$" in patterns
|
||||
|
||||
def test_suggest_patterns_with_competition_name_non_braced():
|
||||
"""
|
||||
Verifies that when the competition name is matched in a non-braced flag,
|
||||
suffix wildcards like '^SKY-.*$' or '^SKY.*$' are suggested.
|
||||
"""
|
||||
patterns = suggest_patterns("SKY-1111-000", comp_name="SKY")
|
||||
|
||||
assert "^SKY-\\d{4}-\\d{3}$" in patterns
|
||||
assert "^SKY-.*$" in patterns
|
||||
|
||||
def test_suggest_patterns_with_competition_name_mismatch():
|
||||
"""
|
||||
Verifies that if the competition name is not in the example flag,
|
||||
it falls back to standard regex suggestion matching.
|
||||
"""
|
||||
patterns_mismatch = suggest_patterns("CTF{easy_flag_123}", comp_name="SKY")
|
||||
patterns_none = suggest_patterns("CTF{easy_flag_123}")
|
||||
|
||||
assert patterns_mismatch == patterns_none
|
||||
|
||||
|
||||
Reference in New Issue
Block a user