From abbdb7454440dcbe1d290888e6c4ef1b1896cb01 Mon Sep 17 00:00:00 2001 From: takenory Date: Thu, 4 Apr 2019 23:33:01 +0900 Subject: [PATCH 2/4] news getting via api --- app/controllers/news_controller.rb | 2 +- app/views/news/show.api.rsb | 24 ++++++++++++++ test/integration/api_test/news_test.rb | 57 +++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 app/views/news/show.api.rsb diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index 2e97b6ce9..7bf828741 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, :create + accept_api_auth :index, :show, :create helper :watchers helper :attachments diff --git a/app/views/news/show.api.rsb b/app/views/news/show.api.rsb new file mode 100644 index 000000000..0dd3a3b78 --- /dev/null +++ b/app/views/news/show.api.rsb @@ -0,0 +1,24 @@ +api.news do + api.id @news.id + api.project(:id => @news.project_id, :name => @news.project.name) unless @news.project.nil? + api.author(:id => @news.author_id, :name => @news.author.name) unless @news.author.nil? + api.title @news.title + api.summary @news.summary unless @news.summary.blank? + api.description @news.description + api.created_on @news.created_on + + api.array :attachments do + @news.attachments.each do |attachment| + render_api_attachment(attachment, api) + end + end if include_in_api_response?('attachments') + + api.array :comments do + @comments.each do |comment| + api.comment :id => comment.id do + api.author(:id => comment.author_id, :name => comment.author.name) unless comment.author.nil? + api.content comment.content + end + end + end if include_in_api_response?('comments') +end diff --git a/test/integration/api_test/news_test.rb b/test/integration/api_test/news_test.rb index de8aa9f52..f3e6c76f1 100644 --- a/test/integration/api_test/news_test.rb +++ b/test/integration/api_test/news_test.rb @@ -27,7 +27,8 @@ class Redmine::ApiTest::NewsTest < Redmine::ApiTest::Base :member_roles, :members, :enabled_modules, - :news + :news, + :comments test "GET /news.xml should return news" do get '/news.xml' @@ -61,6 +62,60 @@ class Redmine::ApiTest::NewsTest < Redmine::ApiTest::Base assert_equal 2, json['news'].first['id'] end + test "GET /news/:id.xml" do + get "/news/1.xml" + assert_response :success + assert_equal 'application/xml', response.content_type + assert_select 'news' do + assert_select 'id', 1 + assert_select "project[id=1][name=\"eCookbook\"]" + assert_select "author[id=2][name=\"John Smith\"]" + assert_select 'title', 'eCookbook first release !' + assert_select 'summary', 'First version was released...' + assert_select 'description', "eCookbook 1.0 has been released.\n\nVisit http://ecookbook.somenet.foo/" + iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/ + assert_select 'news>created_on', :text => iso_date + end + end + + test "GET /news/:id.json" do + get "/news/1.json" + assert_response :success + assert_equal 'application/json', response.content_type + json = ActiveSupport::JSON.decode(response.body) + assert_equal 1, json['news']['id'] + end + + test "GET /news/:id.xml with attachments" do + news = News.find(1) + attachment = Attachment.first + attachment.container = news + attachment.save! + + get "/news/1.xml?include=attachments" + assert_select 'news attachments[type=array]' do + assert_select 'attachment id', :text => '1' do + assert_select '~ filename', :text => 'error281.txt' + assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt' + end + end + end + + test "GET /news/:id.xml with comments" do + get "/news/1.xml?include=comments" + assert_select 'news comments[type=array]' do + assert_select 'comment', 2 + assert_select 'comment[id=1]' do + assert_select 'author[id=1][name="Redmine Admin"]' + assert_select 'content', :text => 'my first comment' + end + assert_select 'comment[id=2]' do + assert_select 'author[id=2][name="John Smith"]' + assert_select 'content', :text => 'This is an other comment' + end + end + end + test "POST /project/:project_id/news.xml should create a news with the attributes" do payload = <<-XML -- 2.14.2