Merge jdong; mostly functional testing work
authorJohn Dong <jdong@mit.edu>
Mon, 20 Aug 2007 20:10:48 +0000 (16:10 -0400)
committerJohn Dong <jdong@mit.edu>
Mon, 20 Aug 2007 20:10:48 +0000 (16:10 -0400)
15 files changed:
app/controllers/quickvote_controller.rb
app/views/election/edit.rhtml
app/views/election/edit_candidate.rhtml
app/views/election/edit_candidates.rhtml
app/views/election/edit_voters.rhtml
app/views/election/new.rhtml
app/views/election/new_voters.rhtml
app/views/quickvote/_candidate_list.rhtml
app/views/quickvote/create.rhtml
app/views/user/login.rhtml
app/views/voter/_vote.rhtml
app/views/voter/index.rhtml
test/functional/quickvote_controller_test.rb
test/functional/site_controller_test.rb
test/unit/selectricityservice_test.rb

index 41d0dc3616050ef909e39ebbcaa2eef7363da12f..5b3acab79f21998d5897e90cc331b71745c37b8c 100644 (file)
@@ -12,7 +12,6 @@ class QuickvoteController < ApplicationController
   def create
     if params[:quickvote] 
       @quickvote = QuickVote.new(params[:quickvote])
-     
       # store the candidate grabbed through ajax and stored in flash
       @quickvote.candidatelist = flash[:candlist]
       @quickvote.description=CGI.escapeHTML(@quickvote.description)
@@ -52,11 +51,9 @@ class QuickvoteController < ApplicationController
 
   def index
     @election = QuickVote.ident_to_quickvote(params[:ident])
-    
     # if the person has specified an election, we show them the voting
     # page. otherwise, we redirect back to main the page
     if @election
-
       # look to see that the voter has been created and has voted in
       # this election, and has confirmed their vote
       @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
@@ -75,8 +72,8 @@ class QuickvoteController < ApplicationController
         @voter = QuickVoter.new
         @voter.election = @election
         @voter.session_id = session.session_id
-        
-             # create new vote and make it the defaulted sorted list
+             
+        # create new vote and make it the defaulted sorted list
         @voter.vote = Vote.new
              @voter.save
              @voter.vote.set_defaults!
@@ -159,7 +156,11 @@ class QuickvoteController < ApplicationController
   ###############################################################
 
   def results
-    @election = QuickVote.ident_to_quickvote(params[:ident])
+    unless @election = QuickVote.ident_to_quickvote(params[:ident])
+      flash[:notice] = "Cannot find quickvote #{params[:ident]}."
+      redirect_to :controller => 'site'
+      return
+    end
     @election.results
     @candidates = {}
     @election.candidates.each {|c| @candidates[c.id] = c}
index af0cb493e5fc6952a3ab44e386dae1b5a971af48..968d1575124c730204089cc2a17e7b5ef094f280 100644 (file)
@@ -1,6 +1,6 @@
 <h1><strong><%= @election.name %>:</strong> Edit Overview</h1>
 
-<%= start_form_tag :action => 'update', :id => @election %>
+<% form_tag(:action => 'update', :id => @election) do %>
   <%= render :partial => 'overview_form' %>
   <%= submit_tag 'Done!' %>
-<%= end_form_tag %>
+<% end %>
index e465ae961714ff6e227fdd8ad707ad967394ff1a..fe56aa08c37f3d20d8c33f03da7c2cef4442f181 100644 (file)
@@ -1,9 +1,9 @@
 <h1>Editing <%= @candidate.name %></h1>
 
 <%= error_messages_for :candidate %>
-<%= form_tag( { :action => :update_candidate, :id => @candidate.id },
-              :multipart => true ) %>
+<% form_tag( { :action => :update_candidate, :id => @candidate.id },
+              :multipart => true ) do %>
 <%= render :partial => 'candidate_form' %>
 <%= submit_tag "Save" %>
-<%= end_form_tag %>
+<% end %>
 
index cc2c16a8a9ebb33225f95609456e9d90f11d2308..99f2f129abd0607b2308d256d52572dc775d6c96 100644 (file)
 
 <p>Please enter new candidates below.</p>
 
