Module: DeclarativeAuthorization::Test::Helpers::ClassMethods

Defined in:
lib/declarative_authorization/test/helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tests_definedObject (readonly)

Returns the value of attribute access_tests_defined.



128
129
130
# File 'lib/declarative_authorization/test/helpers.rb', line 128

def access_tests_defined
  @access_tests_defined
end

Instance Method Details

#access_tests(&block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/declarative_authorization/test/helpers.rb', line 135

def access_tests(&block)
  @access_tests_defined = true
  file_output ||= [ Dir.tmpdir + '/test/profiles/access_checking', ENV['TEST_ENV_NUMBER'] ].compact.join('.')
  unless File.exist?(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

#all_public_actionsObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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) ⇒ Object



191
192
193
# 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) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 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) ⇒ Object



130
131
132
133
# 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

#this_is_an_abstract_controller_so_it_needs_no_access_testsObject Also known as: this_is_a_module_mixed_into_controllers_so_it_needs_no_access_tests, the_access_tests_are_tested_elsewhere_so_no_access_tests_are_needed, access_tests_not_required



148
149
150
151
# 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