Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

tests: update unit tests

Brendan Colese51255a54591
diff --git a/sf.py b/sf.py
index cbf2cd85..7a3fe588 100755
--- a/sf.py
+++ b/sf.py
@@ -54,15 +54,10 @@ dbh = None
def main():
- if len(sys.argv) == 0:
- print("SpiderFoot requires -l <ip>:<port> to start the web server.")
+ if len(sys.argv) <= 1:
+ print("SpiderFoot requires -l <ip>:<port> to start the web server. Try --help for guidance.")
sys.exit(-1)
- if len(sys.argv) > 1:
- if not sys.argv[1].startswith("-"):
- print("SpiderFoot requires -l <ip>:<port> to start the web server.")
- sys.exit(-1)
-
# web server config
sfWebUiConfig = {
'host': '127.0.0.1',
diff --git a/test/unit/test_sf.py b/test/unit/test_sf.py
index c1e6fb57..7a11ba9d 100644
--- a/test/unit/test_sf.py
+++ b/test/unit/test_sf.py
@@ -40,6 +40,12 @@ class TestSf(unittest.TestCase):
out, err = proc.communicate()
return out, err, proc.returncode
+ def test_no_args_should_print_arg_l_required(self):
+ out, err, code = self.execute([sys.executable, "sf.py"])
+ self.assertIn(b"SpiderFoot requires -l <ip>:<port> to start the web server. Try --help for guidance.", out)
+ self.assertEqual(b"", err)
+ self.assertEqual(255, code)
+
def test_help_arg_should_print_help_and_exit(self):
out, err, code = self.execute([sys.executable, "sf.py", "-h"])
self.assertIn(b"show this help message and exit", out)
@@ -63,6 +69,18 @@ class TestSf(unittest.TestCase):
self.assertIn(bytes(f"Starting web server at {listen}", 'utf-8'), err)
self.assertEqual(0, code)
+ def test_debug_arg_should_enable_and_print_debug_output(self):
+ out, err, code = self.execute([sys.executable, "sf.py", "-d", "-m", "example module", "-s", "spiderfoot.net"])
+ self.assertIn(b"[DEBUG]", err)
+ self.assertIn(b"modules.sfp__stor_db : Storing an event: ROOT", err)
+ self.assertEqual(0, code)
+
+ def test_run_scan_invalid_target_should_exit(self):
+ invalid_target = '.'
+ out, err, code = self.execute([sys.executable, "sf.py", "-s", invalid_target])
+ self.assertIn(bytes(f"Could not determine target type. Invalid target: {invalid_target}", 'utf-8'), err)
+ self.assertEqual(255, code)
+
def test_run_scan_with_modules_no_target_should_exit(self):
out, err, code = self.execute([sys.executable, "sf.py", "-m", ",".join(self.default_modules)])
self.assertIn(b"You must specify a target when running in scan mode", err)
@@ -84,8 +102,9 @@ class TestSf(unittest.TestCase):
self.assertIn(b"Based on your criteria, no modules were enabled", err)
self.assertEqual(255, code)
- def test_run_scan_with_modules_should_run_scan_and_exit(self):
- out, err, code = self.execute([sys.executable, "sf.py", "-m", ",".join(self.default_modules), "-s", "spiderfoot.net"])
+ def test_run_scan_should_print_scan_result_and_exit(self):
+ target = "spiderfoot.net"
+ out, err, code = self.execute([sys.executable, "sf.py", "-m", ",".join(self.default_modules), "-s", target, "-o", "csv"])
self.assertIn(b"Scan completed with status FINISHED", err)
self.assertEqual(0, code)
@@ -93,6 +112,15 @@ class TestSf(unittest.TestCase):
with self.subTest(module=module):
self.assertIn(module.encode(), err)
+ expected_output = [
+ "Source,Type,Data",
+ "SpiderFoot UI,Internet Name,spiderfoot.net,spiderfoot.net\n",
+ "SpiderFoot UI,Domain Name,spiderfoot.net,spiderfoot.net\n",
+ "sfp_countryname,Country Name,spiderfoot.net,United States\n",
+ ]
+ for output in expected_output:
+ self.assertIn(bytes(output, 'utf-8'), out)
+
if __name__ == '__main__':
unittest.main()
diff --git a/test/unit/test_spiderfootplugin.py b/test/unit/test_spiderfootplugin.py
index 015c4443..130267c4 100644
--- a/test/unit/test_spiderfootplugin.py
+++ b/test/unit/test_spiderfootplugin.py
@@ -13,7 +13,6 @@ class TestSpiderFootPlugin(unittest.TestCase):
'_debug': False, # Debug
'__logging': True, # Logging in general
'__outputfilter': None, # Event types to filter from modules' output
- '_fatalerrors': False,
'_useragent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0', # User-Agent to use for HTTP requests
'_dnsserver': '', # Override the default resolver
'_fetchtimeout': 5, # number of seconds before giving up on a fetch
@@ -30,7 +29,6 @@ class TestSpiderFootPlugin(unittest.TestCase):
'_socks5pwd': '',
'_socks6dns': True,
'_torctlport': 9051,
- '__logstdout': False
}
def test_init(self):
@@ -209,7 +207,7 @@ class TestSpiderFootPlugin(unittest.TestCase):
temp_storage = sfp.tempStorage()
self.assertIsInstance(temp_storage, dict)
- def test_notifyListeners(self):
+ def test_notifyListeners_should_notify_listener_modules(self):
"""
Test notifyListeners(self, sfEvent)
@@ -233,7 +231,7 @@ class TestSpiderFootPlugin(unittest.TestCase):
self.assertEqual('TBD', 'TBD')
- def test_notifyListeners_invalid_event_should_raise_TypeError(self):
+ def test_notifyListeners_argument_sfEvent_invalid_event_should_raise_TypeError(self):
"""
Test notifyListeners(self, sfEvent)
"""
@@ -243,14 +241,11 @@ class TestSpiderFootPlugin(unittest.TestCase):
for invalid_type in invalid_types:
with self.subTest(invalid_type=invalid_type):
with self.assertRaises(TypeError):
- sfp.notifyListeners(None)
+ sfp.notifyListeners(invalid_type)
def test_checkForStop(self):
"""
Test checkForStop(self)
-
- Todo:
- include other statuses for completeness
"""
sfp = SpiderFootPlugin()
@@ -262,7 +257,13 @@ class TestSpiderFootPlugin(unittest.TestCase):
sfp.__scanId__ = 'example scan id'
# pseudo-parameterized test
- for status, expectedReturnValue in [("RUNNING", False), ("ABORT-REQUESTED", True)]:
+ scan_statuses = [
+ (None, False),
+ ("anything", False),
+ ("RUNNING", False),
+ ("ABORT-REQUESTED", True)
+ ]
+ for status, expectedReturnValue in scan_statuses:
returnValue = sfp.checkForStop()
self.assertEqual(returnValue, expectedReturnValue, status)