Skip to content

Instantly share code, notes, and snippets.

@mhfs
Forked from jnicklas/a_controller_spec.rb
Created October 31, 2009 15:00
Show Gist options
  • Save mhfs/223104 to your computer and use it in GitHub Desktop.
Save mhfs/223104 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/../spec_helper'
describe AccountController do
as_any_user :get => :show do
it { should assign_to(:user, :with => @current_user)}
it { should respond_with(:success) }
end
as_any_user :get => :edit do
it { should assign_to(:user, :with => @current_user)}
it { should respond_with(:success) }
end
as_any_user :put => :update, :user => 'params' do
describe "with valid params" do
before { @current_user.should_receive(:update_attributes).with("params").and_return(true) }
it { should redirect_to(account_url) }
end
describe "with invalid params" do
before { @current_user.should_receive(:update_attributes).with("params").and_return(false) }
it { should render_template("account/edit") }
it { should assign_to(:user, :with => @current_user) }
end
end
end
module LogInContext
def as_visitor(params={}, &block)
describe "(as a visitor)" do
before do
@current_site = mock('current_site')
@current_site.stub!(:locale).and_return("en")
controller.stub!(:current_user).and_return(nil)
controller.stub!(:current_site).and_return(@current_site)
@current_pallet = mock('current_pallet')
controller.stub!(:current_pallet).and_return(@current_pallet)
end
describe(params, &block)
end
end
def as_user(params={}, &block)
describe "(as a user)" do
before do
@current_user = mock('current_user')
@current_site = mock('current_site')
@current_site.stub!(:locale).and_return("en")
controller.stub!(:current_user).and_return(@current_user)
controller.stub!(:current_site).and_return(@current_site)
@current_site.stub!(:admin?).with(@current_user).and_return(false)
@current_site.stub!(:super_admin?).with(@current_user).and_return(false)
@current_pallet = mock('current_pallet')
controller.stub!(:current_pallet).and_return(@current_pallet)
end
describe(params, &block)
end
end
def as_admin(params={}, &block)
describe "(as an administrator)" do
before do
@current_user = mock('current_user')
@current_site = mock('current_site')
@current_site.stub!(:locale).and_return("en")
controller.stub!(:current_user).and_return(@current_user)
controller.stub!(:current_site).and_return(@current_site)
@current_site.stub!(:admin?).with(@current_user).and_return(true)
@current_site.stub!(:super_admin?).with(@current_user).and_return(false)
@current_pallet = mock('current_pallet')
controller.stub!(:current_pallet).and_return(@current_pallet)
end
describe(params, &block)
end
end
def as_super_admin(params={}, &block)
describe "(as an super admin)" do
before do
@current_user = mock('current_user')
@current_site = mock('current_site')
@current_site.stub!(:locale).and_return("en")
controller.stub!(:current_user).and_return(@current_user)
controller.stub!(:current_site).and_return(@current_site)
@current_site.stub!(:admin?).with(@current_user).and_return(false)
@current_site.stub!(:super_admin?).with(@current_user).and_return(true)
@current_pallet = mock('current_pallet')
controller.stub!(:current_pallet).and_return(@current_pallet)
end
describe(params, &block)
end
end
def deny_access_to_visitors(params={})
as_visitor(params) do
it { should redirect_to(new_session_path) }
end
end
def deny_access_to_users(params={})
as_user(params) do
it { should redirect_to(new_session_path) }
end
end
def deny_access_to_admins(params={})
as_admin(params) do
it { should redirect_to(new_session_path) }
end
end
def deny_access_to_super_admins(params={})
as_super_admin(params) do
it { should redirect_to(new_session_path) }
end
end
def as_admin_only(params={}, &block)
as_admin(params.dup, &block)
deny_access_to_visitors(params.dup)
deny_access_to_users(params.dup)
deny_access_to_super_admins(params.dup)
end
def as_super_admin_only(params={}, &block)
as_super_admin(params.dup, &block)
deny_access_to_visitors(params.dup)
deny_access_to_users(params.dup)
deny_access_to_admins(params.dup)
end
def as_anyone(params={}, &block)
as_visitor(params.dup, &block)
as_user(params.dup, &block)
as_admin(params.dup, &block)
as_super_admin(params.dup, &block)
end
def as_any_user(params={}, &block)
deny_access_to_visitors(params.dup)
as_user(params.dup, &block)
as_admin(params.dup, &block)
as_super_admin(params.dup, &block)
end
end
Spec::Rails::Example::ControllerExampleGroup.extend(LogInContext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment