Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add CLI tests #500

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion secator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,12 @@ def health(json, debug, strict):
table = get_health_table()
with Live(table, console=console):
for tool in tools:
info = get_version_info(tool.cmd.split(' ')[0], tool.version_flag, tool.install_github_handle, tool.install_cmd)
info = get_version_info(
tool.cmd.split(' ')[0],
tool.version_flag or f'{tool.opt_prefix}version',
tool.install_github_handle,
tool.install_cmd
)
row = fmt_health_table_row(info, 'tools')
table.add_row(*row)
status['tools'][tool.__name__] = info
Expand Down
5 changes: 2 additions & 3 deletions secator/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def parse_version(ver):
return None


def get_version_info(name, version_flag, install_github_handle=None, install_cmd=None, version=None, opt_prefix='--'):
def get_version_info(name, version_flag=None, install_github_handle=None, install_cmd=None, version=None):
"""Get version info for a command.

Args:
Expand All @@ -304,7 +304,6 @@ def get_version_info(name, version_flag, install_github_handle=None, install_cmd
install_github_handle (str): Github handle.
install_cmd (str): Install command.
version (str): Existing version.
opt_prefix (str, default: '--'): Option prefix.

Return:
dict: Version info.
Expand Down Expand Up @@ -353,7 +352,7 @@ def get_version_info(name, version_flag, install_github_handle=None, install_cmd

# Get current version
version_ret = 1
version_flag = None if version_flag == OPT_NOT_SUPPORTED else version_flag or f'{opt_prefix}version'
version_flag = None if version_flag == OPT_NOT_SUPPORTED else version_flag
if version_flag:
version_cmd = f'{name} {version_flag}'
version, version_ret = get_version(version_cmd)
Expand Down
94 changes: 94 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from click.testing import CliRunner

from secator.cli import cli

import unittest


class TestCli(unittest.TestCase):

def setUp(self):
self.runner = CliRunner()

def test_cli_version(self):
result = self.runner.invoke(cli, ['--version'])
assert not result.exception
assert result.exit_code == 0
assert 'Current version' in result.output

def test_task_command(self):
result = self.runner.invoke(cli, ['task'])
assert not result.exception
assert result.exit_code == 0
assert 'Run a task.' in result.output

def test_workflow_command(self):
result = self.runner.invoke(cli, ['workflow'])
assert not result.exception
assert result.exit_code == 0
assert 'Run a workflow.' in result.output

def test_scan_command(self):
result = self.runner.invoke(cli, ['scan'])
assert not result.exception
assert result.exit_code == 0
assert 'Run a scan.' in result.output

# def test_worker_command(self):
# result = self.runner.invoke(cli, ['worker', '--check'])
# assert not result.exception
# assert result.exit_code == 0
# # Add more assertions based on expected output

def test_util_proxy_command(self):
result = self.runner.invoke(cli, ['util', 'proxy', '--timeout', '0.5', '--number', '2'])
assert not result.exception
assert result.exit_code == 0
# Add more assertions based on expected output

def test_util_revshell_command(self):
result = self.runner.invoke(cli, ['util', 'revshell', '--host', '127.0.0.1', '--port', '9001'])
assert not result.exception
assert result.exit_code == 0
# Add more assertions based on expected output

# def test_util_serve_command(self):
# result = self.runner.invoke(cli, ['util', 'serve', '--directory', '.', '--port', '8000'])
# assert not result.exception
# assert result.exit_code == 0
# # Add more assertions based on expected output

def test_config_get_command(self):
result = self.runner.invoke(cli, ['config', 'get'])
assert not result.exception
assert result.exit_code == 0
# Add more assertions based on expected output

def test_config_set_command(self):
result = self.runner.invoke(cli, ['config', 'set', 'key', 'value'])
assert not result.exception
assert result.exit_code == 0
# Add more assertions based on expected output

def test_report_show_command(self):
result = self.runner.invoke(cli, ['report', 'show'])
assert not result.exception
assert result.exit_code == 0
# Add more assertions based on expected output

def test_install_addons_worker_command(self):
result = self.runner.invoke(cli, ['install', 'addons', 'worker'])
assert not result.exception
assert result.exit_code == 0
# Add more assertions based on expected output

def test_health_command(self):
result = self.runner.invoke(cli, ['health'])
assert not result.exception
assert result.exit_code == 0
# Add more assertions based on expected output

# Add more tests for other commands as needed

if __name__ == '__main__':
unittest.main()