Authenticating before tests

View: New views
6 Messages — Rating Filter:   Alert me  

Authenticating before tests

by Luke Galea-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I'm using Goldberg, an engine that provides roles based access  
control for my app. I need to login before I can do controller tests,  
but I can't find any examples of people doing this.

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).

I've also tried setting session[:user] in before(:all), but session  
is nil at that point!

Any help would be greatly appreciated.

Thanks,
Luke
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users

Re: Authenticating before tests

by Jarkko Laine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 13.9.2007, at 15.25, Luke Galea wrote:

> Hi all,
>
> I'm using Goldberg, an engine that provides roles based access
> control for my app. I need to login before I can do controller tests,
> but I can't find any examples of people doing this.
>
> Could someone point me in the right direction?

This is what we do:

in spec_helper.rb:

   include AuthenticatedTestHelper

   def login_as(user)
     controller.send :current_user=, user
   end

Then, in a before(:all) block, I can say login_as(@user) with a user  
object. This assumes you're using restful_authentication (and should  
work with acts_as_authenticated as well).

>
> 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).

You can't and shouldn't do that. Just find out how your particular  
authentication system determines the login and create a helper method  
like login_as above to simulate the login.

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users

smime.p7s (3K) Download Attachment

Re: Authenticating before tests

by sinclair bain-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

Have you considered mocking / stubbing to test in isolation if that is appropriate ?

sinclair

On 9/13/07, Luke Galea <galeal_lists@...> wrote:
Hi all,

I'm using Goldberg, an engine that provides roles based access
control for my app. I need to login before I can do controller tests,
but I can't find any examples of people doing this.

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).

I've also tried setting session[:user] in before(:all), but session
is nil at that point!

Any help would be greatly appreciated.

Thanks,
Luke
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman /listinfo/rspec-users


_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users

Re: Authenticating before tests

by Jarkko Laine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 13.9.2007, at 19.52, sinclair bain wrote:

> Hi!
>
> Have you considered mocking / stubbing to test in isolation if that  
> is appropriate ?

Right, my example was also assuming that you are using mocks for users.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users

smime.p7s (3K) Download Attachment

Re: Authenticating before tests

by peter.boling :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: Authenticating before tests

by Luke Galea-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?
     GoldbergMigration.load_bootstrap
   end
end

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


Then I use it in my specs:

before(:all) do
     ensure_goldberg_loaded
  end

before(:each) do
     login_as :galeal
  end

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@...
> http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users