Skip to content

Commit

Permalink
api: return 404 when seeding non existent workflow
Browse files Browse the repository at this point in the history
* Fixes error messages and amends documentation.
  • Loading branch information
Diego Rodriguez committed Jan 15, 2018
1 parent e04a8f8 commit 494f38a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
28 changes: 22 additions & 6 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
"description": "Request failed. Either User or Workflow doesn't exist.",
"examples": {
"application/json": {
"message": "Workflow 256b25f4-4cfb-4684-b7a8-73872ef455a1 doesn't exist"
"message": "Workflow 256b25f4-4cfb-4684-b7a8-73872ef455a1 does not exist"
}
}
},
Expand Down Expand Up @@ -429,7 +429,7 @@
"description": "Request succeeded. The file has been added to the workspace.",
"examples": {
"application/json": {
"message": "The file input.csv has been successfully transferred."
"message": "input.csv has been successfully transferred."
}
},
"schema": {
Expand All @@ -447,6 +447,22 @@
"400": {
"description": "Request failed. The incoming data specification seems malformed"
},
"404": {
"description": "Request failed. The specified workflow does not exist.",
"examples": {
"application/json": {
"message": "Workflow cdcf48b1-c2f3-4693-8230-b066e088c6ac does not exist"
}
},
"schema": {
"properties": {
"message": {
"type": "string"
}
},
"type": "object"
}
},
"500": {
"description": "Request failed. Internal controller error."
}
Expand Down Expand Up @@ -510,10 +526,10 @@
"description": "Request failed. The incoming data specification seems malformed."
},
"404": {
"description": "Request failed. User doesn't exist.",
"description": "Request failed. Workflow does not exist.",
"examples": {
"application/json": {
"message": "User 00000000-0000-0000-0000-000000000000 doesn't exist"
"message": "Workflow 256b25f4-4cfb-4684-b7a8-73872ef455a1 does not exist."
}
}
},
Expand Down Expand Up @@ -585,10 +601,10 @@
"description": "Request failed. The incoming data specification seems malformed."
},
"404": {
"description": "Request failed. User doesn't exist.",
"description": "Request failed. Workflow does not exist.",
"examples": {
"application/json": {
"message": "User 00000000-0000-0000-0000-000000000000 doesn't exist"
"message": "Workflow 256b25f4-4cfb-4684-b7a8-73872ef455a1 does not exist."
}
}
},
Expand Down
58 changes: 37 additions & 21 deletions reana_workflow_controller/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,25 @@ def seed_workflow_workspace(workflow_id):
examples:
application/json:
{
"message": "The file input.csv has been successfully
transferred.",
"message": "input.csv has been successfully transferred.",
}
400:
description: >-
Request failed. The incoming data specification seems malformed
404:
description: >-
Request failed. The specified workflow does not exist.
schema:
type: object
properties:
message:
type: string
examples:
application/json:
{
"message": "Workflow cdcf48b1-c2f3-4693-8230-b066e088c6ac does
not exist",
}
500:
description: >-
Request failed. Internal controller error.
Expand All @@ -353,14 +366,20 @@ def seed_workflow_workspace(workflow_id):
file_ = request.files['file_content']
file_name = secure_filename(request.args['file_name'])
if not file_name:
raise ValueError('The file transferred needs to have name.')
raise ValueError('A file name should be provided.')

