Sha256: bfc1314cfa237e8cd582c34eb1e9576ae79d87ebabb976455afb055101dadf12

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 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
      #
      #     # Test::Unit
      #     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
        alias failure_message_for_should failure_message

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

        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

6 entries across 6 versions & 2 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-3.0.0.rc1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-2.8.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-2.8.0.rc2 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-2.8.0.rc1 lib/shoulda/matchers/action_controller/filter_param_matcher.rb
shoulda-matchers-2.7.0 lib/shoulda/matchers/action_controller/filter_param_matcher.rb