Skip to content

Instantly share code, notes, and snippets.

@yosemsweet
Created September 19, 2011 18:26
Show Gist options
  • Save yosemsweet/1227184 to your computer and use it in GitHub Desktop.
Save yosemsweet/1227184 to your computer and use it in GitHub Desktop.
cancan ability caching issue
def should_require_authorization_of(*args, &action)
defaults = {:role => :member, :not_authorized_status => 403}.merge(args.extract_options!)
options = defaults.merge(args.extract_options!)
authorize(options)
action.call
response.should_not return_status(options[:not_authorized_status])
deauthorize(options)
debugger
action.call
response.should return_status(options[:not_authorized_status])
end
def authorize(options)
user = Factory.build(:user)
user.stubs(:canvas_role?).with(instance_of(Canvas), options[:role]).returns(true)
user.stubs(:persisted?).returns(true)
controller.stubs(:current_user).returns(user)
end
def deauthorize(options)
user = Factory.build(:user)
user.stubs(:canvas_role?).with(instance_of(Canvas), options[:role]).returns(false)
user.stubs(:persisted?).returns(true)
controller.stubs(:current_user).returns(user)
end
def should_require_authorization_of(*args, &action)
defaults = {:action => :manage, :object => nil, :not_authorized_status => 403}.merge(args.extract_options!)
options = defaults.merge(args.extract_options!)
user = Factory.build(:user)
user.stubs(:persisted?).returns(true)
controller.stubs(:current_user).returns(user)
authorize(options)
action.call
response.should_not return_status(options[:not_authorized_status])
deauthorize(options)
action.call
response.should return_status(options[:not_authorized_status])
end
def authorize(options)
controller.current_ability.stubs(:can?).with(options[:action], options[:object]).returns(true)
end
def deauthorize(options)
controller.current_ability.stubs(:can?).with(options[:action], options[:object]).returns(false)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment