Skip to content

Commit

Permalink
sfscan: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoles committed Sep 18, 2020
1 parent ed64ceb commit 84ba088
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 34 deletions.
3 changes: 0 additions & 3 deletions sfscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 0 additions & 26 deletions test/unit/modules/test_sfp_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from modules.sfp_template import sfp_template
from sflib import SpiderFoot
from spiderfoot import SpiderFootEvent, SpiderFootTarget


class TestModuletemplate(unittest.TestCase):
Expand Down Expand Up @@ -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)
33 changes: 33 additions & 0 deletions test/unit/test_spiderfootevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
89 changes: 87 additions & 2 deletions test/unit/test_spiderfootplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
32 changes: 31 additions & 1 deletion test/unit/test_spiderfootscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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")
60 changes: 58 additions & 2 deletions test/unit/test_spiderfoottarget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 84ba088

Please sign in to comment.