Module: DeclarativeAuthorization::Test::Helpers::ClassMethods
- Defined in:
- lib/declarative_authorization/test/helpers.rb
Instance Attribute Summary collapse
-
#access_tests_defined ⇒ Object
readonly
Returns the value of attribute access_tests_defined.
-
#only_run_roles ⇒ Object
readonly
Returns the value of attribute only_run_roles.
-
#run_all_assertions ⇒ Object
Returns the value of attribute run_all_assertions.
Instance Method Summary collapse
- #access_tests(only_run_roles: nil, &block) ⇒ Object
- #all_public_actions ⇒ Object
- #define_access_test_params_method(name, &block) ⇒ Object
- #inherited(child) ⇒ Object
- #run_assertion?(assertion_options) ⇒ Boolean
- #run_role_test?(role) ⇒ Boolean
- #skip_access_tests_for_actions(*actions) ⇒ Object
- #this_is_an_abstract_controller_so_it_needs_no_access_tests ⇒ Object (also: #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)
Instance Attribute Details
#access_tests_defined ⇒ Object (readonly)
Returns the value of attribute access_tests_defined.
166 167 168 |
# File 'lib/declarative_authorization/test/helpers.rb', line 166 def access_tests_defined @access_tests_defined end |
#only_run_roles ⇒ Object (readonly)
Returns the value of attribute only_run_roles.
166 167 168 |
# File 'lib/declarative_authorization/test/helpers.rb', line 166 def only_run_roles @only_run_roles end |
#run_all_assertions ⇒ Object
Returns the value of attribute run_all_assertions.
167 168 169 |
# File 'lib/declarative_authorization/test/helpers.rb', line 167 def run_all_assertions @run_all_assertions end |
Instance Method Details
#access_tests(only_run_roles: nil, &block) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/declarative_authorization/test/helpers.rb', line 174 def access_tests(only_run_roles: nil, &block) @access_tests_defined = true @run_all_assertions = true @only_run_roles = only_run_roles 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, AccessTestParser.new(self)) Blockenspiel.invoke(block, AccessTestGenerator.new(self)) end |
#all_public_actions ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/declarative_authorization/test/helpers.rb', line 199 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
233 234 235 |
# File 'lib/declarative_authorization/test/helpers.rb', line 233 def define_access_test_params_method(name, &block) define_method("access_test_params_for_#{name}", &block) end |
#inherited(child) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/declarative_authorization/test/helpers.rb', line 217 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 |
#run_assertion?(assertion_options) ⇒ Boolean
241 242 243 |
# File 'lib/declarative_authorization/test/helpers.rb', line 241 def run_assertion?() @run_all_assertions || [:only] end |
#run_role_test?(role) ⇒ Boolean
237 238 239 |
# File 'lib/declarative_authorization/test/helpers.rb', line 237 def run_role_test?(role) @only_run_roles.nil? || @only_run_roles.include?(role) end |
#skip_access_tests_for_actions(*actions) ⇒ Object
169 170 171 172 |
# File 'lib/declarative_authorization/test/helpers.rb', line 169 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_tests ⇒ Object 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
190 191 192 193 |
# File 'lib/declarative_authorization/test/helpers.rb', line 190 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 |