Commit 9180fb4e by dorp

added unit tests for rest stuff

parent 2b7d398a
...@@ -30,12 +30,12 @@ def create_report_filename(meta_data): ...@@ -30,12 +30,12 @@ def create_report_filename(meta_data):
return '{}_analysis_report.pdf'.format(meta_data['device_name'].replace(' ', '_').replace('/', '__')) return '{}_analysis_report.pdf'.format(meta_data['device_name'].replace(' ', '_').replace('/', '__'))
def generate_report(firmware_uid): def generate_report(firmware_uid, server_url=None):
request_url = create_request_url(firmware_uid) request_url = create_request_url(firmware_uid, server_url)
try: try:
analysis, meta_data = request_firmware_data(request_url) analysis, meta_data = request_firmware_data(request_url)
except KeyError: except RuntimeError as error:
logging.warning('No firmware found with UID {}'.format(firmware_uid)) logging.warning('No firmware found with UID {}: {}'.format(firmware_uid, error))
return 1 return 1
with TemporaryDirectory() as tmp_dir: with TemporaryDirectory() as tmp_dir:
......
from json import JSONDecodeError
import requests import requests
DEFAULT_URL = 'http://localhost:5000'
def create_request_url(firmware_uid):
base_url = 'http://127.0.0.1:5000/rest/firmware/'
return '{}{}'.format(base_url, firmware_uid)
def create_request_url(firmware_uid, server_url):
return '{}/rest/firmware/{}'.format(server_url if server_url else DEFAULT_URL, firmware_uid)
def request_firmware_data(request_url):
response = requests.get(request_url)
firmware_data = response.json()
return firmware_data['firmware']['analysis'], firmware_data['firmware']['meta_data'] def request_firmware_data(request_url):
try:
response = requests.get(request_url)
firmware_data = response.json()
return firmware_data['firmware']['analysis'], firmware_data['firmware']['meta_data']
except (JSONDecodeError, requests.ConnectionError):
raise RuntimeError('FACT server is not reachable') from None
except KeyError:
raise RuntimeError('Response did not contain a valid firmware') from None
import pytest import pytest
from pdf_generator.pre_processing.rest import request_firmware_data
from pdf_generator.tex_generation.template_engine import Engine from pdf_generator.tex_generation.template_engine import Engine
from test.data.test_dict import TEST_DICT from test.data.test_dict import TEST_DICT
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name
class MockResponse:
@staticmethod
def json():
return {'firmware': {'analysis': {}, 'meta_data': {}}}
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def stub_engine(): def stub_engine():
return Engine() return Engine()
def test_anything_mocked(monkeypatch):
monkeypatch.setattr('pdf_generator.pre_processing.rest.requests.get', lambda x: MockResponse())
anything = request_firmware_data('anything')
assert anything
def test_latex_code_generation(stub_engine: Engine): def test_latex_code_generation(stub_engine: Engine):
result = stub_engine.render_meta_template(TEST_DICT) result = stub_engine.render_meta_template(TEST_DICT)
assert result assert result
from json import JSONDecodeError
import pytest
from pdf_generator.pre_processing.rest import DEFAULT_URL, create_request_url, request_firmware_data
class MockResponse:
def __init__(self, response, raises=False):
self._response = response
self._raises = raises
def json(self):
if self._raises:
raise JSONDecodeError('non json response', '', 0)
return self._response
def test_request_firmware_data(monkeypatch):
monkeypatch.setattr('pdf_generator.pre_processing.rest.requests.get', lambda x: MockResponse({'firmware': {'analysis': 'A', 'meta_data': 'B'}}))
assert request_firmware_data(None) == ('A', 'B')
def test_request_firmware_data_no_connection(monkeypatch): # pylint: disable=invalid-name
monkeypatch.setattr('pdf_generator.pre_processing.rest.requests.get', lambda x: MockResponse(None, True))
with pytest.raises(RuntimeError):
request_firmware_data(None)
def test_request_firmware_data_bad_response(monkeypatch): # pylint: disable=invalid-name
monkeypatch.setattr('pdf_generator.pre_processing.rest.requests.get', lambda x: MockResponse({'unknown': 'message'}))
with pytest.raises(RuntimeError):
request_firmware_data(None)
def test_create_request_url():
assert create_request_url('X', None) == '{}/rest/firmware/X'.format(DEFAULT_URL)
assert create_request_url('X', 'Y') == 'Y/rest/firmware/X'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment