[rspec-users] Authenticating before tests

Luke Galea galeal_lists at ideaforge.org
Sat Sep 22 11:15:29 EDT 2007

Hi Peter,

I ended up settling on having a "ensure_goldberg_loaded" helper in  
spec_helper and a login_as that "spoofs" the session in the way that  
set_user would do but without relying on Goldberg. This should make  
it less coupled with Goldberg and let you test your code without  
testing goldberg at the same time.

It really just involves creating a goldberg session object with the  
user_id and a blank menu_history.

Here's the revelant bits of my spec_helper:

def ensure_goldberg_loaded
   if Goldberg::User.find(:all).empty?

def login_as( user = 'admin' )
   request.session[:goldberg] = { :user_id => Goldberg::User.find 
(:first, :conditions => "name=\"#{user}\"").id,
       :menu_history => {} }

Then I use it in my specs:

before(:all) do

before(:each) do
     login_as :galeal

Thanks for your help!

-- Luke

On 17-Sep-07, at 10:45 AM, peter.boling wrote:

> Luke Galea-4 wrote:
>> Could someone point me in the right direction?
>> I thought the simplest way would be to either call the login action
>> from my other tests before(:all), but I can't seem to find how to
>> call another controller from within the spec for a different
>> controller. (Results in @controller is nil).
> This is what we do.  I realize that according to the another poster  
> this is
> the wrong way, but it is the best way we've found for use with  
> Goldberg.
>   def goldbergAuthAdmin()
>     get 'goldberg/auth/login'
>     #Goldberg::AuthController.set_user(session,
> Goldberg::User.find_by_name(:first, "admin").id)
>     #Our Admin user has id 2.
>     Goldberg::AuthController.set_user(session,2)
>   end
> BTW, another important step is bootstrapping goldberg into your test
> database...
> To facilitate this we added two methods to the GoldbergMigration  
> model:
>   def self.clear_for_class(klass, dest)
>     filename = "#{dest}/#{klass.to_s.sub(/^Goldberg::/, '')}.yml"
>     records = klass.delete_all
>   end
>   def self.clear_bootstrap
>     self.goldberg_classes.each do |klass|
>       self.clear_for_class klass, "#{File.dirname(__FILE__)}/../db"
>     end
>   end
> Then add this to spec_helper.rb, or your own included spec_helper
>   def goldbergReload()
>     GoldbergMigration.clear_bootstrap
>     GoldbergMigration.load_bootstrap
>   end
> Then call this from a before(:all) in the first describe of each rspec
> needing goldberg.  Of course you have to have dumped your goldberg  
> bootstrap
> from dev/production first (You do this in the console:
> "GoldbergMigration.dump_bootstrap" to save all goldberg settings,
> controller/action perms, users roles, etc to some yaml files).
> Hope that helps!
> Peter Boling
> Sagebit, LLC
> -- 
> View this message in context: http://www.nabble.com/Authenticating- 
> before-tests-tf4435346.html#a12737465
> Sent from the rspec-users mailing list archive at Nabble.com.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list