fix #1224. Allow existing and non-existing project/context names in REST API

This commit is contained in:
Reinier Balt 2011-11-21 15:24:29 +01:00
parent 960326efb4
commit 50bc2623d9
2 changed files with 43 additions and 3 deletions

View file

@ -313,7 +313,9 @@ class Todo < ActiveRecord::Base
if value.is_a? Context
self.original_context=(value)
else
self.original_context=(Context.create(value))
c = Context.find_by_name(value[:name])
c = Context.create(value) if c.nil?
self.original_context=(c)
end
end
@ -326,8 +328,10 @@ class Todo < ActiveRecord::Base
def project=(value)
if value.is_a? Project
self.original_project=(value)
elsif !value.nil?
self.original_project=(Project.create(value))
elsif !(value.nil? || value.is_a?(NullProject))
p = Project.find_by_name(value[:name])
p = Project.create(value) if p.nil?
self.original_project=(p)
else
self.original_project=value
end

View file

@ -139,6 +139,24 @@ class TodoXmlApiTest < ActionController::IntegrationTest
assert_equal todo.context.name, "@SomeNewContext"
end
def test_post_create_todo_with_name_of_existing_context
authenticated_post_xml_to_todo_create "
<todo>
<description>this will succeed 4</description>
<project_id>#{projects(:timemachine).id}</project_id>
<context>
<name>#{contexts(:office).name}</name>
</context>
</todo>"
assert_response :success
todo = @user.todos.find_by_description("this will succeed 4")
assert_not_nil todo
assert_not_nil todo.context
assert_equal contexts(:office).name, todo.context.name
end
def test_post_create_todo_with_new_project
authenticated_post_xml_to_todo_create "
<todo>
@ -156,6 +174,24 @@ class TodoXmlApiTest < ActionController::IntegrationTest
assert_equal todo.project.name, "Make even more money"
end
def test_post_create_todo_with_name_of_existing_project
authenticated_post_xml_to_todo_create "
<todo>
<description>this will succeed 5</description>
<context_id>#{contexts(:office).id}</context_id>
<project>
<name>#{projects(:timemachine).name}</name>
</project>
</todo>"
assert_response :success
todo = @user.todos.find_by_description("this will succeed 5")
assert_not_nil todo
assert_not_nil todo.project
assert_equal projects(:timemachine).name, todo.project.name
assert 1, @user.projects.all(:conditions => ["projects.name = ?", projects(:timemachine).name]).count # no duplication of project
end
def test_post_create_todo_with_wrong_project_and_context_id
authenticated_post_xml_to_todo_create "
<todo>