test/field_encrypted_test.rb in symmetric-encryption-3.2 vs test/field_encrypted_test.rb in symmetric-encryption-3.3
- old
+ new
@@ -1,19 +1,8 @@
-# Allow examples to be run in-place without requiring a gem install
-$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
+require File.dirname(__FILE__) + '/test_helper'
-require 'rubygems'
-require 'logger'
-require 'erb'
-require 'test/unit'
-require 'shoulda'
-# Since we want both the Mongoid extensions loaded we need to require it first
-require 'active_record'
-require 'mongoid'
-require 'symmetric-encryption'
-
-Mongoid.logger = Logger.new($stdout)
+Mongoid.logger = SemanticLogger[Mongoid]
filename = defined?(Mongoid::VERSION) ? "test/config/mongoid_v3.yml" : "test/config/mongoid_v2.yml"
Mongoid.load!(filename)
class MongoidUser
include Mongoid::Document
@@ -22,12 +11,24 @@
field :encrypted_bank_account_number, :type => String, :encrypted => true
field :encrypted_social_security_number, :type => String, :encrypted => true
field :encrypted_string, :type => String, :encrypted => {:random_iv => true}
field :encrypted_long_string, :type => String, :encrypted => {:random_iv => true, :compress => true}
- # validates :encrypted_bank_account_number, :symmetric_encrypted => true
- # validates :encrypted_social_security_number, :symmetric_encrypted => true
+ field :encrypted_integer_value, :type => String, :encrypted => {:type => :integer}
+ field :encrypted_float_value, :type => String, :encrypted => {:type => :float}
+ field :encrypted_decimal_value, :type => String, :encrypted => {:type => :decimal}
+ field :encrypted_datetime_value, :type => String, :encrypted => {:type => :datetime}
+ field :encrypted_time_value, :type => String, :encrypted => {:type => :time}
+ field :encrypted_date_value, :type => String, :encrypted => {:type => :date}
+ field :encrypted_true_value, :type => String, :encrypted => {:type => :boolean}
+ field :encrypted_false_value, :type => String, :encrypted => {:type => :boolean}
+ field :encrypted_data_yaml, :type => String, :encrypted => {:random_iv => true, :compress => true, :type => :yaml}
+ field :encrypted_data_json, :type => String, :encrypted => {:random_iv => true, :compress => true, :type => :json}
+
+ # TODO Validates should work
+ #validates :encrypted_bank_account_number, :symmetric_encrypted => true
+ #validates :encrypted_social_security_number, :symmetric_encrypted => true
end
# Load Symmetric Encryption keys
SymmetricEncryption.load!(File.join(File.dirname(__FILE__), 'config', 'symmetric-encryption.yml'), 'test')
@@ -53,15 +54,33 @@
@date_encrypted = "WTkSPHo5ApSSHBJMxxWt2A=="
@string = "A string containing some data to be encrypted with a random initialization vector"
@long_string = "A string containing some data to be encrypted with a random initialization vector and compressed since it takes up so much space in plain text form"
- # #TODO Intercept passing in attributes to create etc.
+ @integer_value = 12
+ @float_value = 88.12345
+ @decimal_value = BigDecimal.new("22.51")
+ @datetime_value = DateTime.new(2001, 11, 26, 20, 55, 54, "-5")
+ @time_value = Time.new(2013, 01, 01, 22, 30, 00, "-04:00")
+ @date_value = Date.new(1927, 04, 02)
+ @h = { :a => 'A', :b => 'B' }
+
@user = MongoidUser.new(
:encrypted_bank_account_number => @bank_account_number_encrypted,
:encrypted_social_security_number => @social_security_number_encrypted,
- :name => "Joe Bloggs"
+ :name => "Joe Bloggs",
+ # data type specific fields
+ :integer_value => @integer_value,
+ :float_value => @float_value,
+ :decimal_value => @decimal_value,
+ :datetime_value => @datetime_value,
+ :time_value => @time_value,
+ :date_value => @date_value,
+ :true_value => true,
+ :false_value => false,
+ :data_yaml => @h.dup,
+ :data_json => @h.dup
)
end
should "have encrypted methods" do
assert_equal true, @user.respond_to?(:encrypted_bank_account_number)
@@ -166,8 +185,276 @@
assert_equal @bank_account_number, user.bank_account_number
assert_equal @social_security_number, user.social_security_number
assert_equal @bank_account_number_encrypted, user.encrypted_bank_account_number
assert_equal @social_security_number_encrypted, user.encrypted_social_security_number
end
- end
+ context "data types" do
+ setup do
+ @user.save!
+ @user_clone = MongoidUser.find(@user.id)
+ end
+
+ context "integer values" do
+ should "return correct data type" do
+ assert_equal @integer_value, @user_clone.integer_value
+ assert @user.clone.integer_value.kind_of?(Integer)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.integer_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.integer_value
+ assert_nil @user.encrypted_integer_value
+ end
+
+ should "permit replacing value" do
+ new_integer_value = 98
+ @user_clone.integer_value = new_integer_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_integer_value, @user.integer_value
+ end
+ end
+
+ context "float values" do
+ should "return correct data type" do
+ assert_equal @float_value, @user_clone.float_value
+ assert @user.clone.float_value.kind_of?(Float)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.float_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.float_value
+ assert_nil @user.encrypted_float_value
+ end
+
+ should "permit replacing value" do
+ new_float_value = 45.4321
+ @user_clone.float_value = new_float_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_float_value, @user.float_value
+ end
+ end
+
+ context "decimal values" do
+ should "return correct data type" do
+ assert_equal @decimal_value, @user_clone.decimal_value
+ assert @user.clone.decimal_value.kind_of?(BigDecimal)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.decimal_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.decimal_value
+ assert_nil @user.encrypted_decimal_value
+ end
+
+ should "permit replacing value" do
+ new_decimal_value = BigDecimal.new("99.95")
+ @user_clone.decimal_value = new_decimal_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_decimal_value, @user.decimal_value
+ end
+ end
+
+ context "datetime values" do
+ should "return correct data type" do
+ assert_equal @datetime_value, @user_clone.datetime_value
+ assert @user.clone.datetime_value.kind_of?(DateTime)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.datetime_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.datetime_value
+ assert_nil @user.encrypted_datetime_value
+ end
+
+ should "permit replacing value" do
+ new_datetime_value = DateTime.new(1998, 10, 21, 8, 33, 28, "+5")
+ @user_clone.datetime_value = new_datetime_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_datetime_value, @user.datetime_value
+ end
+ end
+
+ context "time values" do
+ should "return correct data type" do
+ assert_equal @time_value, @user_clone.time_value
+ assert @user.clone.time_value.kind_of?(Time)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.time_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.time_value
+ assert_nil @user.encrypted_time_value
+ end
+
+ should "permit replacing value" do
+ new_time_value = Time.new(1998, 10, 21, 8, 33, 28, "+04:00")
+ @user_clone.time_value = new_time_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_time_value, @user.time_value
+ end
+ end
+
+ context "date values" do
+ should "return correct data type" do
+ assert_equal @date_value, @user_clone.date_value
+ assert @user.clone.date_value.kind_of?(Date)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.date_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.date_value
+ assert_nil @user.encrypted_date_value
+ end
+
+ should "permit replacing value" do
+ new_date_value = Date.new(1998, 10, 21)
+ @user_clone.date_value = new_date_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_date_value, @user.date_value
+ end
+ end
+
+ context "true values" do
+ should "return correct data type" do
+ assert_equal true, @user_clone.true_value
+ assert @user.clone.true_value.kind_of?(TrueClass)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.true_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.true_value
+ assert_nil @user.encrypted_true_value
+ end
+
+ should "permit replacing value" do
+ new_value = false
+ @user_clone.true_value = new_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_value, @user.true_value
+ end
+ end
+
+ context "false values" do
+ should "return correct data type" do
+ assert_equal false, @user_clone.false_value
+ assert @user.clone.false_value.kind_of?(FalseClass)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.false_value = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.false_value
+ assert_nil @user.encrypted_false_value
+ end
+
+ should "permit replacing value" do
+ new_value = true
+ @user_clone.false_value = new_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_value, @user.false_value
+ end
+ end
+
+ context "JSON Serialization" do
+ setup do
+ # JSON Does not support symbols, so they will come back as strings
+ # Convert symbols to string in the test
+ @h.keys.each do |k|
+ @h[k.to_s] = @h[k]
+ @h.delete(k)
+ end
+ end
+
+ should "return correct data type" do
+ assert_equal @h, @user_clone.data_json
+ assert @user.clone.data_json.kind_of?(Hash)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.data_json = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.data_json
+ assert_nil @user.encrypted_data_json
+ end
+
+ should "permit replacing value" do
+ new_value = @h.clone
+ new_value['c'] = 'C'
+ @user_clone.data_json = new_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_value, @user.data_json
+ end
+ end
+
+ context "YAML Serialization" do
+ should "return correct data type" do
+ assert_equal @h, @user_clone.data_yaml
+ assert @user.clone.data_yaml.kind_of?(Hash)
+ end
+
+ should "permit replacing value with nil" do
+ @user_clone.data_yaml = nil
+ @user_clone.save!
+
+ @user.reload
+ assert_nil @user.data_yaml
+ assert_nil @user.encrypted_data_yaml
+ end
+
+ should "permit replacing value" do
+ new_value = @h.clone
+ new_value[:c] = 'C'
+ @user_clone.data_yaml = new_value
+ @user_clone.save!
+
+ @user.reload
+ assert_equal new_value, @user.data_yaml
+ end
+ end
+ end
+
+ end
end
\ No newline at end of file