-<%= form_tag( { :action => :add_candidate, :id => @election.id },
-              :multipart => true ) %>
+<% form_tag( { :action => :add_candidate, :id => @election.id },
+              :multipart => true ) do %>
 <%= render :partial => 'candidate_form' %>
 <%= submit_tag "Add Candidate" %>
-<%= end_form_tag %>
+<% end %>
 
 <%= button_to "Done!", :action => 'show', :id => @election %>
index 429a9077fe15ce98483a69743602eee75e403ced..1e98fbaf93d39ac0dd636d14fe1675749af94203 100644 (file)
@@ -3,8 +3,8 @@
 
 <%= render :partial => 'voter_list' %>
 
-<%= form_tag :action => 'edit_voters', :id => @election.id %>
+<% form_tag (:action => 'edit_voters', :id => @election.id) do %>
 <%= render :partial => 'voters_form' %>
-<%= end_form_tag %>
+<% end %>
 
 <%= button_to 'Done!', :action => 'show', :id => @election.id %>
index fc390a059ed596a6ad8466b86aa11bfb3b0ff486..7b717486c0042739b2fe91d53b0f492e52677901 100644 (file)
@@ -2,8 +2,8 @@
 
 <h2>Vote Overview</h2>
 
-<%= form_tag :action => 'create_election' %>
+<% form_tag (:action => 'create_election') do %>
   <%= render :partial => 'overview_form' %>
   <%= submit_tag "Done!" %>
-<%= end_form_tag %>
+<% end %>
 
index b750085e6706a95157a3ccf78fb3a21fcfb2628a..9c3bad3a6f9c99cb36b07a35f19806ef8503ed7f 100644 (file)
@@ -3,6 +3,6 @@
 
 <%= render :partial => 'voter_list' %>
 
-<%= form_tag :action => 'new_voters', :id => @election.id %>
+<% form_tag(:action => 'new_voters', :id => @election.id) do %>
 <%= render :partial => 'voters_form' %>
-<%= end_form_tag %>
+<% end %>
index f4f1c0628c06d91139300d38a0338cbfa3b02611..64aa97926c228cdb0813e0096965d4f005aec67d 100644 (file)
@@ -1,18 +1,18 @@
 <% %>
 <!-- the list of candidates -->
-<% if @flash[:candlist] %>
+<% if flash[:candlist] %>
   <ul>
-  <% for cand in @flash[:candlist] %>
+  <% for cand in flash[:candlist] %>
     <li><%= cand.capitalize %></li>
   <% end %>
   </ul>
 <% end %>
 
-<%= form_remote_tag :update => 'candlist',
+<% form_remote_tag(:update => 'candlist',
                     :url => { :action => 'add_candidate' },
-                   :complete => "Field.focus('ajax_newcandidate')" %>
+                   :complete => "Field.focus('ajax_newcandidate')") do %>
 <p>
   <%= text_field "ajax", "newcandidate", :size => 40 %>
   <%= submit_tag "Add(+)" %>
 </p>
-<%= end_form_tag %>
+<% end %>
index 6b6ada8b6488acfcd8fe3f797c8e051726b378d8..cf91e198a4e15183020a680bea65c51ed49f9241 100644 (file)
@@ -10,7 +10,7 @@
 <%= render :partial => 'candidate_list' %>
 </div>
 
-<%= form_tag :action => 'create' %>
+<% form_tag(:action => 'create') do %>
 <!--[form:election]-->
 
 <p><label for="quickvote_name">One Word Description (for URL)<br />
@@ -26,5 +26,5 @@
 
 <%= submit_tag "Create Quickvote" -%>
 
-<%= end_form_tag %>
+<% end %>
 
index f4902a4e8e5d628b61b086f5641494b9154fc620..3e2f7461af226bc968594c04677b67834ccfc399 100644 (file)
@@ -2,7 +2,7 @@
   <h3>Please Login</h3>
 
   <div class="form-padding">
-    <%= start_form_tag :action => 'login'  %>
+    <% form_tag(:action => 'login') do %>
       <table>
         <%= form_input :text_field, "Login ID", "login", :size => 30 %>
         <%= form_input :password_field, "Password", "password", :size => 30 %>
@@ -13,6 +13,6 @@
         <p><%= link_to 'Register for an account', :action => 'signup' %></p>
         <p><%= link_to 'Forgot my password', :action => 'forgot_password' %></p>
       </div>
