diff --git a/sfscan.py b/sfscan.py index 651db6ca32..5b51906e9b 100644 --- a/sfscan.py +++ b/sfscan.py @@ -106,9 +106,6 @@ def __init__(self, scanName, scanId, targetValue, targetType, moduleList, global self.__sf.dbh = self.__dbh # Create a unique ID for this scan in the back-end DB. - if not isinstance(scanId, str): - raise TypeError(f"scanId is {type(scanId)}; expected str()") - if scanId: self.__scanId = scanId else: diff --git a/test/unit/modules/test_sfp_template.py b/test/unit/modules/test_sfp_template.py index bb1348d547..caeef97e44 100644 --- a/test/unit/modules/test_sfp_template.py +++ b/test/unit/modules/test_sfp_template.py @@ -3,7 +3,6 @@ from modules.sfp_template import sfp_template from sflib import SpiderFoot -from spiderfoot import SpiderFootEvent, SpiderFootTarget class TestModuletemplate(unittest.TestCase): @@ -53,28 +52,3 @@ def test_watchedEvents_should_return_list(self): def test_producedEvents_should_return_list(self): module = sfp_template() self.assertIsInstance(module.producedEvents(), list) - - @unittest.skip("todo") - def test_handleEvent(self): - """ - Test handleEvent(self, event) - """ - sf = SpiderFoot(self.default_options) - - module = sfp_template() - module.setup(sf, dict()) - - target_value = 'example target value' - target_type = 'IP_ADDRESS' - target = SpiderFootTarget(target_value, target_type) - module.setTarget(target) - - event_type = 'ROOT' - event_data = 'example data' - event_module = '' - source_event = '' - evt = SpiderFootEvent(event_type, event_data, event_module, source_event) - - result = module.handleEvent(evt) - - self.assertIsNone(result) diff --git a/test/unit/test_spiderfootevent.py b/test/unit/test_spiderfootevent.py index 24cbe23b6e..229c8ea481 100644 --- a/test/unit/test_spiderfootevent.py +++ b/test/unit/test_spiderfootevent.py @@ -283,6 +283,39 @@ def test_risk_attribute_setter_invalid_type_should_raise_TypeError(self): with self.assertRaises(TypeError): evt.risk = invalid_type + def test_actualSource_attribute_should_return_actual_source_as_string(self): + event_type = 'ROOT' + event_data = 'example event data' + module = '' + source_event = '' + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + actual_source = 'example actual source' + evt.actualSource = actual_source + + self.assertEqual(actual_source, evt.actualSource) + + def test_sourceEventHash_attribute_should_return_source_event_hash_as_string(self): + event_type = 'ROOT' + event_data = 'example event data' + module = '' + source_event = '' + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + self.assertEqual("ROOT", evt.sourceEventHash) + + def test_moduleDataSource_attribute_should_return_module_data_source_as_string(self): + event_type = 'ROOT' + event_data = 'example event data' + module = '' + source_event = '' + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + module_data_source = 'example module data source' + evt.moduleDataSource = module_data_source + + self.assertEqual(module_data_source, evt.moduleDataSource) + def test_asdict_root_event_should_return_event_as_a_dict(self): """ Test asDict(self) diff --git a/test/unit/test_spiderfootplugin.py b/test/unit/test_spiderfootplugin.py index addc0b55e9..a968332973 100644 --- a/test/unit/test_spiderfootplugin.py +++ b/test/unit/test_spiderfootplugin.py @@ -210,19 +210,104 @@ def test_tempStorage_should_return_a_dict(self): def test_notifyListeners_should_notify_listener_modules(self): """ Test notifyListeners(self, sfEvent) + """ + sfp = SpiderFootPlugin() + sfdb = SpiderFootDb(self.default_options, False) + sfp.setDbh(sfdb) + + event_type = 'ROOT' + event_data = 'test data' + module = 'test module' + source_event = None + evt = SpiderFootEvent(event_type, event_data, module, source_event) + sfp.notifyListeners(evt) + + self.assertEqual('TBD', 'TBD') + + def test_notifyListeners_output_filter_matched_should_notify_listener_modules(self): + """ + Test notifyListeners(self, sfEvent) + """ + sfp = SpiderFootPlugin() + sfdb = SpiderFootDb(self.default_options, False) + sfp.setDbh(sfdb) + + target = SpiderFootTarget("spiderfoot.net", "INTERNET_NAME") + sfp.setTarget(target) + + event_type = 'ROOT' + event_data = 'test data' + module = 'test module' + source_event = None + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + event_type = 'test event type' + event_data = 'test data' + module = 'test module' + source_event = evt + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + sfp.__outputFilter__ = event_type + + sfp.notifyListeners(evt) + + self.assertEqual('TBD', 'TBD') - Todo: - Test with source event + def test_notifyListeners_output_filter_unmatched_should_not_notify_listener_modules(self): + """ + Test notifyListeners(self, sfEvent) """ sfp = SpiderFootPlugin() sfdb = SpiderFootDb(self.default_options, False) sfp.setDbh(sfdb) + target = SpiderFootTarget("spiderfoot.net", "INTERNET_NAME") + sfp.setTarget(target) + event_type = 'ROOT' event_data = 'test data' module = 'test module' source_event = None evt = SpiderFootEvent(event_type, event_data, module, source_event) + + event_type = 'test event type' + event_data = 'test data' + module = 'test module' + source_event = evt + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + sfp.__outputFilter__ = "example unmatched event type" + + sfp.notifyListeners(evt) + + self.assertEqual('TBD', 'TBD') + + def test_notifyListeners_event_type_and_data_same_as_source_event_source_event_should_story_only(self): + """ + Test notifyListeners(self, sfEvent) + """ + sfp = SpiderFootPlugin() + sfdb = SpiderFootDb(self.default_options, False) + sfp.setDbh(sfdb) + + event_type = 'ROOT' + event_data = 'test data' + module = 'test module' + source_event = None + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + event_type = 'test event type' + event_data = 'test data' + module = 'test module' + source_event = evt + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + source_event = evt + evt = SpiderFootEvent(event_type, event_data, module, source_event) + + source_event = evt + evt = SpiderFootEvent(event_type, event_data, module, source_event) + sfp.notifyListeners(evt) self.assertEqual('TBD', 'TBD') diff --git a/test/unit/test_spiderfootscanner.py b/test/unit/test_spiderfootscanner.py index bbc45988ba..fabd555dbe 100644 --- a/test/unit/test_spiderfootscanner.py +++ b/test/unit/test_spiderfootscanner.py @@ -166,7 +166,7 @@ def test_init_argument_moduleList_as_empty_list_should_raise_ValueError(self): module_list = list() with self.assertRaises(ValueError): - SpiderFootScanner("example scan name", scan_id, "spiderfoot.net", "IP_ADDRESS", module_list, dict(), start=False) + SpiderFootScanner("example scan name", scan_id, "spiderfoot.net", "IP_ADDRESS", module_list, self.default_options, start=False) def test_init_argument_globalOpts_of_invalid_type_should_raise_TypeError(self): """ @@ -215,3 +215,33 @@ def test_attribute_status_should_return_status_as_a_string(self): status = sfscan.status self.assertIsInstance(status, str) + + def test__setStatus_argument_status_of_invalid_type_should_raise_TypeError(self): + """ + Test __setStatus(self, status, started=None, ended=None) + """ + opts = self.default_options + opts['__modules__'] = dict() + scan_id = str(uuid.uuid4()) + module_list = ['sfp__stor_db'] + + sfscan = SpiderFootScanner("example scan name", scan_id, "spiderfoot.net", "IP_ADDRESS", module_list, opts, start=False) + + invalid_types = [None, list(), dict(), int()] + for invalid_type in invalid_types: + with self.subTest(invalid_type=invalid_type): + with self.assertRaises(TypeError): + sfscan._SpiderFootScanner__setStatus(invalid_type) + + def test__setStatus_argument_status_with_blank_value_should_raise_ValueError(self): + """ + Test __setStatus(self, status, started=None, ended=None) + """ + opts = self.default_options + opts['__modules__'] = dict() + scan_id = str(uuid.uuid4()) + module_list = ['sfp__stor_db'] + + sfscan = SpiderFootScanner("example scan name", scan_id, "spiderfoot.net", "IP_ADDRESS", module_list, opts, start=False) + with self.assertRaises(ValueError): + sfscan._SpiderFootScanner__setStatus("example invalid scan status") diff --git a/test/unit/test_spiderfoottarget.py b/test/unit/test_spiderfoottarget.py index 3cdd58994e..60cf3fc6be 100644 --- a/test/unit/test_spiderfoottarget.py +++ b/test/unit/test_spiderfoottarget.py @@ -60,7 +60,7 @@ def test_init_supported_target_types(self): self.assertEqual(target.targetType, target_type) self.assertEqual(target.targetValue, target_value) - def test_setAlias(self): + def test_setAlias_invalid_alias_should_not_set_alias(self): """ Test setAlias(self, value, typeName) """ @@ -69,7 +69,36 @@ def test_setAlias(self): target = SpiderFootTarget(target_value, target_type) target.setAlias(None, None) - self.assertEqual('TBD', 'TBD') + target.setAlias("", None) + target.setAlias(None, "") + target.setAlias("", "") + target.setAlias("example value", None) + target.setAlias(None, "example type") + + target_aliases = target.targetAliases + self.assertIsInstance(target_aliases, list) + self.assertEqual([], target_aliases) + + def test_setAlias_should_set_alias(self): + """ + Test setAlias(self, value, typeName) + """ + target_value = 'example target value' + target_type = 'IP_ADDRESS' + target = SpiderFootTarget(target_value, target_type) + + target.setAlias("example value", "example type") + + expected_aliases = [{'type': 'example type', 'value': 'example value'}] + + target_aliases = target.targetAliases + self.assertEqual(expected_aliases, target_aliases) + + # check duplicated aliases aren't created + target.setAlias("example value", "example type") + + target_aliases = target.targetAliases + self.assertEqual(expected_aliases, target_aliases) def test_targetType_attribute_should_return_a_string(self): """ @@ -121,6 +150,17 @@ def test_targetValue_attribute_setter_invalid_type_should_raise_TypeError(self): target = SpiderFootTarget(target_value, target_type) target.targetValue = invalid_type + def test_targetValue_attribute_setter_empty_value_should_raise_ValueError(self): + """ + Test targetValue attribute + """ + target_value = 'example target value' + target_type = 'IP_ADDRESS' + + with self.assertRaises(ValueError): + target = SpiderFootTarget(target_value, target_type) + target.targetValue = "" + def test_targetAliases_attribute_should_return_a_list(self): """ Test targetAliases attribute @@ -142,6 +182,22 @@ def test_getEquivalents_should_return_a_list(self): equivalents = target._getEquivalents(target_type) self.assertEqual(list, type(equivalents)) + def test_getEquivalents_should_return_alias_values(self): + """ + Test _getEquivalents(self, typeName) + """ + target_value = 'example target value' + target_type = 'IP_ADDRESS' + target = SpiderFootTarget(target_value, target_type) + + alias_type = "example type" + alias_value = "example value" + target.setAlias(alias_value, alias_type) + + equivalents = target._getEquivalents(alias_type) + self.assertIsInstance(equivalents, list) + self.assertEqual(equivalents[0], alias_value) + def test_getNames_should_return_a_list(self): """ Test getNames(self)