Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fact_pdf_report
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
fact_pdf_report
Commits
9180fb4e
Commit
9180fb4e
authored
Jul 10, 2019
by
dorp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added unit tests for rest stuff
parent
2b7d398a
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
22 deletions
+52
-22
generator.py
pdf_generator/generator.py
+4
-4
rest.py
pdf_generator/pre_processing/rest.py
+11
-4
test_code_generation.py
test/integration/test_code_generation.py
+0
-14
__init__.py
test/unit/pre_processing/__init__.py
+0
-0
test_rest.py
test/unit/pre_processing/test_rest.py
+37
-0
No files found.
pdf_generator/generator.py
View file @
9180fb4e
...
...
@@ -30,12 +30,12 @@ def create_report_filename(meta_data):
return
'{}_analysis_report.pdf'
.
format
(
meta_data
[
'device_name'
]
.
replace
(
' '
,
'_'
)
.
replace
(
'/'
,
'__'
))
def
generate_report
(
firmware_uid
):
request_url
=
create_request_url
(
firmware_uid
)
def
generate_report
(
firmware_uid
,
server_url
=
None
):
request_url
=
create_request_url
(
firmware_uid
,
server_url
)
try
:
analysis
,
meta_data
=
request_firmware_data
(
request_url
)
except
KeyE
rror
:
logging
.
warning
(
'No firmware found with UID {}
'
.
format
(
firmware_uid
))
except
RuntimeError
as
e
rror
:
logging
.
warning
(
'No firmware found with UID {}
: {}'
.
format
(
firmware_uid
,
error
))
return
1
with
TemporaryDirectory
()
as
tmp_dir
:
...
...
pdf_generator/pre_processing/rest.py
View file @
9180fb4e
from
json
import
JSONDecodeError
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
):
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
test/integration/test_code_generation.py
View file @
9180fb4e
import
pytest
from
pdf_generator.pre_processing.rest
import
request_firmware_data
from
pdf_generator.tex_generation.template_engine
import
Engine
from
test.data.test_dict
import
TEST_DICT
# pylint: disable=redefined-outer-name
class
MockResponse
:
@staticmethod
def
json
():
return
{
'firmware'
:
{
'analysis'
:
{},
'meta_data'
:
{}}}
@pytest.fixture
(
scope
=
'function'
)
def
stub_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
):
result
=
stub_engine
.
render_meta_template
(
TEST_DICT
)
assert
result
test/unit/pre_processing/__init__.py
0 → 100644
View file @
9180fb4e
test/unit/pre_processing/test_rest.py
0 → 100644
View file @
9180fb4e
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'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment