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.
This commit is contained in:
Jorge Manrubia 2021-03-11 18:46:07 +01:00
parent 406fab468e
commit 81afcabd19
21 changed files with 74 additions and 61 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -4,7 +4,3 @@ class TrafficLight < ActiveRecord::Base
serialize :state, Array
serialize :long_state, Array
end
class EncryptedTrafficLight < TrafficLight
encrypts :state
end

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
require "models/traffic_light"
class EncryptedTrafficLight < TrafficLight
encrypts :state
end