1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'selectricity_service_controller'
4 class SelectricityServiceTest < Test::Unit::TestCase
6 @controller=SelectricityServiceController.new
7 @request=ActionController::TestRequest.new
8 @response = ActionController::TestResponse.new
11 def test_list_quickvotes
12 result= invoke_delegated :vote, :list_quickvotes
13 assert_instance_of Array, result
14 assert_equal result.length, 0
16 def test_create_quickvote
17 election = ElectionStruct.new :name => "TestVote", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
18 assert_create_quickvote_succeeds election
20 def test_cast_quickvote
22 election = invoke_delegated :vote, :get_quickvote, "TestVote"
23 casted_vote = election.candidate_ids.sort_by {rand} #Shuffles
24 invoke_delegated :vote, :cast_quickvote, "TestVote", 42, [casted_vote]
25 quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
26 assert_equal quickvote_votes.length, 1
27 assert_equal quickvote_votes[0].vote, casted_vote
29 def test_cast_mass_quickvote
31 election = invoke_delegated :vote, :get_quickvote, "TestVote"
33 casted_vote = election.candidate_ids.sort_by {rand}
34 invoke_delegated :vote, :cast_quickvote, "TestVote", t, [casted_vote]
36 quickvote_votes= invoke_delegated :vote, :get_quickvote_votes, "TestVote"
37 assert_equal quickvote_votes.length, 20
39 def test_create_mass_quickvote
41 election = ElectionStruct.new :name => "test#{t}", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
42 assert_create_quickvote_succeeds election
45 def test_create_quickvote_bad_name
46 election = ElectionStruct.new :name => "invalid space", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
47 assert_create_quickvote_fails election
49 def test_create_quickvote_nil
50 election = ElectionStruct.new
51 assert_create_quickvote_fails election
53 def test_create_quickvote_name_nil
54 election = ElectionStruct.new :name => "", :description => "Test Vote", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
55 assert_create_quickvote_fails election
57 def test_create_quickvote_description_nil
58 election = ElectionStruct.new :name => "foobar", :description => nil, :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
59 assert_create_quickvote_fails election
62 def test_create_quickvote_description_whitespace
63 election = ElectionStruct.new :name => "foobar", :description => " ", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
64 assert_create_quickvote_fails election
65 election = ElectionStruct.new :name => "foobar", :description => "\t\t", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
66 assert_create_quickvote_fails election
68 def test_create_quickvote_candidates_nil
69 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => nil
70 assert_create_quickvote_fails election
72 def test_create_quickvote_insufficient_candidates
73 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple"]
74 assert_create_quickvote_fails election
76 def test_create_quickvote_candidates_whitespace
77 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => [" ", " ", " ", " "]
78 assert_create_quickvote_fails election
80 def test_create_quickvote_dupe_candidates
81 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Apple", "Apple"]
82 assert_create_quickvote_fails election
84 # Previous may pass coincidentally if a uniq! then a sizecheck reveals too few unique names
85 # We don't want this to happen. Dupe canidates should fail regardless of how many are left.
87 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", "Apple", "Orange", "Orange", "Pineapple" , "Pineapple"]
88 assert_create_quickvote_fails election
90 def test_create_quickvote_candidates_nil_mixed
91 election = ElectionStruct.new :name => "foobar", :description => "valid", :candidate_names => ["Apple", nil ]
92 assert_create_quickvote_fails election
94 def test_create_quickvote_description_xmlescape
95 # Will an embedded XML element bork the table?
96 election = ElectionStruct.new :name => "foobar", :description => "test </string>", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
97 assert_create_quickvote_succeeds election
99 def test_create_quickvote_unprintable_description
100 election = ElectionStruct.new :name => "foobar", :description => "test \x01\x02\x03\x04\x05\x06\x07\x08", :candidate_names => ["Apple", "Orange", "Banana", "Pineapple"]
101 assert_create_quickvote_succeeds election
104 def assert_create_quickvote_succeeds(election)
105 # Checks if a created quickvote is identical when retrieved
106 old_len=invoke_delegated(:vote,:list_quickvotes).length
107 result = invoke_delegated :vote, :create_quickvote, election
108 assert_equal result, ""
109 reflection = invoke_delegated :vote, :get_quickvote, election.name
110 assert_equal election.description, reflection.description
111 assert_equal 0, election.name.casecmp(reflection.name)
112 assert_equal election.candidate_names, reflection.candidate_names
113 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len+1)
115 def assert_create_quickvote_fails(election)
116 # Helper function to check that creating this quickvote fails
117 old_len=invoke_delegated(:vote,:list_quickvotes).length
118 result = invoke_delegated :vote, :create_quickvote, election
119 assert_instance_of String, result
120 assert_not_equal result.length, 0
121 assert_equal(invoke_delegated(:vote,:list_quickvotes).length, old_len)