From c35c2af5e90336fc0c6cab74f7635f729a682d8a Mon Sep 17 00:00:00 2001 From: Lucile Quirion Date: Fri, 13 Feb 2015 16:21:37 -0500 Subject: [PATCH] REST API: add files REST API This commit add the REST API for: - getting the list of files for a project and its versions. - creating new files from uploaded attachments. Issue: #7725 --- app/controllers/files_controller.rb | 13 ++- app/views/files/index.api.rsb | 26 ++++++ config/routes.rb | 1 + test/integration/api_test/api_routing_test.rb | 6 ++ test/integration/api_test/files_test.rb | 128 ++++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 app/views/files/index.api.rsb create mode 100644 test/integration/api_test/files_test.rb diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index 253d73b..cbf6d5b 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -20,6 +20,7 @@ class FilesController < ApplicationController before_filter :find_project_by_project_id before_filter :authorize + accept_api_auth :index, :create helper :sort include SortHelper @@ -35,7 +36,11 @@ class FilesController < ApplicationController references(:attachments).reorder(sort_clause).find(@project.id)] @containers += @project.versions.includes(:attachments). references(:attachments).reorder(sort_clause).to_a.sort.reverse - render :layout => !request.xhr? + respond_to do |format| + format.html { render :layout => !request.xhr? } + format.api + end + end def new @@ -50,6 +55,10 @@ class FilesController < ApplicationController if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added') Mailer.attachments_added(attachments[:files]).deliver end - redirect_to project_files_path(@project) + + respond_to do |format| + format.html { redirect_to project_files_path(@project) } + format.api { render_api_ok } + end end end diff --git a/app/views/files/index.api.rsb b/app/views/files/index.api.rsb new file mode 100644 index 0000000..0282950 --- /dev/null +++ b/app/views/files/index.api.rsb @@ -0,0 +1,26 @@ +api.array :files do + @containers.each do |container| + next if container.attachments.empty? + if container.is_a?(Version) + version_id = container.name + end + + container.attachments.each do |file| + api.file do + api.id file.id + + api.filename file.filename + api.author_id file.author_id + api.description file.description + api.content_type file.content_type + + api.created_on file.created_on + api.filesize file.filesize + api.digest file.digest + + api.downloads file.downloads + api.version_id version_id unless version_id.nil? + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 4b9cc77..262a587 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -125,6 +125,7 @@ Rails.application.routes.draw do get 'versions.:format', :to => 'versions#index' get 'roadmap', :to => 'versions#index', :format => false get 'versions', :to => 'versions#index' + post 'versions/:version_id/files.:format' => 'files#create' resources :news, :except => [:show, :edit, :update, :destroy] resources :time_entries, :controller => 'timelog', :except => [:show, :edit, :update, :destroy] do diff --git a/test/integration/api_test/api_routing_test.rb b/test/integration/api_test/api_routing_test.rb index 4fcd9ec..0c5ec00 100644 --- a/test/integration/api_test/api_routing_test.rb +++ b/test/integration/api_test/api_routing_test.rb @@ -32,6 +32,12 @@ class Redmine::ApiTest::ApiRoutingTest < Redmine::ApiTest::Routing should_route 'GET /enumerations/issue_priorities' => 'enumerations#index', :type => 'issue_priorities' end + def test_files + should_route 'GET /projects/foo/files' => 'files#index', :project_id => 'foo' + should_route 'POST /projects/foo/files' => 'files#create', :project_id => 'foo' + should_route 'POST /projects/foo/versions/123/files' => 'files#create', :project_id => 'foo', :version_id => '123' + end + def test_groups should_route 'GET /groups' => 'groups#index' should_route 'POST /groups' => 'groups#create' diff --git a/test/integration/api_test/files_test.rb b/test/integration/api_test/files_test.rb new file mode 100644 index 0000000..585fb1a --- /dev/null +++ b/test/integration/api_test/files_test.rb @@ -0,0 +1,128 @@ +# Redmine - project management software +# Copyright (C) 2015 Savoir-Faire Linux Inc. +# Author: Lucile Quirion +# +# This program 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 Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.expand_path('../../../test_helper', __FILE__) + +class Redmine::ApiTest::FilesTest < Redmine::ApiTest::Base + fixtures :projects, + :users, + :members, + :roles, + :member_roles, + :enabled_modules, + :attachments, + :versions + + test "GET /projects/:project_id/files.xml should return the list of uploaded files" do + get '/projects/1/files.xml', {}, credentials('jsmith') + assert_response :success + assert_select 'files file id', :text => '8' + end + + test "POST /projects/:project_id/files.json should create a file" do + set_tmp_attachments_directory + post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) + token = Attachment.last.token + payload = <<-JSON +{ "attachments": + { "file_1": + {"token": "#{token}" } + } +} + JSON + post '/projects/1/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')) + assert_response :success + assert_equal 1, Attachment.last.container_id + assert_equal "Project", Attachment.last.container_type + end + + test "POST /projects/:project_id/files.xml should create a file" do + set_tmp_attachments_directory + post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) + token = Attachment.last.token + payload = <<-XML + + + #{token} + + + XML + post '/projects/1/files.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith')) + assert_response :success + assert_equal 1, Attachment.last.container_id + assert_equal "Project", Attachment.last.container_type + end + + test "POST /projects/:project_id/files.json should accept :filename, :description, :content_type as parameters" do + set_tmp_attachments_directory + post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) + token = Attachment.last.token + payload = <<-JSON +{ "attachments": + { "file_1": + { "filename": "New filename", + "description": "New description", + "content_type": "application/txt", + "token": "#{token}" + } + } +} + JSON + post '/projects/1/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')) + assert_response :success + assert_equal "New filename", Attachment.last.filename + assert_equal "New description", Attachment.last.description + assert_equal "application/txt", Attachment.last.content_type + end + + test "POST /projects/:project_id/versions/:version_id/files.json should attach an attachment to a version" do + set_tmp_attachments_directory + post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) + token = Attachment.last.token + payload = <<-JSON +{ "attachments": + { "1": + { "token": "#{token}" } + } +} + JSON + post '/projects/1/versions/3/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')) + assert_equal 3, Attachment.last.container_id + assert_equal "Version", Attachment.last.container_type + end + + test "POST /projects/:project_id/files.json should accept :version_id to attach the file to a version" do + set_tmp_attachments_directory + post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith')) + token = Attachment.last.token + payload = <<-JSON +{ "version_id": 3, + "attachments": + { "1": + { "filename": "New filename", + "description": "New description", + "token": "#{token}" + } + } +} + JSON + post '/projects/1/files.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')) + assert_equal 3, Attachment.last.container_id + assert_equal "Version", Attachment.last.container_type + end +end -- 2.1.0