Sha256: 998474073ad1bb3622e04a5a7a2eb9f5f5e1be9691209ad51b741428577acea2

Contents?: true

Size: 1.68 KB

Versions: 13

Compression:

Stored size: 1.68 KB

Contents

module Shoulda
  module Matchers
    module ActionController
      # The `filter_param` matcher is used to test parameter filtering
      # configuration. Specifically, it asserts that the given parameter is
      # present in `config.filter_parameters`.
      #
      #     class MyApplication < Rails::Application
      #       config.filter_parameters << :secret_key
      #     end
      #
      #     # RSpec
      #     RSpec.describe ApplicationController, type: :controller do
      #       it { should filter_param(:secret_key) }
      #     end
      #
      #     # Minitest (Shoulda)
      #     class ApplicationControllerTest < ActionController::TestCase
      #       should filter_param(:secret_key)
      #     end
      #
      # @return [FilterParamMatcher]
      #
      def filter_param(key)
        FilterParamMatcher.new(key)
      end

      # @private
      class FilterParamMatcher
        def initialize(key)
          @key = key
        end

        def matches?(_controller)
          filters_key?
        end

        def failure_message
          "Expected #{@key} to be filtered; filtered keys:"\
            " #{filtered_keys.join(', ')}"
        end

        def failure_message_when_negated
          "Did not expect #{@key} to be filtered"
        end

        def description
          "filter #{@key}"
        end

        private

        def filters_key?
          filtered_keys.any? do |filter|
            case filter
            when Regexp
              filter =~ @key
            else
              filter == @key
            end
          end
        end

        def filtered_keys
          Rails.application.config.filter_parameters
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shoulda-matchers-6.4.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-6.3.1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-6.3.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-6.2.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-6.1.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-6.0.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-5.3.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-5.2.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-5.1.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-5.0.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-5.0.0.rc1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.5.1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.5.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb