attr_encrypted 3.1.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'test_helper'
2
4
 
5
+ RAILS_VERSION = Gem::Version.new(::ActiveRecord::VERSION::STRING).freeze
3
6
  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
4
7
 
5
8
  def create_tables
@@ -43,16 +46,14 @@ end
43
46
 
44
47
  ActiveRecord::MissingAttributeError = ActiveModel::MissingAttributeError unless defined?(ActiveRecord::MissingAttributeError)
45
48
 
46
- if ::ActiveRecord::VERSION::STRING > "4.0"
47
- module Rack
48
- module Test
49
- class UploadedFile; end
50
- end
49
+ module Rack
50
+ module Test
51
+ class UploadedFile; end
51
52
  end
52
-
53
- require 'action_controller/metal/strong_parameters'
54
53
  end
55
54
 
55
+ require 'action_controller/metal/strong_parameters'
56
+
56
57
  class Person < ActiveRecord::Base
57
58
  self.attr_encrypted_options[:mode] = :per_attribute_iv_and_salt
58
59
  attr_encrypted :email, key: SECRET_KEY
@@ -83,7 +84,7 @@ class Account < ActiveRecord::Base
83
84
  attr_encrypted :password, key: :password_encryption_key
84
85
 
85
86
  def encrypting?(attr)
86
- encrypted_attributes[attr][:operation] == :encrypting
87
+ attr_encrypted_encrypted_attributes[attr][:operation] == :encrypting
87
88
  end
88
89
 
89
90
  def password_encryption_key
@@ -104,7 +105,6 @@ end
104
105
  class UserWithProtectedAttribute < ActiveRecord::Base
105
106
  self.table_name = 'users'
106
107
  attr_encrypted :password, key: SECRET_KEY
107
- attr_protected :is_admin if ::ActiveRecord::VERSION::STRING < "4.0"
108
108
  end
109
109
 
110
110
  class PersonUsingAlias < ActiveRecord::Base
@@ -219,52 +219,53 @@ class ActiveRecordTest < Minitest::Test
219
219
  assert_equal pw.reverse, account.password
220
220
  end
221
221
 
222
- if ::ActiveRecord::VERSION::STRING > "4.0"
223
- def test_should_assign_attributes
224
- @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
225
- @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login)
226
- assert_equal 'modified', @user.login
222
+ if Gem::Requirement.new('>= 5.2').satisfied_by?(RAILS_VERSION)
223
+ def test_should_create_will_save_change_to_predicate
224
+ person = Person.create!(email: 'test@example.com')
225
+ refute person.will_save_change_to_email?
226
+ person.email = 'test@example.com'
227
+ refute person.will_save_change_to_email?
228
+ person.email = 'test2@example.com'
229
+ assert person.will_save_change_to_email?
227
230
  end
228
231
 
229
- def test_should_not_assign_protected_attributes
230
- @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
231
- @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login)
232
- assert !@user.is_admin?
232
+ def test_should_create_saved_change_to_predicate
233
+ person = Person.create!(email: 'test@example.com')
234
+ assert person.saved_change_to_email?
235
+ person.reload
236
+ person.email = 'test@example.com'
237
+ refute person.saved_change_to_email?
238
+ person.email = nil
239
+ refute person.saved_change_to_email?
240
+ person.email = 'test2@example.com'
241
+ refute person.saved_change_to_email?
242
+ person.save
243
+ assert person.saved_change_to_email?
233
244
  end
245
+ end
234
246
 
235
- def test_should_raise_exception_if_not_permitted
236
- @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
237
- assert_raises ActiveModel::ForbiddenAttributesError do
238
- @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true)
239
- end
240
- end
247
+ def test_should_assign_attributes
248
+ @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
249
+ @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login)
250
+ assert_equal 'modified', @user.login
251
+ end
241
252
 
