From 3dc9b777150da2a4349682fa82f50c7d75b59f8e Mon Sep 17 00:00:00 2001 From: Diego Rodriguez Date: Fri, 12 Jan 2018 13:38:32 +0100 Subject: [PATCH] api: return 404 when file does not exist --- docs/openapi.json | 4 ++-- reana_workflow_controller/rest.py | 11 +++++---- tests/test_views.py | 37 ++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/docs/openapi.json b/docs/openapi.json index 704aca65..f8a685a0 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -652,10 +652,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" } } }, diff --git a/reana_workflow_controller/rest.py b/reana_workflow_controller/rest.py index 0d7278dd..75ab0564 100644 --- a/reana_workflow_controller/rest.py +++ b/reana_workflow_controller/rest.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of REANA. -# Copyright (C) 2017 CERN. +# Copyright (C) 2017, 2018 CERN. # # REANA is free software; you can redistribute it and/or modify it under the # terms of the GNU General Public License as published by the Free Software @@ -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 @@ -413,12 +414,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: >- @@ -451,6 +451,9 @@ def get_workflow_outputs_file(workflow_id, file_name): # noqa 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 diff --git a/tests/test_views.py b/tests/test_views.py index af9e597d..01326755 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of REANA. -# Copyright (C) 2017 CERN. +# Copyright (C) 2017, 2018 CERN. # # REANA is free software; you can redistribute it and/or modify it under the # terms of the GNU General Public License as published by the Free Software @@ -180,6 +180,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."""