aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/change_email_validator_spec.rb
blob: b667a23d12a5a6f07eddac185e56fb994396d723 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

def validator_with_user_and_params(user, params = {})
    validator = ChangeEmailValidator.new(params)
    validator.logged_in_user = user
    validator
end

describe ChangeEmailValidator do

    let(:user) { FactoryGirl.create(:user) }

    describe :old_email do

        it 'must have an old email' do
            params = { :old_email => nil,
                       :new_email => 'new@example.com',
                       :user_circumstance => 'change_email',
                       :password => 'jonespassword' }
            validator = validator_with_user_and_params(user, params)

            msg = 'Please enter your old email address'
            expect(validator.errors_on(:old_email)).to include(msg)
        end

        it 'must be a valid email' do
            params = { :old_email => 'old',
                       :new_email => 'new@example.com',
                       :user_circumstance => 'change_email',
                       :password => 'jonespassword' }
            validator = validator_with_user_and_params(user, params)

            msg = "Old email doesn't look like a valid address"
            expect(validator.errors_on(:old_email)).to include(msg)
        end

        it 'must have the same email as the logged in user' do
            params = { :old_email => user.email,
                       :new_email => 'new@example.com',
                       :user_circumstance => 'change_email',
                       :password => 'jonespassword' }
            validator = validator_with_user_and_params(user, params)
            validator.logged_in_user = FactoryGirl.build(:user)

            msg = "Old email address isn't the same as the address of the account you are logged in with"
            expect(validator.errors_on(:old_email)).to include(msg)
        end

    end

    describe :new_email do

        it 'must have a new email' do
            params = { :old_email => user.email,
                       :new_email => nil,
                       :user_circumstance => 'change_email',
                       :password => 'jonespassword' }
            validator = validator_with_user_and_params(user, params)

            msg = 'Please enter your new email address'
            expect(validator.errors_on(:new_email)).to include(msg)
        end

        it 'must be a valid email' do
            params = { :old_email => user.email,
                       :new_email => 'new',
                       :user_circumstance => 'change_email',
                       :password => 'jonespassword' }
            validator = validator_with_user_and_params(user, params)

            msg = "New email doesn't look like a valid address"
            expect(validator.errors_on(:new_email)).to include(msg)
        end

    end

    describe :password do

        pending 'password_and_format_of_email validation fails when password is nil' do
            it 'must have a password' do
                params = { :old_email => user.email,
                           :new_email => 'new@example.com',
                           :password => nil }
                validator = validator_with_user_and_params(user, params)

                msg = 'Please enter your password'
                expect(validator.errors_on(:password)).to include(msg)
            end
        end

        it 'does not require a password if changing email' do
            params = { :old_email => user.email,
                       :new_email => 'new@example.com',
                       :user_circumstance => 'change_email',
                       :password => '' }
            validator = validator_with_user_and_params(user, params)

            expect(validator).to have(0).errors_on(:password)
        end

        it 'must have a password if not changing email' do
            params = { :old_email => user.email,
                       :new_email => 'new@example.com',
                       :user_circumstance => 'unknown',
                       :password => '' }
            validator = validator_with_user_and_params(user, params)

            msg = 'Please enter your password'
            expect(validator.errors_on(:password)).to include(msg)
        end

        it 'must be the correct password' do
            params = { :old_email => user.email,
                       :new_email => 'new@example.com',
                       :password => 'incorrectpass' }
            validator = validator_with_user_and_params(user, params)

            msg = 'Password is not correct'
            expect(validator.errors_on(:password)).to include(msg)
        end

    end

end