aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2015-01-05 17:43:38 +0000
committerLouise Crow <louise.crow@gmail.com>2015-04-27 16:55:02 +0100
commit61faadbac1b4a71148248382425beb4368cb4cb0 (patch)
treea86c572057b20b89d24c112a1c81375caeaac737
parentd16b7e1f15e9c7abe3db986850f5e60ff186c271 (diff)
Validate the uniqueness of cookies within the scope of info requests.
-rw-r--r--app/models/widget_vote.rb1
-rw-r--r--spec/models/widget_vote_spec.rb16
2 files changed, 17 insertions, 0 deletions
diff --git a/app/models/widget_vote.rb b/app/models/widget_vote.rb
index 33697f0fb..021c38b30 100644
--- a/app/models/widget_vote.rb
+++ b/app/models/widget_vote.rb
@@ -15,4 +15,5 @@ class WidgetVote < ActiveRecord::Base
attr_accessible :cookie
validates :cookie, :length => { :is => 20 }
+ validates_uniqueness_of :cookie, :scope => :info_request_id
end
diff --git a/spec/models/widget_vote_spec.rb b/spec/models/widget_vote_spec.rb
index f1e30ed22..db5e4385d 100644
--- a/spec/models/widget_vote_spec.rb
+++ b/spec/models/widget_vote_spec.rb
@@ -32,6 +32,22 @@ describe WidgetVote do
widget_vote.should be_valid
end
+ it 'enforces uniqueness of cookie per info request' do
+ info_request = FactoryGirl.create(:info_request)
+ widget_vote = info_request.widget_votes.create(:cookie => 'x' * 20)
+ duplicate_vote = info_request.widget_votes.build(:cookie => 'x' * 20)
+ duplicate_vote.should_not be_valid
+ duplicate_vote.errors[:cookie].should == ["has already been taken"]
+ end
+
+ it 'allows the same cookie to be used across info requests' do
+ info_request = FactoryGirl.create(:info_request)
+ second_info_request = FactoryGirl.create(:info_request)
+ widget_vote = info_request.widget_votes.create(:cookie => 'x' * 20)
+ second_request_vote = second_info_request.widget_votes.build(:cookie => 'x' * 20)
+ second_request_vote.should be_valid
+ end
+
end
end