Skip to content
This repository has been archived by the owner on Jun 24, 2019. It is now read-only.

Commit

Permalink
increased test coverage, added tests for aliases
Browse files Browse the repository at this point in the history
git-svn-id: http://rolerequirement.googlecode.com/svn/trunk@102 0128ec72-9f35-0410-83f6-31648bef6b25
  • Loading branch information
timcharper committed Nov 20, 2007
1 parent 93fe180 commit e762853
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion generators/shared_templates/role_requirement_system.rb.erb
Expand Up @@ -49,7 +49,7 @@ module RoleRequirementSystem
roles = [roles] unless Array===roles

options[:only] ||= options[:for] if options[:for]
options[:except] ||= options[:for_all_but] if options[:for_all_but]
options[:except] ||= options[:for_all_except] if options[:for_all_except]

# convert any actions into symbols
for key in [:only, :except]
Expand Down
63 changes: 53 additions & 10 deletions test/functional/test_role_controller.rb
Expand Up @@ -20,7 +20,7 @@ def setup

def test__required_role_for_all__should_require
@controller.class.require_role "admin"
@controller.current_user = User.new(:roles => "")
set_roles ""

assert(!@controller.check_roles)

Expand All @@ -29,8 +29,8 @@ def test__required_role_for_all__should_require
end

def test_required_role_for_some_actions_only__should_require
@controller.class.require_role "admin", :only => [:update, :destroy, :delete]
@controller.current_user = User.new(:roles => "")
require_role "admin", :only => [:update, :destroy, :delete]
set_roles ""

@controller.params = {:action => "update" }
assert(!@controller.check_roles, "shouldn't pass")
Expand All @@ -39,9 +39,25 @@ def test_required_role_for_some_actions_only__should_require
assert(@controller.check_roles, "should pass")
end

def test__for_alias
require_role "admin", :for => [:update, :destroy, :delete]

set_roles ""
assert_fails_with :action => "update"
assert_passes_with :action => "index"
end

def test__for_all_except_alias
require_role "admin", :for_all_except => [:index]

set_roles ""
assert_fails_with :action => "update"
assert_passes_with :action => "index"
end

def test_required_role_for_excluded_actions_only__should_require
@controller.class.require_role "admin", :except => :index
@controller.current_user = User.new(:roles => "")
require_role "admin", :except => :index
set_roles ""

@controller.params = {:action => "update" }
assert(!@controller.check_roles, "shouldn't pass")
Expand Down Expand Up @@ -69,7 +85,7 @@ def test__no_user__role_not_required__returns_true

def test_required_role_for_if_proc__should_require
@controller.class.require_role "admin", :only => :list, :if => Proc.new {|params| params[:status] == "completed" }
@controller.current_user = User.new(:roles => "")
set_roles ""

@controller.params = {:action => "list", :status => "pending" }
assert(@controller.check_roles, "should pass")
Expand All @@ -89,7 +105,7 @@ def test__controllers_dont_share_requirements
assert(@controller2.check_roles, "should pass")

@controller2.current_user = User.new(:roles => "")
@controller.current_user = User.new(:roles => "")
set_roles ""
assert(!@controller.check_roles, "shouldn't pass")
assert(!@controller2.check_roles, "shouldn't pass")
end
Expand All @@ -98,7 +114,7 @@ def test__url_options_authenticate
MyController.require_role "admin", :only => :destroy
OtherController.require_role "admin", :only => :destroy

@controller.current_user = User.new(:roles => "")
set_roles ""

assert ! @controller.send(:url_options_authenticate?, { :controller => :my, :action => "destroy" } ), "shouldn't pass"
assert ! @controller.send(:url_options_authenticate?, { :controller => :other, :action => "destroy" } ), "shouldn't pass"
Expand All @@ -115,18 +131,45 @@ def test__url_options_authenticate__nil_value__should_handle

def test__url_options_authenticate__nil_value__should_treat_as_index
MyController.require_role "admin", :only => :index
@controller.current_user = User.new(:roles => "")
set_roles ""

assert ! @controller.send(:url_options_authenticate?, { :controller => :my } ), "shouldn't pass"
assert @controller.send(:url_options_authenticate?, { :controller => :my, :action => "destroy" } ), "should pass"
end

def test__url_options_authenticate__no_controller__assumes_self
MyController.require_role "admin", :only => :index
@controller.current_user = User.new(:roles => "")
set_roles ""

assert ! @controller.send(:url_options_authenticate?, { :action => "index" } ), "shouldn't pass"
assert @controller.send(:url_options_authenticate?, { :action => "boogy" } ), "shouldn't pass"

end

protected
def require_role(*params)
@controller.class.require_role(*params)
end

def reset_role_requirements!
@controller.class.reset_role_requirements!
end

def set_roles(roles = "")
@controller.current_user = User.new(:roles => roles)
end

def passes_with_params?(params = {})
@controller.params = params
@controller.check_roles
end

def assert_passes_with(params = {})
assert passes_with_params?(params), "should pass"
end

def assert_fails_with(params = {})
assert ! passes_with_params?(params), "should pass"
end

end

0 comments on commit e762853

Please sign in to comment.