Skip to content

Commit

Permalink
test report content
Browse files Browse the repository at this point in the history
  • Loading branch information
KissPeter committed Nov 16, 2019
1 parent 2a884f2 commit 10f57b1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ addons:
script:
- python fuzzer.py -h
- pip3 install -r test/requirements_for_test.txt
- cd test && pytest test.py
- cd test && pytest -rfs --durations=10 test.py
18 changes: 9 additions & 9 deletions apifuzzer/fuzzer_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,30 @@ def transmit(self, **kwargs):
self.logger.debug('Request url:{}\nRequest method: {}\nRequest headers: {}\nRequest body: {}'.format(
request_url, method, json.dumps(dict(kwargs.get('headers',{})), indent=2), kwargs.get('params')))
self.report.set_status(Report.PASSED)
self.report.add('request_url', try_b64encode(request_url))
self.report.add('request_method', try_b64encode(method))
self.report.add('request_headers', try_b64encode(kwargs.get('headers')))
self.report.add('request_url', request_url)
self.report.add('request_method', method)
self.report.add('request_headers', json.dumps(dict(kwargs.get('headers', {}))))
try:
_return = requests.request(method=method, url=request_url, verify=False, timeout=10, **kwargs)
except Exception as e:
self.report.set_status(Report.FAILED)
self.logger.error('Request failed, reason: {}'.format(e))
self.report.add('request_sending_failed', e.reason if hasattr(e, 'reason') else e)
self.report.add('request_method', try_b64encode(method))
self.report.add('request_method', method)
return
# overwrite request headers in report, add auto generated ones
self.report.add('request_headers', try_b64encode(_return.request.headers))
self.report.add('request_headers', try_b64encode(json.dumps(dict(_return.request.headers))))
self.logger.debug('Response code:{}\nResponse headers: {}\nResponse body: {}'.format(
_return.status_code, json.dumps(dict(_return.headers), indent=2), _return.content))
self.report.add('request_body', try_b64encode(_return.request.body))
self.report.add('response', try_b64encode(_return.content))
self.report.add('request_body', _return.request.body)
self.report.add('response', _return.content.decode())
status_code = _return.status_code
if not status_code:
self.report.set_status(Report.FAILED)
self.logger.warn('Failed to parse http response code')
self.report.failed('Failed to parse http response code')
elif status_code not in self.accepted_status_codes:
self.report.add('Parsed status_code', status_code)
self.report.add('parsed_status_code', status_code)
self.report.set_status(Report.FAILED)
self.logger.warn('Return code %s is not in the expected list', status_code)
self.report.failed(('Return code %s is not in the expected list', status_code))
Expand Down Expand Up @@ -135,7 +135,7 @@ def save_report_to_disc(self):
with open('{}/{}_{}.json'.format(self.report_dir, self.test_number, time()), 'w') as report_dump_file:
report_dump_file.write(json.dumps(self.report.to_dict()))
except Exception as e:
self.logger.error('Failed to save report "{}" to {} because: {}'.format(self.report, self.report_dir, e))
self.logger.error('Failed to save report "{}" to {} because: {}'.format(self.report.to_dict(), self.report_dir, e))
pass

def expand_path_variables(self, url, path_parameters):
Expand Down
2 changes: 1 addition & 1 deletion apifuzzer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def set_logger(level='warning'):


def transform_data_to_bytes(data_in):
# print('data_in: {}, type: {}'.format(data_in, type(data_in)))
print('data_in: {}, type: {}'.format(data_in, type(data_in)))
if isinstance(data_in, float):
return bytes(int(data_in))
elif isinstance(data_in, str):
Expand Down
1 change: 1 addition & 0 deletions test/requirements_for_test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Flask==1.0.2
pytest==3.6.3
pytest-cov==2.5.1
pytest-hidecaptured==0.2.2
sphinx
sphinx_rtd_theme
psutil
21 changes: 20 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@

class TestClass(object):


@classmethod
def setup_class(cls):
print('\nsetup_class()')
cls.report_dir = tempfile.mkdtemp()
cls.report_files = list()
cls.test_app_url = "http://127.0.0.1:5000/"
print('Setup_class with report dir: {}'.format(cls.report_dir))
if not get_test_server_pid():
print('Start test app')
os.system("python3 ./test_application.py 2>&1 | logger -t $0 &")
with open('./test_swagger_definition.json', 'r') as f:
cls.swagger = json.loads(f.read())

def teardown_method(self, method):
for f in self.report_files:
os.remove('{}/{}'.format(self.report_dir,f))

@classmethod
def teardown_class(cls):
pid = get_test_server_pid()
Expand Down Expand Up @@ -51,6 +57,11 @@ def fuzz(self, api_resources):
prog.prepare()
prog.run()

def get_last_report_file(self):
self.report_files = os.listdir(self.report_dir)
with open("{}/{}".format(self.report_dir, self.report_files[0]), mode='r', encoding='utf-8') as f:
return json.loads(f.read())

def test_integer_status_code(self):
api_endpoint_to_test = self.swagger['paths']['/exception/{integer_id}']
print('API to test: {}'.format(api_endpoint_to_test))
Expand All @@ -64,3 +75,11 @@ def test_integer_status_code(self):
last_value_sent = last_call['req_path'].replace('/exception/', '')
assert not isinstance(last_value_sent, int), last_value_sent
assert last_call['resp_status'] == 500, last_call['resp_status'] + "Received"

def test_integer_status_code_in_report(self):
required_report_fields = ['status', 'sub_reports', 'name', 'request_body', 'parsed_status_code',
'request_headers', 'state', 'request_method', 'reason', 'request_url',
'response', 'test_number']
last_report = self.get_last_report_file()
assert sorted(required_report_fields) == sorted(last_report.keys())
assert last_report['parsed_status_code'] == 500
6 changes: 4 additions & 2 deletions test/test_application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python2.7
import time
from functools import wraps

from flask import Flask, jsonify, request
Expand All @@ -11,6 +10,9 @@ class LastRequestData(object):
def __init__(self):
self.last_request_data = dict()

def wipe_data(self):
self.last_request_data = dict()

def set_data(self, data=None):
if data is not None:
self.last_request_data.update(data)
Expand Down Expand Up @@ -49,7 +51,7 @@ def transform(integer_id):
@app.route('/last_call', methods=['GET'])
def last_call():
_return = jsonify(last_request_data.get_data())
last_request_data.set_data({})
last_request_data.wipe_data()
return _return


Expand Down

0 comments on commit 10f57b1

Please sign in to comment.