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')
|