-    <%= end_form_tag %>
+    <% end %>
   </div>
 </div>
index d1447916c846451b64d19d05e580411977f12bc0..b95fc96209151e9c27ade5fce98d281fb940689c 100644 (file)
@@ -12,12 +12,12 @@ least preferred. Please list <em>all</em> choices in every vote.
 <em>(For example, 123 or 321 or 213, etc.)</em></p>
 
 <% if @voter.election.quickvote? %>
-  <%= form_tag quickaction_url( :ident => @voter.election.name, :action => 'confirm') %>
+  <% form_tag(quickaction_url( :ident => @voter.election.name, :action => 'confirm')) do  %>
 <% else %>
   <%= form_tag :action => 'review', :id => @voter.password %>
 <% end %>
 
 <%= text_field :vote, :votestring -%>
 <%= submit_tag "Vote!" %>
-<%= end_form_tag %>
+<% end %>
 
index 7e31c920a0b7a47400cd1c53c9947510f546509d..5c2ea2702c6e91ef88d0cb219ba165d46e06b141 100644 (file)
@@ -2,7 +2,7 @@
 
 <p>Please enter your password/token to log in and vote:</p>
 
-<%= form_tag :action => 'index' %>
+<% form_tag(:action => 'index') do %>
 <%= text_field :vote, :password %>
 <%= submit_tag "Log In" %>
-<%= end_form_tag %>
+<% end %>
index d0b37d90c170891a78723739631300e12ec9c88a..076a1047c0780dfe1c10a97f8b6a7475ae3ff6c1 100644 (file)
@@ -1,6 +1,16 @@
 require File.dirname(__FILE__) + '/../test_helper'
 require 'quickvote_controller'
 
+class ActionController::TestSession
+  def session_id
+    # Override this so we can set session ID
+    # pass in the 'test_session_id' session variable to override default
+    return "12345678" unless @attributes
+    if @attributes.has_key? 'test_session_id'
+      return @attributes['test_session_id']
+    end
+  end
+end
 # Re-raise errors caught by the controller.
 class QuickvoteController; def rescue_action(e) raise e end; end
 
@@ -12,7 +22,97 @@ class QuickvoteControllerTest < Test::Unit::TestCase
   end
 
   # Replace this with your real tests.
-  def test_truth
-    assert true
+  def test_index
+    get :index
+    assert_response 302
+  end
+
+  def test_create_quickvote
+    post(:create, {'commit' =>"Create Quickvote", 'quickvote' =>{'name' =>"variable", 'description' =>"Favorite variable."}}, nil, {:candlist=>["foo", "bar", "foobar"]})
+    assert_template "quickvote/success"
+    get :index, { 'ident' => "variable"}
+    assert_response :success
+  end
+
+  def test_create_dupe_quickvote
+    test_create_quickvote
+    assert_raise(Test::Unit::AssertionFailedError) do
+      test_create_quickvote
+    end
+  end
+
+  def test_create_quickvote_badname
+    post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "foobar"]})
+    assert_template "quickvote/create"
+  end
+
+  def test_create_quickvote_dupe_candidate
+    post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => ["foo", "bar", "bar",  "foobar"]})
+    assert_template "quickvote/create"
+  end
+  
+  def test_create_quickvote_nil_candidate
+    post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => nil})
+    assert_template "quickvote/create"
+    post(:create, {'commit' => "Create Quickvote", 'quickvote' => {'name' => "has a space", 'description' => "Foobar"}}, nil, {:candlist => []})
+    assert_template "quickvote/create"
+  end
+
+  def test_get_quickvote_nonexistent
+    get :index, { 'ident' => "idontexist" }
+    assert_redirected_to :controller => 'site'
+  end
+
+  def test_get_result_empty_vote
+    test_create_quickvote
+    get :results, { 'ident' => 'variable' }
+    assert_response :success
+  end
+
+  def test_get_result_nonexistent
+    test_create_quickvote
+    get :results, { 'ident' => 'asdflaksdjf' }
+    assert_redirected_to :controller => 'site'
+  end
+
+  def test_get_result_with_a_vote
+    test_cast_quickvote
+    get :results, { 'ident' => 'variable' }
+    assert_response :success
+  end
+
+  def test_get_result_multi_votes
+    test_create_quickvote
+    votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
+    5.times do |time|
+      get :index, { 'ident' => 'variable' }, { 'test_session_id'  =>  (time+1)*50 }
+      assert_response :success
+      post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }, { 'test_session_id' => (time+1)*50 }
+      assert_template 'quickvote/thanks'
+    end
+    get :results, { 'ident' => 'variable' }
+    assert_response :success
+  end
+
+  def test_cast_quickvote
+    test_create_quickvote
+    votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
+    post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
+    assert_template 'quickvote/thanks'
+  end
+
+  def test_cast_dupe_quickvote
+    test_create_quickvote
+    votes = QuickVote.ident_to_quickvote('variable').candidates.collect { |c| c.id}
+    get :index, { 'ident' => 'variable' }
+    assert_response :success
+
+    post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
+    assert_template 'quickvote/thanks'
+
+    get :index, { 'ident' => 'variable' }
+    assert_response :success
+    post :confirm, { 'ident' => 'variable', 'rankings-list' => votes.sort_by {rand} }
+    assert_redirected_to :controller => 'quickvote', :ident => 'variable'
   end
 end
