chintala-strongbox 0.6.1

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,86 @@
1
+ ROOT = File.join(File.dirname(__FILE__), '..')
2
+ RAILS_ROOT = ROOT
3
+ $LOAD_PATH << File.join(ROOT, 'lib')
4
+
5
+ require 'rubygems'
6
+ require 'test/unit'
7
+ require 'sqlite3'
8
+ require 'active_record'
9
+ require 'logger'
10
+ gem 'thoughtbot-shoulda', ">= 2.9.0"
11
+ require 'shoulda'
12
+ begin require 'redgreen'; rescue LoadError; end
13
+
14
+ require 'strongbox'
15
+
16
+ ENV['RAILS_ENV'] ||= 'test'
17
+
18
+ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
19
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
20
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
21
+ ActiveRecord::Base.establish_connection(config['test'])
22
+
23
+
24
+ # rebuild_model and rebuild_class are borrowed directly from the Paperclip gem
25
+ #
26
+ # http://thoughtbot.com/projects/paperclip
27
+
28
+ # rebuild_model (re)creates a database table for our Dummy model.
29
+ # Call this to initial create a model, or to reset the database.
30
+
31
+ def rebuild_model options = {}
32
+ ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
33
+ table.string :in_the_clear
34
+ table.binary :secret
35
+ table.binary :secret_key
36
+ table.binary :secret_iv
37
+ table.binary :segreto
38
+ end
39
+ rebuild_class options
40
+ end
41
+
42
+ # rebuild_class creates or replaces the Dummy ActiveRecord Model.
43
+ # Call this when changing the options to encrypt_with_public_key
44
+
45
+ def rebuild_class options = {}
46
+ ActiveRecord::Base.send(:include, Strongbox)
47
+ Object.send(:remove_const, "Dummy") rescue nil
48
+ Object.const_set("Dummy", Class.new(ActiveRecord::Base))
49
+ Dummy.class_eval do
50
+ include Strongbox
51
+ encrypt_with_public_key :secret, options
52
+ end
53
+ Dummy.reset_column_information
54
+ end
55
+
56
+ def assert_has_errors_on(model,attribute)
57
+ # Rails 2.X && Rails 3.X
58
+ !model.errors[attribute].empty?
59
+ end
60
+
61
+ def assert_does_not_have_errors_on(model,attribute)
62
+ # Rails 2.X Rails 3.X
63
+ model.errors[attribute].nil? || model.errors[attribute].empty?
64
+ end
65
+
66
+ def generate_key_pair(password = nil,size = 2048)
67
+ rsa_key = OpenSSL::PKey::RSA.new(size)
68
+ # If no password is provided, don't encrypt the key
69
+ return rsa_key if password.blank?
70
+ cipher = OpenSSL::Cipher::Cipher.new('des3')
71
+ key_pair = rsa_key.to_pem(cipher,password)
72
+ key_pair << rsa_key.public_key.to_pem
73
+ return key_pair
74
+ end
75
+
76
+ class Test::Unit::TestCase
77
+ def self.should_encypted_and_decrypt
78
+ should 'return "*encrypted*" when locked' do
79
+ assert_equal '*encrypted*', @dummy.secret.decrypt
80
+ end
81
+
82
+ should 'return secret when unlocked' do
83
+ assert_equal 'Shhhh', @dummy.secret.decrypt(@password)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,101 @@
1
+ require 'test/test_helper'
2
+
3
+ class ValidationsTest < Test::Unit::TestCase
4
+ context 'with validations' do
5
+ setup do
6
+ rebuild_model :key_pair => File.join(FIXTURES_DIR,'keypair.pem')
7
+ end
8
+
9
+ context 'using validates_presence_of' do
10
+ setup do
11
+ Dummy.send(:validates_presence_of, :secret)
12
+ @valid = Dummy.new(:secret => 'Shhhh')
13
+ @invalid = Dummy.new(:secret => nil)
14
+ end
15
+
16
+ should 'not have an error on the secret when valid' do
17
+ assert @valid.valid?
18
+ assert_does_not_have_errors_on(@valid,:secret)
19
+ end
20
+
21
+ should 'have an error on the secret when invalid' do
22
+ assert !@invalid.valid?
23
+ assert_has_errors_on(@invalid,:secret)
24
+ end
25
+ end
26
+
27
+ context 'using validates_length_of' do
28
+ setup do
29
+ rebuild_class(:key_pair => File.join(FIXTURES_DIR,'keypair.pem'))
30
+ Dummy.send(:validates_length_of,
31
+ :secret,
32
+ :in => 5..10,
33
+ :allow_nil => true,
34
+ :allow_blank => true
35
+ )
36
+ @valid = Dummy.new(:secret => 'Shhhh')
37
+ @valid_nil = Dummy.new(:secret => nil)
38
+ @valid_blank = Dummy.new(:secret => '')
39
+ @invalid = Dummy.new(:secret => '1')
40
+ end
41
+
42
+ should 'not have an error on the secret when in range' do
43
+ assert @valid.valid?
44
+ assert_does_not_have_errors_on(@valid,:secret)
45
+ end
46
+
47
+ should 'not have an error on the secret when nil' do
48
+ assert @valid_nil.valid?
49
+ assert_does_not_have_errors_on(@valid_nil,:secret)
50
+ end
51
+
52
+ should 'not have an error on the secret when blank' do
53
+ assert @valid_blank.valid?
54
+ assert_does_not_have_errors_on(@valid_blank,:secret)
55
+ end
56
+
57
+ should 'have an error on the secret when invalid' do
58
+ assert !@invalid.valid?
59
+ assert_has_errors_on(@invalid,:secret)
60
+ end
61
+ end
62
+
63
+
64
+ if defined?(ActiveModel::Validations) # Rails 3
65
+ context 'using validates for length' do
66
+ setup do
67
+ rebuild_class(:key_pair => File.join(FIXTURES_DIR,'keypair.pem'))
68
+ Dummy.send(:validates,
69
+ :secret,
70
+ :length => {:minimum => 4, :maximum => 16})
71
+ @valid = Dummy.new(:secret => 'Shhhh')
72
+ @out_of_range = [Dummy.new(:secret => 'x' * 3),
73
+ Dummy.new(:secret => 'x' * 17)]
74
+ @blank = [Dummy.new(:secret => nil),
75
+ Dummy.new(:secret => '')]
76
+ end
77
+
78
+ should 'not have an error on the secret when in range' do
79
+ assert @valid.valid?
80
+ assert_does_not_have_errors_on(@valid,:secret)
81
+ end
82
+
83
+ should 'have an error on the secret when out of range' do
84
+ @out_of_range.each do |instance|
85
+ assert !instance.valid?
86
+ assert_has_errors_on(instance,:secret)
87
+ end
88
+ end
89
+
90
+ should 'have an error on the secret when blank' do
91
+ @blank.each do |instance|
92
+ assert !instance.valid?
93
+ assert_has_errors_on(instance,:secret)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ end
101
+
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chintala-strongbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spike Ilacqua
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: thoughtbot-shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! " Strongbox provides Public Key Encryption for ActiveRecord. By
63
+ using a\n public key sensitive information can be encrypted and stored automatically.\n
64
+ \ Once stored a password is required to access the information. dependencies\n
65
+ \ are specified in standard Ruby syntax.\n"
66
+ email: spike@stuff-things.net
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - Gemfile
73
+ - LICENSE
74
+ - README.textile
75
+ - Rakefile
76
+ - init.rb
77
+ - lib/strongbox.rb
78
+ - lib/strongbox/lock.rb
79
+ - rails/init.rb
80
+ - strongbox.gemspec
81
+ - test/database.yml
82
+ - test/fixtures/encrypted
83
+ - test/fixtures/keypair.pem
84
+ - test/method_key_test.rb
85
+ - test/missing_attributes_test.rb
86
+ - test/proc_key_test.rb
87
+ - test/strongbox_multiply_test.rb
88
+ - test/strongbox_test.rb
89
+ - test/test_helper.rb
90
+ - test/validations_test.rb
91
+ homepage: http://stuff-things.net/strongbox
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.21
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Secures ActiveRecord fields with public key encryption.
115
+ test_files:
116
+ - test/database.yml
117
+ - test/fixtures/encrypted
118
+ - test/fixtures/keypair.pem
119
+ - test/method_key_test.rb
120
+ - test/missing_attributes_test.rb
121
+ - test/proc_key_test.rb
122
+ - test/strongbox_multiply_test.rb
123
+ - test/strongbox_test.rb
124
+ - test/test_helper.rb
125
+ - test/validations_test.rb