diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index ea45397..d7637aa 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -22,7 +22,7 @@ class AttachmentsController < ApplicationController before_filter :delete_authorize, :only => :destroy before_filter :authorize_global, :only => :upload - accept_api_auth :show, :download, :thumbnail, :upload, :destroy + accept_api_auth :show, :download, :thumbnail, :upload, :update, :destroy def show respond_to do |format| @@ -109,11 +109,17 @@ class AttachmentsController < ApplicationController def update if params[:attachments].is_a?(Hash) if Attachment.update_attachments(@attachments, params[:attachments]) - redirect_back_or_default home_path - return + respond_to do |format| + format.html { redirect_back_or_default home_path } + format.api { render_api_ok } + end + return end end - render :action => 'edit' + respond_to do |format| + format.html { render :action => 'edit' } + format.api { render_validation_errors(@attachments) } + end end def destroy diff --git a/test/integration/api_test/attachments_test.rb b/test/integration/api_test/attachments_test.rb index 640a782..f551a60 100644 --- a/test/integration/api_test/attachments_test.rb +++ b/test/integration/api_test/attachments_test.rb @@ -99,6 +99,46 @@ class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base assert_nil Attachment.find_by_id(7) end + test "PATCH /attachments/issues/:id.json should return ok and updated Attachment" do + token = json_upload('File content 1', credentials('jsmith')) + + payload = <<-JSON +{ + "issue": { + "project_id": "1", + "tracker_id": "1", + "subject": "Issue with attachment", + "uploads": [ + {"token": "#{token}", "filename": "test1.txt", "description": "test1"} + ] + } +} +JSON + + assert_difference 'Issue.count' do + post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')) + assert_response :created + end + + issue = Issue.order('id DESC').first + assert_equal 1, issue.attachments.count + + attachment = issue.attachments.first + attachment.filename = "test1_updated.txt" + attachment.description = "test1_updated" + + data = {:attachments => {attachment.id => {:filename => attachment.filename, :description => attachment.description}}} + patch "/attachments/issues/#{issue.id}.json", data, credentials('jsmith') + assert_response :ok + + updatedIssue = Issue.find(issue.id) + updatedAttachment = updatedIssue.attachments.first; + + assert_equal "Issue with attachment", updatedIssue.subject + assert_equal "test1_updated.txt", updatedAttachment.filename + assert_equal "test1_updated", updatedAttachment.description + end + test "POST /uploads.xml should return the token" do set_tmp_attachments_directory assert_difference 'Attachment.count' do