]> source.dussan.org Git - redmine.git/commitdiff
REST API for updating news (#13468).
authorGo MAEDA <maeda@farend.jp>
Mon, 9 Sep 2019 09:03:41 +0000 (09:03 +0000)
committerGo MAEDA <maeda@farend.jp>
Mon, 9 Sep 2019 09:03:41 +0000 (09:03 +0000)
Patch by Takenori TAKAKI.

git-svn-id: http://svn.redmine.org/redmine/trunk@18443 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/news_controller.rb
test/integration/api_test/news_test.rb

index 3ea0abea7f51039f93b6f2224012988933d41138..5d63f449a0062d12baa35bf424f8b90bc7d0d70a 100644 (file)
@@ -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
 
index 0ec7824d27845a9b22f0d5a7447bd0b48724e6eb..a6ca416a8e4045ee280ff126c1c1cec870024f7d 100644 (file)
@@ -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')