workflow = Workflow.query.filter(Workflow.id_ == workflow_id).first()
file_.save(os.path.join(current_app.config['SHARED_VOLUME_PATH'],
workflow.workspace_path,
current_app.config['INPUTS_RELATIVE_PATH'],
file_name))
return jsonify({'message': 'File successfully transferred'}), 200
if workflow:
file_.save(os.path.join(current_app.config['SHARED_VOLUME_PATH'],
workflow.workspace_path,
current_app.config['INPUTS_RELATIVE_PATH'],
file_name))
return jsonify(
{'message': '{0} has been successfully trasferred.'.
format(file_name)}), 200
else:
return jsonify({'message': 'Workflow {0} does not exist.'.
format(workflow_id)}), 404
except KeyError as e:
return jsonify({"message": str(e)}), 400
except ValueError as e:
Expand Down Expand Up @@ -443,9 +462,6 @@ def get_workflow_outputs_file(workflow_id, file_name): # noqa
current_app.config['SHARED_VOLUME_PATH'],
workflow.workspace_path,
'outputs')
# fix, we don't know wich encoding is being used
# check how to add it to HTTP headers with `send_from_directory`
# or `send_file`
return send_from_directory(outputs_directory,
file_name,
mimetype='multipart/form-data',
Expand Down Expand Up @@ -511,12 +527,12 @@ def get_workflow_inputs(workflow_id): # noqa
Request failed. The incoming data specification seems malformed.
404:
description: >-
Request failed. User doesn't exist.
Request failed. Workflow does not exist.
examples:
application/json:
{
"message": "User 00000000-0000-0000-0000-000000000000 doesn't
exist"
"message": "Workflow 256b25f4-4cfb-4684-b7a8-73872ef455a1 does
not exist."
}
500:
description: >-
Expand Down Expand Up @@ -544,7 +560,7 @@ def get_workflow_inputs(workflow_id): # noqa
outputs_list = list_directory_files(outputs_directory)
return jsonify(outputs_list), 200
else:
return jsonify({'message': 'The workflow {} doesn\'t exist'.
return jsonify({'message': 'Workflow {} does not exist.'.
format(str(workflow.id_))}), 404

except KeyError:
Expand Down Expand Up @@ -604,12 +620,12 @@ def get_workflow_outputs(workflow_id): # noqa
Request failed. The incoming data specification seems malformed.
404:
description: >-
Request failed. User doesn't exist.
Request failed. Workflow does not exist.
examples:
application/json:
{
"message": "User 00000000-0000-0000-0000-000000000000 doesn't
exist"
"message": "Workflow 256b25f4-4cfb-4684-b7a8-73872ef455a1 does
not exist."
}
500:
description: >-
Expand Down Expand Up @@ -637,7 +653,7 @@ def get_workflow_outputs(workflow_id): # noqa
outputs_list = list_directory_files(outputs_directory)
return jsonify(outputs_list), 200
else:
return jsonify({'message': 'The workflow {} doesn\'t exist'.
return jsonify({'message': 'Workflow {} does not exist.'.
format(str(workflow.id_))}), 404

except KeyError:
Expand Down Expand Up @@ -1029,7 +1045,7 @@ def set_workflow_status(workflow_id): # noqa
application/json:
{
"message": "Workflow 256b25f4-4cfb-4684-b7a8-73872ef455a1
doesn't exist"
does not exist"
}
500:
description: >-
Expand All @@ -1045,7 +1061,7 @@ def set_workflow_status(workflow_id): # noqa
return jsonify({'message': 'Status {0} is not one of: {1}'.
format(status, ", ".join(STATUSES))}), 400
if not workflow:
return jsonify({'message': 'Workflow {} does not exist'.
return jsonify({'message': 'Workflow {} does not exist.'.
format(workflow_id)}), 404
if not str(workflow.owner_id) == user_uuid:
return jsonify(
Expand Down
24 changes: 21 additions & 3 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,6 @@ def test_seed_workflow_workspace(app, db_session, default_user,
# create file
file_name = 'dataset.csv'
file_binary_content = b'1,2,3,4\n5,6,7,8'
absolute_path_workflow_workspace = \
os.path.join(tmp_shared_volume_path,
workflow.workspace_path)

res = client.post(
url_for('api.seed_workflow_workspace', workflow_id=workflow_uuid),
Expand All @@ -664,3 +661,24 @@ def test_seed_workflow_workspace(app, db_session, default_user,

with open(file_path, 'rb') as f:
assert f.read() == file_binary_content


def test_seed_unknown_workflow_workspace(app, db_session, default_user,
tmp_shared_volume_path):
"""Test download output file."""
with app.test_client() as client:
random_workflow_uuid = uuid.uuid4()
# create file
file_name = 'dataset.csv'
file_binary_content = b'1,2,3,4\n5,6,7,8'

res = client.post(
url_for('api.seed_workflow_workspace',
workflow_id=random_workflow_uuid),
query_string={"user": default_user.id_,
"organization": "default",
"file_name": file_name},
content_type='multipart/form-data',
data={'file_content': (io.BytesIO(file_binary_content),
file_name)})
assert res.status_code == 404

0 comments on commit 494f38a

Please sign in to comment.