0002-news-getting-via-api.patch

Takenori TAKAKI, 2019-04-04 17:07

Download (4.71 KB)

View differences:

app/controllers/news_controller.rb
26 26
  before_action :authorize, :except => [:index]
27 27
  before_action :find_optional_project, :only => :index
28 28
  accept_rss_auth :index
29
  accept_api_auth :index, :create
29
  accept_api_auth :index, :show, :create
30 30

  
31 31
  helper :watchers
32 32
  helper :attachments
app/views/news/show.api.rsb
1
api.news do
2
  api.id @news.id
3
  api.project(:id => @news.project_id, :name => @news.project.name) unless @news.project.nil?
4
  api.author(:id => @news.author_id, :name => @news.author.name) unless @news.author.nil?
5
  api.title @news.title
6
  api.summary @news.summary unless @news.summary.blank?
7
  api.description @news.description
8
  api.created_on @news.created_on
9

  
10
  api.array :attachments do
11
    @news.attachments.each do |attachment|
12
      render_api_attachment(attachment, api)
13
    end
14
  end if include_in_api_response?('attachments')
15

  
16
  api.array :comments do
17
    @comments.each do |comment|
18
      api.comment :id => comment.id do
19
        api.author(:id => comment.author_id, :name => comment.author.name) unless comment.author.nil?
20
        api.content comment.content
21
      end
22
    end
23
  end if include_in_api_response?('comments')
24
end
test/integration/api_test/news_test.rb
27 27
           :member_roles,
28 28
           :members,
29 29
           :enabled_modules,
30
           :news
30
           :news,
31
           :comments
31 32

  
32 33
  test "GET /news.xml should return news" do
33 34
    get '/news.xml'
......
61 62
    assert_equal 2, json['news'].first['id']
62 63
  end
63 64

  
65
  test "GET /news/:id.xml" do
66
    get "/news/1.xml"
67
    assert_response :success
68
    assert_equal 'application/xml', response.content_type
69
    assert_select 'news' do
70
      assert_select 'id', 1
71
      assert_select "project[id=1][name=\"eCookbook\"]"
72
      assert_select "author[id=2][name=\"John Smith\"]"
73
      assert_select 'title', 'eCookbook first release !'
74
      assert_select 'summary', 'First version was released...'
75
      assert_select 'description', "eCookbook 1.0 has been released.\n\nVisit http://ecookbook.somenet.foo/"
76
      iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
77
      assert_select 'news>created_on', :text => iso_date
78
    end
79
  end
80

  
81
  test "GET /news/:id.json" do
82
    get "/news/1.json"
83
    assert_response :success
84
    assert_equal 'application/json', response.content_type
85
    json = ActiveSupport::JSON.decode(response.body)
86
    assert_equal 1, json['news']['id']
87
  end
88

  
89
  test "GET /news/:id.xml with attachments" do
90
    news = News.find(1)
91
    attachment = Attachment.first
92
    attachment.container = news
93
    attachment.save!
94

  
95
    get "/news/1.xml?include=attachments"
96
    assert_select 'news attachments[type=array]' do
97
      assert_select 'attachment id', :text => '1' do
98
        assert_select '~ filename', :text => 'error281.txt'
99
        assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt'
100
      end
101
    end
102
  end
103

  
104
  test "GET /news/:id.xml with comments" do
105
    get "/news/1.xml?include=comments"
106
    assert_select 'news comments[type=array]' do
107
      assert_select 'comment', 2
108
      assert_select 'comment[id=1]' do
109
        assert_select 'author[id=1][name="Redmine Admin"]'
110
        assert_select 'content', :text => 'my first comment'
111
      end
112
      assert_select 'comment[id=2]' do
113
        assert_select 'author[id=2][name="John Smith"]'
114
        assert_select 'content', :text => 'This is an other comment'
115
      end
116
    end
117
  end
118

  
64 119
  test "POST /project/:project_id/news.xml should create a news with the attributes" do
65 120
    payload = <<-XML
66 121
<?xml version="1.0" encoding="UTF-8" ?>
67
-