121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
# tests/test_commands.py
|
|
from pathlib import Path
|
|
from click.testing import CliRunner
|
|
from ctf.commands import basic_group
|
|
from ctf.config import Config
|
|
|
|
# {{{ test_basic_test_cli
|
|
def test_basic_test_cli():
|
|
"""
|
|
Verifies that 'basic test' runs successfully and outputs 'hello from test'.
|
|
"""
|
|
runner = CliRunner()
|
|
result = runner.invoke(basic_group, ["test"])
|
|
assert result.exit_code == 0
|
|
assert "hello from test" in result.output
|
|
# }}}
|
|
|
|
# {{{ test_basic_greet_cli
|
|
def test_basic_greet_cli():
|
|
"""
|
|
Verifies that 'basic greet' runs successfully and greets the name argument.
|
|
"""
|
|
runner = CliRunner()
|
|
result = runner.invoke(basic_group, ["greet", "Alice"])
|
|
assert result.exit_code == 0
|
|
assert "hello Alice" in result.output
|
|
# }}}
|
|
|
|
# {{{ 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.
|
|
"""
|
|
runner = CliRunner()
|
|
|
|
cfg = Config()
|
|
assert cfg.data["Competition"]["flag_format"] == r"picoCTF\{.*\}"
|
|
|
|
result = runner.invoke(basic_group, ["set-flag-format", "TEST_FLAG{[a-z]+}"])
|
|
assert result.exit_code == 0
|
|
assert "Flag format set to" in result.output
|
|
|
|
updated_cfg = Config()
|
|
assert updated_cfg.data["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
|
|
# }}}
|
|
|
|
# {{{ 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.
|
|
"""
|
|
runner = CliRunner()
|
|
|
|
result = runner.invoke(basic_group, ["set-flag-format", "-o", "SKY-1111-000"], input="2\n")
|
|
assert result.exit_code == 0
|
|
assert "Select a pattern index" in result.output
|
|
assert "Flag format set to" in result.output
|
|
|
|
updated_cfg = Config()
|
|
selected_pattern = updated_cfg.data["Competition"]["flag_format"]
|
|
assert len(selected_pattern) > 0
|
|
# }}}
|
|
|
|
# {{{ test_basic_set_flag_format_validation_cli
|
|
def test_basic_set_flag_format_validation_cli():
|
|
"""
|
|
Verifies validation rules:
|
|
- Specifying both PATTERN and --original should fail.
|
|
- Specifying neither PATTERN nor --original should fail.
|
|
- Specifying empty/whitespace-only original flag should fail.
|
|
"""
|
|
runner = CliRunner()
|
|
|
|
# Both specified
|
|
result1 = runner.invoke(basic_group, ["set-flag-format", "PAT", "-o", "SKY-1111-000"])
|
|
assert result1.exit_code != 0
|
|
assert "Cannot specify both PATTERN and --original/-o option." in result1.output
|
|
|
|
# Neither specified
|
|
result2 = runner.invoke(basic_group, ["set-flag-format"])
|
|
assert result2.exit_code != 0
|
|
assert "Either PATTERN positional argument or --original/-o option must be specified." in result2.output
|
|
|
|
# Empty original
|
|
result3 = runner.invoke(basic_group, ["set-flag-format", "-o", " "])
|
|
assert result3.exit_code != 0
|
|
assert "Original flag cannot be empty or whitespace only." in result3.output
|
|
# }}}
|
|
|
|
# {{{ test_basic_set_competition_cli
|
|
def test_basic_set_competition_cli():
|
|
"""
|
|
Verifies that 'basic set-competition' updates the competition name in config.toml.
|
|
"""
|
|
runner = CliRunner()
|
|
result = runner.invoke(basic_group, ["set-competition", "CyberCTF2026"])
|
|
assert result.exit_code == 0
|
|
assert "Competition name set to: CyberCTF2026" in result.output
|
|
|
|
updated_cfg = Config()
|
|
assert updated_cfg.data["Competition"]["competition"] == "CyberCTF2026"
|
|
# }}}
|
|
|
|
# {{{ 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,
|
|
set-flag-format outputs a warning.
|
|
"""
|
|
runner = CliRunner()
|
|
|
|
# Set competition name first
|
|
cfg = Config()
|
|
cfg.data["Competition"]["competition"] = "SECURE"
|
|
cfg.save(cfg.data)
|
|
|
|
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
|
|
# }}}
|