added new tests and fixtures
[selectricity] / test / functional / quickvote_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'quickvote_controller'
3
4 class ActionController::TestSession
5   def session_id
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']
11     end
12   end
13 end
14 # Re-raise errors caught by the controller.
15 class QuickvoteController; def rescue_action(e) raise e end; end
16
17 class QuickvoteControllerTest < Test::Unit::TestCase
18   def setup
19     @controller = QuickvoteController.new
20     @request    = ActionController::TestRequest.new
21     @response   = ActionController::TestResponse.new
22   end
23
24   def test_index
25     get :index
26     assert_response 302
27   end
28
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
36   end
37
38   #def test_create_dupe_quickvote
39   #  test_create_quickvote
40   #  assert_raise(Test::Unit::AssertionFailedError) do
41   #    test_create_quickvote
42   #  end
43   #end
44
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"
50   end
51
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"
57   end
58  
59   # TODO these should be testing for something better than a non-method
60   # error!
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})
66     end
67     
68     assert_raise(NoMethodError) do 
69       post(:create, {'commit' => "Create Quickvote",
70         'quickvote' => {'name' => "has a space", 'description' => "Foobar"}},
71         nil, {:candidate_names => []})
72     end
73   end
74
75   def test_get_quickvote_nonexistent
76     get :index, { 'ident' => "idontexist" }
77     assert_redirected_to :controller => 'front'
78   end
79
80   def test_get_result_empty_vote
81     test_create_quickvote
82     get :results, { 'ident' => 'variable' }
83     assert_response :success
84   end
85
86   def test_get_result_nonexistent
87     test_create_quickvote
88     get :results, { 'ident' => 'asdflaksdjf' }
89     assert_redirected_to :controller => 'front'
90   end
91
92   def test_get_result_with_a_vote
93     test_cast_quickvote
94     get :results, { 'ident' => 'variable' }
95     assert_response :success
96   end
97
98   def test_get_result_multi_votes
99     test_create_quickvote
100     votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
101     5.times do |time|
102       get :index, { 'ident' => 'variable' }, { 'test_session_id'  =>  (time+1)*50 }
103       assert_response :success
104       post :confirm,
105         { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }, 
106         { 'test_session_id' => (time+1)*50 }
107       assert_template 'quickvote/thanks'
108     end
109     get :results, { 'ident' => 'variable' }
110     assert_response :success
111   end
112
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'
118   end
119
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
125
126     post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
127     assert_template 'quickvote/thanks'
128
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'
133   end
134
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" />']
142     qv.save!
143
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" } }
152
153     # actually vote
154     votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
155     post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
156
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" }
162
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" }
169   end
170 end

Benjamin Mako Hill || Want to submit a patch?