| 1 |
import contextlib |
| 2 |
from io import StringIO |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from censys.cli import main as cli_main |
| 7 |
from censys.cli.commands import __all__ as cli_commands |
| 8 |
from censys.common import __version__ |
| 9 |
from tests.utils import CensysTestCase |
| 10 |
|
| 11 |
|
| 12 |
class CensysCliTest(CensysTestCase): |
| 13 |
def test_default_help(self): |
| 14 |
# Mock |
| 15 |
self.patch_args(["censys"]) |
| 16 |
temp_stdout = StringIO() |
| 17 |
# Actual call |
| 18 |
with contextlib.redirect_stdout(temp_stdout), pytest.raises(SystemExit): |
| 19 |
cli_main() |
| 20 |
# Assertions |
| 21 |
assert temp_stdout.getvalue().strip().startswith("usage: censys") |
| 22 |
|
| 23 |
def test_help(self): |
| 24 |
# Mock |
| 25 |
self.patch_args(["censys", "--help"]) |
| 26 |
temp_stdout = StringIO() |
| 27 |
# Actual call |
| 28 |
with contextlib.redirect_stdout(temp_stdout), pytest.raises(SystemExit): |
| 29 |
cli_main() |
| 30 |
|
| 31 |
stdout = temp_stdout.getvalue().strip() |
| 32 |
# Assertions |
| 33 |
assert stdout.startswith("usage: censys") |
| 34 |
assert "{" + ",".join(sorted(cli_commands)) + "}" in stdout |
| 35 |
assert "-v, --version" in stdout |
| 36 |
|
| 37 |
def test_version(self): |
| 38 |
# Mock |
| 39 |
self.patch_args(["censys", "-v"]) |
| 40 |
temp_stdout = StringIO() |
| 41 |
# Actual call |
| 42 |
with contextlib.redirect_stdout(temp_stdout), pytest.raises(SystemExit): |
| 43 |
cli_main() |
| 44 |
# Assertion |
| 45 |
assert __version__ in temp_stdout.getvalue() |