Skip to content

Commit

Permalink
#35 Add unit tests for corteutils.Analyzer class, error and report me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
nadouani committed Apr 6, 2017
1 parent f8856fd commit 2c25fa8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions contrib/cortexutils/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Analyzer:

def __init__(self):
self.__set_encoding()
#self.__set_encoding()

# Prepare in/out/err streams
self.fperror = sys.stderr
Expand Down Expand Up @@ -168,7 +168,7 @@ def report(self, full_report, ensure_ascii=False):
'artifacts': self.artifacts(full_report),
'full': full_report
}
json.dump(report, self.fpoutput, ensure_ascii=False)
json.dump(report, self.fpoutput, ensure_ascii=ensure_ascii)

def run(self):
"""Overwritten by analyzers"""
Expand Down
10 changes: 10 additions & 0 deletions contrib/tests/fixtures/test-error-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dataType": "ip",
"data": "1.1.1.1",
"config": {
"password": "secret",
"key": "secret",
"apikey": "secret",
"api_key": "secret"
}
}
4 changes: 4 additions & 0 deletions contrib/tests/fixtures/test-report-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dataType": "ip",
"data": "1.1.1.1"
}
49 changes: 49 additions & 0 deletions contrib/tests/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import sys
import json
import unittest
from io import StringIO
from io import open
Expand All @@ -16,6 +17,7 @@ def load_test_fixture(fixture_path):
input = fixture_file.read()
fixture_file.close()
sys.stdin = StringIO(input)
sys.stdout = StringIO()

class TestMinimalConfig(unittest.TestCase):

Expand Down Expand Up @@ -85,5 +87,52 @@ def test_check_tlp_ok(self):
# __check_tlp
self.assertEqual(self.analyzer._Analyzer__check_tlp(), True)

class TestErrorResponse(unittest.TestCase):

def setUp(self):
load_test_fixture('fixtures/test-error-response.json')
self.analyzer = Analyzer()

def test_error_response(self):
self.assertEqual(self.analyzer.get_param('config.password'), "secret")
self.assertEqual(self.analyzer.get_param('config.key'), "secret")
self.assertEqual(self.analyzer.get_param('config.apikey'), "secret")
self.assertEqual(self.analyzer.get_param('config.api_key'), "secret")

# Run the error method
with self.assertRaises(SystemExit):
self.analyzer.error('Error', True)

# Get the output
output = self.analyzer.fpoutput.getvalue().strip()
json_output = json.loads(output)

self.assertEqual(json_output['success'], False)
self.assertEqual(json_output['errorMessage'], 'Error')
self.assertEqual(json_output['input']['dataType'], 'ip')
self.assertEqual(json_output['input']['data'], '1.1.1.1')
self.assertEqual(json_output['input']['config']['password'], 'REMOVED')
self.assertEqual(json_output['input']['config']['key'], 'REMOVED')
self.assertEqual(json_output['input']['config']['apikey'], 'REMOVED')
self.assertEqual(json_output['input']['config']['api_key'], 'REMOVED')

class TestReportResponse(unittest.TestCase):

def setUp(self):
load_test_fixture('fixtures/test-report-response.json')
self.analyzer = Analyzer()

def test_error_response(self):
# Run the analyzer report method
self.analyzer.report({'report_id':'12345'})

# Get the output
output = self.analyzer.fpoutput.getvalue().strip()
json_output = json.loads(output)

self.assertEqual(json_output.get('success'), True)
self.assertEqual(json_output.get('errorMessage', None), None)
self.assertEqual(json_output['full']['report_id'], '12345')

if __name__ == '__main__':
unittest.main()

0 comments on commit 2c25fa8

Please sign in to comment.