index 69b93489364759d2d4de7bc89a8d8c66db52b20e..ad9f06cd9d8aa2b3f60abe8cca525d84b3be315e 100644 (file)
@@ -12,7 +12,8 @@ class SiteControllerTest < Test::Unit::TestCase
   end
 
   # Replace this with your real tests.
-  def test_truth
-    assert true
+  def test_index
+    get :index
+    assert_response :success
   end
 end
index 0596dc715fe8d17ed1401c6cd2f6b0f01ae1584a..c0f73c02789236df5c23e8728cc0d79aca329144 100644 (file)
@@ -13,16 +13,19 @@ class SelectricityServiceTest < Test::Unit::TestCase
     assert_instance_of Array, result
     assert_equal result.length, 0
   end
+
   def test_create_quickvote
     election = ElectionStruct.new :name => "TestVote", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_succeeds election
   end
+
   def test_cast_quickvote
     test_create_quickvote
     election = invoke_delegated :vote, :get_quickvote, "TestVote"
     casted_vote = election.candidate_ids.sort_by {rand} #Shuffles
     assert_cast_quickvote_succeeds "TestVote", 42, [casted_vote]
   end
+
   def test_cast_nil_quickvote
     assert_cast_quickvote_fails nil, nil, nil
     assert_cast_quickvote_fails "foo", nil, nil
@@ -31,6 +34,7 @@ class SelectricityServiceTest < Test::Unit::TestCase
     assert_cast_quickvote_fails "TestVote",42,nil
     assert_cast_quickvote_fails "TestVote",nil,[]
   end
+
   def test_cast_malformed_votelist
     test_create_quickvote
     election = invoke_delegated :vote, :get_quickvote, "TestVote"
@@ -38,17 +42,21 @@ class SelectricityServiceTest < Test::Unit::TestCase
     assert_cast_quickvote_fails "TestVote", 11, [1,2]
     assert_cast_quickvote_fails "TestVote", 11, [election.candidate_ids[0],election.candidate_ids[0], election.candidate_ids[1], election.candidate_ids[1], election.candidate_ids[2]]
   end
+
   def test_get_nonexistent_quickvote
     assert_raises ArgumentError do
       qv = invoke_delegated :vote, :get_quickvote, "asdfasdfasdf"
     end
   end
+
   def test_get_voters_nonexistent_quickvote
     assert_raises(ArgumentError) {invoke_delegated :vote, :get_quickvote_votes, "asdfasdf"}
   end
+
   def test_get_candidate_map_nonexistent_quickvote
     assert_raises(ArgumentError) { invoke_delegated :vote, :get_quickvote_candidate_map, "asdfasdf"}
   end
+
   def test_cast_mass_quickvote
     test_create_quickvote
     election = invoke_delegated :vote, :get_quickvote, "TestVote"
@@ -57,43 +65,52 @@ class SelectricityServiceTest < Test::Unit::TestCase
       assert_cast_quickvote_succeeds "TestVote", t, [casted_vote]
     end
   end
