Adding filters generation based on devise mappings.

This commit is contained in:
Carlos A. da Silva
2009-10-11 08:15:48 -03:00
parent 198167d978
commit 62bef5605a
9 changed files with 184 additions and 20 deletions

View File

@@ -0,0 +1,111 @@
require 'test/test_helper'
class FiltersController < ApplicationController
before_filter :user_authenticate!, :only => :user_action
before_filter :admin_authenticate!, :only => :admin_action
before_filter :require_no_authentication, :only => :not_authenticated_action
def public_action
render :text => 'public'
end
def not_authenticated_action
render :text => 'not_authenticated'
end
def user_action
render :text => 'user'
end
def admin_action
render :text => 'admin'
end
end
class FiltersTest < ActionController::TestCase
tests FiltersController
test 'generate user_authenticate! filter' do
assert @controller.respond_to?(:user_authenticate!)
end
test 'proxy user_authenticate! to authenticate with user scope' do
@controller.expects(:authenticate!).with('user')
@controller.user_authenticate!
end
test 'generate admin_authenticate! filter' do
assert @controller.respond_to?(:admin_authenticate!)
end
test 'proxy admin_authenticate! to authenticate with user scope' do
@controller.expects(:authenticate!).with('admin')
@controller.admin_authenticate!
end
test 'not authenticated user should be able to access public action' do
get :public_action
assert_response :success
assert_equal 'public', @response.body
end
test 'not authenticated as user should not be able to access user action' do
@controller.expects(:authenticated?).with('user').returns(false)
get :user_action
assert_response :redirect
assert_redirected_to new_user_session_path
end
test 'authenticated as user should be able to access user action' do
@controller.expects(:authenticated?).with('user').returns(true)
get :user_action
assert_response :success
assert_equal 'user', @response.body
end
test 'not authenticated as admin should not be able to access admin action' do
@controller.expects(:authenticated?).with('admin').returns(false)
get :admin_action
assert_response :redirect
assert_redirected_to new_admin_session_path
end
test 'authenticated as admin should be able to access admin action' do
@controller.expects(:authenticated?).with('admin').returns(true)
get :admin_action
assert_response :success
assert_equal 'admin', @response.body
end
test 'authenticated as user should not be able to access not authenticated action' do
@controller.expects(:authenticated?).with('user').returns(true)
@controller.expects(:authenticated?).with('admin').returns(false)
get :not_authenticated_action
assert_response :redirect
assert_redirected_to root_path
end
test 'authenticated as admin should not be able to access not authenticated action' do
@controller.expects(:authenticated?).with('user').returns(false)
@controller.expects(:authenticated?).with('admin').returns(true)
get :not_authenticated_action
assert_response :redirect
assert_redirected_to root_path
end
test 'not authenticated should access not_authenticated_action' do
@controller.expects(:authenticated?).with('user').returns(false)
@controller.expects(:authenticated?).with('admin').returns(false)
get :not_authenticated_action
assert_response :success
assert_equal 'not_authenticated', @response.body
end
end

View File

@@ -6,21 +6,31 @@ class RoutesTest < ActionController::TestCase
def test_path_and_url(name, prepend_path=nil)
@request.path = '/users/session'
prepend_path = "#{prepend_path}_" if prepend_path
# No params
assert_equal @controller.send(:"#{prepend_path}#{name}_path"),
send(:"#{prepend_path}user_#{name}_path")
assert_equal @controller.send(:"#{prepend_path}#{name}_url"),
send(:"#{prepend_path}user_#{name}_url")
# Default url params
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :param => 123),
send(:"#{prepend_path}user_#{name}_path", :param => 123)
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :param => 123),
send(:"#{prepend_path}user_#{name}_url", :param => 123)
@request.path = nil
# With an AR object
assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
send(:"#{prepend_path}user_#{name}_path")
assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
send(:"#{prepend_path}user_#{name}_url")
# Using a symbol
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user),
send(:"#{prepend_path}user_#{name}_path")
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user),
send(:"#{prepend_path}user_#{name}_url")
end

View File

@@ -2,4 +2,7 @@ ActionController::Routing::Routes.draw do |map|
map.resources :users, :only => :index
map.resources :admins, :only => :index
map.root :controller => :home
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end