Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from KentaaNL/fix-concurrency-problem
Browse files Browse the repository at this point in the history
Fix concurrency problem
  • Loading branch information
twanmaus authored Sep 10, 2018
2 parents c3dde48 + d01839e commit 399c5dd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
6 changes: 5 additions & 1 deletion lib/attr_encrypted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ def encrypt(attribute, value)
# and their corresponding options as values to the instance
#
def encrypted_attributes
@encrypted_attributes ||= self.class.encrypted_attributes.dup
@encrypted_attributes ||= begin
duplicated= {}
self.class.encrypted_attributes.map { |key, value| duplicated[key] = value.dup }
duplicated
end
end

protected
Expand Down
37 changes: 23 additions & 14 deletions test/attr_encrypted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class User
attr_encrypted :with_false_unless, :key => SECRET_KEY, :unless => false, mode: :per_attribute_iv_and_salt
attr_encrypted :with_if_changed, :key => SECRET_KEY, :if => :should_encrypt
attr_encrypted :with_allow_empty_value, key: SECRET_KEY, allow_empty_value: true, marshal: true
attr_encrypted :with_unchanged_false, key: SECRET_KEY, update_unchanged: false

attr_encryptor :aliased, :key => SECRET_KEY

Expand Down Expand Up @@ -469,17 +470,16 @@ def test_should_not_by_default_generate_iv_when_attribute_is_empty

def test_should_not_generate_iv_if_same_value_when_option_is_false
user = User.new
User.encrypted_attributes[:email][:update_unchanged] = false
assert_nil user.encrypted_email_iv
user.email = '[email protected]'
refute_nil(old_value = user.encrypted_email_iv)
user.email = '[email protected]'
assert_equal old_value, user.encrypted_email_iv
assert_nil user.encrypted_with_unchanged_false_iv
user.with_unchanged_false = '[email protected]'
old_value = user.encrypted_with_unchanged_false_iv
refute_nil(old_value)
user.with_unchanged_false = '[email protected]'
assert_equal old_value, user.encrypted_with_unchanged_false_iv
end

def test_should_generate_iv_if_same_value_when_option_is_true
user = User.new
User.encrypted_attributes[:email][:update_unchanged] = true
assert_nil user.encrypted_email_iv
user.email = '[email protected]'
refute_nil(old_value = user.encrypted_email_iv)
Expand All @@ -488,21 +488,30 @@ def test_should_generate_iv_if_same_value_when_option_is_true
end

def test_should_not_update_iv_if_same_value_when_option_is_false
user = User.new(email: '[email protected]')
User.encrypted_attributes[:email][:update_unchanged] = false
old_encrypted_email_iv = user.encrypted_email_iv
refute_nil old_encrypted_email_iv
user.email = '[email protected]'
assert_equal old_encrypted_email_iv, user.encrypted_email_iv
user = User.new
user.with_unchanged_false = '[email protected]'
old_encrypted_with_unchanged_false_iv = user.encrypted_with_unchanged_false_iv
refute_nil old_encrypted_with_unchanged_false_iv
user.with_unchanged_false = '[email protected]'
assert_equal old_encrypted_with_unchanged_false_iv, user.encrypted_with_unchanged_false_iv
end

def test_should_not_update_iv_if_same_value_when_option_is_true
user = User.new(email: '[email protected]')
User.encrypted_attributes[:email][:update_unchanged] = true
old_encrypted_email_iv = user.encrypted_email_iv
refute_nil old_encrypted_email_iv
user.email = '[email protected]'
refute_nil user.encrypted_email_iv
refute_equal old_encrypted_email_iv, user.encrypted_email_iv
end

def test_encrypted_attributes_state_is_not_shared
user = User.new
user.ssn = '123456789'

another_user = User.new

assert_equal :encrypting, user.encrypted_attributes[:ssn][:operation]
assert_nil another_user.encrypted_attributes[:ssn][:operation]
end
end

0 comments on commit 399c5dd

Please sign in to comment.