Skip to content

Commit

Permalink
Test handle_error (#131)
Browse files Browse the repository at this point in the history
* Test handle_error

* Suggested changes including other tests

* minor change

* pep8

* test directory fix

* minor change

* Small change

* Changes

* unique directory creater

* minor changes

* Suggested changes
  • Loading branch information
viskey98 authored and afeena committed May 23, 2018
1 parent 3b8bb1a commit b764276
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/test_handle_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import unittest
from unittest.mock import Mock
import asyncio
import argparse
import aiohttp
import shutil
import os
from utils.asyncmock import AsyncMock
from snare import HttpRequestHandler
from utils.page_path_generator import generate_unique_path


class TestHandleError(unittest.TestCase):
def setUp(self):
self.meta = {}
run_args = argparse.ArgumentParser()
run_args.add_argument("--tanner")
run_args.add_argument("--page-dir")
self.main_page_path = generate_unique_path()
os.makedirs(self.main_page_path)
self.page_dir = self.main_page_path.rsplit('/')[-1]
self.args = run_args.parse_args(['--page-dir', self.page_dir])
self.loop = asyncio.new_event_loop()
self.status = 500
self.message = "test"
self.payload = "test"
self.exc = "[Errno 0] test"
self.headers = "test"
self.reason = "test"
self.data = dict(
method='GET',
path='/',
headers="test_headers",
uuid="test_uuid",
peer="test_peer",
status="test_status",
error=self.exc
)
aiohttp.server.ServerHttpProtocol.handle_error = Mock()
self.handler = HttpRequestHandler(self.meta, self.args)
self.handler.create_data = Mock(return_value=self.data)
self.handler.submit_data = AsyncMock()

def test_create_error_data(self):

async def test():
await self.handler.handle_error(
self.status, self.message, self.payload, self.exc, self.headers, self.reason)
self.loop.run_until_complete(test())
self.handler.create_data.assert_called_with(self.message, self.status)

def test_submit_error_data(self):

async def test():
await self.handler.handle_error(
self.status, self.message, self.payload, self.exc, self.headers, self.reason)
self.loop.run_until_complete(test())
self.handler.submit_data.assert_called_with(self.data)

def test_handle_error_data(self):

async def test():
await self.handler.handle_error(
self.status, self.message, self.payload, self.exc, self.headers, self.reason)
self.loop.run_until_complete(test())
aiohttp.server.ServerHttpProtocol.handle_error.assert_called_with(
self.status, self.message, self.payload, self.exc, self.headers, self.reason)

def tearDown(self):
shutil.rmtree(self.main_page_path)

0 comments on commit b764276

Please # to comment.