diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb
index 6bc59fa0b..2e97b6ce9 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
+ accept_api_auth :index, :create
helper :watchers
helper :attachments
@@ -71,13 +71,21 @@ class NewsController < ApplicationController
def create
@news = News.new(:project => @project, :author => User.current)
@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_create)
- redirect_to project_news_index_path(@project)
+ respond_to do |format|
+ format.html {
+ render_attachment_warning_if_needed(@news)
+ flash[:notice] = l(:notice_successful_create)
+ redirect_to project_news_index_path(@project)
+ }
+ format.api { render_api_ok }
+ end
else
- render :action => 'new'
+ respond_to do |format|
+ format.html { render :action => 'new' }
+ 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 1489ddd5f..6c43548bb 100644
--- a/test/integration/api_test/news_test.rb
+++ b/test/integration/api_test/news_test.rb
@@ -60,4 +60,152 @@ class Redmine::ApiTest::NewsTest < Redmine::ApiTest::Base
assert_kind_of Hash, json['news'].first
assert_equal 2, json['news'].first['id']
end
+
+ test "POST /project/:project_id/news.xml should create a news with the attributes" do
+ payload = <<-XML
+
+
+ NewsXmlApiTest
+ News XML-API Test
+ This is the description
+
+XML
+
+ assert_difference('News.count') do
+ post '/projects/1/news.xml',
+ :params => payload,
+ :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
+ end
+ news = News.find_by(:title => 'NewsXmlApiTest')
+ assert_not_nil news
+ assert_equal 'News XML-API Test', news.summary
+ assert_equal 'This is the description', news.description
+ assert_equal User.find_by_login('jsmith'), news.author
+ assert_equal Project.find(1), news.project
+ assert_response :no_content
+ end
+
+ test "POST /project/:project_id/news.xml with failure should return errors" do
+ assert_no_difference('News.count') do
+ post '/projects/1/news.xml',
+ :params => {:news => {:title => ''}},
+ :headers => credentials('jsmith')
+ end
+ assert_select 'errors error', :text => "Title cannot be blank"
+ end
+
+ test "POST /project/:project_id/news.json should create a news with the attributes" do
+ payload = <<-JSON
+{
+ "news": {
+ "title": "NewsJsonApiTest",
+ "summary": "News JSON-API Test",
+ "description": "This is the description"
+ }
+}
+JSON
+
+ assert_difference('News.count') do
+ post '/projects/1/news.json',
+ :params => payload,
+ :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
+ end
+ news = News.find_by(:title => 'NewsJsonApiTest')
+ assert_not_nil news
+ assert_equal 'News JSON-API Test', news.summary
+ assert_equal 'This is the description', news.description
+ assert_equal User.find_by_login('jsmith'), news.author
+ assert_equal Project.find(1), news.project
+ assert_response :no_content
+ end
+
+ test "POST /project/:project_id/news.json with failure should return errors" do
+ assert_no_difference('News.count') do
+ post '/projects/1/news.json',
+ :params => {:news => {:title => ''}},
+ :headers => credentials('jsmith')
+ end
+ json = ActiveSupport::JSON.decode(response.body)
+ assert json['errors'].include?("Title cannot be blank")
+ end
+
+ test "POST /project/:project_id/news.xml with attachment should create a news with attachment" do
+ token = xml_upload('test_create_with_attachment', credentials('jsmith'))
+ attachment = Attachment.find_by_token(token)
+
+ assert_difference 'News.count' do
+ post '/projects/1/news.xml',
+ :params => {:news => {:title => 'News XML-API with Attachment',
+ :description => 'desc'},
+ :attachments => [{:token => token, :filename => 'test.txt',
+ :content_type => 'text/plain'}]},
+ :headers => credentials('jsmith')
+ assert_response :no_content
+ end
+ news = News.find_by(:title => 'News XML-API with Attachment')
+ assert_equal attachment, news.attachments.first
+
+ attachment.reload
+ assert_equal 'test.txt', attachment.filename
+ assert_equal 'text/plain', attachment.content_type
+ assert_equal 'test_create_with_attachment'.size, attachment.filesize
+ assert_equal 2, attachment.author_id
+ end
+
+ test "POST /project/:project_id/news.xml with multiple attachment should create a news with attachments" do
+ token1 = xml_upload('File content 1', credentials('jsmith'))
+ token2 = xml_upload('File content 2', credentials('jsmith'))
+ payload = <<-XML
+
+
+ News XML-API with attachments
+ News with multiple attachments
+
+
+ #{token1}
+ test1.txt
+
+
+ #{token2}
+ test2.txt
+
+
+
+XML
+
+ assert_difference('News.count') do
+ post '/projects/1/news.xml',
+ :params => payload,
+ :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
+ assert_response :no_content
+ end
+ news = News.find_by(:title => 'News XML-API with attachments')
+ assert_equal 2, news.attachments.count
+ end
+
+ test "POST /project/:project_id/json.xml with multiple attachment should create 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 JSON-API with attachments",
+ "description": "News with multiple attachments",
+ "uploads": [
+ {"token": "#{token1}", "filename": "test1.txt"},
+ {"token": "#{token2}", "filename": "test2.txt"}
+ ]
+ }
+}
+JSON
+
+ assert_difference('News.count') do
+ post '/projects/1/news.json',
+ :params => payload,
+ :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
+ assert_response :no_content
+ end
+ news = News.find_by(:title => 'News JSON-API with attachments')
+ assert_equal 2, news.attachments.count
+ end
end