Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Test as a Service
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
INS Courses
AutPy
Test as a Service
Commits
85988318
Commit
85988318
authored
1 year ago
by
Florian Bruhin
Browse files
Options
Downloads
Patches
Plain Diff
Adjust for new project submission
parent
c3722cc8
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#314868
passed
1 year ago
Stage: lint
Stage: test
Stage: build
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
flask_app.py
+10
-12
10 additions, 12 deletions
flask_app.py
tests/test_flask_app.py
+22
-39
22 additions, 39 deletions
tests/test_flask_app.py
with
32 additions
and
51 deletions
flask_app.py
+
10
−
12
View file @
85988318
...
...
@@ -19,7 +19,6 @@ import run_tests
UPLOAD_FOLDER
=
os
.
getenv
(
"
SUBMISSION_REPO
"
,
"
/taas/submission_repo
"
)
TEST_FOLDER
=
os
.
getenv
(
"
TEST_REPO
"
,
"
/taas/test_repo/notebooks
"
)
ALLOWED_EXTENSIONS
=
{
"
ipynb
"
}
ALLOWED_EXTENSIONS_PROJECT
=
{
"
zip
"
}
app
=
Flask
(
__name__
,
static_url_path
=
"
/static
"
)
app
.
config
[
"
UPLOAD_FOLDER
"
]
=
UPLOAD_FOLDER
...
...
@@ -74,11 +73,11 @@ def ensure_allowed_file(filename: str, allowed_extensions: set[str]) -> None:
)
def
_get_file_name
(
request
:
Request
,
project
:
bool
=
False
,
key
:
str
=
"
file
"
)
->
str
:
def
_get_file_name
(
request
:
Request
)
->
str
:
# check if the post request has the file part
if
key
not
in
request
.
files
:
if
"
file
"
not
in
request
.
files
:
raise
APIError
(
"
No file part
"
)
file
=
request
.
files
[
key
]
file
=
request
.
files
[
"
file
"
]
# If the user does not select a file, the browser submits an
# empty file without a filename.
...
...
@@ -89,8 +88,7 @@ def _get_file_name(request: Request, project: bool = False, key: str = "file") -
app
.
logger
.
debug
(
"
file %s
"
,
file
.
filename
)
extensions
=
ALLOWED_EXTENSIONS_PROJECT
if
project
else
ALLOWED_EXTENSIONS
ensure_allowed_file
(
file
.
filename
,
extensions
)
ensure_allowed_file
(
file
.
filename
,
ALLOWED_EXTENSIONS
)
app
.
logger
.
debug
(
"
filename is %s
"
,
file
.
filename
)
filename
=
secure_filename
(
file
.
filename
)
...
...
@@ -98,10 +96,12 @@ def _get_file_name(request: Request, project: bool = False, key: str = "file") -
def
_upload_file
(
filename
:
str
,
file
:
FileStorage
,
destination
:
Path
,
delete
:
bool
=
True
filename
:
str
,
file
:
FileStorage
,
destination
:
Path
,
)
->
Path
:
app
.
logger
.
debug
(
"
Creating destination dir %s
"
,
destination
)
if
delete
and
destination
.
is_dir
():
if
destination
.
is_dir
():
shutil
.
rmtree
(
destination
)
destination
.
mkdir
(
exist_ok
=
True
,
parents
=
True
)
file_path
=
destination
/
filename
...
...
@@ -147,10 +147,8 @@ def upload_project() -> Response:
directory
=
submissions_path
/
"
project
"
zip_filename
=
_get_file_name
(
request
,
project
=
True
)
_upload_file
(
zip_filename
,
request
.
files
[
"
file
"
],
directory
)
nb_filename
=
_get_file_name
(
request
,
key
=
"
notebook
"
)
_upload_file
(
nb_filename
,
request
.
files
[
"
notebook
"
],
directory
,
delete
=
False
)
nb_filename
=
_get_file_name
(
request
)
_upload_file
(
nb_filename
,
request
.
files
[
"
file
"
],
directory
)
result
=
run_tests
.
run_project
(
submissions_path
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_flask_app.py
+
22
−
39
View file @
85988318
...
...
@@ -18,30 +18,23 @@ import flask_app
@pytest.mark.parametrize
(
"
filename,
allowed_extensions,
is_allowed
"
,
"
filename, is_allowed
"
,
[
(
"
testblah
"
,
flask_app
.
ALLOWED_EXTENSIONS
,
False
),
(
"
testblah
"
,
flask_app
.
ALLOWED_EXTENSIONS_PROJECT
,
False
),
(
"
testipynb
"
,
flask_app
.
ALLOWED_EXTENSIONS
,
False
),
(
"
testzip
"
,
flask_app
.
ALLOWED_EXTENSIONS_PROJECT
,
False
),
(
"
test.zip
"
,
flask_app
.
ALLOWED_EXTENSIONS
,
False
),
(
"
test.ipynb
"
,
flask_app
.
ALLOWED_EXTENSIONS_PROJECT
,
False
),
(
"
test.iPyNb
"
,
flask_app
.
ALLOWED_EXTENSIONS
,
True
),
(
"
test.ipynb
"
,
flask_app
.
ALLOWED_EXTENSIONS
,
True
),
(
"
test.zip
"
,
flask_app
.
ALLOWED_EXTENSIONS_PROJECT
,
True
),
(
"
test.Zip
"
,
flask_app
.
ALLOWED_EXTENSIONS_PROJECT
,
True
),
(
"
testblah
"
,
False
),
(
"
testipynb
"
,
False
),
(
"
test.zip
"
,
False
),
(
"
test.iPyNb
"
,
True
),
(
"
test.ipynb
"
,
True
),
],
)
def
test_allowed_file
(
filename
:
str
,
allowed_extensions
:
set
[
str
],
is_allowed
:
bool
)
->
None
:
def
test_allowed_file
(
filename
:
str
,
is_allowed
:
bool
)
->
None
:
if
is_allowed
:
flask_app
.
ensure_allowed_file
(
filename
,
allowed_extensions
)
flask_app
.
ensure_allowed_file
(
filename
,
flask_app
.
ALLOWED_EXTENSIONS
)
else
:
with
pytest
.
raises
(
Exception
,
match
=
"
File extension not allowed
"
# FIXME be more granular?
):
flask_app
.
ensure_allowed_file
(
filename
,
allowed_extensions
)
flask_app
.
ensure_allowed_file
(
filename
,
flask_app
.
ALLOWED_EXTENSIONS
)
def
get_request
(
data
:
dict
[
str
,
Any
])
->
flask
.
wrappers
.
Request
:
...
...
@@ -51,37 +44,28 @@ def get_request(data: dict[str, Any]) -> flask.wrappers.Request:
@pytest.mark.parametrize
(
"
filename,
project,
expected
"
,
"
filename, expected
"
,
[
(
"
bla.ipynb
"
,
False
,
"
bla.ipynb
"
),
(
"
/unsafe.ipynb
"
,
False
,
"
unsafe.ipynb
"
),
(
"
bla.zip
"
,
True
,
"
bla.zip
"
),
(
"
/unsafe.zip
"
,
True
,
"
unsafe.zip
"
),
(
"
bla.ipynb
"
,
"
bla.ipynb
"
),
(
"
/unsafe.ipynb
"
,
"
unsafe.ipynb
"
),
],
)
def
test_get_file_name
(
filename
:
str
,
project
:
bool
,
expected
:
str
)
->
None
:
def
test_get_file_name
(
filename
:
str
,
expected
:
str
)
->
None
:
request
=
get_request
({
"
file
"
:
(
os
.
devnull
,
filename
)})
assert
flask_app
.
_get_file_name
(
request
,
project
)
==
expected
assert
flask_app
.
_get_file_name
(
request
)
==
expected
@pytest.mark.parametrize
(
"
data,
project,
message
"
,
"
data, message
"
,
[
({},
True
,
"
No file part
"
),
({},
False
,
"
No file part
"
),
({
"
file
"
:
(
os
.
devnull
,
""
)},
True
,
"
No selected file
"
),
({
"
file
"
:
(
os
.
devnull
,
""
)},
False
,
"
No selected file
"
),
(
{
"
file
"
:
(
os
.
devnull
,
"
bla.ipynb
"
)},
True
,
"
File extension not allowed
"
,
),
({
"
file
"
:
(
os
.
devnull
,
"
bla.zip
"
)},
False
,
"
File extension not allowed
"
),
({},
"
No file part
"
),
({
"
file
"
:
(
os
.
devnull
,
""
)},
"
No selected file
"
),
({
"
file
"
:
(
os
.
devnull
,
"
bla.zip
"
)},
"
File extension not allowed
"
),
],
)
def
test_get_file_name_bad
(
data
:
dict
[
str
,
Any
],
project
:
bool
,
message
:
str
)
->
None
:
def
test_get_file_name_bad
(
data
:
dict
[
str
,
Any
],
message
:
str
)
->
None
:
with
pytest
.
raises
(
flask_app
.
APIError
,
match
=
re
.
escape
(
message
)):
flask_app
.
_get_file_name
(
get_request
(
data
)
,
project
)
flask_app
.
_get_file_name
(
get_request
(
data
))
@pytest.mark.parametrize
(
...
...
@@ -258,8 +242,7 @@ class TestRend:
r
=
client
.
post
(
"
/project
"
,
data
=
{
"
file
"
:
(
io
.
BytesIO
(
b
"
not-a-zip-lol
"
),
"
project.zip
"
),
"
notebook
"
:
(
io
.
BytesIO
(
b
"
nb
"
),
"
notebook.ipynb
"
),
"
file
"
:
(
io
.
BytesIO
(
b
"
nb
"
),
"
notebook.ipynb
"
),
},
)
...
...
@@ -272,7 +255,7 @@ class TestRend:
assert
commit_msg
.
startswith
(
"
Submission at
"
)
project_path
=
pathlib
.
PurePath
(
"
project
"
)
expected_files
=
{
project_path
/
"
project.zip
"
,
project_path
/
"
notebook.ipynb
"
}
expected_files
=
{
project_path
/
"
notebook.ipynb
"
}
assert
submissions_repo
.
ls_files
()
==
expected_files
@pytest.mark.parametrize
(
"
path
"
,
[
"
/testbook
"
,
"
/project
"
])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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