Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 13 additions & 15 deletions tests/cli/test_account.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import contextlib
import json
from io import StringIO
from unittest.mock import patch

import pytest
import responses
Expand All @@ -13,15 +12,13 @@


class CensysCliAccountTest(CensysTestCase):
@patch(
"argparse._sys.argv",
[

def test_table(self):
# Mock
self.patch_args([
"censys",
"account",
]
+ CensysTestCase.cli_args,
)
def test_table(self):
], search_auth=True)
self.responses.add(
responses.GET,
V1_URL + "/account",
Expand All @@ -30,27 +27,26 @@ def test_table(self):
)

temp_stdout = StringIO()
# Actual call
with contextlib.redirect_stdout(temp_stdout), pytest.raises(
SystemExit, match="0"
):
cli_main()

cli_response = temp_stdout.getvalue().strip()
# Assertions
assert ACCOUNT_JSON["email"] in cli_response
assert ACCOUNT_JSON["login"] in cli_response
quota = ACCOUNT_JSON["quota"]
assert f"{quota['used']} / {quota['allowance']}" in cli_response

@patch(
"argparse._sys.argv",
[
def test_json(self):
# Mock
self.patch_args([
"censys",
"account",
"--json",
Comment thread
thehappydinoa marked this conversation as resolved.
Outdated
]
+ CensysTestCase.cli_args,
)
def test_json(self):
], search_auth=True)
self.responses.add(
responses.GET,
V1_URL + "/account",
Expand All @@ -59,10 +55,12 @@ def test_json(self):
)

temp_stdout = StringIO()
# Actual call
with contextlib.redirect_stdout(temp_stdout), pytest.raises(
SystemExit, match="0"
):
cli_main()

cli_response = temp_stdout.getvalue().strip()
# Assertions
assert ACCOUNT_JSON == json.loads(cli_response)
17 changes: 12 additions & 5 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,36 @@


class CensysCliTest(CensysTestCase):
@patch("argparse._sys.argv", ["censys"])
def test_default_help(self):
# Mock
self.patch_args(["censys"])
temp_stdout = StringIO()
# Actual call
with contextlib.redirect_stdout(temp_stdout), pytest.raises(SystemExit):
cli_main()

# Assertions
assert temp_stdout.getvalue().strip().startswith("usage: censys")

@patch("argparse._sys.argv", ["censys", "--help"])
def test_help(self):
# Mock
self.patch_args(["censys", "--help"])
temp_stdout = StringIO()
# Actual call
with contextlib.redirect_stdout(temp_stdout), pytest.raises(SystemExit):
cli_main()

stdout = temp_stdout.getvalue().strip()
# Assertions
assert stdout.startswith("usage: censys")
assert "{" + ",".join(sorted(cli_commands)) + "}" in stdout
assert "-v, --version" in stdout

@patch("argparse._sys.argv", ["censys", "-v"])
def test_version(self):
# Mock
self.patch_args(["censys", "-v"])
temp_stdout = StringIO()
# Actual call
with contextlib.redirect_stdout(temp_stdout), pytest.raises(SystemExit):
cli_main()

# Assertion
assert __version__ in temp_stdout.getvalue()
153 changes: 73 additions & 80 deletions tests/cli/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest.mock import MagicMock, Mock, mock_open, patch

import pytest
import responses

Expand All @@ -15,11 +13,7 @@
get_config,
)

os = MagicMock()
os.path = MagicMock()
os.path.isfile = Mock(return_value=False)
os.path.exists = Mock(return_value=False)
os.makedirs = Mock()
TEST_CONFIG_PATH = CONFIG_PATH + ".test"


def prompt_side_effect(arg, **kwargs):
Expand All @@ -40,32 +34,30 @@ def confirm_side_effect(arg, **kwargs):
return False


Prompt = MagicMock()
Prompt.ask = Mock(side_effect=prompt_side_effect)

Confirm = MagicMock()
Confirm.ask = Mock(side_effect=confirm_side_effect)

test_config_path = CONFIG_PATH + ".test"


