validators 2.5.3 → 2.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +8 -4
- data/Rakefile +9 -4
- data/data/disposable.json +1412 -23
- data/data/tld.json +391 -5
- data/lib/validators/version.rb +1 -1
- data/{spec → test}/schema.rb +0 -0
- data/{spec → test}/support/dates.rb +0 -0
- data/{spec → test}/support/emails.rb +0 -0
- data/{spec → test}/support/hostnames.rb +0 -0
- data/{spec → test}/support/ips.rb +0 -0
- data/{spec → test}/support/models.rb +0 -0
- data/{spec → test}/support/translations.yml +0 -0
- data/{spec → test}/support/urls.rb +0 -0
- data/test/test_helper.rb +41 -0
- data/test/validators/disposable_email_test.rb +23 -0
- data/test/validators/ip_test.rb +21 -0
- data/{spec/validators/validates_cnpj_format_of_spec.rb → test/validators/validates_cnpj_format_of_test.rb} +10 -10
- data/{spec/validators/validates_cpf_format_of_spec.rb → test/validators/validates_cpf_format_of_test.rb} +10 -10
- data/test/validators/validates_datetime/after_option_test.rb +75 -0
- data/test/validators/validates_datetime/before_option_test.rb +20 -0
- data/test/validators/validates_datetime/defaults_test.rb +37 -0
- data/test/validators/validates_email_format_of_test.rb +76 -0
- data/test/validators/validates_hostname_format_of/with_tld_validation_test.rb +25 -0
- data/test/validators/validates_hostname_format_of/without_tld_validation_test.rb +27 -0
- data/test/validators/validates_ip_address/ipv4_test.rb +20 -0
- data/test/validators/validates_ip_address/ipv6_test.rb +20 -0
- data/test/validators/validates_ip_address_test.rb +33 -0
- data/test/validators/validates_ownership_of_test.rb +65 -0
- data/test/validators/validates_ssh_private_key/bits_test.rb +44 -0
- data/test/validators/validates_ssh_private_key/common_test.rb +37 -0
- data/test/validators/validates_ssh_private_key/dsa_test.rb +36 -0
- data/test/validators/validates_ssh_private_key/rsa_test.rb +36 -0
- data/test/validators/validates_ssh_public_key_test.rb +37 -0
- data/test/validators/validates_url_format_of/with_tld_validation_test.rb +15 -0
- data/test/validators/validates_url_format_of/without_tld_validation_test.rb +48 -0
- data/validators.gemspec +2 -2
- metadata +65 -49
- data/.rspec +0 -1
- data/spec/spec_helper.rb +0 -38
- data/spec/validators/disposable_email_spec.rb +0 -31
- data/spec/validators/ip_spec.rb +0 -25
- data/spec/validators/validates_datetime_spec.rb +0 -125
- data/spec/validators/validates_email_format_of_spec.rb +0 -70
- data/spec/validators/validates_hostname_format_of_spec.rb +0 -53
- data/spec/validators/validates_ip_address_spec.rb +0 -59
- data/spec/validators/validates_ownership_of_spec.rb +0 -69
- data/spec/validators/validates_ssh_private_key_spec.rb +0 -152
- data/spec/validators/validates_ssh_public_key_spec.rb +0 -39
- data/spec/validators/validates_url_format_of_spec.rb +0 -65
data/lib/validators/version.rb
CHANGED
data/{spec → test}/schema.rb
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "active_record"
|
6
|
+
require "validators"
|
7
|
+
require "active_support/all"
|
8
|
+
|
9
|
+
require "minitest/utils"
|
10
|
+
require "minitest/autorun"
|
11
|
+
|
12
|
+
Time.zone = "America/Sao_Paulo"
|
13
|
+
TLDs = Validators::TLD.all.sample(50)
|
14
|
+
DISPOSABLE_EMAILS = Validators::DisposableHostnames.all.sample(50)
|
15
|
+
|
16
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }
|
17
|
+
|
18
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
19
|
+
load "schema.rb"
|
20
|
+
|
21
|
+
I18n.enforce_available_locales = false
|
22
|
+
I18n.load_path << File.dirname(__FILE__) + "/support/translations.yml"
|
23
|
+
|
24
|
+
module Minitest
|
25
|
+
class Test
|
26
|
+
setup do
|
27
|
+
I18n.locale = :en
|
28
|
+
Time.zone = "America/Sao_Paulo"
|
29
|
+
|
30
|
+
ActiveRecord::Base.descendants.each do |model|
|
31
|
+
next if %w[ActiveRecord::InternalMetadata ActiveRecord::SchemaMigration].include?(model.name)
|
32
|
+
|
33
|
+
model.delete_all
|
34
|
+
|
35
|
+
Object.class_eval { remove_const model.name if const_defined?(model.name) }
|
36
|
+
end
|
37
|
+
|
38
|
+
load File.dirname(__FILE__) + "/support/models.rb"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class DisposableEmailTest < Minitest::Test
|
4
|
+
DISPOSABLE_EMAILS.each do |domain|
|
5
|
+
test "rejects disposable e-mail (#{domain})" do
|
6
|
+
User.validates_email_format_of :email
|
7
|
+
|
8
|
+
user = User.new(email: "user@#{domain}")
|
9
|
+
user.valid?
|
10
|
+
|
11
|
+
assert_includes user.errors[:email], I18n.t("activerecord.errors.messages.disposable_email")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
test "accepts disposable e-mail" do
|
16
|
+
User.validates_email_format_of :email, disposable: true
|
17
|
+
|
18
|
+
user = User.new(email: "user@mailinator.com")
|
19
|
+
user.valid?
|
20
|
+
|
21
|
+
assert user.errors[:email].empty?
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class IpTest < Minitest::Test
|
4
|
+
VALID_IPV4.each do |ip|
|
5
|
+
test "accepts #{ip.inspect} as ip address (v4)" do
|
6
|
+
assert Validators::Ip.v4?(ip)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
VALID_IPV6.each do |ip|
|
11
|
+
test "accepts #{ip.inspect} as ip address (v6)" do
|
12
|
+
assert Validators::Ip.v6?(ip)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
(VALID_IPV4 + VALID_IPV6).each do |ip|
|
17
|
+
test "accepts #{ip.inspect} as ip address" do
|
18
|
+
assert Validators::Ip.valid?(ip)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require "
|
1
|
+
require "test_helper"
|
2
2
|
|
3
|
-
|
3
|
+
class ValidatesCnpjFormatOfTest < Minitest::Test
|
4
4
|
let(:model) { Class.new {
|
5
5
|
def self.name
|
6
6
|
"User"
|
@@ -11,33 +11,33 @@ describe ".validates_cnpj_format_of" do
|
|
11
11
|
attr_accessor :document
|
12
12
|
} }
|
13
13
|
|
14
|
-
|
14
|
+
test "requires valid CNPJ" do
|
15
15
|
record = model.new(document: "invalid")
|
16
16
|
record.valid?
|
17
17
|
|
18
|
-
|
18
|
+
refute record.errors[:document].empty?
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
test "accepts formatted CNPJ" do
|
22
22
|
record = model.new(document: CNPJ.generate(true))
|
23
23
|
record.valid?
|
24
24
|
|
25
|
-
|
25
|
+
assert record.errors[:document].empty?
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
test "accepts stripped CNPJ" do
|
29
29
|
record = model.new(document: CNPJ.generate)
|
30
30
|
record.valid?
|
31
31
|
|
32
|
-
|
32
|
+
assert record.errors[:document].empty?
|
33
33
|
end
|
34
34
|
|
35
|
-
|
35
|
+
test "sets translated error message" do
|
36
36
|
I18n.locale = "pt-BR"
|
37
37
|
|
38
38
|
record = model.new
|
39
39
|
record.valid?
|
40
40
|
|
41
|
-
|
41
|
+
assert_includes record.errors[:document], "não é um CNPJ válido"
|
42
42
|
end
|
43
43
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require "
|
1
|
+
require "test_helper"
|
2
2
|
|
3
|
-
|
3
|
+
class ValidatesCpfFormatOfTest < Minitest::Test
|
4
4
|
let(:model) { Class.new {
|
5
5
|
def self.name
|
6
6
|
"User"
|
@@ -11,33 +11,33 @@ describe ".validates_cpf_format_of" do
|
|
11
11
|
attr_accessor :document
|
12
12
|
} }
|
13
13
|
|
14
|
-
|
14
|
+
test "requires valid CPF" do
|
15
15
|
record = model.new(document: "invalid")
|
16
16
|
record.valid?
|
17
17
|
|
18
|
-
|
18
|
+
refute record.errors[:document].empty?
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
test "accepts formatted CPF" do
|
22
22
|
record = model.new(document: CPF.generate(true))
|
23
23
|
record.valid?
|
24
24
|
|
25
|
-
|
25
|
+
assert record.errors[:document].empty?
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
test "accepts stripped CPF" do
|
29
29
|
record = model.new(document: CPF.generate)
|
30
30
|
record.valid?
|
31
31
|
|
32
|
-
|
32
|
+
assert record.errors[:document].empty?
|
33
33
|
end
|
34
34
|
|
35
|
-
|
35
|
+
test "sets translated error message" do
|
36
36
|
I18n.locale = "pt-BR"
|
37
37
|
|
38
38
|
record = model.new
|
39
39
|
record.valid?
|
40
40
|
|
41
|
-
|
41
|
+
assert_includes record.errors[:document], "não é um CPF válido"
|
42
42
|
end
|
43
43
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ValidatesDatetimeAfterOptionTest < Minitest::Test
|
4
|
+
let(:user) { User.new }
|
5
|
+
|
6
|
+
test "rejects when date is set to before :after option" do
|
7
|
+
future_date = 1.week.from_now
|
8
|
+
User.validates_datetime :registered_at, :after => future_date
|
9
|
+
user.registered_at = Time.now
|
10
|
+
|
11
|
+
refute user.valid?
|
12
|
+
assert_includes user.errors[:registered_at], "needs to be after #{I18n.l(future_date)}"
|
13
|
+
end
|
14
|
+
|
15
|
+
test "accepts when date is set accordingly to the :after option" do
|
16
|
+
User.validates_datetime :registered_at, :after => 1.week.from_now
|
17
|
+
user.registered_at = 2.weeks.from_now
|
18
|
+
|
19
|
+
assert user.valid?
|
20
|
+
end
|
21
|
+
|
22
|
+
test "validates using today as date" do
|
23
|
+
User.validates_datetime :registered_at, :after => :today
|
24
|
+
|
25
|
+
user.registered_at = Time.now
|
26
|
+
refute user.valid?
|
27
|
+
|
28
|
+
user.registered_at = Date.today
|
29
|
+
refute user.valid?
|
30
|
+
|
31
|
+
user.registered_at = Date.tomorrow
|
32
|
+
assert user.valid?
|
33
|
+
|
34
|
+
user.registered_at = 1.day.from_now
|
35
|
+
assert user.valid?
|
36
|
+
end
|
37
|
+
|
38
|
+
test "validates using now as date" do
|
39
|
+
User.validates_datetime :registered_at, :after => :now
|
40
|
+
|
41
|
+
user.registered_at = Time.now
|
42
|
+
refute user.valid?
|
43
|
+
|
44
|
+
user.registered_at = Date.today
|
45
|
+
refute user.valid?
|
46
|
+
|
47
|
+
user.registered_at = Date.tomorrow
|
48
|
+
assert user.valid?
|
49
|
+
|
50
|
+
user.registered_at = 1.day.from_now
|
51
|
+
assert user.valid?
|
52
|
+
end
|
53
|
+
|
54
|
+
test "validates using method as date" do
|
55
|
+
User.validates_datetime :starts_at
|
56
|
+
User.validates_datetime :ends_at, :after => :starts_at, :if => :starts_at?
|
57
|
+
|
58
|
+
user.starts_at = nil
|
59
|
+
user.ends_at = Time.now
|
60
|
+
|
61
|
+
refute user.valid?
|
62
|
+
assert user.errors[:ends_at].empty?
|
63
|
+
|
64
|
+
user.starts_at = Time.parse("Apr 26 2010")
|
65
|
+
user.ends_at = Time.parse("Apr 25 2010")
|
66
|
+
|
67
|
+
refute user.valid?
|
68
|
+
assert_includes user.errors[:ends_at], "needs to be after #{I18n.l(Time.parse("Apr 26 2010"))}"
|
69
|
+
|
70
|
+
user.starts_at = Time.now
|
71
|
+
user.ends_at = 1.hour.from_now
|
72
|
+
|
73
|
+
assert user.valid?
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ValidatesDatetimeBeforeOptionTest < Minitest::Test
|
4
|
+
let(:user) { User.new }
|
5
|
+
|
6
|
+
test "rejects when date is set to after :before option" do
|
7
|
+
User.validates_datetime :registered_at, :before => 1.week.ago
|
8
|
+
user.registered_at = Time.now
|
9
|
+
|
10
|
+
refute user.valid?
|
11
|
+
assert_includes user.errors[:registered_at], "needs to be before #{I18n.l(1.week.ago)}"
|
12
|
+
end
|
13
|
+
|
14
|
+
test "accepts when date is set accordingly to the :before option" do
|
15
|
+
User.validates_datetime :registered_at, :before => 1.week.ago
|
16
|
+
user.registered_at = 2.weeks.ago
|
17
|
+
|
18
|
+
assert user.valid?
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ValidatesDatetimeDefaultsTest < Minitest::Test
|
4
|
+
let(:user) { User.new }
|
5
|
+
|
6
|
+
setup do
|
7
|
+
User.validates_datetime :registered_at
|
8
|
+
User.validates :birthday, :datetime => true
|
9
|
+
end
|
10
|
+
|
11
|
+
VALID_DATES.each do |date|
|
12
|
+
test "accepts #{date.inspect} as valid date" do
|
13
|
+
user.registered_at = date
|
14
|
+
user.birthday = date
|
15
|
+
|
16
|
+
assert user.valid?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
INVALID_DATES.each do |date|
|
21
|
+
test "rejects #{date.inspect} as valid date" do
|
22
|
+
user.registered_at = date
|
23
|
+
user.birthday = date
|
24
|
+
|
25
|
+
refute user.valid?
|
26
|
+
refute user.errors[:registered_at].empty?
|
27
|
+
refute user.errors[:birthday].empty?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
test "includes default error message" do
|
32
|
+
user.registered_at = nil
|
33
|
+
|
34
|
+
refute user.valid?
|
35
|
+
assert_includes user.errors[:registered_at], "is not a valid date"
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ValidatesEmailFormatOfTest < Minitest::Test
|
4
|
+
setup do
|
5
|
+
User.validates_email_format_of :email, :corporate_email, :allow_blank => false
|
6
|
+
Buyer.validates_email_format_of :email, :message => "is not a valid e-mail"
|
7
|
+
Person.validates :email, :email => true
|
8
|
+
end
|
9
|
+
|
10
|
+
VALID_EMAILS.each do |email|
|
11
|
+
test "accepts #{email.inspect} as a valid email" do
|
12
|
+
user = User.new(:email => email, :corporate_email => email)
|
13
|
+
assert user.valid?
|
14
|
+
|
15
|
+
user = Person.new(:email => email)
|
16
|
+
assert user.valid?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
INVALID_EMAILS.each do |email|
|
21
|
+
test "rejects #{email.inspect} as a valid email" do
|
22
|
+
user = User.new(:email => "invalid", :corporate_email => "invalid")
|
23
|
+
refute user.valid?
|
24
|
+
|
25
|
+
user = Person.new(:email => "invalid")
|
26
|
+
refute user.valid?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
test "uses default error message" do
|
31
|
+
user = User.new(:email => "invalid")
|
32
|
+
|
33
|
+
refute user.valid?
|
34
|
+
assert_includes user.errors[:email], "is not a valid address"
|
35
|
+
end
|
36
|
+
|
37
|
+
test "rejects nil value" do
|
38
|
+
user = User.new(:email => nil)
|
39
|
+
|
40
|
+
refute user.valid?
|
41
|
+
refute user.errors[:email].empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
test "rejects empty value" do
|
45
|
+
user = User.new(:email => "")
|
46
|
+
|
47
|
+
refute user.valid?
|
48
|
+
refute user.errors[:email].empty?
|
49
|
+
end
|
50
|
+
|
51
|
+
test "validates multiple attributes" do
|
52
|
+
user = User.new(:corporate_email => "invalid")
|
53
|
+
|
54
|
+
refute user.valid?
|
55
|
+
assert_includes user.errors[:corporate_email], "is not a valid address"
|
56
|
+
end
|
57
|
+
|
58
|
+
test "uses custom error message as :message options" do
|
59
|
+
buyer = Buyer.new(:email => "invalid")
|
60
|
+
|
61
|
+
refute buyer.valid?
|
62
|
+
assert_includes buyer.errors[:email], "is not a valid e-mail"
|
63
|
+
end
|
64
|
+
|
65
|
+
test "uses I18n string as error message [pt-BR]" do
|
66
|
+
I18n.locale = :'pt-BR'
|
67
|
+
user = User.new(:email => "invalid")
|
68
|
+
|
69
|
+
refute user.valid?
|
70
|
+
assert_includes user.errors[:email], "não parece ser um e-mail válido"
|
71
|
+
end
|
72
|
+
|
73
|
+
test "defines alias method" do
|
74
|
+
assert User.respond_to?(:validates_email)
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class WithTldValidationTest < Minitest::Test
|
4
|
+
test "rejects invalid TLD" do
|
5
|
+
server = ServerWithTLD.new("example.xy")
|
6
|
+
refute server.valid?
|
7
|
+
end
|
8
|
+
|
9
|
+
test "rejects only host label" do
|
10
|
+
server = ServerWithTLD.new("com")
|
11
|
+
refute server.valid?
|
12
|
+
end
|
13
|
+
|
14
|
+
TLDs.each do |tld|
|
15
|
+
test "accepts #{tld} as TLD" do
|
16
|
+
server = ServerWithTLD.new("example.#{tld}")
|
17
|
+
assert server.valid?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
test "accepts example.com.br" do
|
22
|
+
server = ServerWithTLD.new("example.com.br")
|
23
|
+
assert server.valid?
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class WithoutTldValidationTest < Minitest::Test
|
4
|
+
VALID_HOSTNAMES.each do |host|
|
5
|
+
test "accepts #{host}" do
|
6
|
+
server = ServerWithoutTLD.new(host)
|
7
|
+
assert server.valid?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
INVALID_HOSTNAMES.each do |host|
|
12
|
+
test "rejects #{host}" do
|
13
|
+
server = ServerWithoutTLD.new(host)
|
14
|
+
refute server.valid?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
test "rejects nil hostname" do
|
19
|
+
server = ServerWithoutTLD.new(nil)
|
20
|
+
refute server.valid?
|
21
|
+
end
|
22
|
+
|
23
|
+
test "rejects blank hostname" do
|
24
|
+
server = ServerWithoutTLD.new("")
|
25
|
+
refute server.valid?
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class Ipv4Test < Minitest::Test
|
4
|
+
let(:user) { User.new }
|
5
|
+
|
6
|
+
test "is valid" do
|
7
|
+
User.validates_ip_address :url, :only => :v4
|
8
|
+
user.url = "192.168.1.2"
|
9
|
+
|
10
|
+
assert user.valid?
|
11
|
+
end
|
12
|
+
|
13
|
+
test "is invalid" do
|
14
|
+
User.validates_ip_address :url, :only => :v4
|
15
|
+
user.url = "FE80:0000:0000:0000:0202:B3FF:FE1E:8329"
|
16
|
+
|
17
|
+
refute user.valid?
|
18
|
+
assert_includes user.errors[:url], "is not a valid IPv4 address"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class Ipv6Test < Minitest::Test
|
4
|
+
let(:user) { User.new }
|
5
|
+
|
6
|
+
test "is valid" do
|
7
|
+
User.validates_ip_address :url, :only => :v6
|
8
|
+
user.url = "FE80:0000:0000:0000:0202:B3FF:FE1E:8329"
|
9
|
+
|
10
|
+
assert user.valid?
|
11
|
+
end
|
12
|
+
|
13
|
+
test "is invalid" do
|
14
|
+
User.validates_ip_address :url, :only => :v6
|
15
|
+
user.url = "192.168.1.2"
|
16
|
+
|
17
|
+
refute user.valid?
|
18
|
+
assert_includes user.errors[:url], "is not a valid IPv6 address"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ValidatesIpAddressTest < Minitest::Test
|
4
|
+
let(:user) { User.new }
|
5
|
+
|
6
|
+
test "is valid with IPv4" do
|
7
|
+
User.validates_ip_address :url
|
8
|
+
user.url = "192.168.1.2"
|
9
|
+
|
10
|
+
assert user.valid?
|
11
|
+
end
|
12
|
+
|
13
|
+
test "is valid with IPv6" do
|
14
|
+
User.validates_ip_address :url
|
15
|
+
user.url = "FE80:0000:0000:0000:0202:B3FF:FE1E:8329"
|
16
|
+
|
17
|
+
assert user.valid?
|
18
|
+
end
|
19
|
+
|
20
|
+
test "allows blank values" do
|
21
|
+
User.validates_ip_address :url, :allow_blank => true
|
22
|
+
user.url = ""
|
23
|
+
|
24
|
+
assert user.valid?
|
25
|
+
end
|
26
|
+
|
27
|
+
test "allows nil values" do
|
28
|
+
User.validates_ip_address :url, :allow_nil => true
|
29
|
+
user.url = nil
|
30
|
+
|
31
|
+
assert user.valid?
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ValidatesOwnershipOfTest < Minitest::Test
|
4
|
+
let(:user) { User.create! }
|
5
|
+
let(:another_user) { User.create! }
|
6
|
+
let(:category) { Category.create!(:user => user) }
|
7
|
+
let(:another_category) { Category.create!(:user => another_user) }
|
8
|
+
let(:task) { Task.new(:user => user, :category => category) }
|
9
|
+
|
10
|
+
setup do
|
11
|
+
user
|
12
|
+
another_user
|
13
|
+
category
|
14
|
+
another_category
|
15
|
+
task
|
16
|
+
|
17
|
+
Task.validates_ownership_of :category, :with => :user
|
18
|
+
end
|
19
|
+
|
20
|
+
test "is valid when record is owned by the correct user" do
|
21
|
+
assert task.valid?
|
22
|
+
end
|
23
|
+
|
24
|
+
test "is invalid when record is owned by a different user" do
|
25
|
+
task.category = another_category
|
26
|
+
refute task.valid?
|
27
|
+
end
|
28
|
+
|
29
|
+
test "raises error without :with option" do
|
30
|
+
assert_raises(ArgumentError) { Task.validates_ownership_of :category }
|
31
|
+
end
|
32
|
+
|
33
|
+
test "raises error when :with options is not a valid type" do
|
34
|
+
assert_raises(ArgumentError) { Task.validates_ownership_of :category, :with => user }
|
35
|
+
end
|
36
|
+
|
37
|
+
test "is invalid when owner is not present" do
|
38
|
+
task.user = nil
|
39
|
+
refute task.valid?
|
40
|
+
end
|
41
|
+
|
42
|
+
test "is invalid when attribute owner is not present" do
|
43
|
+
task.category.user = nil
|
44
|
+
refute task.valid?
|
45
|
+
end
|
46
|
+
|
47
|
+
test "is valid when both owners are nil" do
|
48
|
+
task.category.user = nil
|
49
|
+
task.user = nil
|
50
|
+
|
51
|
+
assert task.valid?
|
52
|
+
end
|
53
|
+
|
54
|
+
test "is valid when attribute is nil" do
|
55
|
+
task.category = nil
|
56
|
+
assert task.valid?
|
57
|
+
end
|
58
|
+
|
59
|
+
test "sets error message" do
|
60
|
+
task.user = nil
|
61
|
+
|
62
|
+
refute task.valid?
|
63
|
+
assert_includes task.errors[:category], "is not associated with your user"
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class BitsTest < Minitest::Test
|
4
|
+
let(:model) { Class.new {
|
5
|
+
def self.name
|
6
|
+
"User"
|
7
|
+
end
|
8
|
+
|
9
|
+
include ActiveModel::Model
|
10
|
+
validates_ssh_private_key :key, bits: 2048
|
11
|
+
attr_accessor :key
|
12
|
+
} }
|
13
|
+
|
14
|
+
test "accepts bits equals to required" do
|
15
|
+
record = model.new(key: SSHKey.generate(bits: 2048).private_key)
|
16
|
+
record.valid?
|
17
|
+
|
18
|
+
assert record.errors[:key].empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
test "accepts bits greater than required" do
|
22
|
+
record = model.new(key: SSHKey.generate(bits: 4096).private_key)
|
23
|
+
record.valid?
|
24
|
+
|
25
|
+
assert record.errors[:key].empty?
|
26
|
+
end
|
27
|
+
|
28
|
+
test "rejects invalid bits" do
|
29
|
+
record = model.new(key: SSHKey.generate(bits: 1024).private_key)
|
30
|
+
record.valid?
|
31
|
+
|
32
|
+
refute record.errors[:key].empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
test "sets translated error message" do
|
36
|
+
I18n.locale = "pt-BR"
|
37
|
+
message = "precisa ter pelo menos 2048 bits; a sua chave tem 1024 bits"
|
38
|
+
|
39
|
+
record = model.new(key: SSHKey.generate(bits: 1024).private_key)
|
40
|
+
record.valid?
|
41
|
+
|
42
|
+
assert_includes record.errors[:key], message
|
43
|
+
end
|
44
|
+
end
|