Skip to content

Commit

Permalink
refactor and fix edge cases test
Browse files Browse the repository at this point in the history
  • Loading branch information
nsyed22 committed Jan 18, 2023
1 parent 719c94a commit f5548b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
29 changes: 17 additions & 12 deletions pybossa/view/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,30 +518,35 @@ def clone(short_name):
project=project_sanitized
))

def is_editor_disabled():
return (not current_user.admin and
current_app.config.get(
'DISABLE_TASK_PRESENTER_EDITOR'))

def is_admin_or_owner(project):
return (current_user.admin or
(project.owner_id == current_user.id or
current_user.id in project.owners_ids))

@blueprint.route('/<short_name>/tasks/taskpresenterimageupload', methods=['GET', 'POST'])
@login_required
@admin_or_subadmin_required
@csrf.exempt
def upload_task_guidelines_image(short_name):
error = False
return_code = 200
project = project_by_shortname(short_name)
disable_editor = (not current_user.admin and
current_app.config.get(
'DISABLE_TASK_PRESENTER_EDITOR'))

is_admin_or_owner = (
current_user.admin or
(project.owner_id == current_user.id or
current_user.id in project.owners_ids))
imgurls = []
if disable_editor:
if is_editor_disabled():
flash(gettext('Task presenter editor disabled!'), 'error')
error = True
elif not is_admin_or_owner:
return_code = 400
elif not is_admin_or_owner(project):
flash(gettext('Ooops! Only project owners can upload files.'), 'error')
error = True
return_code = 400
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)
Expand All @@ -557,15 +562,15 @@ def upload_task_guidelines_image(short_name):
))
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
return_code = 413

response = {
"imgurls" : imgurls,
"error": error
}

return jsonify(response), 200 if large_file == False else 413
return jsonify(response), return_code

@blueprint.route('/<short_name>/tasks/taskpresentereditor', methods=['GET', 'POST'])
@login_required
Expand Down
16 changes: 7 additions & 9 deletions test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -4784,7 +4784,7 @@ def test_task_presenter_multiple_image_upload(self, mock):
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)
@patch('pybossa.view.projects.is_admin_or_owner', return_value=False)
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...")
Expand All @@ -4801,23 +4801,22 @@ def test_task_presenter_image_upload_user_not_owner_or_admin(self, mock):
'subproduct': 'def',
},
owner=user)
user2 = UserFactory.create(id=505)
headers = [('Authorization', user2.api_key)]
headers = [('Authorization', user.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 res.status_code == 400, "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):
@patch('pybossa.view.projects.is_editor_disabled', return_value=True)
def test_task_presenter_image_upload_task_presenter_disabled(self, disable_editor):

"""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)
Expand All @@ -4841,8 +4840,7 @@ def test_task_presenter_image_upload_task_presenter_disabled(self, mock, mock_us
# 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 res.status_code == 400, "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"

Expand Down

0 comments on commit f5548b1

Please sign in to comment.