Module: FilterAssertions::Assertions

Included in:
ActiveSupport::TestCase
Defined in:
lib/filter_assertions/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_before_filter(before_filter_name, options = {}) ⇒ Object

This method is the matching assertion to the before_filter or append_before_filter methods

Examples:

  • assert_before_filter :authenticate

  • assert_before_filter :authenticate, :only => [:index, :new]

  • assert_before_filter :authenticate, :except => :index

Raises:

  • (NameError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/filter_assertions/assertions.rb', line 10

def assert_before_filter(before_filter_name, options = {})
  filter = find_matching_before_filter(before_filter_name)

  raise NameError, "Couldn't find filter '#{before_filter_name}'" if filter.nil?

  if options.blank?
    # When the filter's options are empty, it means the filter applies to all controller methods.
    assert (filter.options[:only].blank? and filter.options[:except].blank?), "The filter #{before_filter_name} has only/except options and does not apply to all actions."
  else
    # The Array() cast makes sure that if you only enter one action without brackets
    # (instead of listing multiple actions), that it's converted to an Array
    # and can be processed with the code below
    actions_only   = Array(options[:only])
    actions_except = Array(options[:except])

    raise ArgumentError, "Unrecognised options. Valid options: :only and :except" if actions_only.blank? && actions_except.blank?

    assert_block "The filter '#{before_filter_name}' does NOT apply to all the actions specified." do
      filter_only_applies_to_actions?(filter, actions_only)
    end if actions_only.present?

    assert_block "The filter '#{before_filter_name}' applies to more actions than specified." do
      filter_is_excepted_from_actions?(filter, actions_except)
    end if actions_except.present?
  end
end

#assert_forgery_protection(options = {}) ⇒ Object

The protect_from_forgery method is just a wrapper for another before_filter. And so is this assertion method just a wrapper for the assert_before_filter method.



71
72
73
# File 'lib/filter_assertions/assertions.rb', line 71

def assert_forgery_protection(options = {})
  assert_before_filter(:verify_authenticity_token, options)
end

#assert_no_before_filter(before_filter_name, options = {}) ⇒ Object

This method is the matching assertion to the skip_before_filter method

Examples:

  • assert_no_before_filter :authenticate

  • assert_no_before_filter :authenticate, :only => [:index, :new]

  • assert_no_before_filter :authenticate, :except => :index



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/filter_assertions/assertions.rb', line 43

def assert_no_before_filter(before_filter_name, options = {})
  filter = find_matching_before_filter(before_filter_name)

  if options.blank?
    # if the options are blank no filter should have been found, because it is then skipped for all actions
    assert filter.nil?, "The filter #{before_filter_name} has only/except options and is not skipped for all actions."
  else
    # The Array() cast makes sure that if you only enter one action without brackets
    # (instead of listing multiple actions), that it's converted to an Array
    # and can be processed with the code below
    actions_only   = Array(options[:only])
    actions_except = Array(options[:except])

    raise ArgumentError, "Unrecognised options. Valid options are :only and :except" if actions_only.blank? && actions_except.blank?

    assert_block "The filter '#{before_filter_name}' is NOT skipped for all the actions specified." do
      filter_is_excepted_from_actions?(filter, actions_only)
    end if actions_only.present?

    assert_block "The filter '#{before_filter_name}' is skipped for more actions than specified." do
      filter_only_applies_to_actions?(filter, actions_except)
    end if actions_except.present?
  end
end