aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/widget_vote_spec.rb
blob: 1a6d3833c58586c19e144f2a6e0c133e0a255d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- encoding : utf-8 -*-
# == Schema Information
#
# Table name: widget_votes
#
#  id              :integer          not null, primary key
#  cookie          :string(255)
#  info_request_id :integer
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe WidgetVote do

    describe :new do

        it 'requires an info request' do
            widget_vote = WidgetVote.new
            widget_vote.should_not be_valid
            widget_vote.errors[:info_request].should == ["can't be blank"]
        end

        it 'validates the cookie length' do
            widget_vote = WidgetVote.new
            widget_vote.should_not be_valid
            widget_vote.errors[:cookie].should == ["is the wrong length (should be 20 characters)"]
        end

        it 'is valid with a cookie and info request' do
            widget_vote = FactoryGirl.create(:widget_vote)
            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