Sha256: 19eba789e20d0189ea72b43f606e4840cb35181eb40fb93aec28fe50939cde50

Contents?: true

Size: 1.66 KB

Versions: 11

Compression:

Stored size: 1.66 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

11 entries across 11 versions & 1 rubygems

Version Path
shoulda-matchers-4.4.1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.4.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.3.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.2.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.1.2 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.1.1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.1.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.0.1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-3.1.3 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-4.0.0.rc1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-3.1.2 lib/shoulda/matchers/action_controller/filter_param_matcher.rb