Created
July 1, 2010 19:34
-
-
Save jstewart/460442 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In Functional Test | |
def self.authorize_actions | |
[ | |
"get :index", | |
"get :new", | |
"get :edit, :id => 1", | |
"post :create", | |
"put :update, :id => 1", | |
"delete :destroy, :id => 1" | |
] | |
end | |
should_require_login *authorize_actions | |
should_be_authorized("admin", *authorize_actions) { sign_in(Factory(:admin_user)) } | |
should_be_authorized("opportunity admin", *authorize_actions) { sign_in(Factory(:user, :crud_opportunities => true)) } | |
should_not_be_authorized("plain old user", *authorize_actions) { sign_in(Factory(:user)) } | |
# Shoulda Macro | |
Test::Unit::TestCase.class_eval do | |
def self.should_require_login(*actions) | |
actions.each do |action| | |
should "Require login for '#{action}' action" do | |
eval action | |
assert_redirected_to user_session_path | |
end | |
end | |
end | |
def self.should_be_authorized(description, *actions, &block) | |
context "Checking that user is authorized" do | |
setup do | |
instance_eval(&block) | |
end | |
actions.each do |action| | |
should "#{description} should be authorized for '#{action}' action" do | |
eval action | |
unauth = respond_with(:unauthorized) | |
assert_rejects unauth, @controller | |
end | |
end | |
end | |
end | |
def self.should_not_be_authorized(description, *actions, &block) | |
context "Checking that user is not authorized" do | |
setup do | |
instance_eval(&block) | |
end | |
actions.each do |action| | |
should "#{description} shouldn't be authorized for '#{action}' action" do | |
eval action | |
unauth = respond_with(:unauthorized) | |
assert_accepts unauth, @controller | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment