Update validates_email_format_of plugin
[rails.git] / vendor / plugins / validates_email_format_of / test / validates_email_format_of_test.rb
1 require File.dirname(__FILE__) + '/test_helper'
2
3 class ValidatesEmailFormatOfTest < TEST_CASE
4   fixtures :people, :peoplemx
5
6   def setup
7     @valid_email = 'valid@example.com'
8     @invalid_email = 'invalid@example.'
9   end
10
11   def test_without_activerecord
12     assert_nil ValidatesEmailFormatOf::validate_email_format('valid@example.com')
13     err = ValidatesEmailFormatOf::validate_email_format('valid@example-com')
14     assert_equal 1, err.size
15   end
16
17   def test_should_allow_valid_email_addresses
18     ['valid@example.com',
19      'Valid@test.example.com',
20      'valid+valid123@test.example.com',
21      'valid_valid123@test.example.com',
22      'valid-valid+123@test.example.co.uk',
23      'valid-valid+1.23@test.example.com.au',
24      'valid@example.co.uk',
25      'v@example.com',
26      'valid@example.ca',
27      'valid_@example.com',
28      'valid123.456@example.org',
29      'valid123.456@example.travel',
30      'valid123.456@example.museum',
31      'valid@example.mobi',
32      'valid@example.info',
33      'valid-@example.com',
34   # from RFC 3696, page 6
35      'customer/department=shipping@example.com',
36      '$A12345@example.com',
37      '!def!xyz%abc@example.com',
38      '_somename@example.com',
39   # apostrophes
40      "test'test@example.com",
41      ].each do |email|
42       p = create_person(:email => email)
43       save_passes(p, email)
44     end
45   end
46
47   def test_should_not_allow_invalid_email_addresses
48     ['invalid@example-com',
49   # period can not start local part
50      '.invalid@example.com',
51   # period can not end local part
52      'invalid.@example.com', 
53   # period can not appear twice consecutively in local part
54      'invali..d@example.com',
55   # should not allow underscores in domain names
56      'invalid@ex_mple.com',
57      'invalid@example.com.',
58      'invalid@example.com_',
59      'invalid@example.com-',
60      'invalid-example.com',
61      'invalid@example.b#r.com',
62      'invalid@example.c',
63      'invali d@example.com',
64      'invalidexample.com',
65      'invalid@example.'].each do |email|
66       p = create_person(:email => email)
67       save_fails(p, email)
68     end
69   end
70
71   # from http://www.rfc-editor.org/errata_search.php?rfc=3696
72   def test_should_allow_quoted_characters
73     ['"Abc\@def"@example.com',     
74      '"Fred\ Bloggs"@example.com',
75      '"Joe.\\Blow"@example.com',
76      ].each do |email|
77       p = create_person(:email => email)
78       save_passes(p, email)
79     end
80   end
81
82   # from http://tools.ietf.org/html/rfc3696, page 5
83   # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
84   def test_should_not_allow_escaped_characters_without_quotes
85     ['Fred\ Bloggs_@example.com',
86      'Abc\@def+@example.com',
87      'Joe.\\Blow@example.com'
88      ].each do |email|
89       p = create_person(:email => email)
90       save_fails(p, email)
91     end
92   end
93
94   def test_should_check_length_limits
95     ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@example.com',
96      'test@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com'
97      ].each do |email|
98       p = create_person(:email => email)
99       save_fails(p, email)
100     end
101   end
102
103   def test_should_respect_validate_on_option
104     p = create_person(:email => @valid_email)
105     save_passes(p)
106     
107     # we only asked to validate on :create so this should fail
108     assert p.update_attributes(:email => @invalid_email)
109     assert_equal @invalid_email, p.email
110   end
111   
112   def test_should_allow_custom_error_message
113     p = create_person(:email => @invalid_email)
114     save_fails(p)
115     assert_equal 'fails with custom message', p.errors.on(:email)
116   end
117
118   def test_should_allow_nil
119     p = create_person(:email => nil)
120     save_passes(p)
121   end
122
123   def test_check_mx
124     pmx = MxRecord.new(:email => 'test@dunae.ca')
125     save_passes(pmx)
126
127     pmx = MxRecord.new(:email => 'test@example.com')
128     save_fails(pmx)
129   end
130
131   protected
132     def create_person(params)
133       Person.new(params)
134     end
135
136     def save_passes(p, email = '')
137       assert p.valid?, " validating #{email}"
138       assert p.save
139       assert_nil p.errors.on(:email)
140     end
141     
142     def save_fails(p, email = '')
143       assert !p.valid?, " validating #{email}"
144       assert !p.save
145       assert p.errors.on(:email)
146     end
147 end