def test_field_max(self): with self.assertRaises(CensysCLIException) as exit_event: cli_main() self.assertEqual( str(exit_event.exception), "Too many fields specified. The maximum number of fields is 20.", )
def test_no_creds(self, mock_file): with self.assertRaises(CensysException) as exit_event: cli_main() mock_file.assert_called_with(config_path, "w") self.assertEqual(str(exit_event.exception), "No API ID or API secret configured.")
def test_search_help(self): temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): with self.assertRaises(SystemExit) as exit_event: cli_main() self.assertEqual(exit_event.exception.code, 0) self.assertTrue( temp_stdout.getvalue().strip().startswith("usage: censys search"))
def test_hnri_not_found(self, view): temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() view.assert_called_with("8.8.8.8") stdout = temp_stdout.getvalue().strip() self.assertIn("No Risks were found on your network", stdout)
def test_hnri_high(self): # Using towel.blinkenlights.nl/94.142.241.111 temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() stdout = temp_stdout.getvalue().strip() self.assertIn("High Risks Found:", stdout) self.assertIn("telnet on 23", stdout)
def test_hnri_medium(self): temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() stdout = temp_stdout.getvalue().strip() self.assertIn("Medium Risks Found:", stdout) self.assertIn("https on 443", stdout) self.assertIn("dns on 53", stdout)
def test_write_screen(self): temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() json_response = json.loads(temp_stdout.getvalue().strip()) self.assertGreaterEqual(len(json_response), 1) self.assertIn("443.https.get.headers.server", json_response[0].keys())
def test_hnri_no_medium(self): temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() stdout = temp_stdout.getvalue().strip() self.assertIn("High Risks Found:", stdout) self.assertIn("telnet on 23", stdout) self.assertIn("You don't have any Medium Risks in your network", stdout)
def test_write_output_path(self): output_path = "censys-certs.json" cli_main() self.assertTrue(os.path.isfile(output_path)) with open(output_path) as json_file: json_response = json.load(json_file) self.assertGreaterEqual(len(json_response), 1) self.assertIn("parsed.issuer.country", json_response[0].keys()) # Cleanup os.remove(output_path)
def test_overwrite(self): expected_fields = { "domain", "ports", "protocols", "443.https.tls.version", "443.https.tls.cipher_suite.name", "443.https.get.title", "443.https.get.headers.server", } temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() json_response = json.loads(temp_stdout.getvalue().strip()) self.assertGreaterEqual(len(json_response), 1) self.assertSetEqual(expected_fields, set(json_response[0].keys()))
def test_write_json(self): temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() PREFIX = "Wrote results to file" cli_response = temp_stdout.getvalue().strip() self.assertTrue(cli_response.startswith(PREFIX)) json_path = cli_response.replace(PREFIX, "").strip() self.assertTrue(json_path.endswith(".json")) self.assertTrue(json_path.startswith("censys-query-output.")) self.assertTrue(os.path.isfile(json_path)) with open(json_path) as json_file: json_response = json.load(json_file) self.assertGreaterEqual(len(json_response), 1) self.assertIn("parsed.issuer.country", json_response[0].keys()) # Cleanup os.remove(json_path)
def test_write_csv(self): temp_stdout = StringIO() with contextlib.redirect_stdout(temp_stdout): cli_main() PREFIX = "Wrote results to file" cli_response = temp_stdout.getvalue().strip() self.assertTrue(cli_response.startswith(PREFIX)) csv_path = cli_response.replace(PREFIX, "").strip() self.assertTrue(csv_path.endswith(".csv")) self.assertTrue(csv_path.startswith("censys-query-output.")) self.assertTrue(os.path.isfile(csv_path)) with open(csv_path) as csv_file: csv_reader = csv.reader(csv_file) header = next(csv_reader) self.assertIn("protocols", header) # Cleanup os.remove(csv_path)