aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/public_body_change_request_spec.rb
blob: 653a218e3eeeea2a1af941620b9a251185e6df26 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# == Schema Information
#
# Table name: public_body_change_requests
#
#  id                :integer          not null, primary key
#  user_email        :string(255)
#  user_name         :string(255)
#  user_id           :integer
#  public_body_name  :text
#  public_body_id    :integer
#  public_body_email :string(255)
#  source_url        :text
#  notes             :text
#  is_open           :boolean          default(TRUE), not null
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#

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

describe PublicBodyChangeRequest, 'when validating' do

    it 'should not be valid without a public body name' do
        change_request = PublicBodyChangeRequest.new()
        change_request.valid?.should be_false
        change_request.errors[:public_body_name].should == ['Please enter the name of the authority']
    end

    it 'should not be valid without a user name if there is no user' do
        change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
        change_request.valid?.should be_false
        change_request.errors[:user_name].should == ['Please enter your name']
    end

    it 'should not be valid without a user email address if there is no user' do
        change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
        change_request.valid?.should be_false
        change_request.errors[:user_email].should == ['Please enter your email address']
    end

    it 'should be valid with a user and no name or email address' do
        user = FactoryGirl.build(:user)
        change_request = PublicBodyChangeRequest.new(:user => user,
                                                     :public_body_name => 'New Body')
        change_request.valid?.should be_true
    end

    it 'should validate the format of a user email address entered' do
        change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
                                                     :user_email => '@example.com')
        change_request.valid?.should be_false
        change_request.errors[:user_email].should == ["Your email doesn't look like a valid address"]
    end

    it 'should validate the format of a public body email address entered' do
        change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
                                                    :public_body_email => '@example.com')
        change_request.valid?.should be_false
        change_request.errors[:public_body_email].should == ["The authority email doesn't look like a valid address"]
    end

end

describe PublicBodyChangeRequest, 'get_user_name' do

    it 'should return the user_name field if there is no user association' do
        change_request = PublicBodyChangeRequest.new(:user_name => 'Test User')
        change_request.get_user_name.should == 'Test User'
    end

    it 'should return the name of the associated user if there is one' do
        user = FactoryGirl.build(:user)
        change_request = PublicBodyChangeRequest.new(:user => user)
        change_request.get_user_name.should == user.name
    end

end


describe PublicBodyChangeRequest, 'get_user_email' do

    it 'should return the user_email field if there is no user association' do
        change_request = PublicBodyChangeRequest.new(:user_email => 'user@example.com')
        change_request.get_user_email.should == 'user@example.com'
    end

    it 'should return the email of the associated user if there is one' do
        user = FactoryGirl.build(:user)
        change_request = PublicBodyChangeRequest.new(:user => user)
        change_request.get_user_email.should == user.email
    end

end


describe PublicBodyChangeRequest, 'get_public_body_name' do

    it 'should return the public_body_name field if there is no public body association' do
        change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Authority')
        change_request.get_public_body_name.should == 'Test Authority'
    end

    it 'should return the name of the associated public body if there is one' do
        public_body = FactoryGirl.build(:public_body)
        change_request = PublicBodyChangeRequest.new(:public_body => public_body)
        change_request.get_public_body_name.should == public_body.name
    end

end