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

Rails 6.0 #6

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby-version: ['2.3', '2.4', '2.5']
rails-version: ['4.2.0', '5.0.0', '5.1.1', '5.2.0']
ruby-version: ['2.7']
rails-version: ['5.1.1', '5.2.0', '6.0.0']
env:
ACTIVERECORD: ${{ matrix.rails-version }}
steps:
Expand Down
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
attr_encrypted
2 changes: 1 addition & 1 deletion attr_encrypted.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('activerecord-jdbcsqlite3-adapter')
s.add_development_dependency('jdbc-sqlite3', '< 3.8.7') # 3.8.7 is nice and broke
else
s.add_development_dependency('sqlite3', '~> 1.3.0', '>= 1.3.6')
s.add_development_dependency('sqlite3', '~> 1.4.0', '>= 1.4')
end
s.add_development_dependency('dm-sqlite-adapter')
s.add_development_dependency('simplecov')
Expand Down
6 changes: 4 additions & 2 deletions lib/attr_encrypted/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def attr_encrypted(*attrs)

if ::ActiveRecord::VERSION::STRING >= "4.1"
define_method("#{attr}_changed?") do |options = {}|
attribute_changed?(attr, options)
attribute_changed?(attr, **options)
end
else
define_method("#{attr}_changed?") do
Expand All @@ -81,7 +81,9 @@ def attr_encrypted(*attrs)
if ::ActiveRecord::VERSION::STRING >= "5.2"
# This is needed support attribute_was before a record has
# been saved
set_attribute_was(attr, __send__(attr)) if value != __send__(attr)
if ::ActiveRecord::VERSION::STRING < "6.0"
set_attribute_was(attr, __send__(attr)) if value != __send__(attr)
end
# This is needed to support attribute_was after a record has
# been saved
@attributes.write_from_user(attr.to_s, value) if value != __send__(attr)
Expand Down
32 changes: 17 additions & 15 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,23 @@ def test_should_create_was_predicate
assert_equal old_zipcode, address.zipcode_was
end

def test_attribute_was_works_when_options_for_old_encrypted_value_are_different_than_options_for_new_encrypted_value
pw = 'password'
crypto_key = SecureRandom.urlsafe_base64(24)
old_iv = SecureRandom.random_bytes(12)
account = Account.create
encrypted_value = Encryptor.encrypt(value: pw, iv: old_iv, key: crypto_key)
Account.where(id: account.id).update_all(key: crypto_key, encrypted_password_iv: [old_iv].pack('m'), encrypted_password: [encrypted_value].pack('m'))
account = Account.find(account.id)
assert_equal pw, account.password
account.password = pw.reverse
assert_equal pw, account.password_was
account.save
account.reload
assert_equal Account::ACCOUNT_ENCRYPTION_KEY, account.key
assert_equal pw.reverse, account.password
if ::ActiveRecord::VERSION::STRING < "6.0"
def test_attribute_was_works_when_options_for_old_encrypted_value_are_different_than_options_for_new_encrypted_value
pw = 'password'
crypto_key = SecureRandom.urlsafe_base64(24)
old_iv = SecureRandom.random_bytes(12)
account = Account.create
encrypted_value = Encryptor.encrypt(value: pw, iv: old_iv, key: crypto_key)
Account.where(id: account.id).update_all(key: crypto_key, encrypted_password_iv: [old_iv].pack('m'), encrypted_password: [encrypted_value].pack('m'))
account = Account.find(account.id)
assert_equal pw, account.password
account.password = pw.reverse
assert_equal pw, account.password_was
account.save
account.reload
assert_equal Account::ACCOUNT_ENCRYPTION_KEY, account.key
assert_equal pw.reverse, account.password
end
end

# ActiveRecord 5.2 specific methods
Expand Down