| 1 |
import contextlib |
| 2 |
import json |
| 3 |
import os |
| 4 |
from io import StringIO |
| 5 |
from unittest.mock import mock_open |
| 6 |
|
| 7 |
import pytest |
| 8 |
import responses |
| 9 |
|
| 10 |
from censys.cli import main as cli_main |
| 11 |
from censys.common.exceptions import CensysCLIException, CensysException |
| 12 |
from tests.cli.test_search import WROTE_PREFIX |
| 13 |
from tests.search.v2.test_certs import VIEW_CERT_JSON |
| 14 |
from tests.search.v2.test_hosts import VIEW_HOST_JSON |
| 15 |
from tests.utils import V2_URL, CensysTestCase |
| 16 |
|
| 17 |
|
| 18 |
class CensysCliViewTest(CensysTestCase): |
| 19 |
def test_search_help(self): |
| 20 |
# Mock |
| 21 |
self.patch_args(["censys", "view", "--help"]) |
| 22 |
temp_stdout = StringIO() |
| 23 |
# Actual call |
| 24 |
with contextlib.redirect_stdout(temp_stdout), pytest.raises(SystemExit): |
| 25 |
cli_main() |
| 26 |
# Assertions |
| 27 |
assert temp_stdout.getvalue().strip().startswith("usage: censys view") |
| 28 |
|
| 29 |
def test_no_creds(self): |
| 30 |
# Mock |
| 31 |
self.patch_args(["censys", "view", "test"]) |
| 32 |
self.mocker.patch("builtins.open", new_callable=mock_open, read_data="[DEFAULT]") |
| 33 |
self.mocker.patch.dict("os.environ", {"CENSYS_API_ID": "", "CENSYS_API_SECRET": ""}) |
| 34 |
# Actual call |
| 35 |
with pytest.raises(CensysException, match="No API ID or API secret configured."): |
| 36 |
cli_main() |
| 37 |
|
| 38 |
def test_write_json(self): |
| 39 |
# Mock |
| 40 |
self.patch_args( |
| 41 |
[ |
| 42 |
"censys", |
| 43 |
"view", |
| 44 |
"8.8.8.8", |
| 45 |
"--index-type", |
| 46 |
"hosts", |
| 47 |
"-o", |
| 48 |
"censys-view-8.8.8.8.json", |
| 49 |
], |
| 50 |
search_auth=True, |
| 51 |
) |
| 52 |
self.responses.add( |
| 53 |
responses.GET, |
| 54 |
V2_URL + "/hosts/8.8.8.8", |
| 55 |
status=200, |
| 56 |
json=VIEW_HOST_JSON, |
| 57 |
) |
| 58 |
|
| 59 |
temp_stdout = StringIO() |
| 60 |
# Actual call |
| 61 |
with contextlib.redirect_stdout(temp_stdout): |
| 62 |
cli_main() |
| 63 |
|
| 64 |
cli_response = temp_stdout.getvalue().strip() |
| 65 |
# Assertions |
| 66 |
assert cli_response.startswith(WROTE_PREFIX) |
| 67 |
|
| 68 |
json_path = cli_response.replace(WROTE_PREFIX, "").strip() |
| 69 |
assert json_path.endswith(".json") |
| 70 |
assert "censys-view-8.8.8.8." in json_path |
| 71 |
|
| 72 |
with open(json_path) as json_file: |
| 73 |
json_response = json.load(json_file) |
| 74 |
|
| 75 |
assert VIEW_HOST_JSON["result"] == json_response |
| 76 |
|
| 77 |
# Cleanup |
| 78 |
os.remove(json_path) |
| 79 |
|
| 80 |
def test_write_output_path(self): |
| 81 |
# Mock |
| 82 |
self.patch_args( |
| 83 |
[ |
| 84 |
"censys", |
| 85 |
"view", |
| 86 |
"8.8.8.8", |
| 87 |
"--output", |
| 88 |
"censys-google-dns.json", |
| 89 |
], |
| 90 |
search_auth=True, |
| 91 |
) |
| 92 |
self.responses.add( |
| 93 |
responses.GET, |
| 94 |
V2_URL + "/hosts/8.8.8.8", |
| 95 |
status=200, |
| 96 |
json=VIEW_HOST_JSON, |
| 97 |
) |
| 98 |
|
| 99 |
output_path = "censys-google-dns.json" |
| 100 |
# Actual call |
| 101 |
cli_main() |
| 102 |
# Assertions |
| 103 |
assert os.path.isfile(output_path) |
| 104 |
|
| 105 |
with open(output_path) as json_file: |
| 106 |
json_response = json.load(json_file) |
| 107 |
|
| 108 |
assert VIEW_HOST_JSON["result"] == json_response |
| 109 |
|
| 110 |
# Cleanup |
| 111 |
os.remove(output_path) |
| 112 |
|
| 113 |
def test_write_screen(self): |
| 114 |
# Mock |
| 115 |
self.patch_args( |
| 116 |
[ |
| 117 |
"censys", |
| 118 |
"view", |
| 119 |
"8.8.8.8", |
| 120 |
"--index-type", |
| 121 |
"hosts", |
| 122 |
], |
| 123 |
search_auth=True, |
| 124 |
) |
| 125 |
self.responses.add( |
| 126 |
responses.GET, |
| 127 |
V2_URL + "/hosts/8.8.8.8", |
| 128 |
status=200, |
| 129 |
json=VIEW_HOST_JSON, |
| 130 |
) |
| 131 |
|
| 132 |
temp_stdout = StringIO() |
| 133 |
# Actual call |
| 134 |
with contextlib.redirect_stdout(temp_stdout): |
| 135 |
cli_main() |
| 136 |
|
| 137 |
json_response = json.loads(temp_stdout.getvalue().strip()) |
| 138 |
# Assertions |
| 139 |
assert VIEW_HOST_JSON["result"] == json_response |
| 140 |
|
| 141 |
def test_at_time(self): |
| 142 |
# Mock |
| 143 |
self.patch_args( |
| 144 |
[ |
| 145 |
"censys", |
| 146 |
"view", |
| 147 |
"8.8.8.8", |
| 148 |
"--index-type", |
| 149 |
"hosts", |
| 150 |
"-o", |
| 151 |
"censys-view-8.8.8.8.json", |
| 152 |
"--at-time", |
| 153 |
"2021-05-20", |
| 154 |
], |
| 155 |
search_auth=True, |
| 156 |
) |
| 157 |
self.responses.add( |
| 158 |
responses.GET, |
| 159 |
V2_URL + "/hosts/8.8.8.8?at_time=2021-05-20T00%3A00%3A00.000000Z", |
| 160 |
status=200, |
| 161 |
json=VIEW_HOST_JSON, |
| 162 |
) |
| 163 |
|
| 164 |
temp_stdout = StringIO() |
| 165 |
# Actual call |
| 166 |
with contextlib.redirect_stdout(temp_stdout): |
| 167 |
cli_main() |
| 168 |
|
| 169 |
cli_response = temp_stdout.getvalue().strip() |
| 170 |
# Assertions |
| 171 |
assert cli_response.startswith(WROTE_PREFIX) |
| 172 |
|
| 173 |
json_path = cli_response.replace(WROTE_PREFIX, "").strip() |
| 174 |
assert json_path.endswith(".json") |
| 175 |
assert "censys-view-8.8.8.8." in json_path |
| 176 |
|
| 177 |
with open(json_path) as json_file: |
| 178 |
json_response = json.load(json_file) |
| 179 |
|
| 180 |
assert VIEW_HOST_JSON["result"] == json_response |
| 181 |
|
| 182 |
# Cleanup |
| 183 |
os.remove(json_path) |
| 184 |
|
| 185 |
def test_ip_plus_name(self): |
| 186 |
# Mock |
| 187 |
self.patch_args( |
| 188 |
[ |
| 189 |
"censys", |
| 190 |
"view", |
| 191 |
"1.1.1.1+one.one.one.one", |
| 192 |
"--index-type", |
| 193 |
"hosts", |
| 194 |
"-o", |
| 195 |
"censys-view-1.1.1.1.json", |
| 196 |
], |
| 197 |
search_auth=True, |
| 198 |
) |
| 199 |
self.responses.add( |
| 200 |
responses.GET, |
| 201 |
V2_URL + "/hosts/1.1.1.1+one.one.one.one", |
| 202 |
status=200, |
| 203 |
json=VIEW_HOST_JSON, |
| 204 |
) |
| 205 |
|
| 206 |
# Actual call |
| 207 |
temp_stdout = StringIO() |
| 208 |
with contextlib.redirect_stdout(temp_stdout): |
| 209 |
cli_main() |
| 210 |
|
| 211 |
# Assertions |
| 212 |
cli_response = temp_stdout.getvalue().strip() |
| 213 |
assert cli_response.startswith(WROTE_PREFIX) |
| 214 |
|
| 215 |
json_path = cli_response.replace(WROTE_PREFIX, "").strip() |
| 216 |
assert json_path.endswith(".json") |
| 217 |
assert "censys-view-1.1.1.1." in json_path |
| 218 |
|
| 219 |
with open(json_path) as json_file: |
| 220 |
json_response = json.load(json_file) |
| 221 |
|
| 222 |
assert VIEW_HOST_JSON["result"] == json_response |
| 223 |
|
| 224 |
# Cleanup |
| 225 |
os.remove(json_path) |
| 226 |
|
| 227 |
def test_incorrect_index_type_certs(self): |
| 228 |
# Mock |
| 229 |
self.patch_args( |
| 230 |
[ |
| 231 |
"censys", |
| 232 |
"view", |
| 233 |
"9b00121b4e85d50667ded1a8aa39855771bdb67ceca6f18726b49374b41f0041", |
| 234 |
"--index-type", |
| 235 |
"hosts", |
| 236 |
"-o", |
| 237 |
"censys-view-cert.json", |
| 238 |
], |
| 239 |
search_auth=True, |
| 240 |
) |
| 241 |
self.responses.add( |
| 242 |
responses.GET, |
| 243 |
V2_URL + "/certificates/9b00121b4e85d50667ded1a8aa39855771bdb67ceca6f18726b49374b41f0041", |
| 244 |
status=200, |
| 245 |
json=VIEW_CERT_JSON, |
| 246 |
) |
| 247 |
|
| 248 |
temp_stdout = StringIO() |
| 249 |
# Actual call |
| 250 |
with contextlib.redirect_stdout(temp_stdout): |
| 251 |
cli_main() |
| 252 |
|
| 253 |
# Assertions |
| 254 |
cli_response = temp_stdout.getvalue().strip() |
| 255 |
assert WROTE_PREFIX in cli_response |
| 256 |
|
| 257 |
json_path = cli_response.replace(WROTE_PREFIX, "").strip() |
| 258 |
assert json_path.endswith(".json") |
| 259 |
assert "censys-view-cert." in json_path |
| 260 |
|
| 261 |
with open(json_path) as json_file: |
| 262 |
json_response = json.load(json_file) |
| 263 |
|
| 264 |
assert VIEW_CERT_JSON["result"] == json_response |
| 265 |
|
| 266 |
# Cleanup |
| 267 |
os.remove(json_path) |
| 268 |
|
| 269 |
def test_has_at_time_for_certs(self): |
| 270 |
# Mock |
| 271 |
self.patch_args( |
| 272 |
[ |
| 273 |
"censys", |
| 274 |
"view", |
| 275 |
"9b00121b4e85d50667ded1a8aa39855771bdb67ceca6f18726b49374b41f0041", |
| 276 |
"--index-type", |
| 277 |
"certificates", |
| 278 |
"--at-time", |
| 279 |
"2021-05-20", |
| 280 |
"-o", |
| 281 |
"censys-view-cert.json", |
| 282 |
], |
| 283 |
search_auth=True, |
| 284 |
) |
| 285 |
self.responses.add( |
| 286 |
responses.GET, |
| 287 |
V2_URL + "/certificates/9b00121b4e85d50667ded1a8aa39855771bdb67ceca6f18726b49374b41f0041", |
| 288 |
status=200, |
| 289 |
json=VIEW_CERT_JSON, |
| 290 |
) |
| 291 |
|
| 292 |
temp_stdout = StringIO() |
| 293 |
# Actual call |
| 294 |
with contextlib.redirect_stdout(temp_stdout): |
| 295 |
cli_main() |
| 296 |
|
| 297 |
# Assertions |
| 298 |
cli_response = temp_stdout.getvalue().strip() |
| 299 |
assert WROTE_PREFIX in cli_response |
| 300 |
|
| 301 |
json_path = cli_response.replace(WROTE_PREFIX, "").strip() |
| 302 |
assert json_path.endswith(".json") |
| 303 |
assert "censys-view-cert." in json_path |
| 304 |
|
| 305 |
with open(json_path) as json_file: |
| 306 |
json_response = json.load(json_file) |
| 307 |
|
| 308 |
assert VIEW_CERT_JSON["result"] == json_response |
| 309 |
|
| 310 |
# Cleanup |
| 311 |
os.remove(json_path) |
| 312 |
|
| 313 |
def test_incorrect_ip_address(self): |
| 314 |
# Mock |
| 315 |
self.patch_args( |
| 316 |
[ |
| 317 |
"censys", |
| 318 |
"view", |
| 319 |
"abc:123", |
| 320 |
"--index-type", |
| 321 |
"hosts", |
| 322 |
], |
| 323 |
search_auth=True, |
| 324 |
) |
| 325 |
|
| 326 |
# Actual call |
| 327 |
with pytest.raises(CensysCLIException, match="Invalid IP address: abc:123"): |
| 328 |
cli_main() |
| 329 |
|
| 330 |
def test_open(self): |
| 331 |
# Mock |
| 332 |
self.patch_args(["censys", "view", "8.8.8.8", "--open"], search_auth=True) |
| 333 |
mock_open = self.mocker.patch("censys.cli.commands.view.webbrowser.open") |
| 334 |
# Actual call |
| 335 |
with pytest.raises(SystemExit, match="0"): |
| 336 |
cli_main() |
| 337 |
# Assertions |
| 338 |
mock_open.assert_called_with("https://search.censys.io/hosts/8.8.8.8") |