242
- def test_should_raise_exception_on_init_if_not_permitted
243
- assert_raises ActiveModel::ForbiddenAttributesError do
244
- @user = UserWithProtectedAttribute.new ActionController::Parameters.new(login: 'modified', is_admin: true)
245
- end
246
- end
247
- else
248
- def test_should_assign_attributes
249
- @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
250
- @user.attributes = { login: 'modified', is_admin: true }
251
- assert_equal 'modified', @user.login
252
- end
253
+ def test_should_not_assign_protected_attributes
254
+ @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
255
+ @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true).permit(:login)
256
+ assert !@user.is_admin?
257
+ end
253
258
 
254
- def test_should_not_assign_protected_attributes
255
- @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
256
- @user.attributes = { login: 'modified', is_admin: true }
257
- assert !@user.is_admin?
259
+ def test_should_raise_exception_if_not_permitted
260
+ @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
261
+ assert_raises ActiveModel::ForbiddenAttributesError do
262
+ @user.attributes = ActionController::Parameters.new(login: 'modified', is_admin: true)
258
263
  end
264
+ end
259
265
 
260
- def test_should_assign_protected_attributes
261
- @user = UserWithProtectedAttribute.new(login: 'login', is_admin: false)
262
- if ::ActiveRecord::VERSION::STRING > "3.1"
263
- @user.send(:assign_attributes, { login: 'modified', is_admin: true }, without_protection: true)
264
- else
265
- @user.send(:attributes=, { login: 'modified', is_admin: true }, false)
266
- end
267
- assert @user.is_admin?
266
+ def test_should_raise_exception_on_init_if_not_permitted
267
+ assert_raises ActiveModel::ForbiddenAttributesError do
268
+ @user = UserWithProtectedAttribute.new ActionController::Parameters.new(login: 'modified', is_admin: true)
268
269
  end
269
270
  end
270
271
 
@@ -277,23 +278,21 @@ class ActiveRecordTest < Minitest::Test
277
278
  @person = PersonWithProcMode.create(email: 'test@example.com', credentials: 'password123')
278
279
 
279
280
  # Email is :per_attribute_iv_and_salt
280
- assert_equal @person.class.encrypted_attributes[:email][:mode].class, Proc
281
- assert_equal @person.class.encrypted_attributes[:email][:mode].call, :per_attribute_iv_and_salt
281
+ assert_equal @person.class.attr_encrypted_encrypted_attributes[:email][:mode].class, Proc
282
+ assert_equal @person.class.attr_encrypted_encrypted_attributes[:email][:mode].call, :per_attribute_iv_and_salt
282
283
  refute_nil @person.encrypted_email_salt
283
284
  refute_nil @person.encrypted_email_iv
284
285
 
285
286
  # Credentials is :single_iv_and_salt
286
- assert_equal @person.class.encrypted_attributes[:credentials][:mode].class, Proc
287
- assert_equal @person.class.encrypted_attributes[:credentials][:mode].call, :single_iv_and_salt
287
+ assert_equal @person.class.attr_encrypted_encrypted_attributes[:credentials][:mode].class, Proc
288
+ assert_equal @person.class.attr_encrypted_encrypted_attributes[:credentials][:mode].call, :single_iv_and_salt
288
289
  assert_nil @person.encrypted_credentials_salt
289
290
  assert_nil @person.encrypted_credentials_iv
290
291
  end
291
292
 
292
- if ::ActiveRecord::VERSION::STRING > "3.1"
293
- def test_should_allow_assign_attributes_with_nil
294
- @person = Person.new
295
- assert_nil(@person.assign_attributes nil)
296
- end
293
+ def test_should_allow_assign_attributes_with_nil
294
+ @person = Person.new
295
+ assert_nil(@person.assign_attributes nil)
297
296
  end
298
297
 
299
298
  def test_that_alias_encrypts_column
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # encoding: UTF-8
2
4
  require_relative 'test_helper'
3
5
 
@@ -81,11 +83,11 @@ class AttrEncryptedTest < Minitest::Test
81
83
  end
82
84
 
83
85
  def test_should_store_email_in_encrypted_attributes
84
- assert User.encrypted_attributes.include?(:email)
86
+ assert User.attr_encrypted_encrypted_attributes.include?(:email)
85
87
  end
86
88
 
87
89
  def test_should_not_store_salt_in_encrypted_attributes
