From 0e54aafd8482c58802e931956e3aeec9aabf2adc Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 24 Feb 2011 11:29:59 -0300 Subject: [PATCH] Fixing create_enterprise task crash --- app/controllers/my_profile/tasks_controller.rb | 22 ++++++++++++---------- test/functional/tasks_controller_test.rb | 26 +++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index 81ea65c..9649917 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -16,16 +16,18 @@ class TasksController < MyProfileController def close failed = {} - params[:tasks].each do |id, value| - decision = value[:decision] - if request.post? && VALID_DECISIONS.include?(decision) && id && decision != 'skip' - task = profile.find_in_all_tasks(id) - task.update_attributes!(value[:task]) - begin - task.send(decision) - rescue Exception => ex - message = "#{task.title} (#{task.requestor ? task.requestor.name : task.author_name})" - failed[ex.clean_message] ? failed[ex.clean_message] << message : failed[ex.clean_message] = [message] + if params[:tasks] + params[:tasks].each do |id, value| + decision = value[:decision] + if request.post? && VALID_DECISIONS.include?(decision) && id && decision != 'skip' + task = profile.find_in_all_tasks(id) + begin + task.update_attributes(value[:task]) + task.send(decision) + rescue Exception => ex + message = "#{task.title} (#{task.requestor ? task.requestor.name : task.author_name})" + failed[ex.clean_message] ? failed[ex.clean_message] << message : failed[ex.clean_message] = [message] + end end end end diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 6e882dd..a4824f1 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -217,16 +217,18 @@ class TasksControllerTest < Test::Unit::TestCase assert_includes c_blog2.children(true), p_article end - should 'raise error if there is an enterprise with the same identifier and keep the task active' do + should 'display error if there is an enterprise with the same identifier and keep the task active' do e = Environment.default e.add_admin(profile) task = CreateEnterprise.create!(:name => "My Enterprise", :identifier => "my-enterprise", :requestor => profile, :target => e) enterprise = fast_create(Enterprise, :name => "My Enterprise", :identifier => "my-enterprise") - assert_raise ActiveRecord::RecordInvalid do - post :close, :tasks => {task.id => { :task => {:reject_explanation => "Bla bla"}, :decision => "cancel"}} + assert_nothing_raised do + post :close, :tasks => {task.id => {:decision => "finish"}} end + assert_match /Validation.failed/, @response.body + task.reload assert_equal Task::Status::ACTIVE, task.status end @@ -280,4 +282,22 @@ class TasksControllerTest < Test::Unit::TestCase assert_equal 'new source', TinyMceArticle.find(:first).source_name end + should "not crash if accessing close without tasks parameter" do + assert_nothing_raised do + post :close + end + end + + should 'close create enterprise if trying to cancel even if there is already an existing identifier' do + identifier = "common-identifier" + task = CreateEnterprise.create!(:identifier => identifier, :name => identifier, :requestor => profile, :target => profile) + fast_create(Profile, :identifier => identifier) + + assert_nothing_raised do + post :close, :tasks => {task.id => {:task => {:reject_explanation => "Some explanation"}, :decision => 'cancel'}} + end + + task.reload + assert_equal Task::Status::CANCELLED, task.status + end end -- libgit2 0.21.2