Permalink
Browse files

Fixes for validates length

  • Loading branch information...
1 parent 6064927 commit 0522bcae27a7452627b3a2e92828b49f2fc50a20 @spikex committed Mar 18, 2011
Showing with 103 additions and 59 deletions.
  1. +1 −1 lib/strongbox.rb
  2. +1 −1 lib/strongbox/lock.rb
  3. +0 −57 test/strongbox_test.rb
  4. +101 −0 test/validations_test.rb
View
@@ -5,7 +5,7 @@
module Strongbox
- VERSION = "0.4.4"
+ VERSION = "0.4.5"
RSA_PKCS1_PADDING = OpenSSL::PKey::RSA::PKCS1_PADDING
RSA_SSLV23_PADDING = OpenSSL::PKey::RSA::SSLV23_PADDING
@@ -8,7 +8,7 @@ def initialize name, instance, options = {}
@name = name
@instance = instance
- @size = nil
+ @size = 0
options = Strongbox.options.merge(options)
@@ -191,63 +191,6 @@ class StrongboxTest < Test::Unit::TestCase
end
end
- context 'with validations' do
- context 'using validates_presence_of' do
- setup do
- rebuild_class(:key_pair => File.join(FIXTURES_DIR,'keypair.pem'))
- Dummy.send(:validates_presence_of, :secret)
- @valid = Dummy.new(:secret => 'Shhhh')
- @invalid = Dummy.new(:secret => nil)
- end
-
- should 'not have an error on the secret when valid' do
- assert @valid.valid?
- assert_does_not_have_errors_on(@valid,:secret)
- end
-
- should 'have an error on the secret when invalid' do
- assert !@invalid.valid?
- assert_has_errors_on(@invalid,:secret)
- end
- end
-
- context 'using validates_length_of' do
- setup do
- rebuild_class(:key_pair => File.join(FIXTURES_DIR,'keypair.pem'))
- Dummy.send(:validates_length_of,
- :secret,
- :in => 5..10,
- :allow_nil => true,
- :allow_blank => true
- )
- @valid = Dummy.new(:secret => 'Shhhh')
- @valid_nil = Dummy.new(:secret => nil)
- @valid_blank = Dummy.new(:secret => '')
- @invalid = Dummy.new(:secret => '1')
- end
-
- should 'not have an error on the secret when in range' do
- assert @valid.valid?
- assert_does_not_have_errors_on(@valid,:secret)
- end
-
- should 'not have an error on the secret when nil' do
- assert @valid_nil.valid?
- assert_does_not_have_errors_on(@valid_nil,:secret)
- end
-
- should 'not have an error on the secret when blank' do
- assert @valid_blank.valid?
- assert_does_not_have_errors_on(@valid_blank,:secret)
- end
-
- should 'have an error on the secret when invalid' do
- assert !@invalid.valid?
- assert_has_errors_on(@invalid,:secret)
- end
- end
- end
-
context 'A Class with two secured fields' do
setup do
@password = 'boost facile'
@@ -0,0 +1,101 @@
+require 'test/test_helper'
+
+class ValidationsTest < Test::Unit::TestCase
+ context 'with validations' do
+ setup do
+ rebuild_model :key_pair => File.join(FIXTURES_DIR,'keypair.pem')
+ end
+
+ context 'using validates_presence_of' do
+ setup do
+ Dummy.send(:validates_presence_of, :secret)
+ @valid = Dummy.new(:secret => 'Shhhh')
+ @invalid = Dummy.new(:secret => nil)
+ end
+
+ should 'not have an error on the secret when valid' do
+ assert @valid.valid?
+ assert_does_not_have_errors_on(@valid,:secret)
+ end
+
+ should 'have an error on the secret when invalid' do
+ assert !@invalid.valid?
+ assert_has_errors_on(@invalid,:secret)
+ end
+ end
+
+ context 'using validates_length_of' do
+ setup do
+ rebuild_class(:key_pair => File.join(FIXTURES_DIR,'keypair.pem'))
+ Dummy.send(:validates_length_of,
+ :secret,
+ :in => 5..10,
+ :allow_nil => true,
+ :allow_blank => true
+ )
+ @valid = Dummy.new(:secret => 'Shhhh')
+ @valid_nil = Dummy.new(:secret => nil)
+ @valid_blank = Dummy.new(:secret => '')
+ @invalid = Dummy.new(:secret => '1')
+ end
+
+ should 'not have an error on the secret when in range' do
+ assert @valid.valid?
+ assert_does_not_have_errors_on(@valid,:secret)
+ end
+
+ should 'not have an error on the secret when nil' do
+ assert @valid_nil.valid?
+ assert_does_not_have_errors_on(@valid_nil,:secret)
+ end
+
+ should 'not have an error on the secret when blank' do
+ assert @valid_blank.valid?
+ assert_does_not_have_errors_on(@valid_blank,:secret)
+ end
+
+ should 'have an error on the secret when invalid' do
+ assert !@invalid.valid?
+ assert_has_errors_on(@invalid,:secret)
+ end
+ end
+
+
+ if defined?(ActiveModel::Validations) # Rails 3
+ context 'using validates for length' do
+ setup do
+ rebuild_class(:key_pair => File.join(FIXTURES_DIR,'keypair.pem'))
+ Dummy.send(:validates,
+ :secret,
+ :length => {:minimum => 4, :maximum => 16})
+ @valid = Dummy.new(:secret => 'Shhhh')
+ @out_of_range = [Dummy.new(:secret => 'x' * 3),
+ Dummy.new(:secret => 'x' * 17)]
+ @blank = [Dummy.new(:secret => nil),
+ Dummy.new(:secret => '')]
+ end
+
+ should 'not have an error on the secret when in range' do
+ assert @valid.valid?
+ assert_does_not_have_errors_on(@valid,:secret)
+ end
+
+ should 'have an error on the secret when out of range' do
+ @out_of_range.each do |instance|
+ assert !instance.valid?
+ assert_has_errors_on(instance,:secret)
+ end
+ end
+
+ should 'have an error on the secret when blank' do
+ @blank.each do |instance|
+ assert !instance.valid?
+ assert_has_errors_on(instance,:secret)
+ end
+ end
+ end
+ end
+ end
+
+end
+

0 comments on commit 0522bca

Please sign in to comment.