From 6bec8101af26036b1e6dd9ace6b3c57fb660fb26 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Tue, 23 Oct 2007 07:05:37 +0000 Subject: [PATCH] Handled the negative case, when a todo failed to be created via XML API. Respond with 422 and xml-formatted validation errors. git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@625 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/todos_controller.rb | 7 +++++-- tracks/test/functional/todos_controller_test.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/tracks/app/controllers/todos_controller.rb b/tracks/app/controllers/todos_controller.rb index 958622f3..64f049db 100644 --- a/tracks/app/controllers/todos_controller.rb +++ b/tracks/app/controllers/todos_controller.rb @@ -95,8 +95,11 @@ class TodosController < ApplicationController render :action => 'create' end format.xml do - @todo.reload - render :xml => @todo.to_xml( :root => 'todo', :except => :user_id ), :status => 201 + if @saved + render :xml => @todo.to_xml( :root => 'todo', :except => :user_id ), :status => 201 + else + render :xml => @todo.errors.to_xml, :status => 422 + end end end end diff --git a/tracks/test/functional/todos_controller_test.rb b/tracks/test/functional/todos_controller_test.rb index d308d35b..f458e224 100644 --- a/tracks/test/functional/todos_controller_test.rb +++ b/tracks/test/functional/todos_controller_test.rb @@ -88,6 +88,16 @@ class TodosControllerTest < Test::Rails::TestCase assert_xml_select "id", /\d+/ end end + + def test_fail_to_create_todo_via_xml + login_as(:admin_user) + #try to create with no context, which is not valid + put :create, :format => "xml", "request" => { "project_name"=>"Build a working time machine", "todo"=>{"notes"=>"", "description"=>"Call Warren Buffet to find out how much he makes per day", "due"=>"30/11/2006"}, "tag_list"=>"foo bar" } + assert_response 422 + assert_xml_select "errors" do + assert_xml_select "error", "Context can't be blank" + end + end def test_create_deferred_todo original_todo_count = Todo.count