module DeclarativeAuthorization::Test::Helpers::ClassMethods

Attributes

access_tests_defined[R]

Public Instance Methods

access_tests(&block) click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 135
def access_tests(&block)
  @access_tests_defined = true
  file_output ||= [ 'test/profiles/access_checking', ENV['TEST_ENV_NUMBER'] ].compact.join('.')
  unless File.exists?(file_output)
    FileUtils.mkdir_p(File.dirname(file_output))
  end
  File.open(file_output, "a+") do |file|
    file.puts self.controller_class.name
  end

  Blockenspiel.invoke(block, AccessTestGenerator.new(self))
end
access_tests_not_required()
all_public_actions() click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 157
def all_public_actions
  actions = []
  if defined?(Grape) && [Grape::API, Grape::API::Instance].any? { |base| controller_class < base }
    actions += controller_class.routes.map { |api| "#{api.request_method} #{api.origin}" }
  else
    actions += controller_class.public_instance_methods(false)
    actions += controller_class.superclass.public_instance_methods(false)
  end

  actions.reject! do |method|
    method =~ /^_/ ||
      method =~ /^rescue_action/ ||
      (@skipped_access_test_actions.is_a?(Array) && @skipped_access_test_actions.include?(method))
  end

  actions.uniq
end
define_access_test_params_method(name, &block) click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 191
def define_access_test_params_method(name, &block)
  define_method("access_test_params_for_#{name}", &block)
end
inherited(child) click to toggle source
Calls superclass method
# File lib/declarative_authorization/test/helpers.rb, line 175
def inherited(child)
  super

  child.send(:define_method, :test_access_tests_defined) do
    assert self.class.access_tests_defined, 'Access tests needed but not defined.'
  end

  child.send(:define_method, :test_all_public_actions_covered_by_role_tests) do
    test_methods = self.public_methods(false).select { |method| method =~ /^test_/ }
    untested_actions = self.class.all_public_actions.select { |action| !test_methods.any? { |method| method =~ /^test_#{action}__access_/} }
    unless untested_actions.empty?
      flunk "In #{self.class.name}, it appears that #{untested_actions.map(&:inspect).to_sentence} are not tested by any access_tests. Did you forget them?"
    end
  end
end
skip_access_tests_for_actions(*actions) click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 130
def skip_access_tests_for_actions(*actions)
  @skipped_access_test_actions ||= []
  @skipped_access_test_actions += actions.map(&:to_sym)
end
the_access_tests_are_tested_elsewhere_so_no_access_tests_are_needed()
this_is_a_module_mixed_into_controllers_so_it_needs_no_access_tests()
this_is_an_abstract_controller_so_it_needs_no_access_tests() click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 148
def this_is_an_abstract_controller_so_it_needs_no_access_tests
  undef_method :test_access_tests_defined if self.method_defined? :test_access_tests_defined
  undef_method :test_all_public_actions_covered_by_role_tests if self.method_defined? :test_all_public_actions_covered_by_role_tests
end