From 81afcabd1985ba2c1b816d223b18eda6f0953282 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Thu, 11 Mar 2021 18:46:07 +0100 Subject: [PATCH] Extract encrypted models to their own files We are not encrypting attributes when loading models with the table missing. This way we make sure we only load the encrypted models when necessary during the encryption tests and prevent the problem of missing encrypted attributes due to having cached the class without them encrypted. --- .../test/cases/encryption/concurrency_test.rb | 2 +- .../cases/encryption/configurable_test.rb | 2 +- .../test/cases/encryption/contexts_test.rb | 4 ++-- .../encryption/encryptable_record_api_test.rb | 6 +++--- .../encryption/encryptable_record_test.rb | 8 +++---- .../encryption/encrypted_fixtures_test.rb | 2 +- .../encryption/encryption_schemes_test.rb | 2 +- .../extended_deterministic_queries_test.rb | 2 +- .../cases/encryption/mass_encryption_test.rb | 4 ++-- .../encryption_performance_test.rb | 4 ++-- .../envelope_encryption_performance_test.rb | 2 +- ..._deterministic_queries_performance_test.rb | 2 +- .../encryption/unencrypted_attributes_test.rb | 2 +- activerecord/test/models/author.rb | 6 ------ activerecord/test/models/author_encrypted.rb | 9 ++++++++ activerecord/test/models/book.rb | 18 ---------------- activerecord/test/models/book_encrypted.rb | 21 +++++++++++++++++++ activerecord/test/models/post.rb | 12 ----------- activerecord/test/models/post_encrypted.rb | 15 +++++++++++++ activerecord/test/models/traffic_light.rb | 4 ---- .../test/models/traffic_light_encrypted.rb | 8 +++++++ 21 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 activerecord/test/models/author_encrypted.rb create mode 100644 activerecord/test/models/book_encrypted.rb create mode 100644 activerecord/test/models/post_encrypted.rb create mode 100644 activerecord/test/models/traffic_light_encrypted.rb diff --git a/activerecord/test/cases/encryption/concurrency_test.rb b/activerecord/test/cases/encryption/concurrency_test.rb index a25520face..0740533f03 100644 --- a/activerecord/test/cases/encryption/concurrency_test.rb +++ b/activerecord/test/cases/encryption/concurrency_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/post" +require "models/post_encrypted" class ActiveRecord::Encryption::ConcurrencyTest < ActiveRecord::TestCase setup do diff --git a/activerecord/test/cases/encryption/configurable_test.rb b/activerecord/test/cases/encryption/configurable_test.rb index c0a0316e28..80f09c398d 100644 --- a/activerecord/test/cases/encryption/configurable_test.rb +++ b/activerecord/test/cases/encryption/configurable_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/book" +require "models/book_encrypted" class ActiveRecord::Encryption::ConfigurableTest < ActiveRecord::TestCase test "can access context properties with top level getters" do diff --git a/activerecord/test/cases/encryption/contexts_test.rb b/activerecord/test/cases/encryption/contexts_test.rb index e4fcc5753d..f9a18b4549 100644 --- a/activerecord/test/cases/encryption/contexts_test.rb +++ b/activerecord/test/cases/encryption/contexts_test.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/book" -require "models/post" +require "models/book_encrypted" +require "models/post_encrypted" class ActiveRecord::Encryption::ContextsTest < ActiveRecord::TestCase fixtures :posts diff --git a/activerecord/test/cases/encryption/encryptable_record_api_test.rb b/activerecord/test/cases/encryption/encryptable_record_api_test.rb index 528d55415b..d716a67884 100644 --- a/activerecord/test/cases/encryption/encryptable_record_api_test.rb +++ b/activerecord/test/cases/encryption/encryptable_record_api_test.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/author" -require "models/book" -require "models/post" +require "models/author_encrypted" +require "models/book_encrypted" +require "models/post_encrypted" class ActiveRecord::Encryption::EncryptableRecordApiTest < ActiveRecord::TestCase fixtures :posts diff --git a/activerecord/test/cases/encryption/encryptable_record_test.rb b/activerecord/test/cases/encryption/encryptable_record_test.rb index 8be878aee8..0b0a36b30f 100644 --- a/activerecord/test/cases/encryption/encryptable_record_test.rb +++ b/activerecord/test/cases/encryption/encryptable_record_test.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/author" -require "models/book" -require "models/post" -require "models/traffic_light" +require "models/author_encrypted" +require "models/book_encrypted" +require "models/post_encrypted" +require "models/traffic_light_encrypted" class ActiveRecord::Encryption::EncryptableRecordTest < ActiveRecord::TestCase fixtures :books, :posts diff --git a/activerecord/test/cases/encryption/encrypted_fixtures_test.rb b/activerecord/test/cases/encryption/encrypted_fixtures_test.rb index 218d28737f..e05d94b384 100644 --- a/activerecord/test/cases/encryption/encrypted_fixtures_test.rb +++ b/activerecord/test/cases/encryption/encrypted_fixtures_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/book" +require "models/book_encrypted" class ActiveRecord::Encryption::EncryptableFixtureTest < ActiveRecord::TestCase fixtures :encrypted_books, :encrypted_book_that_ignores_cases diff --git a/activerecord/test/cases/encryption/encryption_schemes_test.rb b/activerecord/test/cases/encryption/encryption_schemes_test.rb index 59bc8d0d9a..fbcd9b88d3 100644 --- a/activerecord/test/cases/encryption/encryption_schemes_test.rb +++ b/activerecord/test/cases/encryption/encryption_schemes_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/author" +require "models/author_encrypted" class ActiveRecord::Encryption::EncryptionSchemesTest < ActiveRecord::TestCase test "can decrypt encrypted_value encrypted with a different encryption scheme" do diff --git a/activerecord/test/cases/encryption/extended_deterministic_queries_test.rb b/activerecord/test/cases/encryption/extended_deterministic_queries_test.rb index 7904748892..1c8c1bb922 100644 --- a/activerecord/test/cases/encryption/extended_deterministic_queries_test.rb +++ b/activerecord/test/cases/encryption/extended_deterministic_queries_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/book" +require "models/book_encrypted" class ActiveRecord::Encryption::ExtendedDeterministicQueriesTest < ActiveRecord::TestCase setup do diff --git a/activerecord/test/cases/encryption/mass_encryption_test.rb b/activerecord/test/cases/encryption/mass_encryption_test.rb index 14efd17fb7..085d320a96 100644 --- a/activerecord/test/cases/encryption/mass_encryption_test.rb +++ b/activerecord/test/cases/encryption/mass_encryption_test.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/author" -require "models/post" +require "models/author_encrypted" +require "models/post_encrypted" class ActiveRecord::Encryption::MassEncryptionTest < ActiveRecord::TestCase setup do diff --git a/activerecord/test/cases/encryption/performance/encryption_performance_test.rb b/activerecord/test/cases/encryption/performance/encryption_performance_test.rb index 0ccd4252de..57673ecf70 100644 --- a/activerecord/test/cases/encryption/performance/encryption_performance_test.rb +++ b/activerecord/test/cases/encryption/performance/encryption_performance_test.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/book" -require "models/post" +require "models/book_encrypted" +require "models/post_encrypted" class ActiveRecord::Encryption::EncryptionPerformanceTest < ActiveRecord::TestCase fixtures :encrypted_books, :posts diff --git a/activerecord/test/cases/encryption/performance/envelope_encryption_performance_test.rb b/activerecord/test/cases/encryption/performance/envelope_encryption_performance_test.rb index ce66bb3431..a96eed2ceb 100644 --- a/activerecord/test/cases/encryption/performance/envelope_encryption_performance_test.rb +++ b/activerecord/test/cases/encryption/performance/envelope_encryption_performance_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/book" +require "models/book_encrypted" class ActiveRecord::Encryption::EvenlopeEncryptionPerformanceTest < ActiveRecord::TestCase fixtures :encrypted_books diff --git a/activerecord/test/cases/encryption/performance/extended_deterministic_queries_performance_test.rb b/activerecord/test/cases/encryption/performance/extended_deterministic_queries_performance_test.rb index 434ca3450a..79fdd63964 100644 --- a/activerecord/test/cases/encryption/performance/extended_deterministic_queries_performance_test.rb +++ b/activerecord/test/cases/encryption/performance/extended_deterministic_queries_performance_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/book" +require "models/book_encrypted" class ActiveRecord::Encryption::ExtendedDeterministicQueriesPerformanceTest < ActiveRecord::TestCase # TODO: Is this failing only with SQLite/in memory adapter? diff --git a/activerecord/test/cases/encryption/unencrypted_attributes_test.rb b/activerecord/test/cases/encryption/unencrypted_attributes_test.rb index ba4b3ae048..f651cde4ed 100644 --- a/activerecord/test/cases/encryption/unencrypted_attributes_test.rb +++ b/activerecord/test/cases/encryption/unencrypted_attributes_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/encryption/helper" -require "models/post" +require "models/post_encrypted" class ActiveRecord::Encryption::UnencryptedAttributesTest < ActiveRecord::TestCase test "when :support_unencrypted_data is off, it works with unencrypted attributes normally" do diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index f2e8fc6d0f..4d9541221e 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -258,9 +258,3 @@ class AuthorFavoriteWithScope < ActiveRecord::Base belongs_to :author belongs_to :favorite_author, class_name: "Author" end - -class EncryptedAuthor < Author - self.table_name = "authors" - - encrypts :name, key: "my very own key", previous: { deterministic: true } -end diff --git a/activerecord/test/models/author_encrypted.rb b/activerecord/test/models/author_encrypted.rb new file mode 100644 index 0000000000..7512c25c78 --- /dev/null +++ b/activerecord/test/models/author_encrypted.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "models/author" + +class EncryptedAuthor < Author + self.table_name = "authors" + + encrypts :name, key: "my very own key", previous: { deterministic: true } +end diff --git a/activerecord/test/models/book.rb b/activerecord/test/models/book.rb index 2b284fec5e..e3e7862336 100644 --- a/activerecord/test/models/book.rb +++ b/activerecord/test/models/book.rb @@ -33,21 +33,3 @@ class PublishedBook < ActiveRecord::Base validates_uniqueness_of :isbn end - -class EncryptedBook < Book - self.table_name = "books" - - encrypts :name, deterministic: true -end - -class EncryptedBookWithDowncaseName < Book - self.table_name = "books" - - encrypts :name, deterministic: true, downcase: true -end - -class EncryptedBookThatIgnoresCase < Book - self.table_name = "books" - - encrypts :name, deterministic: true, ignore_case: true -end diff --git a/activerecord/test/models/book_encrypted.rb b/activerecord/test/models/book_encrypted.rb new file mode 100644 index 0000000000..ba32ee3c76 --- /dev/null +++ b/activerecord/test/models/book_encrypted.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "models/book" + +class EncryptedBook < Book + self.table_name = "books" + + encrypts :name, deterministic: true +end + +class EncryptedBookWithDowncaseName < Book + self.table_name = "books" + + encrypts :name, deterministic: true, downcase: true +end + +class EncryptedBookThatIgnoresCase < Book + self.table_name = "books" + + encrypts :name, deterministic: true, ignore_case: true +end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index c7760875c5..d55d1e16ae 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -377,15 +377,3 @@ class Postesque < ActiveRecord::Base belongs_to :author_with_address, class_name: "Author", foreign_key: :author_id belongs_to :author_with_the_letter_a, class_name: "Author", foreign_key: :author_id end - -class EncryptedPost < Post - self.table_name = "posts" - - # We want to modify the key for testing purposes - class MutableDerivedSecretKeyProvider < ActiveRecord::Encryption::DerivedSecretKeyProvider - attr_accessor :key - end - - encrypts :title - encrypts :body, key_provider: MutableDerivedSecretKeyProvider.new("my post body secret!") -end diff --git a/activerecord/test/models/post_encrypted.rb b/activerecord/test/models/post_encrypted.rb new file mode 100644 index 0000000000..7275f34d8e --- /dev/null +++ b/activerecord/test/models/post_encrypted.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "models/post" + +class EncryptedPost < Post + self.table_name = "posts" + + # We want to modify the key for testing purposes + class MutableDerivedSecretKeyProvider < ActiveRecord::Encryption::DerivedSecretKeyProvider + attr_accessor :key + end + + encrypts :title + encrypts :body, key_provider: MutableDerivedSecretKeyProvider.new("my post body secret!") +end diff --git a/activerecord/test/models/traffic_light.rb b/activerecord/test/models/traffic_light.rb index a557f61fa2..0b88815cbd 100644 --- a/activerecord/test/models/traffic_light.rb +++ b/activerecord/test/models/traffic_light.rb @@ -4,7 +4,3 @@ class TrafficLight < ActiveRecord::Base serialize :state, Array serialize :long_state, Array end - -class EncryptedTrafficLight < TrafficLight - encrypts :state -end diff --git a/activerecord/test/models/traffic_light_encrypted.rb b/activerecord/test/models/traffic_light_encrypted.rb new file mode 100644 index 0000000000..fd23e95e99 --- /dev/null +++ b/activerecord/test/models/traffic_light_encrypted.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + + +require "models/traffic_light" + +class EncryptedTrafficLight < TrafficLight + encrypts :state +end