Skip to content

Commit

Permalink
Merge 494f38a into 08f2eef
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego committed Jan 15, 2018
2 parents 08f2eef + 494f38a commit fc3cd0d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 35 deletions.
32 changes: 24 additions & 8 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 Expand Up @@ -652,10 +668,10 @@
"description": "Request failed. The incoming data specification seems malformed."
},
"404": {
"description": "Request failed. User doesn't exist.",
"description": "Request failed. `file_name` does not exist.",
"examples": {
"application/json": {
"message": "User 00000000-0000-0000-0000-000000000000 doesn't exist"
"message": "input.csv does not exist"
}
}
},
Expand Down
67 changes: 43 additions & 24 deletions reana_workflow_controller/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from flask import (Blueprint, abort, current_app, jsonify, request,
send_from_directory)
from werkzeug.exceptions import NotFound
from werkzeug.utils import secure_filename

from .factory import db
Expand Down Expand Up @@ -338,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 @@ -352,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 @@ -415,12 +435,11 @@ def get_workflow_outputs_file(workflow_id, file_name): # noqa
Request failed. The incoming data specification seems malformed.
404:
description: >-
Request failed. User doesn't exist.
Request failed. `file_name` does not exist.
examples:
application/json:
{
"message": "User 00000000-0000-0000-0000-000000000000 doesn't
exist"
"message": "input.csv does not exist"
}
500:
description: >-
Expand All @@ -443,16 +462,16 @@ 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',
as_attachment=True), 200

except KeyError:
return jsonify({"message": "Malformed request."}), 400
except NotFound as e:
return jsonify(
{"message": "{0} does not exist.".format(file_name)}), 404
except Exception as e:
return jsonify({"message": str(e)}), 500

Expand Down Expand Up @@ -508,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 @@ -541,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 @@ -601,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 @@ -634,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 @@ -1026,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 @@ -1042,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
59 changes: 56 additions & 3 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,41 @@ def test_create_workflow_wrong_user(app, db_session, tmp_shared_volume_path):
assert not os.path.exists(workflow_workspace)


def test_get_workflow_outputs_absent_file(app, db_session, default_user,
tmp_shared_volume_path):
"""Test download output file."""
with app.test_client() as client:
# create workflow
organization = 'default'
data = {'parameters': {'min_year': '1991',
'max_year': '2001'},
'specification': {'first': 'do this',
'second': 'do that'},
'type': 'cwl'}
res = client.post(url_for('api.create_workflow'),
query_string={
"user": default_user.id_,
"organization": organization},
content_type='application/json',
data=json.dumps(data))

assert res.status_code == 201
response_data = json.loads(res.get_data(as_text=True))
workflow_uuid = response_data.get('workflow_id')
file_name = 'input.csv'
res = client.get(
url_for('api.get_workflow_outputs_file', workflow_id=workflow_uuid,
file_name=file_name),
query_string={"user": default_user.id_,
"organization": organization},
content_type='application/json',
data=json.dumps(data))

assert res.status_code == 404
response_data = json.loads(res.get_data(as_text=True))
assert response_data == {'message': 'input.csv does not exist.'}


def test_get_workflow_outputs_file(app, db_session, default_user,
tmp_shared_volume_path):
"""Test download output file."""
Expand Down Expand Up @@ -603,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 @@ -629,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 fc3cd0d

Please sign in to comment.