+
   def test_cast_quickvote_nonexistent
     assert_cast_quickvote_fails "ASDFJOFASF", "me", [1,2,3]
   end
+
   def test_cast_quickvote_nonexistent_candidates
     test_create_quickvote
     election = invoke_delegated :vote, :get_quickvote, "TestVote"
     assert_cast_quickvote_fails "TestVote", 42, [123,342314,5342,1,1,2]
   end
+
   def test_create_mass_quickvote
     10.times do |t|
       election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
       assert_create_quickvote_succeeds election
     end
   end
+
   def test_create_quickvote_bad_name
     election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
   end
+
   def test_create_quickvote_nil
     election =  ElectionStruct.new
     assert_create_quickvote_fails election
   end
+
   def test_create_quickvote_name_nil
     election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
   end
+
   def test_create_quickvote_description_nil
     election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
     
   end
+  
   def test_create_quickvote_description_whitespace
     election = ElectionStruct.new :name => "foobar", :description => "       ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
     election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
     assert_create_quickvote_fails election
   end
+  
   def test_create_quickvote_candidates_nil
     election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
     assert_create_quickvote_fails election
@@ -158,38 +175,46 @@ class SelectricityServiceTest < Test::Unit::TestCase
     assert_equal results.ssd_winners, [candidates["jdong"]]
   end
   private
+
   def assert_cast_quickvote_succeeds(shortname, id, vote)
     assert_nothing_raised do
-      old_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
-      invoke_delegated :vote, :cast_quickvote, shortname, id, vote
-      new_votes = invoke_delegated :vote, :get_quickvote_votes, shortname
-      assert_equal old_votes.length, new_votes.length-1
-      assert_not_nil(reflection = new_votes.find_all { |v| v.voter_session_id == "XMLRPC:#{id}" })
-      assert_equal reflection.length, 1
-      assert_equal reflection[0].vote, vote[0]
+      old_votes = invoke_delegated(:vote, :get_quickvote_votes, shortname)
+      invoke_delegated(:vote, :cast_quickvote, shortname, id, vote)
+
+      new_votes = invoke_delegated(:vote, :get_quickvote_votes, shortname)
+      assert_equal(old_votes.length, new_votes.length - 1)
+
+      reflection = new_votes.find_all { |v| v.voter_session_id == "XMLRPC:#{id}" }
+      assert_not_nil(reflection)
+      assert_equal(reflection.length, 1)
+      assert_equal(reflection[0].vote, vote[0])
     end
   end
+
   def assert_cast_quickvote_fails(shortname, id, vote)
-    assert_raise Test::Unit::AssertionFailedError do 
+    assert_raise Test::Unit::AssertionFailedError do
       assert_cast_quickvote_succeeds(shortname, id, vote)
-    end
+     end
   end
+
   def assert_create_quickvote_succeeds(election)
-    # Checks if a created quickvote is identical when retrieved
+    # checks if a created quickvote is identical when retrieved
     assert_nothing_raised do
-      old_len=invoke_delegated(:vote,:list_quickvotes).length
-      result = invoke_delegated :vote, :create_quickvote, election
-      assert_equal result, ""
-      reflection = invoke_delegated :vote, :get_quickvote, election.name
-      assert_equal election.description, reflection.description
-      assert_equal 0, election.name.casecmp(reflection.name)
-      assert_equal election.candidate_names, reflection.candidate_names
-      assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
+      old_len = invoke_delegated(:vote, :list_quickvotes).length
+      result = invoke_delegated(:vote, :create_quickvote, election)
+      assert_equal(result, "")
+
+      reflection = invoke_delegated(:vote, :get_quickvote, election.name)
+      assert_equal(election.description, reflection.description)
+      assert_equal(0, election.name.casecmp(reflection.name))
+      assert_equal(election.candidate_names, reflection.candidate_names)
+      assert_equal(invoke_delegated(:vote, :list_quickvotes).length, old_len+1)
     end
   end
+  
   def assert_create_quickvote_fails(election)
     assert_raise Test::Unit::AssertionFailedError do
-      assert_create_quickvote_succeeds election
+      assert_create_quickvote_succeeds(election)
     end
   end
 end

Benjamin Mako Hill || Want to submit a patch?