File: email_validator_spec.rb

package info (click to toggle)
ruby-valid-email 0.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: ruby: 575; makefile: 4
file content (298 lines) | stat: -rw-r--r-- 8,657 bytes parent folder | download
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
require 'spec_helper'

describe EmailValidator do
  email_class = Class.new do
    include ActiveModel::Validations

    attr_accessor :email

    def self.model_name
      ActiveModel::Name.new(self, nil, "TestModel")
    end
  end

  person_class = Class.new(email_class) do
    validates :email, :email => true
  end

  person_class_mx = Class.new(email_class) do
    validates :email, :email => {:mx => true}
  end

  person_class_mx_with_fallback = Class.new(email_class) do
    validates :email, :email => {:mx_with_fallback => true}
  end

  person_class_disposable_email = Class.new(email_class) do
    validates :email, :email => {:ban_disposable_email => true}
  end

  person_class_nil_allowed = Class.new(email_class) do
    validates :email, :email => {:allow_nil => true}
  end

  person_class_blank_allowed = Class.new(email_class) do
    validates :email, :email => {:allow_blank => true}
  end

  person_class_mx_separated = Class.new(email_class) do
    validates :email, :mx => true
  end

  person_class_mx_with_fallback_separated = Class.new(email_class) do
    validates :email, :mx_with_fallback => true
  end

  person_class_domain = Class.new(email_class) do
    validates :email, :domain => true
  end

  person_message_specified = Class.new(email_class) do
    validates :email, :email => { :message => 'custom message', :ban_disposable_email => true }
  end

  shared_examples_for "Invalid model" do
    before { subject.valid? }

    it { is_expected.not_to be_valid }
    specify { expect(subject.errors[:email]).to match_array errors }
  end

  shared_examples_for "Validating emails" do

    before :each do
      I18n.locale = locale
    end

    describe "validating email" do
      subject { person_class.new }

      it "fails when email empty" do
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when email is not valid" do
        subject.email = 'joh@doe'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when email domain is prefixed with dot" do
        subject.email = 'john@.doe'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when email domain contains two consecutive dots" do
        subject.email = 'john@doe-two..com'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when email ends with a period" do
        subject.email = 'john@doe.com.'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when email ends with special characters" do
        subject.email = 'john@doe.com&'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when email is valid with information" do
        subject.email = '"John Doe" <john@doe.com>'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "passes when email is simple email address" do
        subject.email = 'john@doe.com'
        expect(subject.valid?).to be_truthy
        expect(subject.errors[:email]).to be_empty
      end

      it "fails when email is simple email address not stripped" do
        subject.email = 'john@doe.com            '
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when domain contains a space" do
        subject.email = 'john@doe .com'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when passing multiple simple email addresses" do
        subject.email = 'john@doe.com, maria@doe.com'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

    end

    describe "validating email with MX and fallback to A" do
      subject { person_class_mx_with_fallback.new }

      xit "passes when email domain has MX record" do
        subject.email = 'john@gmail.com'
        expect(subject.valid?).to be_truthy
        expect(subject.errors[:email]).to be_empty
      end

      xit "passes when email domain has no MX record but has an A record" do
        subject.email = 'john@subdomain.rubyonrails.org'
        expect(subject.valid?).to be_truthy
        expect(subject.errors[:email]).to be_empty
      end

      it "fails when domain does not exists" do
        subject.email = 'john@nonexistentdomain.abc'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end
    end

    describe "validating email with MX" do
      subject { person_class_mx.new }

      xit "passes when email domain has MX record" do
        subject.email = 'john@gmail.com'
        expect(subject.valid?).to be_truthy
        expect(subject.errors[:email]).to be_empty
      end

      it "fails when email domain has no MX record" do
        subject.email = 'john@subdomain.rubyonrails.org'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "fails when domain does not exists" do
        subject.email = 'john@nonexistentdomain.abc'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end
    end

    describe "validating MX with fallback to A" do
      subject { person_class_mx_with_fallback_separated.new }

      context "when domain is not specified" do
        before { subject.email = 'john' }
        it_behaves_like "Invalid model"
      end

      context "when domain is not specified but @ is" do
        before { subject.email = 'john@' }
        it_behaves_like "Invalid model"
      end
    end

    describe "validating MX" do
      subject { person_class_mx_separated.new }

      context "when domain is not specified" do
        before { subject.email = 'john' }
        it_behaves_like "Invalid model"
      end

      context "when domain is not specified but @ is" do
        before { subject.email = 'john@' }
        it_behaves_like "Invalid model"
      end
    end

    describe "validating email from disposable service" do
      subject { person_class_disposable_email.new }

      it "passes when email from trusted email services" do
        subject.email = 'john@mail.ru'
        expect(subject.valid?).to be_truthy
        expect(subject.errors[:email]).to be_empty
      end

      it "fails when email from disposable email services" do
        subject.email = 'john@grr.la'
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end
    end

    describe "validating domain" do
      subject { person_class_domain.new }

      it "does not pass with an invalid domain" do
        subject.email = "test@example.org$\'"
        expect(subject.valid?).to be_falsey
        expect(subject.errors[:email]).to eq errors
      end

      it "passes with valid domain" do
        subject.email = 'john@example.org'
        expect(subject.valid?).to be_truthy
        expect(subject.errors[:email]).to be_empty
      end
    end
  end

  describe "Can allow nil" do
    subject { person_class_nil_allowed.new }

    it "passes even if mail isn't set" do
      subject.email = nil
      expect(subject).to be_valid
      expect(subject.errors[:email]).to be_empty
    end
  end

  describe "Can allow blank" do
    subject { person_class_blank_allowed.new }

    it "passes even if mail is a blank string set" do
      subject.email = ''
      expect(subject).to be_valid
      expect(subject.errors[:email]).to be_empty
    end
  end

  describe "Accepts custom messages" do
    subject { person_message_specified.new }

    it "adds only the custom error" do
      subject.email = 'bad@mailnator.com'
      expect(subject.valid?).to be_falsey
      expect(subject.errors[:email]).to match_array [ 'custom message' ]
    end
  end

  describe "Translating in english" do
    let!(:locale){ :en }
    let!(:errors) { [ "is invalid" ] }
    it_behaves_like "Validating emails"
  end

  describe "Translating in french" do
    let!(:locale){ :fr }
    let!(:errors) { [ "est invalide" ] }
    it_behaves_like "Validating emails"
  end

  describe 'Translating in czech' do
    let!(:locale){ :cs }
    let!(:errors) do
      [
        I18n.t(
          :invalid,
          locale: locale,
          scope: [:valid_email, :validations, :email]
        )
      ]
    end

    it_behaves_like 'Validating emails'
  end
end