test/field_encrypted_test.rb in symmetric-encryption-3.3 vs test/field_encrypted_test.rb in symmetric-encryption-3.4.0
- old
+ new
@@ -12,10 +12,11 @@
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}
field :encrypted_integer_value, :type => String, :encrypted => {:type => :integer}
+ field :aiv, :type => String, :encrypted => {:type => :integer, decrypt_as: :aliased_integer_value}
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}
@@ -68,10 +69,11 @@
:encrypted_bank_account_number => @bank_account_number_encrypted,
:encrypted_social_security_number => @social_security_number_encrypted,
:name => "Joe Bloggs",
# data type specific fields
:integer_value => @integer_value,
+ :aliased_integer_value => @integer_value,
:float_value => @float_value,
:decimal_value => @decimal_value,
:datetime_value => @datetime_value,
:time_value => @time_value,
:date_value => @date_value,
@@ -108,10 +110,15 @@
assert_equal true, @user.respond_to?(:string=)
assert_equal true, @user.respond_to?(:long_string=)
assert_equal true, @user.respond_to?(:name=)
end
+ should "support aliased fields" do
+ assert_equal true, @user.respond_to?(:aliased_integer_value=)
+ assert_equal true, @user.respond_to?(:aliased_integer_value)
+ end
+
should "have unencrypted values" do
assert_equal @bank_account_number, @user.bank_account_number
assert_equal @social_security_number, @user.social_security_number
end
@@ -192,16 +199,29 @@
setup do
@user.save!
@user_clone = MongoidUser.find(@user.id)
end
+ context "aliased fields" do
+ should "return correct data type" do
+ @user_clone.aliased_integer_value = "5"
+ assert_equal 5, @user_clone.aliased_integer_value
+ end
+ 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 "coerce data type before save" do
+ u = MongoidUser.new(:integer_value => "5")
+ assert_equal 5, u.integer_value
+ assert u.integer_value.kind_of?(Integer)
+ end
+
should "permit replacing value with nil" do
@user_clone.integer_value = nil
@user_clone.save!
@user.reload
@@ -223,10 +243,16 @@
should "return correct data type" do
assert_equal @float_value, @user_clone.float_value
assert @user.clone.float_value.kind_of?(Float)
end
+ should "coerce data type before save" do
+ u = MongoidUser.new(:float_value => "5.6")
+ assert_equal 5.6, u.float_value
+ assert u.float_value.kind_of?(Float)
+ end
+
should "permit replacing value with nil" do
@user_clone.float_value = nil
@user_clone.save!
@user.reload
@@ -248,10 +274,16 @@
should "return correct data type" do
assert_equal @decimal_value, @user_clone.decimal_value
assert @user.clone.decimal_value.kind_of?(BigDecimal)
end
+ should "coerce data type before save" do
+ u = MongoidUser.new(:decimal_value => "99.95")
+ assert_equal BigDecimal.new("99.95"), u.decimal_value
+ assert u.decimal_value.kind_of?(BigDecimal)
+ end
+
should "permit replacing value with nil" do
@user_clone.decimal_value = nil
@user_clone.save!
@user.reload
@@ -273,10 +305,17 @@
should "return correct data type" do
assert_equal @datetime_value, @user_clone.datetime_value
assert @user.clone.datetime_value.kind_of?(DateTime)
end
+ should "coerce data type before save" do
+ now = Time.now
+ u = MongoidUser.new(:datetime_value => now)
+ assert_equal now, u.datetime_value
+ assert u.datetime_value.kind_of?(DateTime)
+ end
+
should "permit replacing value with nil" do
@user_clone.datetime_value = nil
@user_clone.save!
@user.reload
@@ -298,10 +337,17 @@
should "return correct data type" do
assert_equal @time_value, @user_clone.time_value
assert @user.clone.time_value.kind_of?(Time)
end
+ should "coerce data type before save" do
+ now = Time.now
+ u = MongoidUser.new(:time_value => now)
+ assert_equal now, u.time_value
+ assert u.time_value.kind_of?(Time)
+ end
+
should "permit replacing value with nil" do
@user_clone.time_value = nil
@user_clone.save!
@user.reload
@@ -323,10 +369,17 @@
should "return correct data type" do
assert_equal @date_value, @user_clone.date_value
assert @user.clone.date_value.kind_of?(Date)
end
+ should "coerce data type before save" do
+ now = Time.now
+ u = MongoidUser.new(:date_value => now)
+ assert_equal now.to_date, u.date_value
+ assert u.date_value.kind_of?(Date)
+ end
+
should "permit replacing value with nil" do
@user_clone.date_value = nil
@user_clone.save!
@user.reload
@@ -348,10 +401,16 @@
should "return correct data type" do
assert_equal true, @user_clone.true_value
assert @user.clone.true_value.kind_of?(TrueClass)
end
+ should "coerce data type before save" do
+ u = MongoidUser.new(:true_value => "1")
+ assert_equal true, u.true_value
+ assert u.true_value.kind_of?(TrueClass)
+ end
+
should "permit replacing value with nil" do
@user_clone.true_value = nil
@user_clone.save!
@user.reload
@@ -373,10 +432,16 @@
should "return correct data type" do
assert_equal false, @user_clone.false_value
assert @user.clone.false_value.kind_of?(FalseClass)
end
+ should "coerce data type before save" do
+ u = MongoidUser.new(:false_value => "0")
+ assert_equal false, u.false_value
+ assert u.false_value.kind_of?(FalseClass)
+ end
+
should "permit replacing value with nil" do
@user_clone.false_value = nil
@user_clone.save!
@user.reload
@@ -407,10 +472,16 @@
should "return correct data type" do
assert_equal @h, @user_clone.data_json
assert @user.clone.data_json.kind_of?(Hash)
end
+ should "not coerce data type (leaves as hash) before save" do
+ u = MongoidUser.new(:data_json => @h)
+ assert_equal @h, u.data_json
+ assert u.data_json.kind_of?(Hash)
+ end
+
should "permit replacing value with nil" do
@user_clone.data_json = nil
@user_clone.save!
@user.reload
@@ -433,10 +504,16 @@
should "return correct data type" do
assert_equal @h, @user_clone.data_yaml
assert @user.clone.data_yaml.kind_of?(Hash)
end
+ should "not coerce data type (leaves as hash) before save" do
+ u = MongoidUser.new(:data_yaml => @h)
+ assert_equal @h, u.data_yaml
+ assert u.data_yaml.kind_of?(Hash)
+ end
+
should "permit replacing value with nil" do
@user_clone.data_yaml = nil
@user_clone.save!
@user.reload
@@ -452,9 +529,10 @@
@user.reload
assert_equal new_value, @user.data_yaml
end
end
+
end
end
end
\ No newline at end of file