merged changes back from live
[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   # Replace this with your real tests.
25   def test_index
26     get :index
27     assert_response 302
28   end
29
30   def test_create_quickvote
31     post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candidate_names=>["foo", "bar", "foobar"]})
32     assert_template "quickvote/success"
33     get :index, { 'ident' => "variable"}
34     assert_response :success
35   end
36
37   def test_create_dupe_quickvote
38     test_create_quickvote
39     assert_raise(Test::Unit::AssertionFailedError) do
40       test_create_quickvote
41     end
42   end
43
44   def test_create_quickvote_badname
45     post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => ["foo", "bar", "foobar"]})
46     assert_template "quickvote/_create_sidebar"
47   end
48
49   def test_create_quickvote_dupe_candidate
50     post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => ["foo", "bar", "bar",  "foobar"]})
51     assert_template "quickvote/_create_sidebar"
52   end
53   
54   def test_create_quickvote_nil_candidate
55     post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => nil})
56     assert_template "quickvote/_create_sidebar"
57     post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candidate_names => []})
58     assert_template "quickvote/_create_sidebar"
59   end
60
61   def test_get_quickvote_nonexistent
62     get :index, { 'ident' => "idontexist" }
63     assert_redirected_to :controller => 'front'
64   end
65
66   def test_get_result_empty_vote
67     test_create_quickvote
68     get :results, { 'ident' => 'variable' }
69     assert_response :success
70   end
71
72   def test_get_result_nonexistent
73     test_create_quickvote
74     get :results, { 'ident' => 'asdflaksdjf' }
75     assert_redirected_to :controller => 'front'
76   end
77
78   def test_get_result_with_a_vote
79     test_cast_quickvote
80     get :results, { 'ident' => 'variable' }
81     assert_response :success
82   end
83
84   def test_get_result_multi_votes
85     test_create_quickvote
86     votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
87     5.times do |time|
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'
92     end
93     get :results, { 'ident' => 'variable' }
94     assert_response :success
95   end
96
97   def test_cast_quickvote
98     test_create_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'
102   end
103
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
109
110     post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
111     assert_template 'quickvote/thanks'
112
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'
117   end
118
119   def test_display_tainted_quickvote
120     # create quickvote with tainted data
121     test_create_quickvote
122     qv=QuickVote.ident_to_quickvote('variable')
123     qv.description="<object>foo</object>"
124     qv.candidate_names = ["<object>foo", "bar<object>", "<foobar>",
125                           '<img src="foo" alt="bar" />']
126     qv.save!
127
128     # display the vote/index page and check for bad tags and the ability
129     # to make an image tag
130     get :index, { 'ident' => 'variable' }
131     assert_response :success
132     assert_no_tag :tag => "object"
133     assert_no_tag :tag => "foobar"
134     assert_tag :tag => "img",
135                :parent => { :tag => "li", :attributes => { :class => "moveable" } }
136
137     # actually vote
138     votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
139     post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
140
141     # check for bad/good tags
142     assert_template('quickvote/thanks')
143     assert_no_tag :tag => "object"
144     assert_no_tag :tag => "foobar"
145     assert_tag :tag => "img", :parent => { :tag => "li" }
146
147     # get the results page and check for good/bad tags
148     get :results, { 'ident' => 'variable' }
149     assert_response :success
150     assert_no_tag :tag => "object"
151     assert_no_tag :tag => "foobar"
152     assert_tag :tag => "img", :parent => { :tag => "li" }
153   end
154 end

Benjamin Mako Hill || Want to submit a patch?