Project

General

Profile

Patch #13468 » news-creating-via-api.patch

Takenori TAKAKI, 2019-03-29 07:33

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
29
  accept_api_auth :index, :create
30 30

  
31 31
  helper :watchers
32 32
  helper :attachments
......
71 71
  def create
72 72
    @news = News.new(:project => @project, :author => User.current)
73 73
    @news.safe_attributes = params[:news]
74
    @news.save_attachments(params[:attachments])
74
    @news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads]))
75 75
    if @news.save
76
      render_attachment_warning_if_needed(@news)
77
      flash[:notice] = l(:notice_successful_create)
78
      redirect_to project_news_index_path(@project)
76
      respond_to do |format|
77
        format.html {
78
          render_attachment_warning_if_needed(@news)
79
          flash[:notice] = l(:notice_successful_create)
80
          redirect_to project_news_index_path(@project)
81
        }
82
        format.api  { render_api_ok }
83
      end
79 84
    else
80
      render :action => 'new'
85
      respond_to do |format|
86
        format.html { render :action => 'new' }
87
        format.api  { render_validation_errors(@news) }
88
      end
81 89
    end
82 90
  end
83 91

  
test/integration/api_test/news_test.rb
60 60
    assert_kind_of Hash, json['news'].first
61 61
    assert_equal 2, json['news'].first['id']
62 62
  end
63

  
64
  test "POST /project/:project_id/news.xml should create a news with the attributes" do
65
    payload = <<-XML
66
<?xml version="1.0" encoding="UTF-8" ?>
67
<news>
68
  <title>NewsXmlApiTest</title>
69
  <summary>News XML-API Test</summary>
70
  <description>This is the description</description>
71
</news>
72
XML
73

  
74
    assert_difference('News.count') do
75
      post '/projects/1/news.xml',
76
        :params => payload,
77
        :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
78
    end
79
    news = News.find_by(:title => 'NewsXmlApiTest')
80
    assert_not_nil news
81
    assert_equal 'News XML-API Test', news.summary
82
    assert_equal 'This is the description', news.description
83
    assert_equal User.find_by_login('jsmith'), news.author
84
    assert_equal Project.find(1), news.project
85
    assert_response :no_content
86
  end
87

  
88
  test "POST /project/:project_id/news.xml with failure should return errors" do
89
    assert_no_difference('News.count') do
90
      post '/projects/1/news.xml',
91
        :params => {:news => {:title => ''}},
92
        :headers => credentials('jsmith')
93
    end
94
    assert_select 'errors error', :text => "Title cannot be blank"
95
  end
96

  
97
  test "POST /project/:project_id/news.json should create a news with the attributes" do
98
    payload = <<-JSON
99
{
100
  "news": {
101
    "title": "NewsJsonApiTest",
102
    "summary": "News JSON-API Test",
103
    "description": "This is the description"
104
  }
105
}
106
JSON
107

  
108
    assert_difference('News.count') do
109
      post '/projects/1/news.json',
110
        :params => payload,
111
        :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
112
    end
113
    news = News.find_by(:title => 'NewsJsonApiTest')
114
    assert_not_nil news
115
    assert_equal 'News JSON-API Test', news.summary
116
    assert_equal 'This is the description', news.description
117
    assert_equal User.find_by_login('jsmith'), news.author
118
    assert_equal Project.find(1), news.project
119
    assert_response :no_content
120
  end
121

  
122
  test "POST /project/:project_id/news.json with failure should return errors" do
123
    assert_no_difference('News.count') do
124
      post '/projects/1/news.json',
125
        :params => {:news => {:title => ''}},
126
        :headers => credentials('jsmith')
127
    end
128
    json = ActiveSupport::JSON.decode(response.body)
129
    assert json['errors'].include?("Title cannot be blank")
130
  end
131

  
132
  test "POST /project/:project_id/news.xml with attachment should create a news with attachment" do
133
    token = xml_upload('test_create_with_attachment', credentials('jsmith'))
134
    attachment = Attachment.find_by_token(token)
135

  
136
    assert_difference 'News.count' do
137
      post '/projects/1/news.xml',
138
        :params => {:news => {:title => 'News XML-API with Attachment',
139
                              :description => 'desc'},
140
                    :attachments => [{:token => token, :filename => 'test.txt',
141
                                      :content_type => 'text/plain'}]},
142
        :headers => credentials('jsmith')
143
      assert_response :no_content
144
    end
145
    news = News.find_by(:title => 'News XML-API with Attachment')
146
    assert_equal attachment, news.attachments.first
147

  
148
    attachment.reload
149
    assert_equal 'test.txt', attachment.filename
150
    assert_equal 'text/plain', attachment.content_type
151
    assert_equal 'test_create_with_attachment'.size, attachment.filesize
152
    assert_equal 2, attachment.author_id
153
  end
154

  
155
  test "POST /project/:project_id/news.xml with multiple attachment should create a news with attachments" do
156
    token1 = xml_upload('File content 1', credentials('jsmith'))
157
    token2 = xml_upload('File content 2', credentials('jsmith'))
158
    payload = <<-XML
159
<?xml version="1.0" encoding="UTF-8" ?>
160
<news>
161
  <title>News XML-API with attachments</title>
162
  <description>News with multiple attachments</description>
163
  <uploads type="array">
164
    <upload>
165
      <token>#{token1}</token>
166
      <filename>test1.txt</filename>
167
    </upload>
168
    <upload>
169
      <token>#{token2}</token>
170
      <filename>test2.txt</filename>
171
    </upload>
172
  </uploads>
173
</news>
174
XML
175

  
176
    assert_difference('News.count') do
177
      post '/projects/1/news.xml',
178
        :params => payload,
179
        :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
180
      assert_response :no_content
181
    end
182
    news = News.find_by(:title => 'News XML-API with attachments')
183
    assert_equal 2, news.attachments.count
184
  end
185

  
186
  test "POST /project/:project_id/json.xml with multiple attachment should create a news with attachments" do
187
    token1 = json_upload('File content 1', credentials('jsmith'))
188
    token2 = json_upload('File content 2', credentials('jsmith'))
189
    payload = <<-JSON
190
{
191
  "news": {
192
    "title": "News JSON-API with attachments",
193
    "description": "News with multiple attachments",
194
    "uploads": [
195
      {"token": "#{token1}", "filename": "test1.txt"},
196
      {"token": "#{token2}", "filename": "test2.txt"}
197
    ]
198
  }
199
}
200
JSON
201

  
202
    assert_difference('News.count') do
203
      post '/projects/1/news.json',
204
        :params => payload,
205
        :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
206
      assert_response :no_content
207
    end
208
    news = News.find_by(:title => 'News JSON-API with attachments')
209
    assert_equal 2, news.attachments.count
210
  end
63 211
end
(2-2/6)