From c22469838d33250a1615352accaa0ff9d4e59d04 Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Mon, 9 Sep 2019 08:52:13 +0000 Subject: REST API for creating news (#13468). Patch by Takenori TAKAKI. git-svn-id: http://svn.redmine.org/redmine/trunk@18440 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- test/integration/api_test/news_test.rb | 148 +++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) (limited to 'test/integration') diff --git a/test/integration/api_test/news_test.rb b/test/integration/api_test/news_test.rb index 0d33e5545..b1b436ad0 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/news.json 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 -- cgit v1.2.3