88
- refute User.encrypted_attributes.include?(:salt)
90
+ refute User.attr_encrypted_encrypted_attributes.include?(:salt)
89
91
  end
90
92
 
91
93
  def test_attr_encrypted_should_return_true_for_email
@@ -93,7 +95,7 @@ class AttrEncryptedTest < Minitest::Test
93
95
  end
94
96
 
95
97
  def test_attr_encrypted_should_not_use_the_same_attribute_name_for_two_attributes_in_the_same_line
96
- refute_equal User.encrypted_attributes[:email][:attribute], User.encrypted_attributes[:without_encoding][:attribute]
98
+ refute_equal User.attr_encrypted_encrypted_attributes[:email][:attribute], User.attr_encrypted_encrypted_attributes[:without_encoding][:attribute]
97
99
  end
98
100
 
99
101
  def test_attr_encrypted_should_return_false_for_salt
@@ -152,7 +154,7 @@ class AttrEncryptedTest < Minitest::Test
152
154
  def test_should_decrypt_email_when_reading
153
155
  @user = User.new
154
156
  assert_nil @user.email
155
- options = @user.encrypted_attributes[:email]
157
+ options = @user.attr_encrypted_encrypted_attributes[:email]
156
158
  iv = @user.send(:generate_iv, options[:algorithm])
157
159
  encoded_iv = [iv].pack(options[:encode_iv])
158
160
  salt = SecureRandom.random_bytes
@@ -221,7 +223,7 @@ class AttrEncryptedTest < Minitest::Test
221
223
  end
222
224
 
223
225
  def test_should_inherit_encrypted_attributes
