forked from spikex/strongbox
/
test_helper.rb
72 lines (59 loc) · 2.12 KB
/
test_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
ROOT = File.join(File.dirname(__FILE__), '..')
RAILS_ROOT = ROOT
$LOAD_PATH << File.join(ROOT, 'lib')
require 'rubygems'
require 'test/unit'
require 'sqlite3'
require 'active_record'
require 'logger'
gem 'thoughtbot-shoulda', ">= 2.9.0"
require 'shoulda'
begin require 'redgreen'; rescue LoadError; end
require 'strongbox'
ENV['RAILS_ENV'] ||= 'test'
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config['test'])
# rebuild_model and rebuild_class are borrowed directly from the Paperclip gem
#
# http://thoughtbot.com/projects/paperclip
# rebuild_model (re)creates a database table for our Dummy model.
# Call this to initial create a model, or to reset the database.
def rebuild_model options = {}
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
table.string :in_the_clear
table.binary :secret
table.binary :secret_key
table.binary :secret_iv
end
rebuild_class options
end
# rebuild_class creates or replaces the Dummy ActiveRecord Model.
# Call this when changing the options to encrypt_with_public_key
def rebuild_class options = {}
ActiveRecord::Base.send(:include, Strongbox)
Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base))
Dummy.class_eval do
include Strongbox
encrypt_with_public_key :secret, options
end
end
def assert_has_errors_on(model,attribute)
# Rails 2.X && Rails 3.X
!model.errors[attribute].empty?
end
def assert_does_not_have_errors_on(model,attribute)
# Rails 2.X Rails 3.X
model.errors[attribute].nil? || model.errors[attribute].empty?
end
def generate_key_pair(password = nil,size = 2048)
rsa_key = OpenSSL::PKey::RSA.new(size)
# If no password is provided, don't encrypt the key
return rsa_key if password.blank?
cipher = OpenSSL::Cipher::Cipher.new('des3')
key_pair = rsa_key.to_pem(cipher,password)
key_pair << rsa_key.public_key.to_pem
return key_pair
end