Skip to content

Instantly share code, notes, and snippets.

@jschoolcraft
Created September 10, 2009 12:27
Show Gist options
  • Save jschoolcraft/184518 to your computer and use it in GitHub Desktop.
Save jschoolcraft/184518 to your computer and use it in GitHub Desktop.
This is how I spec AuthLogic be9-acl9 using rspec
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
require 'spec/autorun'
require 'spec/rails'
require 'webrat'
require 'remarkable_rails'
require "email_spec/helpers"
require "email_spec/matchers"
Spec::Runner.configure do |config|
# If you're not using ActiveRecord you should remove these
# lines, delete config/database.yml and disable :active_record
# in your config/boot.rb
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
def current_user(stubs = {})
return @current_user if @current_user
roles = (stubs.delete(:roles) || {}).symbolize_keys
@current_user = stub_model(User, stubs.merge(:roles => roles))
def @current_user.has_role?(role, object=nil)
result = !! if object.nil?
roles.has_key?(role.to_sym)
else
roles[role.to_sym] == object
end
result
end
def @current_user.has_no_roles!
roles = {}
end
def @current_user.has_role!(role, object=nil)
roles[role] = object
end
@current_user
end
def user_session(stubs = {}, user_stubs = {})
@current_user_session ||= mock_model(UserSession, {:user => current_user(user_stubs)}.merge(stubs))
end
def login(session_stubs = {}, user_stubs = {})
UserSession.stub!(:find).and_return(user_session(session_stubs, user_stubs))
end
def logout
@current_user = nil
@user_session = nil
end
# super_user admin
def current_super_user(stubs = {})
@current_super_user ||= stub_model(SuperUser, stubs)
end
def super_user_session(stubs = {}, user_stubs = {})
@current_super_user_session ||= mock_model(SuperUserSession, {:super_user => current_super_user(user_stubs)}.merge(stubs))
end
def admin_login(session_stubs = {}, user_stubs = {})
SuperUserSession.stub!(:find).and_return(super_user_session(session_stubs, user_stubs))
end
def admin_logout
@super_user_session = nil
end
end
Webrat.configure do |config|
config.mode = :rails
end
require File.dirname(__FILE__) + '/../spec_helper'
describe SurveysController do
before(:each) do
login({}, { :roles => {'admin' => nil} })
end
# ...
end
class SurveysController < ApplicationController
before_filter :find_survey
access_control do
allow :admin
allow :customer
allow :ratee, :of => :survey, :only => :ratee_start
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment