activevalidators 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ require 'test_helper.rb'
2
+
3
+ describe "Phone Validation" do
4
+ def build_phone_validation phone, attrs = {}
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :phone, :phone => phone
7
+ TestRecord.new attrs
8
+ end
9
+
10
+
11
+ describe "when no country is given" do
12
+ it 'should validate format of phone with ###-###-####' do
13
+ subject = build_phone_validation true
14
+ subject.phone = '999-999-9999'
15
+ subject.valid?.must_equal true
16
+ subject.errors.size.must_equal 0
17
+ end
18
+
19
+ it 'should validate format of phone with ##########' do
20
+ subject = build_phone_validation true
21
+ subject.phone = '9999999999'
22
+ subject.valid?.must_equal true
23
+ subject.errors.size.must_equal 0
24
+ end
25
+
26
+ it 'should validate format of phone with ###.###.####' do
27
+ subject = build_phone_validation true
28
+ subject.phone = '999.999.9999'
29
+ subject.valid?.must_equal true
30
+ subject.errors.size.must_equal 0
31
+ end
32
+
33
+ it 'should validate format of phone with ### ### ####' do
34
+ subject = build_phone_validation true
35
+ subject.phone = '999 999 9999'
36
+ subject.valid?.must_equal true
37
+ subject.errors.size.must_equal 0
38
+ end
39
+
40
+ it 'should validate format of phone with (###) ###-####' do
41
+ subject = build_phone_validation true
42
+ subject.phone = '(999) 999-9999'
43
+ subject.valid?.must_equal true
44
+ subject.errors.size.must_equal 0
45
+ end
46
+
47
+ end
48
+
49
+ ActiveModel::Validations::PhoneValidator.known_formats.each do |country, formats|
50
+ describe "when given a :#{country} country parameter" do
51
+ formats.each do |format|
52
+ it "should validate format of phone with #{format}" do
53
+ subject = build_phone_validation :country => country
54
+ subject.phone = format.gsub('#','9')
55
+ subject.valid?.must_equal true
56
+ subject.errors.size.must_equal 0
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+
63
+ describe "for invalid formats" do
64
+ it "rejects invalid formats" do
65
+ subject = build_phone_validation true, :phone => '999'
66
+ subject.valid?.must_equal false
67
+ subject.errors.size.must_equal 1
68
+ end
69
+
70
+ it "generates an error message of type invalid" do
71
+ subject = build_phone_validation true, :phone => '999'
72
+ subject.valid?.must_equal false
73
+ subject.errors[:phone].include?(subject.errors.generate_message(:phone, :invalid)).must_equal true
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper.rb'
2
+
3
+ describe "Postal Code Validation" do
4
+ def build_postal_code_record postal_code, attrs = {}
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :postal_code, :postal_code => postal_code
7
+ TestRecord.new attrs
8
+ end
9
+
10
+ describe "when no country is given" do
11
+ it 'should validate format of postal code with #####' do
12
+ subject = build_postal_code_record true
13
+ subject.postal_code = '11211'
14
+ subject.valid?.must_equal true
15
+ subject.errors.size.must_equal 0
16
+ end
17
+
18
+ it 'should validate format of postal code with #####-#####' do
19
+ subject = build_postal_code_record true
20
+ subject.postal_code = '94117-1234'
21
+ subject.valid?.must_equal true
22
+ subject.errors.size.must_equal 0
23
+ end
24
+ end
25
+
26
+ ActiveModel::Validations::PostalCodeValidator.known_formats.each do |country, formats|
27
+ describe "when given a :#{country} country parameter" do
28
+ formats.each do |format|
29
+ it "should validate format of postal code with #{format}" do
30
+ subject = build_postal_code_record :country => country
31
+ subject.postal_code = format.gsub('#','9')
32
+ subject.valid?.must_equal true
33
+ subject.errors.size.must_equal 0
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+ describe "for invalid formats" do
41
+ it "rejects invalid formats" do
42
+ subject = build_postal_code_record true, :postal_code => '999'
43
+ subject.valid?.must_equal false
44
+ subject.errors.size.must_equal 1
45
+ end
46
+
47
+ it "generates an error message of type invalid" do
48
+ subject = build_postal_code_record true, :postal_code => '999'
49
+ subject.valid?.must_equal false
50
+ subject.errors[:postal_code].include?(subject.errors.generate_message(:postal_code, :invalid)).must_equal true
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper.rb'
2
+
3
+ describe "Respond To Validation" do
4
+ def build_respond_to_record attrs = {}
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :responder, :respond_to => { :call => true, :if => :local_condition }, :if => :global_condition
7
+ TestRecord.new attrs
8
+ end
9
+
10
+ it "respond_to?" do
11
+ subject = build_respond_to_record
12
+ subject.responder = lambda {}
13
+ subject.global_condition = true
14
+ subject.local_condition = true
15
+
16
+ subject.valid?.must_equal true
17
+ subject.errors.size.must_equal 0
18
+ end
19
+
20
+ describe "when does not respond_to?" do
21
+ it "rejects the responder" do
22
+ subject = build_respond_to_record :responder => 42, :global_condition => true, :local_condition => true
23
+ subject.valid?.must_equal false
24
+ subject.errors.size.must_equal 1
25
+ end
26
+
27
+ it "generates an error message of type invalid" do
28
+ subject = build_respond_to_record :responder => 42, :global_condition => true, :local_condition => true
29
+ subject.valid?.must_equal false
30
+ subject.errors[:responder].include?(subject.errors.generate_message(:responder, :invalid)).must_equal true
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper.rb'
2
+
3
+ describe "Slug Validation" do
4
+ def build_slug_validation attrs = {}
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :slug, :slug => true
7
+ TestRecord.new attrs
8
+ end
9
+
10
+ it "accepts valid slugs" do
11
+ subject = build_slug_validation
12
+ subject.slug = '1234567890-foo-bar-bar'
13
+ subject.valid?.must_equal true
14
+ subject.errors.size.must_equal 0
15
+ end
16
+
17
+ describe "for invalid slugs" do
18
+ it "rejects invalid slugs" do
19
+ subject = build_slug_validation :slug => '@#$%^'
20
+ subject.valid?.must_equal false
21
+ subject.errors.size.must_equal 1
22
+ end
23
+
24
+ it "generates an error message of type invalid" do
25
+ subject = build_slug_validation :slug => '@#$%^'
26
+ subject.valid?.must_equal false
27
+ subject.errors[:slug].include?(subject.errors.generate_message(:slug, :invalid)).must_equal true
28
+ end
29
+ end
30
+
31
+ describe "for empty slugs" do
32
+ it "generates an error message of type blank" do
33
+ subject = build_slug_validation :slug => nil
34
+ subject.valid?.must_equal false
35
+ subject.errors[:slug].include?(subject.errors.generate_message(:slug, :blank)).must_equal true
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,151 @@
1
+ require 'test_helper.rb'
2
+
3
+ describe "Tracking Number Validation" do
4
+ def build_tracking_number_record carrier_opts, attrs = {}
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :tracking_number, :tracking_number => carrier_opts
7
+ TestRecord.new(attrs).tap do |record|
8
+ yield record if block_given?
9
+ end
10
+ end
11
+
12
+ def assert_valid_tracking_number(carrier_opts, tracking_number, &block)
13
+ subject = build_tracking_number_record(carrier_opts, &block)
14
+ subject.tracking_number = tracking_number
15
+ subject.valid?.must_equal true
16
+ subject.errors.size.must_equal 0
17
+ end
18
+
19
+ def assert_invalid_tracking_number(carrier_opts, tracking_number, &block)
20
+ subject = build_tracking_number_record(carrier_opts, &block)
21
+ subject.tracking_number = tracking_number
22
+ subject.valid?.must_equal false
23
+ subject.errors.size.must_equal 1
24
+ subject.errors[:tracking_number].must_include subject.errors.generate_message(:tracking_number, :invalid)
25
+ end
26
+
27
+ describe "when no carrier parameter is given" do
28
+ it "raises an exception" do
29
+ assert_raises(RuntimeError, "Carrier option required") { build_tracking_number_record({:carrier=>true}).valid? }
30
+ end
31
+ end
32
+
33
+ describe "when given a ups carrier parameter" do
34
+ it 'should validate format of tracking number with 1Z................' do
35
+ assert_valid_tracking_number({:carrier => :ups}, '1Z12345E0205271688')
36
+ end
37
+
38
+ it 'should validate format of tracking number with ............' do
39
+ assert_valid_tracking_number({:carrier => :ups}, '9999V999J999')
40
+ end
41
+
42
+ it 'should validate format of tracking number with T..........' do
43
+ assert_valid_tracking_number({:carrier => :ups}, 'T99F99E9999')
44
+ end
45
+
46
+ it 'should validate format of tracking number with .........' do
47
+ assert_valid_tracking_number({:carrier => :ups}, '990728071')
48
+ end
49
+
50
+ describe "for invalid formats" do
51
+ it "rejects invalid formats and generates an error message of type invalid" do
52
+ assert_invalid_tracking_number({:carrier => :ups}, '1Z12345E020_271688')
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "when given a usps carrier parameter" do
58
+ describe "with valid formats" do
59
+ it 'USS39 tracking number with valid MOD10 check digit' do
60
+ assert_valid_tracking_number({:carrier => :usps}, 'EA123456784US')
61
+ end
62
+
63
+ it 'USS39 tracking number with valid MOD11 check digit' do
64
+ assert_valid_tracking_number({:carrier => :usps}, 'RB123456785US')
65
+ end
66
+
67
+ it '20 character USS128 tracking number with valid MOD10 check digit' do
68
+ assert_valid_tracking_number({:carrier => :usps}, '71123456789123456787')
69
+ end
70
+
71
+ it '22 character USS128 tracking number with valid MOD10 check digit' do
72
+ assert_valid_tracking_number({:carrier => :usps}, '9171969010756003077385')
73
+ end
74
+ end
75
+
76
+ describe "with invalid formats" do
77
+ it 'USS39 tracking number with invalid check digit' do
78
+ assert_invalid_tracking_number({:carrier => :usps}, 'EA123456782US')
79
+ end
80
+
81
+ it 'USS39 tracking number that is too short' do
82
+ assert_invalid_tracking_number({:carrier => :usps}, '123456784US')
83
+ end
84
+
85
+ it 'USS39 tracking number that is too long' do
86
+ assert_invalid_tracking_number({:carrier => :usps}, 'EAB123456784US')
87
+ end
88
+
89
+ it 'USS39 tracking number with non-"US" product id' do
90
+ assert_invalid_tracking_number({:carrier => :usps}, 'EA123456784UT')
91
+ end
92
+
93
+ it 'USS128 tracking number with invalid check-digit' do
94
+ assert_invalid_tracking_number({:carrier => :usps}, '71123456789123456788')
95
+ end
96
+
97
+ it 'USS128 tracking number that is too short' do
98
+ assert_invalid_tracking_number({:carrier => :usps}, '7112345678912345678')
99
+ end
100
+
101
+ it 'USS128 tracking number that is too long' do
102
+ assert_invalid_tracking_number({:carrier => :usps}, '711234567891234567879287')
103
+ end
104
+
105
+ it 'USS128 tracking number with invalid chars' do
106
+ assert_invalid_tracking_number({:carrier => :usps}, 'U11234567891234567879')
107
+ end
108
+ end
109
+ end
110
+
111
+ describe "when given a record-based carrier parameter" do
112
+ describe "when record gives 'ups' as carrier" do
113
+ it 'with valid ups tracking number' do
114
+ assert_valid_tracking_number({:carrier_field => :carrier}, '1Z12345E0205271688') do |subject|
115
+ subject.carrier = 'ups'
116
+ end
117
+ end
118
+
119
+ it 'with invalid ups tracking number' do
120
+ assert_invalid_tracking_number({:carrier_field => :carrier}, '1Z12__5E0205271688') do |subject|
121
+ subject.carrier = 'ups'
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "when record gives 'usps' as carrier" do
127
+ it 'with valid usps tracking number' do
128
+ assert_valid_tracking_number({:carrier_field => :carrier}, 'EA123456784US') do |subject|
129
+ subject.carrier = 'usps'
130
+ end
131
+ end
132
+
133
+ it 'with invalid usps tracking number' do
134
+ assert_invalid_tracking_number({:carrier_field => :carrier}, 'EA123456784UZ') do |subject|
135
+ subject.carrier = 'usps'
136
+ end
137
+ end
138
+ end
139
+
140
+ describe "when record gives an unsupported carrier" do
141
+ it "raises an error when validating" do
142
+ assert_raises(RuntimeError, "Carrier option required") do
143
+ build_tracking_number_record({:carrier=>:carrier}, :tracking_number => 'EA123456784US') do |subject|
144
+ subject.carrier = 'fedex'
145
+ subject.tracking_number = 'EA123456784US'
146
+ end.valid?
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,161 @@
1
+ # coding: utf-8
2
+ require 'test_helper.rb'
3
+
4
+ describe "Twitter Validation" do
5
+ def build_twitter_record format, attrs = {}
6
+ TestRecord.reset_callbacks(:validate)
7
+ TestRecord.validates :twitter_username, :twitter => format
8
+ TestRecord.new attrs
9
+ end
10
+
11
+ it "rejects invalid urls" do
12
+ subject = build_twitter_record true
13
+ subject.valid?.must_equal false
14
+ subject.errors.size.must_equal 1
15
+ end
16
+
17
+ it "generates an error message of type blank" do
18
+ subject = build_twitter_record true
19
+ subject.valid?.must_equal false
20
+ subject.errors[:twitter_username].include?(subject.errors.generate_message(:twitter_username, :blank)).must_equal true
21
+ end
22
+
23
+ describe "for twitter url validator" do
24
+ it "validates with http" do
25
+ subject = build_twitter_record :format => :url
26
+ subject.twitter_username = 'http://twitter.com/garrettb'
27
+ subject.valid?.must_equal true
28
+ end
29
+
30
+ it "validates with https protocol" do
31
+ subject = build_twitter_record :format => :url
32
+ subject.twitter_username = 'https://twitter.com/garrettb'
33
+ subject.valid?.must_equal true
34
+ end
35
+
36
+ it "generate error with ftp protocol" do
37
+ subject = build_twitter_record :format => :url
38
+ subject.twitter_username = 'ftp://twitter.com/garrettb'
39
+ subject.valid?.must_equal false
40
+ subject.errors.size.must_equal 1
41
+ end
42
+
43
+ it "validates with www and http" do
44
+ subject = build_twitter_record :format => :url
45
+ subject.twitter_username = 'http://www.twitter.com/garrettb'
46
+ subject.valid?.must_equal true
47
+ end
48
+
49
+ it "generate error without www dot" do
50
+ subject = build_twitter_record :format => :url
51
+ subject.twitter_username = 'http://wwwtwitter.com/garrettb'
52
+ subject.valid?.must_equal false
53
+ subject.errors.size.must_equal 1
54
+ end
55
+
56
+ it "generate error without no username" do
57
+ subject = build_twitter_record :format => :url
58
+ subject.twitter_username = 'http://twitter.com'
59
+ subject.valid?.must_equal false
60
+ subject.errors.size.must_equal 1
61
+ end
62
+
63
+ it "generate error without no username and trailing slash" do
64
+ subject = build_twitter_record :format => :url
65
+ subject.twitter_username = 'http://twitter.com/'
66
+ subject.valid?.must_equal false
67
+ subject.errors.size.must_equal 1
68
+ end
69
+
70
+ it "generate error with too long of username" do
71
+ subject = build_twitter_record :format => :url
72
+ subject.twitter_username = 'http://twitter.com/garrettbjerkhoelwashere'
73
+ subject.valid?.must_equal false
74
+ subject.errors.size.must_equal 1
75
+ end
76
+
77
+ it "generate error with invalid character" do
78
+ subject = build_twitter_record :format => :url
79
+ subject.twitter_username = 'http://twitter.com/garrettbjerkhoé'
80
+ subject.valid?.must_equal false
81
+ subject.errors.size.must_equal 1
82
+ end
83
+ end
84
+
85
+ describe "for twitter at sign validator" do
86
+ it "validate with valid username" do
87
+ subject = build_twitter_record :format => :username_with_at
88
+ subject.twitter_username = '@garrettb'
89
+ subject.valid?.must_equal true
90
+ end
91
+
92
+ it "validate with one character" do
93
+ subject = build_twitter_record :format => :username_with_at
94
+ subject.twitter_username = '@a'
95
+ subject.valid?.must_equal true
96
+ end
97
+
98
+ it "generate error with too long of username" do
99
+ subject = build_twitter_record :format => :username_with_at
100
+ subject.twitter_username = '@garrettbjerkhoelwashere'
101
+ subject.valid?.must_equal false
102
+ subject.errors.size.must_equal 1
103
+ end
104
+
105
+ it "generate error with no username" do
106
+ subject = build_twitter_record :format => :username_with_at
107
+ subject.twitter_username = '@'
108
+ subject.valid?.must_equal false
109
+ subject.errors.size.must_equal 1
110
+ end
111
+
112
+ it "generate error with invalid character" do
113
+ subject = build_twitter_record :format => :username_with_at
114
+ subject.twitter_username = '@érik'
115
+ subject.valid?.must_equal false
116
+ subject.errors.size.must_equal 1
117
+ end
118
+ end
119
+
120
+ describe "for twitter without at sign validator" do
121
+ it "validate with valid username" do
122
+ subject = build_twitter_record true
123
+ subject.twitter_username = 'garrettb'
124
+ subject.valid?.must_equal true
125
+ end
126
+
127
+ it "validate with one character" do
128
+ subject = build_twitter_record true
129
+ subject.twitter_username = 'a'
130
+ subject.valid?.must_equal true
131
+ end
132
+
133
+ it "generate error with too long of username" do
134
+ subject = build_twitter_record true
135
+ subject.twitter_username = 'garrettbjerkhoelwashere'
136
+ subject.valid?.must_equal false
137
+ subject.errors.size.must_equal 1
138
+ end
139
+
140
+ it "generate error with no username" do
141
+ subject = build_twitter_record true
142
+ subject.twitter_username = ''
143
+ subject.valid?.must_equal false
144
+ subject.errors.size.must_equal 1
145
+ end
146
+
147
+ it "generate error with invalid character" do
148
+ subject = build_twitter_record true
149
+ subject.twitter_username = 'érik'
150
+ subject.valid?.must_equal false
151
+ subject.errors.size.must_equal 1
152
+ end
153
+
154
+ it "generate error with at sign character" do
155
+ subject = build_twitter_record true
156
+ subject.twitter_username = '@garrettb'
157
+ subject.valid?.must_equal false
158
+ subject.errors.size.must_equal 1
159
+ end
160
+ end
161
+ end