Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test tanner-parse-response #192

Merged
merged 6 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions snare/tests/test_parse_tanner_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ async def test():

def test_parse_type_one_error(self):
self.detection = {"type": 1}
meta_content = {"/index.html": {}}
self.requested_name = 'something/'
self.handler = TannerHandler(self.args, meta_content, self.uuid)
self.expected_status_code = 404

async def test():
Expand Down Expand Up @@ -135,14 +133,12 @@ def test_parse_type_two_error(self):
self.detection = {
"type": 2,
"payload": {
"page": "/index.html",
"page": "/something",
"value": "test"
}
}
meta_content = {"/index.html": {}}
self.handler = TannerHandler(self.args, meta_content, self.uuid)
self.expected_content = b'<html><body><div>test</div></body></html>'
self.content_type = r'text\html'
self.content_type = r'text/html'

async def test():
(self.res1, self.res2,
Expand Down Expand Up @@ -185,5 +181,17 @@ async def test():
self.loop.run_until_complete(test())
self.handler.html_handler.handle_content.assert_called_with(self.call_content)

def test_parse_exception(self):
self.detection = {}
self.call_content = b'<html><body></body></html>'
self.expected_content = self.page_content

async def test():
(self.res1, self.res2,
self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection)

with self.assertRaises(KeyError):
self.loop.run_until_complete(test())

def tearDown(self):
shutil.rmtree(self.main_page_path)
19 changes: 19 additions & 0 deletions snare/tests/test_submit_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import json
import yarl
import aiohttp
import logging
from unittest import mock
from snare.utils.asyncmock import AsyncMock
from snare.tanner_handler import TannerHandler
from snare.utils.page_path_generator import generate_unique_path

logger = logging.getLogger(__name__)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you place it in setUp?



class TestSubmitData(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -65,6 +69,21 @@ async def test():
self.loop.run_until_complete(test())
self.assertEqual(self.result, dict(detection={'type': 1}, sess_uuid="test_uuid"))

def test_submit_data_error(self):

async def mock_json_decode_error(data):
logger.error('Error submitting data: JSONDecodeError {}'.format(data))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, logger creates a file for errors, is this also doing the same? If so please do the cleanup.


self.handler = mock.Mock()
self.handler.submit_data = mock_json_decode_error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use this instead of mocking the entire submit_data method.


async def test():
self.result = await self.handler.submit_data(self.data)

with self.assertLogs(level='ERROR') as log:
self.loop.run_until_complete(test())
self.assertIn('Error submitting data: JSONDecodeError {}'.format(self.data), log.output[0])

def test_event_result_exception(self):
aiohttp.ClientResponse.json = AsyncMock(side_effect=Exception())

Expand Down