Skip to content

Commit

Permalink
initial push for edge case tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nsyed22 committed Jan 18, 2023
1 parent f622f6c commit 719c94a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 25 deletions.
42 changes: 20 additions & 22 deletions pybossa/view/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ def clone(short_name):
def upload_task_guidelines_image(short_name):
error = False
project = project_by_shortname(short_name)

disable_editor = (not current_user.admin and
current_app.config.get(
'DISABLE_TASK_PRESENTER_EDITOR'))
Expand All @@ -534,33 +533,32 @@ def upload_task_guidelines_image(short_name):
current_user.admin or
(project.owner_id == current_user.id or
current_user.id in project.owners_ids))

imgurls = []
if disable_editor:
flash(gettext('Task presenter editor disabled!'), 'error')
error = True
elif not is_admin_or_owner:
flash(gettext('Ooops! Only project owners can upload files.'), 'error')
error = True

imgurls = []
large_file = False
for file in request.files.getlist("image"):
file_size_mb = file.seek(0, os.SEEK_END) / 1024 / 1024
file.seek(0, os.SEEK_SET)
file.filename = secure_filename(file.filename)
if file_size_mb < current_app.config.get('MAX_IMAGE_UPLOAD_SIZE_MB', 5):
container = "user_%s" % current_user.id
uploader.upload_file(file, container=container)
imgurls.append(get_avatar_url(
current_app.config.get('UPLOAD_METHOD'),
file.filename,
container,
current_app.config.get('AVATAR_ABSOLUTE')
))
else:
flash(gettext('File must be smaller than ' + str(current_app.config.get('MAX_IMAGE_UPLOAD_SIZE_MB', 5)) + ' MB.'))
large_file = True
error = True
else:
large_file = False
for file in request.files.getlist("image"):
file_size_mb = file.seek(0, os.SEEK_END) / 1024 / 1024
file.seek(0, os.SEEK_SET)
file.filename = secure_filename(file.filename)
if file_size_mb < current_app.config.get('MAX_IMAGE_UPLOAD_SIZE_MB', 5):
container = "user_%s" % current_user.id
uploader.upload_file(file, container=container)
imgurls.append(get_avatar_url(
current_app.config.get('UPLOAD_METHOD'),
file.filename,
container,
current_app.config.get('AVATAR_ABSOLUTE')
))
else:
flash(gettext('File must be smaller than ' + str(current_app.config.get('MAX_IMAGE_UPLOAD_SIZE_MB', 5)) + ' MB.'))
large_file = True
error = True

response = {
"imgurls" : imgurls,
Expand Down
67 changes: 65 additions & 2 deletions test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from test import db, Fixtures, with_context, with_context_settings, \
FakeResponse, mock_contributions_guard, with_request_context
from test.helper import web
from test.test_authorization import mock_current_user
from unittest.mock import patch, Mock, call, MagicMock
from flask import redirect
from itsdangerous import BadSignature
Expand Down Expand Up @@ -4697,7 +4698,7 @@ def test_48_task_presenter_editor_works_json(self, mock):
@patch('pybossa.view.projects.uploader.upload_file', return_value=True)
def test_task_presenter_large_image_upload(self, mock):
"""Test API /tasks/taskpresenterimageupload should not upload images with size > 5 MB"""
print("running test_task_presenter_image_upload...")
print("running test_task_presenter_large_image_upload...")
user = UserFactory.create(id=500)
project = ProjectFactory.create(
short_name='test_project',
Expand Down Expand Up @@ -4754,7 +4755,7 @@ def test_task_presenter_image_upload(self, mock):
@patch('pybossa.view.projects.uploader.upload_file', return_value=True)
def test_task_presenter_multiple_image_upload(self, mock):
"""Test API /tasks/taskpresenterimageupload to upload multiple task presenter guidelines images"""
print("running test_task_presenter_image_upload...")
print("running test_task_presenter_multiple_image_upload...")
user = UserFactory.create(id=500)
project = ProjectFactory.create(
short_name='test_project',
Expand Down Expand Up @@ -4782,6 +4783,68 @@ def test_task_presenter_multiple_image_upload(self, mock):
assert len(res_data['imgurls']) == 2, "Successful count of uploaded images 2."
assert res_data['error'] == False, "There should be no errors for normal file upload"

@with_context
@patch('pybossa.view.projects.uploader.upload_file', return_value=True)
def test_task_presenter_image_upload_user_not_owner_or_admin(self, mock):
"""Test API /tasks/taskpresenterimageupload to upload a task presenter guidelines image"""
print("running test_task_presenter_image_upload_user_not_owner_or_admin...")
user = UserFactory.create(id=500)
project = ProjectFactory.create(
short_name='test_project',
name='Test Project',
info={
'total': 150,
'task_presenter': 'foo',
'data_classification': dict(input_data="L4 - public", output_data="L4 - public"),
'kpi': 0.5,
'product': 'abc',
'subproduct': 'def',
},
owner=user)
user2 = UserFactory.create(id=505)
headers = [('Authorization', user2.api_key)]
with open('./test/files/small-image1.jpg', 'rb') as img:
imgStringIO = BytesIO(img.read())
# Call API method to upload image.
res = self.app.post('/project/{}/tasks/taskpresenterimageupload'.format(project.short_name), headers=headers, data={'image': (imgStringIO, 'large-image.jpg')})
res_data = json.loads(res.data)
assert res.status_code == 200, "POST image upload should be successful"
assert len(res_data['imgurls']) == 0, "Image should not be uploaded."
assert res_data['error'] == True, "There should be an error since the user is not owner or admin"

mock_authenticated=mock_current_user(anonymous=False, admin=False, id=2)

@with_context
@patch('pybossa.view.projects.uploader.upload_file', return_value=True)
@patch('flask_login.current_user', admin=False)
def test_task_presenter_image_upload_task_presenter_disabled(self, mock, mock_user):
"""Test API /tasks/taskpresenterimageupload to upload a task presenter guidelines image"""
print("running test_task_presenter_image_upload_task_presenter_disabled...")
user = UserFactory.create(id=2, admin=False)
project = ProjectFactory.create(
short_name='test_project',
name='Test Project',
info={
'total': 150,
'task_presenter': 'foo',
'data_classification': dict(input_data="L4 - public", output_data="L4 - public"),
'kpi': 0.5,
'product': 'abc',
'subproduct': 'def',
},
owner=user)

headers = [('Authorization', user.api_key)]
with open('./test/files/small-image1.jpg', 'rb') as img:
imgStringIO = BytesIO(img.read())
with patch.dict(self.flask_app.config, {'DISABLE_TASK_PRESENTER_EDITOR': True}):
# Call API method to upload image.
res = self.app.post('/project/{}/tasks/taskpresenterimageupload'.format(project.short_name), headers=headers, data={'image': (imgStringIO, 'large-image.jpg')})
res_data = json.loads(res.data)
print(res_data)
assert res.status_code == 200, "POST image upload should be successful"
assert len(res_data['imgurls']) == 0, "Image should not be uploaded."
assert res_data['error'] == True, "There should be an error since the task presenter is disabled"

@with_context
@patch('pybossa.ckan.requests.get')
Expand Down

0 comments on commit 719c94a

Please sign in to comment.