Merge jdong; mostly functional testing work
[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, {:candlist=>["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, {:candlist => ["foo", "bar", "foobar"]})
46     assert_template "quickvote/create"
47   end
48
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"
52   end
53   
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"
59   end
60
61   def test_get_quickvote_nonexistent
62     get :index, { 'ident' => "idontexist" }
63     assert_redirected_to :controller => 'site'
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 => 'site'
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 end

Benjamin Mako Hill || Want to submit a patch?