@patch("censys.common.config.CONFIG_PATH", test_config_path)
@patch(
"builtins.open",
new_callable=mock_open,
read_data="[DEFAULT]\napi_id =\napi_secret =\nasm_api_key =",
)
@patch("rich.prompt.Prompt.ask", Prompt.ask)
@patch("rich.prompt.Confirm.ask", Confirm.ask)
class CensysConfigCliTest(CensysTestCase):
@patch(
"argparse._sys.argv",
[
"censys",
"config",
],
)
def test_search_config(self, mock_file: MagicMock):
def setUp(self):
super().setUp()
self.mocker.patch("censys.common.config.CONFIG_PATH", TEST_CONFIG_PATH)
self.mock_open = self.mocker.patch(
"builtins.open",
new_callable=self.mocker.mock_open,
read_data="[DEFAULT]\napi_id =\napi_secret =\nasm_api_key =",
)
mock_prompt = self.mocker.patch(
"rich.prompt.Prompt.ask", side_effect=prompt_side_effect
)
mock_confirm = self.mocker.patch(
"rich.prompt.Confirm.ask", side_effect=confirm_side_effect
)

def test_search_config(self):
# Mock
self.patch_args(
[
"censys",
"config",
]
)
self.responses.add(
responses.GET,
V1_URL + "/account",
Expand All @@ -77,38 +69,37 @@ def test_search_config(self, mock_file: MagicMock):
cli_main()

# Assert that the config file was read from the right place
mock_file.assert_called_with(test_config_path, "w")

@patch(
"argparse._sys.argv",
[
"censys",
"config",
],
)
def test_search_config_failed(self, mock_file: MagicMock):
self.mock_open.assert_called_with(TEST_CONFIG_PATH, "w")

def test_search_config_failed(self):
# Mock
self.patch_args(
[
"censys",
"config",
]
)
self.responses.add(
responses.GET,
V1_URL + "/account",
status=401,
json={"error": "Unauthorized"},
)

# Actual call/error raising
with pytest.raises(SystemExit, match="1"):
cli_main()

@patch(
"argparse._sys.argv",
[
"censys",
"config",
],
)
@patch("censys.common.config.os.path.isdir", Mock(return_value=False))
@patch("censys.common.config.os.makedirs")
def test_search_config_makedirs(
self, mock_makedirs: MagicMock, mock_file: MagicMock
):
def test_search_config_makedirs(self):
self.patch_args(
[
"censys",
"config",
]
)
self.mocker.patch("censys.common.config.os.path.isdir", return_value=False)
mock_makedirs = self.mocker.patch("censys.common.config.os.makedirs")

self.responses.add(
responses.GET,
V1_URL + "/account",
Expand All @@ -121,25 +112,28 @@ def test_search_config_makedirs(

mock_makedirs.assert_called_with(CENSYS_PATH)

@patch("censys.common.config.os.path.isfile", os.path.isfile)
def test_config_default(self, mock_file: MagicMock):
os.path.isfile.return_value = True
def test_config_default(self):
mock_isfile = self.mocker.patch(
"censys.common.config.os.path.isfile", return_value=True
)
config = get_config()
os.path.isfile.return_value = False
os.path.isfile.assert_called_with(test_config_path)
mock_file.assert_called_once()
mock_isfile.return_value = False
mock_isfile.assert_called_with(TEST_CONFIG_PATH)
self.mock_open.assert_called_once()
for key, value in default_config.items():
assert value == config.get(DEFAULT, key)

@patch(
"argparse._sys.argv",
[
"censys",
"config",
],
)
@patch.dict("censys.common.config.os.environ", {"CENSYS_CONFIG_PATH": "censys.cfg"})
def test_search_config_custom_config(self, mock_file: MagicMock):
def test_search_config_custom_config(self):
self.patch_args(
[
"censys",
"config",
]
)
self.mocker.patch.dict(
"censys.common.config.os.environ", {"CENSYS_CONFIG_PATH": "censys.cfg"}
)

self.responses.add(
responses.GET,
V1_URL + "/account",
Expand All @@ -151,17 +145,16 @@ def test_search_config_custom_config(self, mock_file: MagicMock):
cli_main()

# Assert that the config file was read from the right place
mock_file.assert_called_with("censys.cfg", "w")

@patch(
"argparse._sys.argv",
[
"censys",
"config",
],
)
@patch("censys.common.config.os.access", Mock(return_value=False))
def test_search_config_perm_error(self, mock_file: MagicMock):
self.mock_open.assert_called_with("censys.cfg", "w")

def test_search_config_perm_error(self):
self.patch_args(
[
"censys",
"config",
]
)
self.mocker.patch("censys.common.config.os.access", return_value=False)
self.responses.add(
responses.GET,
V1_URL + "/account",
Expand Down
Loading