104 lines
3.7 KiB
Python
104 lines
3.7 KiB
Python
from pathlib import Path
|
|
import toml
|
|
from click.testing import CliRunner
|
|
from ctf.commands import basic_group
|
|
|
|
TEST_ENV = Path("tests/env")
|
|
|
|
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
|
|
|
|
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
|
|
|
|
def test_basic_set_flag_format_cli():
|
|
"""
|
|
Verifies that 'basic set-flag-format' CLI command writes the pattern to config.toml.
|
|
"""
|
|
from ctf.utils import load_config
|
|
|
|
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:
|
|
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
|
|
|
|
# Verify it was written to config.toml
|
|
updated_config = load_config(str(config_file))
|
|
assert updated_config["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
|
|
finally:
|
|
# Restore original config
|
|
from ctf.utils import write_config
|
|
write_config(original_config, str(config_file))
|
|
|
|
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.
|
|
"""
|
|
from ctf.utils import load_config
|
|
|
|
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")
|
|
assert result.exit_code == 0
|
|
assert "Suggested regex patterns:" in result.output
|
|
assert "Select a pattern index" in result.output
|
|
assert "Flag format set to" in result.output
|
|
|
|
# Verify it was written to config.toml
|
|
updated_config = load_config(str(config_file))
|
|
selected_pattern = updated_config["Competition"]["flag_format"]
|
|
assert selected_pattern.startswith("^") and selected_pattern.endswith("$")
|
|
finally:
|
|
# Restore original config
|
|
from ctf.utils import write_config
|
|
write_config(original_config, str(config_file))
|
|
|
|
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
|
|
|