224
- assert_equal [User.encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, Admin.encrypted_attributes.keys.collect { |key| key.to_s }.sort
226
+ assert_equal [User.attr_encrypted_encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, Admin.attr_encrypted_encrypted_attributes.keys.collect { |key| key.to_s }.sort
225
227
  end
226
228
 
227
229
  def test_should_inherit_attr_encrypted_options
@@ -231,7 +233,7 @@ class AttrEncryptedTest < Minitest::Test
231
233
 
232
234
  def test_should_not_inherit_unrelated_attributes
233
235
  assert SomeOtherClass.attr_encrypted_options.empty?
234
- assert SomeOtherClass.encrypted_attributes.empty?
236
+ assert SomeOtherClass.attr_encrypted_encrypted_attributes.empty?
235
237
  end
236
238
 
237
239
  def test_should_evaluate_a_symbol_option
@@ -302,7 +304,7 @@ class AttrEncryptedTest < Minitest::Test
302
304
  end
303
305
 
304
306
  def test_should_work_with_aliased_attr_encryptor
305
- assert User.encrypted_attributes.include?(:aliased)
307
+ assert User.attr_encrypted_encrypted_attributes.include?(:aliased)
306
308
  end
307
309
 
308
310
  def test_should_always_reset_options
@@ -379,12 +381,12 @@ class AttrEncryptedTest < Minitest::Test
379
381
  @user2 = User.new
380
382
  @user2.email = 'test@example.com'
381
383
 
382
- assert_equal 'test@example.com', @user1.decrypt(:email, @user1.encrypted_email)
384
+ assert_equal 'test@example.com', @user1.attr_encrypted_decrypt(:email, @user1.encrypted_email)
383
385
  end
384
386
 
385
387
  def test_should_specify_the_default_algorithm
386
- assert YetAnotherClass.encrypted_attributes[:email][:algorithm]
387
- assert_equal YetAnotherClass.encrypted_attributes[:email][:algorithm], 'aes-256-gcm'
388
+ assert YetAnotherClass.attr_encrypted_encrypted_attributes[:email][:algorithm]
389
+ assert_equal YetAnotherClass.attr_encrypted_encrypted_attributes[:email][:algorithm], 'aes-256-gcm'
388
390
  end
389
391
 
390
392
  def test_should_not_encode_iv_when_encode_iv_is_false
@@ -466,4 +468,23 @@ class AttrEncryptedTest < Minitest::Test
466
468
  user.with_true_if = nil
467
469
  assert_nil user.encrypted_with_true_if_iv
468
470
  end
471
+
472
+ def test_encrypted_attributes_state_is_not_shared
473
+ user = User.new
474
+ user.ssn = '123456789'
475
+
476
+ another_user = User.new
477
+
478
+ assert_equal :encrypting, user.attr_encrypted_encrypted_attributes[:ssn][:operation]
479
+ assert_nil another_user.attr_encrypted_encrypted_attributes[:ssn][:operation]
480
+ end
481
+
482
+ def test_should_not_by_default_generate_key_when_attribute_is_empty
483
+ user = User.new
484
+ calls = 0
485
+ user.stub(:secret_key, lambda { calls += 1; SECRET_KEY }) do
486
+ user.ssn
487
+ end
488
+ assert_equal 0, calls
489
+ end
469
490
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -*- encoding: utf-8 -*-
2
4
  require_relative 'test_helper'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -*- encoding: utf-8 -*-
2
4
  require_relative 'test_helper'
3
5
 
@@ -27,9 +29,15 @@ class LegacyPerson < ActiveRecord::Base
27
29
  attr_encrypted :email, :key => 'a secret key'
28
30
  attr_encrypted :credentials, :key => Proc.new { |user| Encryptor.encrypt(:value => user.salt, :key => 'some private key', insecure_mode: true, algorithm: 'aes-256-cbc') }, :marshal => true
29
31
 
30
- ActiveSupport::Deprecation.silenced = true
31
- def after_initialize; end
32
- ActiveSupport::Deprecation.silenced = false
32
+ if ActiveRecord.respond_to?(:deprecator)
33
+ ActiveRecord.deprecator.silence do
34
+ def after_initialize; end
35
+ end
36
+ else
37
+ ActiveSupport::Deprecation.silenced = true
38
+ def after_initialize; end
39
+ ActiveSupport::Deprecation.silenced = false
40
+ end
33
41
 
34
42
  after_initialize :initialize_salt_and_credentials
35
43
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -*- encoding: utf-8 -*-
2
4
  require_relative 'test_helper'
3
5
 
@@ -56,11 +58,11 @@ end
56
58
  class LegacyAttrEncryptedTest < Minitest::Test
57
59
 
58
60
  def test_should_store_email_in_encrypted_attributes
59
- assert LegacyUser.encrypted_attributes.include?(:email)
61
+ assert LegacyUser.attr_encrypted_encrypted_attributes.include?(:email)
60
62
  end
61
63
 
62
64
  def test_should_not_store_salt_in_encrypted_attributes
63
- assert !LegacyUser.encrypted_attributes.include?(:salt)
65
+ assert !LegacyUser.attr_encrypted_encrypted_attributes.include?(:salt)
64
66
  end
65
67
 
66
68
  def test_attr_encrypted_should_return_true_for_email
@@ -68,7 +70,7 @@ class LegacyAttrEncryptedTest < Minitest::Test
68
70
  end
69
71
 
70
72
  def test_attr_encrypted_should_not_use_the_same_attribute_name_for_two_attributes_in_the_same_line
71
- refute_equal LegacyUser.encrypted_attributes[:email][:attribute], LegacyUser.encrypted_attributes[:without_encoding][:attribute]
73
+ refute_equal LegacyUser.attr_encrypted_encrypted_attributes[:email][:attribute], LegacyUser.attr_encrypted_encrypted_attributes[:without_encoding][:attribute]
72
74
  end
73
75
 
74
76
  def test_attr_encrypted_should_return_false_for_salt
@@ -199,7 +201,7 @@ class LegacyAttrEncryptedTest < Minitest::Test
199
201
  end
200
202
 
201
203
  def test_should_inherit_encrypted_attributes
202
- assert_equal [LegacyUser.encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, LegacyAdmin.encrypted_attributes.keys.collect { |key| key.to_s }.sort
204
+ assert_equal [LegacyUser.attr_encrypted_encrypted_attributes.keys, :testing].flatten.collect { |key| key.to_s }.sort, LegacyAdmin.attr_encrypted_encrypted_attributes.keys.collect { |key| key.to_s }.sort
203
205
  end
204
206
 
205
207
  def test_should_inherit_attr_encrypted_options
@@ -209,7 +211,7 @@ class LegacyAttrEncryptedTest < Minitest::Test
209
211
 
210
212
  def test_should_not_inherit_unrelated_attributes
211
213
  assert LegacySomeOtherClass.attr_encrypted_options.empty?
212
- assert LegacySomeOtherClass.encrypted_attributes.empty?
214
+ assert LegacySomeOtherClass.attr_encrypted_encrypted_attributes.empty?
213
215
  end
214
216
 
215
217
  def test_should_evaluate_a_symbol_option
@@ -266,7 +268,7 @@ class LegacyAttrEncryptedTest < Minitest::Test
266
268
  end
267
269
 
268
270
  def test_should_work_with_aliased_attr_encryptor
269
- assert LegacyUser.encrypted_attributes.include?(:aliased)
271
+ assert LegacyUser.attr_encrypted_encrypted_attributes.include?(:aliased)
270
272
  end
271
273
 
272
274
  def test_should_always_reset_options
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -*- encoding: utf-8 -*-
2
4
  require_relative 'test_helper'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'test_helper'
2
4
 
3
5
  DB.create_table :legacy_humans do
data/test/run.sh CHANGED
@@ -1,12 +1,20 @@
1
- #!/usr/bin/env sh -e
1
+ #!/usr/bin/env bash
2
2
 
3
- for RUBY in 1.9.3 2.0.0 2.1 2.2
3
+ set -e
4
+
5
+ for RUBY in 2.6.10 2.7.6
4
6
  do
5
- for RAILS in 2.3.8 3.0.0 3.1.0 3.2.0 4.0.0 4.1.0 4.2.0
7
+ for ACTIVERECORD in 5.1.1 5.2.8
6
8
  do
7
- if [[ $RUBY -gt 1.9.3 && $RAILS -lt 4.0.0 ]]; then
8
- continue
9
- fi
10
- RBENV_VERSION=$RUBY ACTIVERECORD=$RAILS bundle && bundle exec rake
9
+ echo ">>> Testing with Ruby ${RUBY} and ActiveRecord ${ACTIVERECORD}."
10
+ export RBENV_VERSION=$RUBY
11
+ export ACTIVERECORD=$ACTIVERECORD
12
+
13
+ rbenv install $RUBY --skip-existing
14
+ bundle install
15
+ bundle check
16
+ bundle exec rake test
17
+ rm Gemfile.lock
18
+ echo ">>> Finished testing with Ruby ${RUBY} and ActiveRecord ${ACTIVERECORD}."
11
19
  done
12
20
  done
data/test/sequel_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'test_helper'
2
4
 
3
5
  DB.create_table :humans do
data/test/test_helper.rb CHANGED
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
1
4
  require 'simplecov'
2
5
  require 'simplecov-rcov'
3
- require "codeclimate-test-reporter"
4
6
 
5
7
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(
6
8
  [
7
9
  SimpleCov::Formatter::HTMLFormatter,
8
- SimpleCov::Formatter::RcovFormatter,
9
- CodeClimate::TestReporter::Formatter
10
+ SimpleCov::Formatter::RcovFormatter
10
11
  ]
11
12
  )
12
13
 
@@ -14,20 +15,16 @@ SimpleCov.start do
14
15
  add_filter 'test'
15
16
  end
16
17
 
17
- CodeClimate::TestReporter.start
18
-
19
18
  require 'minitest/autorun'
20
-
21
- # Rails 4.0.x pins to an old minitest
22
- unless defined?(MiniTest::Test)
23
- MiniTest::Test = MiniTest::Unit::TestCase
24
- end
25
-
26
19
  require 'active_record'
27
- require 'data_mapper'
28
20
  require 'digest/sha2'
29
21
  require 'sequel'
30
- ActiveSupport::Deprecation.behavior = :raise
22
+
23
+ if ActiveRecord.respond_to?(:deprecator)
24
+ ActiveRecord.deprecator.behavior = :raise
25
+ else
26
+ ActiveSupport::Deprecation.behavior = :raise
27
+ end
31
28
 
32
29
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
33
30
  $:.unshift(File.dirname(__FILE__))