Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
indicators-parser-saas
backend
Commits
91959316
Verified
Commit
91959316
authored
2 years ago
by
Власов Иван Юрьевич
Browse files
Options
Download
Patches
Plain Diff
Added availability to generate link for report
parent
2716906d
master
No related merge requests found
Pipeline
#98916
passed with stage
in 3 minutes and 39 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
endpoints/__init__.py
+1
-2
endpoints/__init__.py
endpoints/parser.py
+15
-2
endpoints/parser.py
endpoints/routes.py
+1
-0
endpoints/routes.py
responses/errors.py
+7
-0
responses/errors.py
tests/test_indicator.py
+24
-1
tests/test_indicator.py
with
48 additions
and
5 deletions
+48
-5
endpoints/__init__.py
+
1
−
2
View file @
91959316
from
.oauth
import
github_oauth_callback
,
github_oauth_redirect
from
.service
import
ping
from
.user
import
get_me
,
create_bot_token
,
get_bot_tokens
,
delete_bot_token
from
.parser
import
load_report
from
.parser
import
load_report
,
get_report_file
,
get_reports
,
get_page_report
from
.indicators
import
get_indicators_from_group
,
get_indicator_groups
from
.watcher
import
create_watcher
from
.parser
import
get_reports
,
get_page_report
from
.static
import
token
This diff is collapsed.
Click to expand it.
endpoints/parser.py
+
15
−
2
View file @
91959316
...
...
@@ -6,14 +6,15 @@ from aiohttp import request as aiohttp_request
from
starlette.background
import
BackgroundTasks
from
starlette.datastructures
import
UploadFile
from
starlette.requests
import
Request
from
tortoise.expressions
import
Q
from
integrations.s3
import
save_report_to_s3
from
integrations.s3
import
save_report_to_s3
,
generate_report_url
from
models
import
Report
,
IndicatorGroup
from
models.indicator
import
IndicatorGroupPD
from
models.report
import
ReportPD
from
parsers.pdf_parser
import
process_pdf
from
parsers.text_parser
import
CollectedData
,
find_ioc
from
responses.errors
import
ReportNotPresented
,
ReportURLError
from
responses.errors
import
ReportNotPresented
,
ReportURLError
,
ReportNotFound
from
responses.responses
import
OkResponse
...
...
@@ -69,3 +70,15 @@ async def get_page_report(request: Request):
await
group
.
save
()
group_pd
=
IndicatorGroupPD
.
from_orm
(
group
)
return
OkResponse
({
"indicator_group"
:
group_pd
})
async
def
get_report_file
(
request
:
Request
):
""" Returns report file """
try
:
report_id
=
request
.
query_params
[
"report_id"
]
except
KeyError
:
raise
ReportNotFound
report
=
await
Report
.
get_or_none
(
id
=
report_id
)
if
report
is
None
or
(
report
.
owner_id
!=
request
.
state
.
user
.
id
and
not
report
.
is_public
):
raise
ReportNotFound
return
OkResponse
({
"link"
:
generate_report_url
(
report_id
)})
This diff is collapsed.
Click to expand it.
endpoints/routes.py
+
1
−
0
View file @
91959316
...
...
@@ -21,6 +21,7 @@ api_routes = [
Route
(
"/createWatcher"
,
create_watcher
,
methods
=
[
"POST"
]),
Route
(
"/getIndicatorGroups"
,
get_indicator_groups
,
methods
=
[
"GET"
]),
Route
(
"/getReports"
,
get_reports
,
methods
=
[
"GET"
]),
Route
(
"/getReportFilePath"
,
get_report_file
,
methods
=
[
"GET"
]),
Route
(
"/loadPageReport"
,
get_page_report
,
methods
=
[
"GET"
]),
]
...
...
This diff is collapsed.
Click to expand it.
responses/errors.py
+
7
−
0
View file @
91959316
...
...
@@ -68,6 +68,13 @@ class ReportNotPresented(ApiError):
http_code
=
422
class
ReportNotFound
(
ApiError
):
""" Report not found in database """
code
=
302
description
=
"Report not found in database"
http_code
=
404
class
IndicatorGroupDoesNotExist
(
ApiError
):
""" Indicator group not found in database """
code
=
402
...
...
This diff is collapsed.
Click to expand it.
tests/test_indicator.py
+
24
−
1
View file @
91959316
...
...
@@ -5,7 +5,7 @@ from requests import get
from
models.indicator
import
IndicatorGroup
,
Indicator
,
IndicatorType
from
parsers.text_parser
import
CollectedData
from
responses.errors
import
ReportNotPresented
,
IndicatorGroupDoesNotExist
from
responses.errors
import
ReportNotPresented
,
IndicatorGroupDoesNotExist
,
ReportNotFound
from
tests.fixtures.tortoise
import
user
from
tests.fixtures.client
import
sti_auth
from
tests.fixtures.indicators
import
mts_report
...
...
@@ -137,3 +137,26 @@ def test_parse_link_report(sti_auth):
assert
resp
.
status_code
==
200
assert
len
(
resp
.
json
()[
"data"
][
"indicators"
])
==
3
def
test_report_link_generation
(
sti_auth
,
mts_report
):
resp
=
sti_auth
.
post
(
"/api/loadReport"
,
files
=
{
"file"
:
open
(
mts_report
,
"rb"
)})
assert
resp
.
status_code
==
200
report_id
=
resp
.
json
()[
"data"
][
"report_id"
]
resp
=
sti_auth
.
get
(
f
"/api/getReportFilePath?report_id=
{
report_id
}
"
)
assert
resp
.
status_code
==
200
link
=
resp
.
json
()[
"data"
][
"link"
]
print
(
link
)
resp
=
get
(
link
)
assert
resp
.
status_code
==
200
def
test_report_link_generation_not_found
(
sti_auth
):
resp
=
sti_auth
.
get
(
f
"/api/getReportFilePath?report_id=not-exists"
)
assert
resp
.
status_code
==
404
assert
not
resp
.
json
()[
"ok"
]
assert
resp
.
json
()[
"data"
][
"error_code"
]
==
ReportNotFound
.
code
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets