From c474cbb4b778f3741d74de192e5983dd684114c9 Mon Sep 17 00:00:00 2001 From: takenory Date: Thu, 4 Apr 2019 23:38:12 +0900 Subject: [PATCH 4/4] news updating via api --- app/controllers/news_controller.rb | 20 ++++-- 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 37c6e5b8d..f3c8a63d5 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 32ea7bcbb..02aeb6cbc 100644 --- a/test/integration/api_test/news_test.rb +++ b/test/integration/api_test/news_test.rb @@ -264,6 +264,118 @@ JSON assert_equal 2, news.attachments.count end + test "PUT /news/:id.xml" do + payload = <<-XML + + + NewsUpdateXmlApiTest + News Update XML-API Test + update description via xml api + +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 + + + News Update XML-API with attachments + + + #{token1} + test1.txt + + + #{token2} + test2.txt + + + +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') -- 2.14.2