Debian Codegraph @

tests/cli/test_hnri.py

raw
1 import contextlib
2 from io import StringIO
3
4 import pytest
5 import responses
6
7 from censys.cli import main as cli_main
8 from censys.cli.commands.hnri import CensysHNRI
9 from censys.common.exceptions import CensysCLIException
10 from tests.search.v2.test_hosts import VIEW_HOST_JSON
11 from tests.utils import V2_URL, CensysTestCase
12
13
14 class CensysCliHNRITest(CensysTestCase):
15 IPIFY_URL = "https://api.ipify.org?format=json"
16 IP_ADDRESS = "8.8.8.8"
17
18 def setup_method(self):
19 self.api = CensysHNRI(self.api_id, self.api_secret)
20
21 def test_hnri_medium(self):
22 # Mock
23 self.patch_args(["censys", "hnri"], search_auth=True)
24 mock_ip = self.mocker.patch(
25 "censys.cli.commands.hnri.CensysHNRI.get_current_ip",
26 return_value=self.IP_ADDRESS,
27 )
28 # Setup response
29 response = VIEW_HOST_JSON.copy()
30 response["result"]["services"] = [
31 {"port": 443, "service_name": "HTTPS"},
32 {"port": 53, "service_name": "DNS"},
33 ]
34 self.responses.add(
35 responses.GET,
36 f"{V2_URL}/hosts/{self.IP_ADDRESS}",
37 status=200,
38 json=response,
39 )
40
41 temp_stdout = StringIO()
42 # Actual call
43 with contextlib.redirect_stdout(temp_stdout):
44 cli_main()
45
46 stdout = temp_stdout.getvalue().strip()
47 # Assertions
48 assert mock_ip.return_value in stdout
49 assert "Medium Risks Found" in stdout
50 assert "HTTPS" in stdout
51 assert "443" in stdout
52 assert "DNS" in stdout
53 assert "53" in stdout
54
55 def test_hnri_no_medium(self):
56 self.patch_args(["censys", "hnri"], search_auth=True)
57 # Mock
58 mock_ip = self.mocker.patch(
59 "censys.cli.commands.hnri.CensysHNRI.get_current_ip",
60 return_value=self.IP_ADDRESS,
61 )
62 # Setup response
63 response = VIEW_HOST_JSON.copy()
64 response["result"]["services"] = [{"port": 23, "service_name": "VNC"}]
65 self.responses.add(
66 responses.GET,
67 f"{V2_URL}/hosts/{self.IP_ADDRESS}",
68 status=200,
69 json=response,
70 )
71
72 temp_stdout = StringIO()
73 # Actual call
74 with contextlib.redirect_stdout(temp_stdout):
75 cli_main()
76
77 stdout = temp_stdout.getvalue().strip()
78 # Assertions
79 assert mock_ip.return_value in stdout
80 assert "High Risks Found" in stdout
81 assert "VNC" in stdout
82 assert "23" in stdout
83 assert "You don't have any Medium Risks in your network" in stdout
84
85 def test_hnri_not_found(self):
86 self.patch_args(["censys", "hnri"], search_auth=True)
87 # Mock
88 mock_ip = self.mocker.patch(
89 "censys.cli.commands.hnri.CensysHNRI.get_current_ip",
90 return_value=self.IP_ADDRESS,
91 )
92 # Setup response
93 response = VIEW_HOST_JSON.copy()
94 response["result"]["services"] = []
95 self.responses.add(
96 responses.GET,
97 f"{V2_URL}/hosts/{self.IP_ADDRESS}",
98 status=200,
99 json=response,
100 )
101
102 temp_stdout = StringIO()
103 # Actual Call
104 with contextlib.redirect_stdout(temp_stdout):
105 cli_main()
106
107 stdout = temp_stdout.getvalue().strip()
108 # Assertions
109 assert mock_ip.return_value in stdout
110 assert "No Risks were found on your network" in stdout
111
112 def test_get_current_ip(self):
113 # Setup response
114 self.responses.add(
115 responses.GET,
116 self.IPIFY_URL,
117 status=200,
118 json={"ip": self.IP_ADDRESS},
119 )
120 # Actual call
121 ip_address = self.api.get_current_ip()
122 # Assertions
123 assert ip_address == self.IP_ADDRESS
124
125 def test_no_risks(self):
126 # Actual call/error raising
127 with pytest.raises(CensysCLIException):
128 self.api.risks_to_string([], [])
129
130 def test_open(self):
131 self.patch_args(["censys", "hnri", "--open"])
132 # Mock
133 mock_open = self.mocker.patch("censys.cli.commands.view.webbrowser.open")
134 # Actual call/error raising
135 with pytest.raises(SystemExit, match="0"):
136 cli_main()
137 # Assertions
138 mock_open.assert_called_with("https://search.censys.io/me")