1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'quickvote_controller'
4 class ActionController::TestSession
6 # Override this so we can set session ID
7 # pass in the 'test_session_id' session variable to override default
8 return "12345678" unless @attributes
9 if @attributes.has_key? 'test_session_id'
10 return @attributes['test_session_id']
14 # Re-raise errors caught by the controller.
15 class QuickvoteController; def rescue_action(e) raise e end; end
17 class QuickvoteControllerTest < Test::Unit::TestCase
19 @controller = QuickvoteController.new
20 @request = ActionController::TestRequest.new
21 @response = ActionController::TestResponse.new
24 # Replace this with your real tests.
30 def test_create_quickvote
31 post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candlist=>["foo", "bar", "foobar"]})
32 assert_template "quickvote/success"
33 get :index, { 'ident' => "variable"}
34 assert_response :success
37 def test_create_dupe_quickvote
39 assert_raise(Test::Unit::AssertionFailedError) do
44 def test_create_quickvote_badname
45 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "foobar"]})
46 assert_template "quickvote/create"
49 def test_create_quickvote_dupe_candidate
50 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "bar", "foobar"]})
51 assert_template "quickvote/create"
54 def test_create_quickvote_nil_candidate
55 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => nil})
56 assert_template "quickvote/create"
57 post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => []})
58 assert_template "quickvote/create"
61 def test_get_quickvote_nonexistent
62 get :index, { 'ident' => "idontexist" }
63 assert_redirected_to :controller => 'site'
66 def test_get_result_empty_vote
68 get :results, { 'ident' => 'variable' }
69 assert_response :success
72 def test_get_result_nonexistent
74 get :results, { 'ident' => 'asdflaksdjf' }
75 assert_redirected_to :controller => 'site'
78 def test_get_result_with_a_vote
80 get :results, { 'ident' => 'variable' }
81 assert_response :success
84 def test_get_result_multi_votes
86 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
88 get :index, { 'ident' => 'variable' }, { 'test_session_id' => (time+1)*50 }
89 assert_response :success
90 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }, { 'test_session_id' => (time+1)*50 }
91 assert_template 'quickvote/thanks'
93 get :results, { 'ident' => 'variable' }
94 assert_response :success
97 def test_cast_quickvote
99 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
100 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
101 assert_template 'quickvote/thanks'
104 def test_cast_dupe_quickvote
105 test_create_quickvote
106 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
107 get :index, { 'ident' => 'variable' }
108 assert_response :success
110 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
111 assert_template 'quickvote/thanks'
113 get :index, { 'ident' => 'variable' }
114 assert_response :success
115 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
116 assert_redirected_to :controller => 'quickvote', :ident => 'variable'
118 def test_display_tainted_quickvote
119 test_create_quickvote
120 qv=QuickVote.ident_to_quickvote('variable')
121 qv.description="<object>foo</object>"
122 qv.candidatelist = ["<object>foo", "bar<object>", "<foobar>"]
124 get :index, { 'ident' => 'variable' }
125 assert_response :success
126 assert_no_tag :tag => "object"
127 assert_no_tag :tag => "foobar"
128 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
129 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
130 assert_template('quickvote/thanks')
131 assert_no_tag :tag => "object"
132 assert_no_tag :tag => "foobar"
133 get :results, { 'ident' => 'variable' }
134 assert_response :success
135 assert_no_tag :tag => "object"
136 assert_no_tag :tag => "foobar"