| |
| |
| |
| |
|
|
| import os |
| import sys |
|
|
|
|
| class TestPossibleUsernames: |
| def setup(self): |
| self._orig_path = os.getcwd() |
|
|
| self._alerts_path = os.path.join( |
| os.path.dirname(__file__), |
| '../../../alerts') |
|
|
| sys.path.insert(0, self._alerts_path) |
|
|
| def teardown(self): |
| os.chdir(self._orig_path) |
|
|
| sys.path.remove(self._alerts_path) |
|
|
| if 'lib' in sys.modules: |
| del sys.modules['lib'] |
|
|
| def test_enrichment(self): |
| from alerts.plugins.possible_usernames import enrich |
|
|
| events = [ |
| { |
| |
| 'details': { |
| 'username': 'tester1' |
| } |
| }, |
| { |
| |
| 'details': { |
| 'otherthing': 'somevalue' |
| } |
| }, |
| { |
| |
| 'notwhatwewant': { |
| 'something': 'else' |
| } |
| }, |
| { |
| |
| 'details': { |
| 'username': 'tester1' |
| } |
| } |
| ] |
|
|
| alert = { |
| 'details': { |
| 'username': 'tester2' |
| } |
| } |
|
|
| enriched = enrich(alert, events) |
|
|
| |
| assert enriched['details']['username'] == 'tester2' |
|
|
| |
| assert len(enriched['details']['possible_usernames']) == 1 |
| assert enriched['details']['possible_usernames'][0] == 'tester1' |
| assert 'tester1' in enriched['summary'] |
|
|
| def test_hostname_detection(self): |
| from alerts.plugins.possible_usernames import _most_common_hostname |
|
|
| |
| events = [ |
| { |
| |
| 'documentsource': { |
| 'hostname': 'host1', |
| }, |
| }, |
| { |
| |
| 'notdocsource': { |
| 'hostname': 'host1', |
| }, |
| }, |
| { |
| |
| 'documentsource': { |
| 'nothostname': 'notahost', |
| }, |
| }, |
| { |
| |
| 'documentsource': { |
| 'hostname': 'host1', |
| }, |
| }, |
| { |
| |
| 'documentsource': { |
| 'hostname': 'host2', |
| }, |
| }, |
| ] |
|
|
| hostname = _most_common_hostname(events) |
|
|
| |
| assert hostname == 'host1' |
|
|