From 5ff315dac6d354ee5508c95aab1607e0ce4a1b82 Mon Sep 17 00:00:00 2001 From: Stefan Richter Date: Thu, 6 Oct 2011 20:28:58 +0200 Subject: [PATCH 1/6] adding tests for creating todos via REST --- test/integration/todo_xml_api_test.rb | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/integration/todo_xml_api_test.rb b/test/integration/todo_xml_api_test.rb index c7892490..f1128c5e 100644 --- a/test/integration/todo_xml_api_test.rb +++ b/test/integration/todo_xml_api_test.rb @@ -49,6 +49,42 @@ class TodoXmlApiTest < ActionController::IntegrationTest assert_response :success assert_equal @user.todos.count, old_count + 1 end + + def test_post_create_todo_with_dependencies + old_count = @user.todos.count + authenticated_post_xml_to_todo_create " + + this will succeed 2 + 10 + 4 + + 12 + + + 12 + +" + + assert_response :success + assert_equal @user.todos.count, old_count + 1 + end + + def test_post_create_todo_with_tags + old_count = @user.todos.count + authenticated_post_xml_to_todo_create " + + this will succeed 2 + 10 + 4 + + starred + + " + + puts @response.body + assert_response :success + assert_equal @user.todos.count, old_count + 1 + end def test_post_create_todo_with_wrong_project_and_context_id authenticated_post_xml_to_todo_create "this will fail-16-11" From f08e73c81972fefc562dd9b710639cda11dd9c0c Mon Sep 17 00:00:00 2001 From: Stefan Richter Date: Thu, 6 Oct 2011 20:29:49 +0200 Subject: [PATCH 2/6] fixing todo creation when supplying dependencies --- app/models/todo.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/models/todo.rb b/app/models/todo.rb index 06b23699..db5a7453 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -143,6 +143,19 @@ class Todo < ActiveRecord::Base end end + def predecessor_dependencies=(params) + value = params[:predecessor_dependencies] + if !value.nil? + if value.class == Array + value.each do |attrs| + predecessor_dependencies.build(attrs) + end + else + predecessor_dependencies.build(value) + end + end + end + def save_predecessors unless @predecessor_array.nil? # Only save predecessors if they changed current_array = self.predecessors @@ -177,6 +190,19 @@ class Todo < ActiveRecord::Base self.activate! end + def successor_dependencies=(params) + value = params[:successor_dependencies] + if !value.nil? + if value.class == Array + value.each do |attrs| + successor_dependencies.build(attrs) + end + else + successor_dependencies.build(value) + end + end + end + # Returns true if t is equal to self or a successor of self def is_successor?(todo) if self == todo From 3180164ed044061d26ba50348df3a5c7252631b7 Mon Sep 17 00:00:00 2001 From: Stefan Richter Date: Sun, 9 Oct 2011 20:35:50 +0200 Subject: [PATCH 3/6] indentation fix --- test/integration/todo_xml_api_test.rb | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/integration/todo_xml_api_test.rb b/test/integration/todo_xml_api_test.rb index f1128c5e..20171f99 100644 --- a/test/integration/todo_xml_api_test.rb +++ b/test/integration/todo_xml_api_test.rb @@ -69,22 +69,22 @@ class TodoXmlApiTest < ActionController::IntegrationTest assert_equal @user.todos.count, old_count + 1 end - def test_post_create_todo_with_tags - old_count = @user.todos.count - authenticated_post_xml_to_todo_create " - - this will succeed 2 - 10 - 4 - - starred - - " + def test_post_create_todo_with_tags + old_count = @user.todos.count + authenticated_post_xml_to_todo_create " + + this will succeed 2 + 10 + 4 + + starred + +" - puts @response.body - assert_response :success - assert_equal @user.todos.count, old_count + 1 - end + puts @response.body + assert_response :success + assert_equal @user.todos.count, old_count + 1 + end def test_post_create_todo_with_wrong_project_and_context_id authenticated_post_xml_to_todo_create "this will fail-16-11" From 9eae8a7068f156b64fa00a60ac994fc986887d27 Mon Sep 17 00:00:00 2001 From: Stefan Richter Date: Mon, 10 Oct 2011 22:25:51 +0200 Subject: [PATCH 4/6] improving todo creation tests and implement context, project and tag setting/creation --- app/models/todo.rb | 86 ++++++++++++++++++--------- test/integration/todo_xml_api_test.rb | 68 +++++++++++++++------ 2 files changed, 110 insertions(+), 44 deletions(-) diff --git a/app/models/todo.rb b/app/models/todo.rb index db5a7453..fe31417c 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -143,19 +143,6 @@ class Todo < ActiveRecord::Base end end - def predecessor_dependencies=(params) - value = params[:predecessor_dependencies] - if !value.nil? - if value.class == Array - value.each do |attrs| - predecessor_dependencies.build(attrs) - end - else - predecessor_dependencies.build(value) - end - end - end - def save_predecessors unless @predecessor_array.nil? # Only save predecessors if they changed current_array = self.predecessors @@ -190,19 +177,6 @@ class Todo < ActiveRecord::Base self.activate! end - def successor_dependencies=(params) - value = params[:successor_dependencies] - if !value.nil? - if value.class == Array - value.each do |attrs| - successor_dependencies.build(attrs) - end - else - successor_dependencies.build(value) - end - end - end - # Returns true if t is equal to self or a successor of self def is_successor?(todo) if self == todo @@ -314,6 +288,8 @@ class Todo < ActiveRecord::Base end def add_predecessor(t) + return if t.nil? + @predecessor_array = predecessors @predecessor_array << t end @@ -335,7 +311,63 @@ class Todo < ActiveRecord::Base def raw_notes=(value) self[:notes] = value end - + + # XML API fixups + def predecessor_dependencies=(params) + value = params[:predecessor] + + if !value.nil? + if value.class == Array + value.each do |ele| + if ele.is_a? String + add_predecessor(self.user.todos.find_by_id(ele.to_i)) unless ele.blank? + else + predecessor_dependencies.build(value) + end + end + else + if ele.is_a? String + add_predecessor(self.user.todos.find_by_id(ele.to_i)) unless ele.blank? + else + predecessor_dependencies.build(value) + end + end + end + end + + alias_method :original_context=, :context= + def context=(value) + if value.is_a? Context + self.original_context=(value) + else + self.original_context=(Context.create(value)) + end + end + + alias_method :original_project=, :project= + def project=(value) + if value.is_a? Project + self.original_project=(value) + else + self.original_project=(Project.create(value)) + end + end + + alias_method :original_tags=, :tags= + def tags=(params) + value = params[:tag] + + if !value.nil? + if value.class == Array + value.each do |attrs| + tags.build(attrs) + end + else + tags.build(value) + end + end + end + # Rich Todo API def self.from_rich_message(user, default_context_id, description, notes) diff --git a/test/integration/todo_xml_api_test.rb b/test/integration/todo_xml_api_test.rb index 20171f99..21da3fdb 100644 --- a/test/integration/todo_xml_api_test.rb +++ b/test/integration/todo_xml_api_test.rb @@ -8,8 +8,8 @@ class TodoXmlApiTest < ActionController::IntegrationTest def setup assert_test_environment_ok - @user = users(:other_user) - @password = 'sesame' + @user = users(:admin_user) + @password = 'abracadabra' end def test_get_tickler_succeeds @@ -51,39 +51,73 @@ class TodoXmlApiTest < ActionController::IntegrationTest end def test_post_create_todo_with_dependencies - old_count = @user.todos.count authenticated_post_xml_to_todo_create " this will succeed 2 - 10 - 4 + 8 + 1 - 12 + 5 + 6 - - 12 - " assert_response :success - assert_equal @user.todos.count, old_count + 1 + todo = @user.todos.find_by_description("this will succeed 2") + assert_not_nil todo + assert !todo.uncompleted_predecessors.empty? end def test_post_create_todo_with_tags - old_count = @user.todos.count authenticated_post_xml_to_todo_create " - this will succeed 2 - 10 - 4 + this will succeed 3 + 8 + 1 starred + starred2 " - puts @response.body assert_response :success - assert_equal @user.todos.count, old_count + 1 + todo = @user.todos.find_by_description("this will succeed 3") + assert_not_nil todo + assert !todo.starred? + end + + def test_post_create_todo_with_new_context + authenticated_post_xml_to_todo_create " + + this will succeed 4 + 1 + + @SomeNewContext + +" + + assert_response :success + todo = @user.todos.find_by_description("this will succeed 4") + assert_not_nil todo + assert_not_nil todo.context + assert_equal todo.context.name, "@SomeNewContext" + end + + def test_post_create_todo_with_new_context + authenticated_post_xml_to_todo_create " + + this will succeed 5 + 8 + + Make even more money + +" + + assert_response :success + todo = @user.todos.find_by_description("this will succeed 5") + assert_not_nil todo + assert_not_nil todo.project + assert_equal todo.project.name, "Make even more money" end def test_post_create_todo_with_wrong_project_and_context_id @@ -101,7 +135,7 @@ class TodoXmlApiTest < ActionController::IntegrationTest private - def authenticated_post_xml_to_todo_create(postdata = @@valid_postdata, user = users(:other_user).login, password = 'sesame') + def authenticated_post_xml_to_todo_create(postdata = @@valid_postdata, user = @user.login, password = @password) authenticated_post_xml "/todos", user, password, postdata end From c5dd35de80f0e4f7b3132115d8bc4bdde26cec4b Mon Sep 17 00:00:00 2001 From: Stefan Richter Date: Wed, 12 Oct 2011 14:55:05 +0200 Subject: [PATCH 5/6] remove alias_method for tags= ... tags now broken again, but no longer the whole todo model --- app/models/todo.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/todo.rb b/app/models/todo.rb index fe31417c..dd309d94 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -353,7 +353,6 @@ class Todo < ActiveRecord::Base end end - alias_method :original_tags=, :tags= def tags=(params) value = params[:tag] From 081a55d6ac18bab5d117ec396365cd9013881c6e Mon Sep 17 00:00:00 2001 From: Stefan Richter Date: Sun, 13 Nov 2011 21:39:04 +0100 Subject: [PATCH 6/6] Fixup tests --- test/integration/todo_xml_api_test.rb | 33 +++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/test/integration/todo_xml_api_test.rb b/test/integration/todo_xml_api_test.rb index 21da3fdb..6b080a41 100644 --- a/test/integration/todo_xml_api_test.rb +++ b/test/integration/todo_xml_api_test.rb @@ -25,10 +25,10 @@ class TodoXmlApiTest < ActionController::IntegrationTest assert_response 401 end - def test_get_tickler_returns_all_deferred_todos - number = @user.todos.deferred.count + def test_get_tickler_returns_all_deferred_and_pending_todos + number = @user.todos.deferred.count + @user.todos.pending.count authenticated_get_xml "/tickler", @user.login, @password, {} - assert_tag :tag => "todos", :children => { :count => number, :only => { :tag => "todo" } } + assert_tag :tag => "todos", :children => { :count => number } end def test_get_tickler_omits_user_id @@ -36,13 +36,13 @@ class TodoXmlApiTest < ActionController::IntegrationTest assert_no_tag :tag => "user_id" end - def test_create_todo_via_xml_show_from + def test_create_todo_with_show_from old_count = @user.todos.count authenticated_post_xml_to_todo_create " Call Warren Buffet to find out how much he makes per day - #{projects(:attendrailsconf).id} - #{contexts(:office_otheruser).id} + #{contexts(:office).id} + #{projects(:timemachine).id} #{1.week.from_now.xmlschema} " @@ -54,8 +54,8 @@ class TodoXmlApiTest < ActionController::IntegrationTest authenticated_post_xml_to_todo_create " this will succeed 2 - 8 - 1 + #{contexts(:office).id} + #{projects(:timemachine).id} 5 6 @@ -72,8 +72,8 @@ class TodoXmlApiTest < ActionController::IntegrationTest authenticated_post_xml_to_todo_create " this will succeed 3 - 8 - 1 + #{contexts(:office).id} + #{projects(:timemachine).id} starred starred2 @@ -90,7 +90,7 @@ class TodoXmlApiTest < ActionController::IntegrationTest authenticated_post_xml_to_todo_create " this will succeed 4 - 1 + #{projects(:timemachine).id} @SomeNewContext @@ -103,11 +103,11 @@ class TodoXmlApiTest < ActionController::IntegrationTest assert_equal todo.context.name, "@SomeNewContext" end - def test_post_create_todo_with_new_context + def test_post_create_todo_with_new_project authenticated_post_xml_to_todo_create " this will succeed 5 - 8 + #{contexts(:office).id} Make even more money @@ -121,7 +121,12 @@ class TodoXmlApiTest < ActionController::IntegrationTest end def test_post_create_todo_with_wrong_project_and_context_id - authenticated_post_xml_to_todo_create "this will fail-16-11" + authenticated_post_xml_to_todo_create " + + this will fail + -16 + -11 +" assert_response 422 assert_xml_select 'errors' do assert_select 'error', 2