Sha256: 7528c25b00bbe42783d56aa9d72066a2a1a71fcdaa6c43b74c2ac3151ff4331b

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 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
      #     describe ApplicationController 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

4 entries across 4 versions & 1 rubygems

Version Path
shoulda-matchers-3.1.1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-3.1.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-3.0.1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-3.0.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb