diff options
-rw-r--r-- | app/controllers/news_controller.rb | 20 | ||||
-rw-r--r-- | test/integration/api_test/news_test.rb | 112 |
2 files changed, 126 insertions, 6 deletions
diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index 3ea0abea7..5d63f449a 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -26,7 +26,7 @@ class NewsController < ApplicationController before_action :authorize, :except => [:index] before_action :find_optional_project, :only => :index accept_rss_auth :index - accept_api_auth :index, :show, :create, :destroy + accept_api_auth :index, :show, :create, :update, :destroy helper :watchers helper :attachments @@ -94,13 +94,21 @@ class NewsController < ApplicationController def update @news.safe_attributes = params[:news] - @news.save_attachments(params[:attachments]) + @news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads])) if @news.save - render_attachment_warning_if_needed(@news) - flash[:notice] = l(:notice_successful_update) - redirect_to news_path(@news) + respond_to do |format| + format.html { + render_attachment_warning_if_needed(@news) + flash[:notice] = l(:notice_successful_update) + redirect_to news_path(@news) + } + format.api { render_api_ok } + end else - render :action => 'edit' + respond_to do |format| + format.html { render :action => 'edit' } + format.api { render_validation_errors(@news) } + end end end diff --git a/test/integration/api_test/news_test.rb b/test/integration/api_test/news_test.rb index 0ec7824d2..a6ca416a8 100644 --- a/test/integration/api_test/news_test.rb +++ b/test/integration/api_test/news_test.rb @@ -264,6 +264,118 @@ class Redmine::ApiTest::NewsTest < Redmine::ApiTest::Base assert_equal 2, news.attachments.count end + test "PUT /news/:id.xml" do + payload = <<~XML + <?xml version="1.0" encoding="UTF-8" ?> + <news> + <title>NewsUpdateXmlApiTest</title> + <summary>News Update XML-API Test</summary> + <description>update description via xml api</description> + </news> + XML + put '/news/1.xml', + :params => payload, + :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith')) + + news = News.find(1) + assert_equal 'NewsUpdateXmlApiTest', news.title + assert_equal 'News Update XML-API Test', news.summary + assert_equal 'update description via xml api', news.description + end + + test "PUT /news/:id.json" do + payload = <<~JSON + { + "news": { + "title": "NewsUpdateJsonApiTest", + "summary": "News Update JSON-API Test", + "description": "update description via json api" + } + } + JSON + + put '/news/1.json', + :params => payload, + :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')) + + news = News.find(1) + assert_equal 'NewsUpdateJsonApiTest', news.title + assert_equal 'News Update JSON-API Test', news.summary + assert_equal 'update description via json api', news.description + end + + test "PUT /news/:id.xml with failed update" do + put '/news/1.xml', + :params => {:news => {:title => ''}}, + :headers => credentials('jsmith') + + assert_response :unprocessable_entity + assert_select 'errors error', :text => "Title cannot be blank" + end + + test "PUT /news/:id.json with failed update" do + put '/news/1.json', + :params => {:news => {:title => ''}}, + :headers => credentials('jsmith') + + assert_response :unprocessable_entity + json = ActiveSupport::JSON.decode(response.body) + assert json['errors'].include?("Title cannot be blank") + end + + test "PUT /news/:id.xml with multiple attachment should update a news with attachments" do + token1 = xml_upload('File content 1', credentials('jsmith')) + token2 = xml_upload('File content 2', credentials('jsmith')) + payload = <<~XML + <?xml version="1.0" encoding="UTF-8" ?> + <news> + <title>News Update XML-API with attachments</title> + <uploads type="array"> + <upload> + <token>#{token1}</token> + <filename>test1.txt</filename> + </upload> + <upload> + <token>#{token2}</token> + <filename>test2.txt</filename> + </upload> + </uploads> + </news> + XML + + put '/news/1.xml', + :params => payload, + :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith')) + assert_response :no_content + + news = News.find_by(:title => 'News Update XML-API with attachments') + assert_equal 2, news.attachments.count + end + + test "PUT /news/:id.json with multiple attachment should update a news with attachments" do + token1 = json_upload('File content 1', credentials('jsmith')) + token2 = json_upload('File content 2', credentials('jsmith')) + payload = <<~JSON + { + "news": { + "title": "News Update JSON-API with attachments", + "uploads": [ + {"token": "#{token1}", "filename": "test1.txt"}, + {"token": "#{token2}", "filename": "test2.txt"} + ] + } + } + JSON + + put '/news/1.json', + :params => payload, + :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith')) + assert_response :no_content + + news = News.find_by(:title => 'News Update JSON-API with attachments') + assert_equal 2, news.attachments.count + end + test "DELETE /news/:id.xml" do assert_difference('News.count', -1) do delete '/news/1.xml', :headers => credentials('jsmith') |