@@ -31,6 +31,13 @@ def self.safe_list_sanitizer
31
31
end
32
32
end
33
33
34
+ class ::MyCustomKeyProvider
35
+ attr_reader :primary_key
36
+ def initialize ( primary_key ) ; @primary_key = primary_key ; end
37
+ end
38
+
39
+ class ::MyOldKeyProvider ; end
40
+
34
41
module ApplicationTests
35
42
class ConfigurationTest < ActiveSupport ::TestCase
36
43
include ActiveSupport ::Testing ::Isolation
@@ -3819,6 +3826,74 @@ class Post < ActiveRecord::Base
3819
3826
assert_not_includes ActiveRecord ::Base . filter_attributes , :content
3820
3827
end
3821
3828
3829
+ test "ActiveRecord::Encryption.config is ready for encrypted attributes when app is lazy loaded" do
3830
+ add_to_config <<-RUBY
3831
+ config.enable_reloading = false
3832
+ config.eager_load = false
3833
+ RUBY
3834
+
3835
+ app_file "config/initializers/active_record.rb" , <<-RUBY
3836
+ Rails.application.config.active_record.encryption.primary_key = "dummy_key"
3837
+ Rails.application.config.active_record.encryption.previous = [ { key_provider: MyOldKeyProvider.new } ]
3838
+
3839
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
3840
+ ActiveRecord::Migration.verbose = false
3841
+ ActiveRecord::Schema.define(version: 1) do
3842
+ create_table :posts do |t|
3843
+ t.string :content
3844
+ end
3845
+ end
3846
+
3847
+ ActiveRecord::Base.connection.schema_cache.add("posts")
3848
+ RUBY
3849
+
3850
+ app_file "app/models/post.rb" , <<-RUBY
3851
+ class Post < ActiveRecord::Base
3852
+ encrypts :content, key_provider: MyCustomKeyProvider.new(ActiveRecord::Encryption.config.primary_key)
3853
+ end
3854
+ RUBY
3855
+
3856
+ app "development"
3857
+
3858
+ assert_kind_of ::MyOldKeyProvider , Post . attribute_types [ "content" ] . previous_schemes . first . key_provider
3859
+ assert_kind_of ::MyCustomKeyProvider , Post . attribute_types [ "content" ] . scheme . key_provider
3860
+ assert_equal "dummy_key" , Post . attribute_types [ "content" ] . scheme . key_provider . primary_key
3861
+ end
3862
+
3863
+ test "ActiveRecord::Encryption.config is ready for encrypted attributes when app is eager loaded" do
3864
+ add_to_config <<-RUBY
3865
+ config.enable_reloading = false
3866
+ config.eager_load = true
3867
+ RUBY
3868
+
3869
+ app_file "app/models/post.rb" , <<-RUBY
3870
+ class Post < ActiveRecord::Base
3871
+ encrypts :content, key_provider: MyCustomKeyProvider.new(ActiveRecord::Encryption.config.primary_key)
3872
+ end
3873
+ RUBY
3874
+
3875
+ app_file "config/initializers/active_record.rb" , <<-RUBY
3876
+ Rails.application.config.active_record.encryption.primary_key = "dummy_key"
3877
+ Rails.application.config.active_record.encryption.previous = [ { key_provider: MyOldKeyProvider.new } ]
3878
+
3879
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
3880
+ ActiveRecord::Migration.verbose = false
3881
+ ActiveRecord::Schema.define(version: 1) do
3882
+ create_table :posts do |t|
3883
+ t.string :content
3884
+ end
3885
+ end
3886
+
3887
+ ActiveRecord::Base.connection.schema_cache.add("posts")
3888
+ RUBY
3889
+
3890
+ app "production"
3891
+
3892
+ assert_kind_of ::MyOldKeyProvider , Post . attribute_types [ "content" ] . previous_schemes . first &.key_provider
3893
+ assert_kind_of ::MyCustomKeyProvider , Post . attribute_types [ "content" ] . scheme . key_provider
3894
+ assert_equal "dummy_key" , Post . attribute_types [ "content" ] . scheme . key_provider . primary_key
3895
+ end
3896
+
3822
3897
test "ActiveStorage.routes_prefix can be configured via config.active_storage.routes_prefix" do
3823
3898
app_file "config/environments/development.rb" , <<-RUBY
3824
3899
Rails.application.configure do
0 commit comments