0004-news-updating-via-api.patch

Takenori TAKAKI, 2019-04-04 17:07

Download (5.65 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, :show, :create, :destroy
29
  accept_api_auth :index, :show, :create, :update, :destroy
30 30

  
31 31
  helper :watchers
32 32
  helper :attachments
......
94 94

  
95 95
  def update
96 96
    @news.safe_attributes = params[:news]
97
    @news.save_attachments(params[:attachments])
97
    @news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads]))
98 98
    if @news.save
99
      render_attachment_warning_if_needed(@news)
100
      flash[:notice] = l(:notice_successful_update)
101
      redirect_to news_path(@news)
99
      respond_to do |format|
100
        format.html {
101
          render_attachment_warning_if_needed(@news)
102
          flash[:notice] = l(:notice_successful_update)
103
          redirect_to news_path(@news)
104
        }
105
        format.api  { render_api_ok }
106
      end
102 107
    else
103
      render :action => 'edit'
108
      respond_to do |format|
109
        format.html { render :action => 'edit' }
110
        format.api  { render_validation_errors(@news) }
111
      end
104 112
    end
105 113
  end
106 114

  
test/integration/api_test/news_test.rb
264 264
    assert_equal 2, news.attachments.count
265 265
  end
266 266

  
267
  test "PUT /news/:id.xml" do
268
    payload = <<-XML
269
<?xml version="1.0" encoding="UTF-8" ?>
270
<news>
271
  <title>NewsUpdateXmlApiTest</title>
272
  <summary>News Update XML-API Test</summary>
273
  <description>update description via xml api</description>
274
</news>
275
XML
276
    put '/news/1.xml',
277
      :params => payload,
278
      :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
279

  
280
    news = News.find(1)
281
    assert_equal 'NewsUpdateXmlApiTest', news.title
282
    assert_equal 'News Update XML-API Test', news.summary
283
    assert_equal 'update description via xml api', news.description
284
  end
285

  
286
  test "PUT /news/:id.json" do
287
    payload = <<-JSON
288
{
289
  "news": {
290
    "title": "NewsUpdateJsonApiTest",
291
    "summary": "News Update JSON-API Test",
292
    "description": "update description via json api"
293
  }
294
}
295
JSON
296

  
297
    put '/news/1.json',
298
      :params => payload,
299
      :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
300

  
301
    news = News.find(1)
302
    assert_equal 'NewsUpdateJsonApiTest', news.title
303
    assert_equal 'News Update JSON-API Test', news.summary
304
    assert_equal 'update description via json api', news.description
305
  end
306

  
307
  test "PUT /news/:id.xml with failed update" do
308
    put '/news/1.xml',
309
      :params => {:news => {:title => ''}},
310
      :headers => credentials('jsmith')
311

  
312
    assert_response :unprocessable_entity
313
    assert_select 'errors error', :text => "Title cannot be blank"
314
  end
315

  
316
  test "PUT /news/:id.json with failed update" do
317
    put '/news/1.json',
318
      :params => {:news => {:title => ''}},
319
      :headers => credentials('jsmith')
320

  
321
    assert_response :unprocessable_entity
322
    json = ActiveSupport::JSON.decode(response.body)
323
    assert json['errors'].include?("Title cannot be blank")
324
  end
325

  
326
  test "PUT /news/:id.xml with multiple attachment should update a news with attachments" do
327
    token1 = xml_upload('File content 1', credentials('jsmith'))
328
    token2 = xml_upload('File content 2', credentials('jsmith'))
329
    payload = <<-XML
330
<?xml version="1.0" encoding="UTF-8" ?>
331
<news>
332
  <title>News Update XML-API with attachments</title>
333
  <uploads type="array">
334
    <upload>
335
      <token>#{token1}</token>
336
      <filename>test1.txt</filename>
337
    </upload>
338
    <upload>
339
      <token>#{token2}</token>
340
      <filename>test2.txt</filename>
341
    </upload>
342
  </uploads>
343
</news>
344
XML
345

  
346
    put '/news/1.xml',
347
      :params => payload,
348
      :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
349
    assert_response :no_content
350

  
351
    news = News.find_by(:title => 'News Update XML-API with attachments')
352
    assert_equal 2, news.attachments.count
353
  end
354

  
355
  test "PUT /news/:id.json with multiple attachment should update a news with attachments" do
356
    token1 = json_upload('File content 1', credentials('jsmith'))
357
    token2 = json_upload('File content 2', credentials('jsmith'))
358
    payload = <<-JSON
359
{
360
  "news": {
361
    "title": "News Update JSON-API with attachments",
362
    "uploads": [
363
      {"token": "#{token1}", "filename": "test1.txt"},
364
      {"token": "#{token2}", "filename": "test2.txt"}
365
    ]
366
  }
367
}
368
JSON
369

  
370
    put '/news/1.json',
371
      :params => payload,
372
      :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
373
    assert_response :no_content
374

  
375
    news = News.find_by(:title => 'News Update JSON-API with attachments')
376
    assert_equal 2, news.attachments.count
377
  end
378

  
267 379
  test "DELETE /news/:id.xml" do
268 380
    assert_difference('News.count', -1) do
269 381
      delete '/news/1.xml', :headers => credentials('jsmith')
270
-