validates_email_format_of 1.5.3 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +8 -0
- data/.rspec +2 -0
- data/.travis.yml +18 -0
- data/Gemfile +3 -0
- data/README.rdoc +51 -48
- data/config/locales/en.yml +6 -0
- data/config/locales/pl.yml +6 -0
- data/gemfiles/Gemfile.active_model_2_1 +5 -0
- data/gemfiles/Gemfile.active_model_3_0 +5 -0
- data/gemfiles/Gemfile.active_model_3_1 +5 -0
- data/gemfiles/Gemfile.active_model_3_2 +5 -0
- data/gemfiles/Gemfile.active_model_3_3 +5 -0
- data/gemfiles/Gemfile.active_model_4_0 +5 -0
- data/gemfiles/Gemfile.active_model_4_1 +5 -0
- data/lib/validates_email_format_of.rb +35 -78
- data/lib/validates_email_format_of/active_model.rb +24 -0
- data/lib/validates_email_format_of/railtie.rb +7 -0
- data/lib/validates_email_format_of/rspec_matcher.rb +15 -0
- data/lib/validates_email_format_of/version.rb +3 -0
- data/rakefile.rb +1 -27
- data/spec/rspec_matcher_spec.rb +14 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/validates_email_format_of_spec.rb +308 -0
- data/validates_email_format_of.gemspec +21 -0
- metadata +65 -39
- data/init.rb +0 -1
- data/test/database.yml +0 -3
- data/test/fixtures/people.yml +0 -3
- data/test/fixtures/person.rb +0 -33
- data/test/schema.rb +0 -5
- data/test/test_helper.rb +0 -38
- data/test/validates_email_format_of_test.rb +0 -242
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/rails/init')
|
data/test/database.yml
DELETED
data/test/fixtures/people.yml
DELETED
data/test/fixtures/person.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
class Person < ActiveRecord::Base
|
2
|
-
validates_email_format_of :email,
|
3
|
-
:on => :create,
|
4
|
-
:message => 'fails with custom message',
|
5
|
-
:allow_nil => true
|
6
|
-
end
|
7
|
-
|
8
|
-
class PersonForbidNil < ActiveRecord::Base
|
9
|
-
set_table_name 'people'
|
10
|
-
|
11
|
-
validates_email_format_of :email,
|
12
|
-
:on => :create,
|
13
|
-
:allow_nil => false
|
14
|
-
end
|
15
|
-
|
16
|
-
|
17
|
-
class MxRecord < ActiveRecord::Base
|
18
|
-
set_table_name 'people'
|
19
|
-
|
20
|
-
validates_email_format_of :email,
|
21
|
-
:on => :create,
|
22
|
-
:check_mx => true
|
23
|
-
end
|
24
|
-
|
25
|
-
if ActiveRecord::VERSION::MAJOR >= 3
|
26
|
-
class Shorthand < ActiveRecord::Base
|
27
|
-
set_table_name 'people'
|
28
|
-
|
29
|
-
validates :email, :email_format => { :message => 'fails with shorthand message' },
|
30
|
-
:length => { :maximum => 1 }
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
data/test/schema.rb
DELETED
data/test/test_helper.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'test/unit'
|
5
|
-
require 'active_record'
|
6
|
-
require 'active_record/fixtures'
|
7
|
-
require "#{File.dirname(__FILE__)}/../init"
|
8
|
-
|
9
|
-
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
10
|
-
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
11
|
-
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'plugin_test'])
|
12
|
-
|
13
|
-
load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
|
14
|
-
|
15
|
-
if ActiveSupport.const_defined?(:TestCase)
|
16
|
-
ActiveSupport::TestCase.send(:include, ActiveRecord::TestFixtures)
|
17
|
-
TEST_CASE = ActiveSupport::TestCase
|
18
|
-
else
|
19
|
-
TEST_CASE = Test::Unit::TestCase
|
20
|
-
end
|
21
|
-
|
22
|
-
TEST_CASE.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
23
|
-
$:.unshift(TEST_CASE.fixture_path)
|
24
|
-
|
25
|
-
class TEST_CASE #:nodoc:
|
26
|
-
def create_fixtures(*table_names)
|
27
|
-
if block_given?
|
28
|
-
Fixtures.create_fixtures(TEST_CASE.fixture_path, table_names) { yield }
|
29
|
-
else
|
30
|
-
Fixtures.create_fixtures(TEST_CASE.fixture_path, table_names)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
self.use_transactional_fixtures = false
|
35
|
-
|
36
|
-
self.use_instantiated_fixtures = false
|
37
|
-
end
|
38
|
-
|
@@ -1,242 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
3
|
-
|
4
|
-
class ValidatesEmailFormatOfTest < TEST_CASE
|
5
|
-
fixtures :people
|
6
|
-
|
7
|
-
def setup
|
8
|
-
@valid_email = 'valid@example.com'
|
9
|
-
@invalid_email = 'invalid@example.'
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_with_activerecord
|
13
|
-
p = create_person(:email => @valid_email)
|
14
|
-
save_passes(p)
|
15
|
-
|
16
|
-
p = create_person(:email => @invalid_email)
|
17
|
-
save_fails(p)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_without_activerecord
|
21
|
-
assert_valid(@valid_email)
|
22
|
-
assert_invalid(@invalid_email)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_should_allow_valid_email_addresses
|
26
|
-
['valid@example.com',
|
27
|
-
'Valid@test.example.com',
|
28
|
-
'valid+valid123@test.example.com',
|
29
|
-
'valid_valid123@test.example.com',
|
30
|
-
'valid-valid+123@test.example.co.uk',
|
31
|
-
'valid-valid+1.23@test.example.com.au',
|
32
|
-
'valid@example.co.uk',
|
33
|
-
'v@example.com',
|
34
|
-
'valid@example.ca',
|
35
|
-
'valid_@example.com',
|
36
|
-
'valid123.456@example.org',
|
37
|
-
'valid123.456@example.travel',
|
38
|
-
'valid123.456@example.museum',
|
39
|
-
'valid@example.mobi',
|
40
|
-
'valid@example.info',
|
41
|
-
'valid-@example.com',
|
42
|
-
'fake@p-t.k12.ok.us',
|
43
|
-
# allow single character domain parts
|
44
|
-
'valid@mail.x.example.com',
|
45
|
-
'valid@x.com',
|
46
|
-
'valid@example.w-dash.sch.uk',
|
47
|
-
# from RFC 3696, page 6
|
48
|
-
'customer/department=shipping@example.com',
|
49
|
-
'$A12345@example.com',
|
50
|
-
'!def!xyz%abc@example.com',
|
51
|
-
'_somename@example.com',
|
52
|
-
# apostrophes
|
53
|
-
"test'test@example.com",
|
54
|
-
# international domain names
|
55
|
-
'test@xn--bcher-kva.ch',
|
56
|
-
'test@example.xn--0zwm56d',
|
57
|
-
'test@192.192.192.1'
|
58
|
-
].each do |email|
|
59
|
-
assert_valid(email)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_should_not_allow_invalid_email_addresses
|
64
|
-
['invalid@example-com',
|
65
|
-
# period can not start local part
|
66
|
-
'.invalid@example.com',
|
67
|
-
# period can not end local part
|
68
|
-
'invalid.@example.com',
|
69
|
-
# period can not appear twice consecutively in local part
|
70
|
-
'invali..d@example.com',
|
71
|
-
# should not allow underscores in domain names
|
72
|
-
'invalid@ex_mple.com',
|
73
|
-
'invalid@e..example.com',
|
74
|
-
'invalid@p-t..example.com',
|
75
|
-
'invalid@example.com.',
|
76
|
-
'invalid@example.com_',
|
77
|
-
'invalid@example.com-',
|
78
|
-
'invalid-example.com',
|
79
|
-
'invalid@example.b#r.com',
|
80
|
-
'invalid@example.c',
|
81
|
-
'invali d@example.com',
|
82
|
-
# TLD can not be only numeric
|
83
|
-
'invalid@example.123',
|
84
|
-
# unclosed quote
|
85
|
-
"\"a-17180061943-10618354-1993365053@example.com",
|
86
|
-
# too many special chars used to cause the regexp to hang
|
87
|
-
"-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++@foo",
|
88
|
-
'invalidexample.com',
|
89
|
-
# should not allow special chars after a period in the domain
|
90
|
-
'local@sub.)domain.com',
|
91
|
-
'local@sub.#domain.com',
|
92
|
-
# one at a time
|
93
|
-
"foo@example.com\nexample@gmail.com",
|
94
|
-
'invalid@example.',
|
95
|
-
"\"foo\\\\\"\"@bar.com",
|
96
|
-
"foo@mail.com\r\nfoo@mail.com",
|
97
|
-
'@example.com',
|
98
|
-
'foo@',
|
99
|
-
'foo',
|
100
|
-
'Iñtërnâtiônàlizætiøn@hasnt.happened.to.email'
|
101
|
-
].each do |email|
|
102
|
-
assert_invalid(email)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
# from http://www.rfc-editor.org/errata_search.php?rfc=3696
|
107
|
-
def test_should_allow_quoted_characters
|
108
|
-
['"Abc\@def"@example.com',
|
109
|
-
'"Fred\ Bloggs"@example.com',
|
110
|
-
'"Joe.\\Blow"@example.com',
|
111
|
-
].each do |email|
|
112
|
-
assert_valid(email)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_should_required_balanced_quoted_characters
|
117
|
-
assert_valid(%!"example\\\\\\""@example.com!)
|
118
|
-
assert_valid(%!"example\\\\"@example.com!)
|
119
|
-
assert_invalid(%!"example\\\\""example.com!)
|
120
|
-
end
|
121
|
-
|
122
|
-
# from http://tools.ietf.org/html/rfc3696, page 5
|
123
|
-
# corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
|
124
|
-
def test_should_not_allow_escaped_characters_without_quotes
|
125
|
-
['Fred\ Bloggs_@example.com',
|
126
|
-
'Abc\@def+@example.com',
|
127
|
-
'Joe.\\Blow@example.com'
|
128
|
-
].each do |email|
|
129
|
-
assert_invalid(email)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_should_check_length_limits
|
134
|
-
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@example.com',
|
135
|
-
'test@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com'
|
136
|
-
].each do |email|
|
137
|
-
assert_invalid(email)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_overriding_length_checks
|
142
|
-
assert_not_nil ValidatesEmailFormatOf::validate_email_format('valid@example.com', :local_length => 1)
|
143
|
-
assert_not_nil ValidatesEmailFormatOf::validate_email_format('valid@example.com', :domain_length => 1)
|
144
|
-
end
|
145
|
-
|
146
|
-
def test_validating_with_custom_regexp
|
147
|
-
assert_nil ValidatesEmailFormatOf::validate_email_format('012345@789', :with => /[0-9]+\@[0-9]+/)
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_should_respect_validate_on_option
|
151
|
-
p = create_person(:email => @valid_email)
|
152
|
-
save_passes(p)
|
153
|
-
|
154
|
-
# we only asked to validate on :create so this should fail
|
155
|
-
assert p.update_attributes(:email => @invalid_email)
|
156
|
-
assert_equal @invalid_email, p.email
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_should_allow_custom_error_message
|
160
|
-
p = create_person(:email => @invalid_email)
|
161
|
-
save_fails(p)
|
162
|
-
if ActiveRecord::VERSION::MAJOR >= 3
|
163
|
-
assert_equal 'fails with custom message', p.errors[:email].first
|
164
|
-
else
|
165
|
-
assert_equal 'fails with custom message', p.errors.on(:email)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_should_allow_nil
|
170
|
-
p = create_person(:email => nil)
|
171
|
-
save_passes(p)
|
172
|
-
|
173
|
-
p = PersonForbidNil.new(:email => nil)
|
174
|
-
save_fails(p)
|
175
|
-
end
|
176
|
-
|
177
|
-
# TODO: find a future-proof way to check DNS records
|
178
|
-
def test_check_mx
|
179
|
-
pmx = MxRecord.new(:email => 'test@dunae.ca')
|
180
|
-
save_passes(pmx)
|
181
|
-
|
182
|
-
pmx = MxRecord.new(:email => 'test@127.0.0.2')
|
183
|
-
save_fails(pmx)
|
184
|
-
end
|
185
|
-
|
186
|
-
# TODO: find a future-proof way to check DNS records
|
187
|
-
def test_check_mx_fallback_to_a
|
188
|
-
pmx = MxRecord.new(:email => 'test@code.dunae.ca')
|
189
|
-
save_passes(pmx)
|
190
|
-
end
|
191
|
-
|
192
|
-
def test_shorthand
|
193
|
-
if ActiveRecord::VERSION::MAJOR >= 3
|
194
|
-
s = Shorthand.new(:email => 'invalid')
|
195
|
-
assert !s.save
|
196
|
-
assert_equal 2, s.errors[:email].size
|
197
|
-
assert_block do
|
198
|
-
s.errors[:email].any? do |err|
|
199
|
-
err =~ /fails with shorthand message/
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_frozen_string
|
206
|
-
assert_valid(" #{@valid_email} ".freeze)
|
207
|
-
assert_invalid(" #{@invalid_email} ".freeze)
|
208
|
-
end
|
209
|
-
|
210
|
-
protected
|
211
|
-
def create_person(params)
|
212
|
-
Person.new(params)
|
213
|
-
end
|
214
|
-
|
215
|
-
def assert_valid(email)
|
216
|
-
assert_nil ValidatesEmailFormatOf::validate_email_format(email), "#{email} should be considered valid"
|
217
|
-
end
|
218
|
-
|
219
|
-
def assert_invalid(email)
|
220
|
-
assert_not_nil ValidatesEmailFormatOf::validate_email_format(email), "#{email} should not be considered valid"
|
221
|
-
end
|
222
|
-
|
223
|
-
def save_passes(p, email = '')
|
224
|
-
assert p.valid?, " #{email} should pass"
|
225
|
-
assert p.save
|
226
|
-
if ActiveRecord::VERSION::MAJOR >= 3
|
227
|
-
assert p.errors[:email].empty?
|
228
|
-
else
|
229
|
-
assert_nil p.errors.on(:email)
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
def save_fails(p, email = '')
|
234
|
-
assert !p.valid?, " #{email} should fail"
|
235
|
-
assert !p.save
|
236
|
-
if ActiveRecord::VERSION::MAJOR >= 3
|
237
|
-
assert_equal 1, p.errors[:email].size
|
238
|
-
else
|
239
|
-
assert p.errors.on(:email)
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|