summaryrefslogtreecommitdiffstats
path: root/test/integration
diff options
context:
space:
mode:
authorGo MAEDA <maeda@farend.jp>2019-09-09 08:52:13 +0000
committerGo MAEDA <maeda@farend.jp>2019-09-09 08:52:13 +0000
commitc22469838d33250a1615352accaa0ff9d4e59d04 (patch)
tree704e230ada27d48e371087aba28cd677c57d0729 /test/integration
parent0ccc4158f5596c5e7f5b8a417c35ffb1d408682d (diff)
downloadredmine-c22469838d33250a1615352accaa0ff9d4e59d04.tar.gz
redmine-c22469838d33250a1615352accaa0ff9d4e59d04.zip
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
Diffstat (limited to 'test/integration')
-rw-r--r--test/integration/api_test/news_test.rb148
1 files changed, 148 insertions, 0 deletions
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
+ <?xml version="1.0" encoding="UTF-8" ?>
+ <news>
+ <title>NewsXmlApiTest</title>
+ <summary>News XML-API Test</summary>
+ <description>This is the description</description>
+ </news>
+ 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
+ <?xml version="1.0" encoding="UTF-8" ?>
+ <news>
+ <title>News XML-API with attachments</title>
+ <description>News with multiple attachments</description>
+ <uploads type="array">
+ <upload>
+ <token>#{token1}</token>
+ <filename>test1.txt</filename>
+ </upload>
+ <upload>
+ <token>#{token2}</token>
+ <filename>test2.txt</filename>
+ </upload>
+ </uploads>
+ </news>
+ 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