test/mongoid_test.rb in symmetric-encryption-4.0.0 vs test/mongoid_test.rb in symmetric-encryption-4.0.1
- old
+ new
@@ -4,11 +4,11 @@
require_relative '../lib/symmetric_encryption/extensions/mongoid/encrypted'
ENV['RACK_ENV'] = 'test'
Mongoid.load!('test/config/mongoid.yml')
- #@formatter:off
+ # @formatter:off
class MongoidUser
include Mongoid::Document
field :name, type: String
field :encrypted_bank_account_number, type: String, encrypted: true
@@ -40,15 +40,15 @@
validates_uniqueness_of :encrypted_email, allow_blank: true, if: :encrypted_email_changed?
validates_uniqueness_of :encrypted_username, allow_blank: true, if: :encrypted_username_changed?
validates :username,
- length: {in: 3..20},
- format: {with: /\A[\w\-]+\z/},
- allow_blank: true
+ length: {in: 3..20},
+ format: {with: /\A[\w\-]+\z/},
+ allow_blank: true
end
- #@formatter:on
+ # @formatter:on
#
# Unit Tests for field encrypted and validation aspects of SymmetricEncryption
#
class MongoidTest < Minitest::Test
@@ -58,11 +58,11 @@
@bank_account_number_encrypted = 'QEVuQwIAL94ArJeFlJrZp6SYsvoOGA=='
@social_security_number = '987654321'
@social_security_number_encrypted = 'QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA=='
- @integer = 32768
+ @integer = 32_768
@integer_encrypted = 'FA3smFQEKqB/ITv+A0xACg=='
@float = 0.9867
@float_encrypted = 'z7Pwt2JDp74d+u0IXFAdrQ=='
@@ -72,14 +72,14 @@
@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'
@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)
+ @decimal_value = BigDecimal('22.51')
+ @datetime_value = DateTime.new(2001, 11, 26, 20, 55, 54, '-5')
+ @time_value = Time.new(2013, 0o1, 0o1, 22, 30, 0o0, '-04:00')
+ @date_value = Date.new(1927, 0o4, 0o2)
@h = {a: 'A', b: 'B'}
@user = MongoidUser.new(
encrypted_bank_account_number: @bank_account_number_encrypted,
encrypted_social_security_number: @social_security_number_encrypted,
@@ -180,18 +180,18 @@
it 'all paths it lead to the same result 2' do
assert_equal @bank_account_number, (@user.social_security_number = @bank_account_number)
assert_equal @bank_account_number_encrypted, @user.encrypted_social_security_number
end
- it "all paths it lead to the same result, check uninitialized" do
+ it 'all paths it lead to the same result, check uninitialized' do
user = MongoidUser.new
assert_nil user.social_security_number
assert_equal @bank_account_number, (user.social_security_number = @bank_account_number)
assert_equal @bank_account_number, user.social_security_number
assert_equal @bank_account_number_encrypted, user.encrypted_social_security_number
- assert_nil (user.social_security_number = nil)
+ user.social_security_number = nil
assert_nil user.social_security_number
assert_nil user.encrypted_social_security_number
end
it 'allow unencrypted values to be passed to the constructor' do
@@ -214,11 +214,11 @@
before do
@user.save!
end
after do
- @user.destroy if @user
+ @user&.destroy
end
it 'return false if it was not changed' do
assert_equal false, @user.encrypted_bank_account_number_changed?
assert_equal false, @user.bank_account_number_changed?
@@ -236,11 +236,11 @@
@user.save!
@user_clone = MongoidUser.find(@user.id)
end
after do
- @user.destroy if @user
+ @user&.destroy
end
describe 'aliased fields' do
it 'return correct data type' do
@user_clone.aliased_integer_value = '5'
@@ -249,17 +249,17 @@
end
describe 'integer values' do
it 'return correct data type' do
assert_equal @integer_value, @user_clone.integer_value
- assert @user.clone.integer_value.kind_of?(Integer)
+ assert @user.clone.integer_value.is_a?(Integer)
end
it '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)
+ assert u.integer_value.is_a?(Integer)
end
it 'permit replacing value with nil' do
@user_clone.integer_value = nil
@user_clone.save!
@@ -280,17 +280,17 @@
end
describe 'float values' do
it 'return correct data type' do
assert_equal @float_value, @user_clone.float_value
- assert @user.clone.float_value.kind_of?(Float)
+ assert @user.clone.float_value.is_a?(Float)
end
it '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)
+ assert u.float_value.is_a?(Float)
end
it 'permit replacing value with nil' do
@user_clone.float_value = nil
@user_clone.save!
@@ -311,17 +311,17 @@
end
describe 'decimal values' do
it 'return correct data type' do
assert_equal @decimal_value, @user_clone.decimal_value
- assert @user.clone.decimal_value.kind_of?(BigDecimal)
+ assert @user.clone.decimal_value.is_a?(BigDecimal)
end
it '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)
+ assert_equal BigDecimal('99.95'), u.decimal_value
+ assert u.decimal_value.is_a?(BigDecimal)
end
it 'permit replacing value with nil' do
@user_clone.decimal_value = nil
@user_clone.save!
@@ -330,11 +330,11 @@
assert_nil @user.decimal_value
assert_nil @user.encrypted_decimal_value
end
it 'permit replacing value' do
- new_decimal_value = BigDecimal.new('99.95')
+ new_decimal_value = BigDecimal('99.95')
@user_clone.decimal_value = new_decimal_value
@user_clone.save!
@user.reload
assert_equal new_decimal_value, @user.decimal_value
@@ -342,18 +342,18 @@
end
describe 'datetime values' do
it 'return correct data type' do
assert_equal @datetime_value, @user_clone.datetime_value
- assert @user.clone.datetime_value.kind_of?(DateTime)
+ assert @user.clone.datetime_value.is_a?(DateTime)
end
it '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)
+ assert u.datetime_value.is_a?(DateTime)
end
it 'permit replacing value with nil' do
@user_clone.datetime_value = nil
@user_clone.save!
@@ -374,18 +374,18 @@
end
describe 'time values' do
it 'return correct data type' do
assert_equal @time_value, @user_clone.time_value.dup
- assert @user.clone.time_value.kind_of?(Time)
+ assert @user.clone.time_value.is_a?(Time)
end
it 'coerce data type before save' do
now = Time.now
u = MongoidUser.new(time_value: now)
assert_equal now, u.time_value.dup
- assert u.time_value.kind_of?(Time)
+ assert u.time_value.is_a?(Time)
end
it 'permit replacing value with nil' do
@user_clone.time_value = nil
@user_clone.save!
@@ -406,18 +406,18 @@
end
describe 'date values' do
it 'return correct data type' do
assert_equal @date_value, @user_clone.date_value
- assert @user.clone.date_value.kind_of?(Date)
+ assert @user.clone.date_value.is_a?(Date)
end
it '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)
+ assert u.date_value.is_a?(Date)
end
it 'permit replacing value with nil' do
@user_clone.date_value = nil
@user_clone.save!
@@ -438,17 +438,17 @@
end
describe 'true values' do
it 'return correct data type' do
assert_equal true, @user_clone.true_value
- assert @user.clone.true_value.kind_of?(TrueClass)
+ assert @user.clone.true_value.is_a?(TrueClass)
end
it '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)
+ assert u.true_value.is_a?(TrueClass)
end
it 'permit replacing value with nil' do
@user_clone.true_value = nil
@user_clone.save!
@@ -469,17 +469,17 @@
end
describe 'false values' do
it 'return correct data type' do
assert_equal false, @user_clone.false_value
- assert @user.clone.false_value.kind_of?(FalseClass)
+ assert @user.clone.false_value.is_a?(FalseClass)
end
it '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)
+ assert u.false_value.is_a?(FalseClass)
end
it 'permit replacing value with nil' do
@user_clone.false_value = nil
@user_clone.save!
@@ -509,17 +509,17 @@
end
end
it 'return correct data type' do
assert_equal @h, @user_clone.data_json
- assert @user.clone.data_json.kind_of?(Hash)
+ assert @user.clone.data_json.is_a?(Hash)
end
it '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)
+ assert u.data_json.is_a?(Hash)
end
it 'permit replacing value with nil' do
@user_clone.data_json = nil
@user_clone.save!
@@ -541,17 +541,17 @@
end
describe 'YAML Serialization' do
it 'return correct data type' do
assert_equal @h, @user_clone.data_yaml
- assert @user.clone.data_yaml.kind_of?(Hash)
+ assert @user.clone.data_yaml.is_a?(Hash)
end
it '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)
+ assert u.data_yaml.is_a?(Hash)
end
it 'permit replacing value with nil' do
@user_clone.data_yaml = nil
@user_clone.save!
@@ -569,11 +569,10 @@
@user.reload
assert_equal new_value, @user.data_yaml
end
end
-
end
describe 'uniqueness' do
before do
MongoidUniqueUser.destroy_all
@@ -587,12 +586,10 @@
duplicate = MongoidUniqueUser.new(email: @email)
assert_equal false, duplicate.valid?
assert_equal 'is already taken', duplicate.errors.messages[:encrypted_email].first
end
end
-
end
end
-
rescue LoadError
- puts "Not running Mongoid tests because mongoid gem is not installed!!!"
+ puts 'Not running Mongoid tests because mongoid gem is not installed!!!'
end