module DeclarativeAuthorization::Test::Helpers

Protected Instance Methods

access_test_params(name) click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 211
def access_test_params(name)
  return { } unless name.present?

  params    = access_test_params_for_param_methods
  max_arity = params.size

  full_method_name = "access_test_params_for_#{name}"
  method_arity = method(full_method_name).arity

  unless method_arity <= max_arity
    raise InvalidParamsBlockArity.new(name, method_arity, max_arity)
  end

  send(full_method_name, *params[0...method_arity])
end
access_test_params_for_param_methods() click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 207
def access_test_params_for_param_methods
  []
end
access_test_user(role, privilege) click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 227
def access_test_user(role, privilege)
  raise 'MUST IMPLEMENT!!!'
end
forbidden_with_role_and_privilege?(action, role, privilege, params_name = nil, options = {}) click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 231
def forbidden_with_role_and_privilege?(action, role, privilege, params_name = nil, options = {})
  http_method = options[:method] || :get
  xhr = options[:xhr]

  user = access_test_user(role, privilege)
  params = access_test_params(params_name)

  send_args = [http_method, action.to_sym]
  send_kwargs = { params: params }
  send_kwargs[:xhr] = true if xhr

  errors_to_reraise = [
    ActionController::RoutingError,
    ActionController::UrlGenerationError,
    AbstractController::ActionNotFound
  ]

  errors_to_reraise << Mocha::ExpectationError if defined?(Mocha::ExpectationError)

  begin
    send *send_args, **send_kwargs
    return response_forbidden?
  rescue *errors_to_reraise => e
    raise e
  rescue => e
    if options[:print_error]
      puts "\n#{e.class.name} raised in action '#{action}':"
      puts e.message
      puts e.backtrace.join("\n")
    end
    return false
  end
end
response_forbidden?() click to toggle source
# File lib/declarative_authorization/test/helpers.rb, line 199
def response_forbidden?
  flash[:error] == 'You do not have the correct permissions to access that page. Click the back button to return to your previous page.' ||
  flash[:error] =~ /You do not have the correct permissions to view this/ ||
  flash[:error] =~ /You do not have access to/ ||
  flash[:alert] =~ /You need to sign in/ ||
  (@response.location =~ /\/users\/sign_in/ && @response.code == "302")
end