2011-05-31 23:07:20 +02:00
require File . expand_path ( File . dirname ( __FILE__ ) + '/../test_helper' )
2007-03-30 04:36:52 +00:00
# Re-raise errors caught by the controller.
class BackendController ; def rescue_action ( e ) raise e end ; end
2009-06-02 21:22:50 +02:00
class BackendControllerTest < ActionController :: TestCase
2009-01-23 14:15:02 -05:00
fixtures :users , :projects , :contexts , :todos , :recurring_todos , :notes
2007-03-30 04:36:52 +00:00
def setup
@controller = BackendController . new
request , response = ActionController :: TestRequest . new , ActionController :: TestResponse . new
assert_equal " change-me " , Tracks :: Config . salt
end
def test_new_todo_fails_with_incorrect_token
2007-09-26 12:05:21 +00:00
assert_raises_invalid_token { @controller . new_todo ( 'admin' , 'notthecorrecttoken' , contexts ( 'agenda' ) . id , 'test' , 'test' ) }
2007-03-30 04:36:52 +00:00
end
def test_new_todo_fails_with_context_that_does_not_belong_to_user
2007-09-26 12:05:21 +00:00
assert_raise ( CannotAccessContext , " Cannot access a context that does not belong to this user. " ) { @controller . new_todo ( users ( 'other_user' ) . login , users ( 'other_user' ) . token , contexts ( 'agenda' ) . id , 'test' , 'test' ) }
2007-03-30 04:36:52 +00:00
end
def test_new_rich_todo_fails_with_incorrect_token
2007-09-26 12:05:21 +00:00
assert_raises_invalid_token { @controller . new_rich_todo ( 'admin' , 'notthecorrecttoken' , contexts ( 'agenda' ) . id , 'test' , 'test' ) }
2007-03-30 04:36:52 +00:00
end
#"Call mfox @call > Build a working time machine" should create the "Call mfox" todo in the 'call' context and the 'Build a working time machine' project.
def test_new_rich_todo_creates_todo_with_exact_match
assert_new_rich_todo_creates_mfox_todo ( " Call mfox @call > Build a working time machine " )
end
#"Call mfox @cal > Build" should create the "Call mfox" todo in the 'call' context and the 'Build a working time machine' project.
def test_new_rich_todo_creates_todo_with_starts_with_match
assert_new_rich_todo_creates_mfox_todo ( " Call mfox @cal > Build " )
end
#"Call mfox @call > new:Run for president" should create the 'Run for president' project, create the "Call mfox" todo in the 'call' context and the new project.
def test_new_rich_todo_creates_todo_with_new_project
max_todo_id = Todo . maximum ( 'id' )
max_project_id = Project . maximum ( 'id' )
2007-09-26 12:05:21 +00:00
@controller . new_rich_todo ( users ( :admin_user ) . login , users ( :admin_user ) . token , contexts ( :agenda ) . id , 'Call mfox @call > new:Run for president' , 'test' )
2007-03-30 04:36:52 +00:00
todo = Todo . find ( :first , :conditions = > [ " id > ? " , max_todo_id ] )
new_project = Project . find ( :first , :conditions = > [ " id > ? " , max_project_id ] )
assert_equal ( users ( :admin_user ) . id , todo . user_id )
assert_equal ( contexts ( :call ) . id , todo . context_id )
assert_equal ( new_project . id , todo . project_id )
assert_equal ( " Call mfox " , todo . description )
2007-09-26 12:05:21 +00:00
assert_equal ( " test " , todo . notes )
2007-03-30 04:36:52 +00:00
end
def assert_new_rich_todo_creates_mfox_todo ( description_input )
max_id = Todo . maximum ( 'id' )
2007-09-26 12:05:21 +00:00
@controller . new_rich_todo ( users ( :admin_user ) . login , users ( :admin_user ) . token , contexts ( :agenda ) . id , 'Call mfox @cal > Build' , 'test' )
2007-03-30 04:36:52 +00:00
todo = Todo . find ( :first , :conditions = > [ " id > ? " , max_id ] )
assert_equal ( users ( :admin_user ) . id , todo . user_id )
assert_equal ( contexts ( :call ) . id , todo . context_id )
assert_equal ( projects ( :timemachine ) . id , todo . project_id )
2007-09-26 12:05:21 +00:00
assert_equal ( 'test' , todo . notes )
2007-03-30 04:36:52 +00:00
assert_equal ( " Call mfox " , todo . description )
end
def test_new_rich_todo_fails_with_context_that_does_not_belong_to_user
2007-09-26 12:05:21 +00:00
assert_raise ( CannotAccessContext , " Cannot access a context that does not belong to this user. " ) { @controller . new_rich_todo ( users ( 'other_user' ) . login , users ( 'other_user' ) . token , contexts ( 'agenda' ) . id , 'test' , 'test' ) }
2007-03-30 04:36:52 +00:00
end
def test_list_projects_fails_with_incorrect_token
assert_raises_invalid_token { @controller . list_projects ( 'admin' , 'notthecorrecttoken' ) }
end
def test_list_contexts_fails_with_incorrect_token
assert_raises_invalid_token { @controller . list_contexts ( 'admin' , 'notthecorrecttoken' ) }
end
private
def assert_raises_invalid_token
assert_raise ( InvalidToken , " Sorry, you don't have permission to perform this action. " ) { yield }
end
end