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
29 def test_create_quickvote
30 post(:create, {'commit' =>"Create Quickvote",
31 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}},
32 nil, {:candidate_names=>["foo", "bar", "foobar"]})
33 assert_template "quickvote/success"
34 get :index, { 'ident' => "variable"}
35 assert_response :success
38 #def test_create_dupe_quickvote
39 # test_create_quickvote
40 # assert_raise(Test::Unit::AssertionFailedError) do
41 # test_create_quickvote
45 def test_create_quickvote_badname
46 post(:create, {'commit' => "Create Quickvote",
47 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}},
48 nil, {:candidate_names => ["foo", "bar", "foobar"]})
49 assert_template "quickvote/_create_sidebar"
52 def test_create_quickvote_dupe_candidate
53 post(:create, {'commit' => "Create Quickvote",
54 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}},
55 nil, {:candidate_names => ["foo", "bar", "bar", "foobar"]})
56 assert_template "quickvote/_create_sidebar"
59 # TODO these should be testing for something better than a non-method
61 def test_create_quickvote_nil_candidate
62 assert_raise(NoMethodError) do
63 post(:create, {'commit' => "Create Quickvote",
64 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}},
65 nil, {:candidate_names => nil})
68 assert_raise(NoMethodError) do
69 post(:create, {'commit' => "Create Quickvote",
70 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}},
71 nil, {:candidate_names => []})
75 def test_get_quickvote_nonexistent
76 get :index, { 'ident' => "idontexist" }
77 assert_redirected_to :controller => 'front'
80 def test_get_result_empty_vote
82 get :results, { 'ident' => 'variable' }
83 assert_response :success
86 def test_get_result_nonexistent
88 get :results, { 'ident' => 'asdflaksdjf' }
89 assert_redirected_to :controller => 'front'
92 def test_get_result_with_a_vote
94 get :results, { 'ident' => 'variable' }
95 assert_response :success
98 def test_get_result_multi_votes
100 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
102 get :index, { 'ident' => 'variable' }, { 'test_session_id' => (time+1)*50 }
103 assert_response :success
105 { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} },
106 { 'test_session_id' => (time+1)*50 }
107 assert_template 'quickvote/thanks'
109 get :results, { 'ident' => 'variable' }
110 assert_response :success
113 def test_cast_quickvote
114 test_create_quickvote
115 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
116 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
117 assert_template 'quickvote/thanks'
120 def test_cast_dupe_quickvote
121 test_create_quickvote
122 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
123 get :index, { 'ident' => 'variable' }
124 assert_response :success
126 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
127 assert_template 'quickvote/thanks'
129 get :index, { 'ident' => 'variable' }
130 assert_response :success
131 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
132 assert_redirected_to :controller => 'quickvote', :ident => 'variable'
135 def test_display_tainted_quickvote
136 # create quickvote with tainted data
137 test_create_quickvote
138 qv=QuickVote.ident_to_quickvote('variable')
139 qv.description="<object>foo</object>"
140 qv.candidate_names = ["<object>foo", "bar<object>", "<foobar>",
141 '<img src="foo" alt="bar" />']
144 # display the vote/index page and check for bad tags and the ability
145 # to make an image tag
146 get :index, { 'ident' => 'variable' }
147 assert_response :success
148 assert_no_tag :tag => "object"
149 assert_no_tag :tag => "foobar"
150 assert_tag :tag => "img",
151 :parent => { :tag => "li", :attributes => { :class => "moveable" } }
154 votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
155 post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
157 # check for bad/good tags
158 assert_template('quickvote/thanks')
159 assert_no_tag :tag => "object"
160 assert_no_tag :tag => "foobar"
161 assert_tag :tag => "img", :parent => { :tag => "li" }
163 # get the results page and check for good/bad tags
164 get :results, { 'ident' => 'variable' }
165 assert_response :success
166 assert_no_tag :tag => "object"
167 assert_no_tag :tag => "foobar"
168 assert_tag :tag => "img", :parent => { :tag => "li" }