| 1 |
import contextlib |
| 2 |
import json |
| 3 |
from io import StringIO |
| 4 |
|
| 5 |
import pytest |
| 6 |
import responses |
| 7 |
|
| 8 |
from censys.cli import main as cli_main |
| 9 |
from tests.search.v1.test_api import ACCOUNT_JSON |
| 10 |
from tests.utils import V1_URL, CensysTestCase |
| 11 |
|
| 12 |
|
| 13 |
class CensysCliAccountTest(CensysTestCase): |
| 14 |
def test_table(self): |
| 15 |
# Mock |
| 16 |
self.patch_args( |
| 17 |
[ |
| 18 |
"censys", |
| 19 |
"account", |
| 20 |
], |
| 21 |
search_auth=True, |
| 22 |
) |
| 23 |
self.responses.add( |
| 24 |
responses.GET, |
| 25 |
V1_URL + "/account", |
| 26 |
status=200, |
| 27 |
json=ACCOUNT_JSON, |
| 28 |
) |
| 29 |
|
| 30 |
temp_stdout = StringIO() |
| 31 |
# Actual call |
| 32 |
with ( |
| 33 |
contextlib.redirect_stdout(temp_stdout), |
| 34 |
pytest.raises(SystemExit, match="0"), |
| 35 |
): |
| 36 |
cli_main() |
| 37 |
|
| 38 |
cli_response = temp_stdout.getvalue().strip() |
| 39 |
# Assertions |
| 40 |
assert ACCOUNT_JSON["email"] in cli_response |
| 41 |
assert ACCOUNT_JSON["login"] in cli_response |
| 42 |
quota = ACCOUNT_JSON["quota"] |
| 43 |
assert f"{quota['used']} / {quota['allowance']}" in cli_response |
| 44 |
|
| 45 |
def test_json(self): |
| 46 |
# Mock |
| 47 |
self.patch_args( |
| 48 |
[ |
| 49 |
"censys", |
| 50 |
"account", |
| 51 |
"--json", |
| 52 |
], |
| 53 |
search_auth=True, |
| 54 |
) |
| 55 |
self.responses.add( |
| 56 |
responses.GET, |
| 57 |
V1_URL + "/account", |
| 58 |
status=200, |
| 59 |
json=ACCOUNT_JSON, |
| 60 |
) |
| 61 |
|
| 62 |
temp_stdout = StringIO() |
| 63 |
# Actual call |
| 64 |
with ( |
| 65 |
contextlib.redirect_stdout(temp_stdout), |
| 66 |
pytest.raises(SystemExit, match="0"), |
| 67 |
): |
| 68 |
cli_main() |
| 69 |
|
| 70 |
cli_response = temp_stdout.getvalue().strip() |
| 71 |
# Assertions |
| 72 |
assert json.loads(cli_response) == ACCOUNT_JSON |