Repository: rails/rails
File: railties/test/application/configuration_test.rb (Ruby)

Code before:

      assert_equal :generate_and_verify, Rails.application.config.active_record.use_legacy_signed_id_verifier
    end

    test "generate signed Id with legacy options when Rails.application.config.active_record.use_legacy_signed_id_verifier is :generate_and_verify" do
      add_to_config <<-RUBY
        config.active_record.use_legacy_signed_id_verifier = :generate_and_verify
      RUBY

      app "development"

      first_rotate_option = ActiveRecord.message_verifiers.instance_variable_get(:@rotate_options).first.call("active_record/signed_id")
      assert_equal ({ digest: "SHA256", serializer: JSON, url_safe: true }), first_rotate_option
    end

    test "generate signed Id with default options when Rails.application.config.active_record.use_legacy_signed_id_verifier is :verify" do
      add_to_config <<-RUBY
        config.active_record.use_legacy_signed_id_verifier = :verify
      RUBY

      app "development"

      assert_equal ({}), ActiveRecord.message_verifiers.instance_variable_get(:@rotate_options).first

      legacy_rotate_option = ActiveRecord.message_verifiers.instance_variable_get(:@rotate_options).second.call("active_record/signed_id")
      assert_equal ({ digest: "SHA256", serializer: JSON, url_safe: true }), legacy_rotate_option
    end

    test "no legacy rotation when Rails.application.config.active_record.use_legacy_signed_id_verifier is false" do
      add_to_config <<-RUBY
        config.active_record.use_legacy_signed_id_verifier = false
      RUBY

      app "development"

      rotate_options = ActiveRecord.message_verifiers.instance_variable_get(:@rotate_options)
      assert_equal [{}], rotate_options
    end

    test "PostgresqlAdapter.decode_dates is true by default for new apps" do
      app_file "config/initializers/active_record.rb", <<~RUBY
        ActiveRecord::Base.establish_connection(adapter: "postgresql")
      RUBY

      app "development"

      assert_equal true, ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.decode_dates
    end

    test "PostgresqlAdapter.decode_dates is false by default for upgraded apps" do
      remove_from_config '.*config\.load_defaults.*\n'

Review comment (suggestion):
Similar comment here: use a correctly configured message verifier to check the behavior of `Rails.application.message_verifiers["active_record/signed_id"]`.


Code after:

      assert_equal :generate_and_verify, Rails.application.config.active_record.use_legacy_signed_id_verifier
    end

    test "Rails.application.message_verifiers['active_record/signed_id'] generates and verifies messages using legacy options when config.active_record.use_legacy_signed_id_verifier is :generate_and_verify" do
      add_to_config <<-RUBY
        config.active_record.use_legacy_signed_id_verifier = :generate_and_verify
        config.secret_key_base = "secret"
      RUBY

      app "development"

      signed_id_verifier = Rails.application.message_verifiers["active_record/signed_id"]

      secret = app.key_generator.generate_key("active_record/signed_id")
      legacy_verifier = ActiveSupport::MessageVerifier.new(secret, digest: "SHA256", serializer: JSON, url_safe: true)

      assert_equal "message", legacy_verifier.verify(signed_id_verifier.generate("message"))
      assert_equal "message", signed_id_verifier.verify(legacy_verifier.generate("message"))
    end

    test "Rails.application.message_verifiers['active_record/signed_id'] verifies messages using legacy options when config.active_record.use_legacy_signed_id_verifier is :verify" do
      add_to_config <<-RUBY
        config.active_record.use_legacy_signed_id_verifier = :verify
        config.secret_key_base = "secret"
      RUBY

      app "development"

      signed_id_verifier = Rails.application.message_verifiers["active_record/signed_id"]

      secret = app.key_generator.generate_key("active_record/signed_id")
      legacy_verifier = ActiveSupport::MessageVerifier.new(secret, digest: "SHA256", serializer: JSON, url_safe: true)

      assert_equal "message", signed_id_verifier.verify(legacy_verifier.generate("message"))
      assert_raises ActiveSupport::MessageVerifier::InvalidSignature do
        legacy_verifier.verify(signed_id_verifier.generate("message"))
      end
    end

    test "Rails.application.message_verifiers['active_record/signed_id'] does not use legacy options when config.active_record.use_legacy_signed_id_verifier is false" do
      add_to_config <<-RUBY
        config.active_record.use_legacy_signed_id_verifier = false
        config.secret_key_base = "secret"
      RUBY

      app "development"

      signed_id_verifier = Rails.application.message_verifiers["active_record/signed_id"]

      secret = app.key_generator.generate_key("active_record/signed_id")