views:

1261

answers:

5

I'm trying to write a Cucumber scenario that requires me to have a logged in user - that would normally be quite simple but I'm only using OpenID authentication (curtosy of the authentication plugin). However after digging through the open_id_authentication plugins guts I'm not sure how I could achieve this within Cucumber.

+3  A: 

I've figured out a way, if you place this in your features/support/env.rb:

ActionController::Base.class_eval do

    private

        def begin_open_id_authentication(identity_url, options = {})
            yield OpenIdAuthentication::Result.new(:successful), identity_url, nil
        end 
end

Then you can just do something like this in your appropriate step:

Given /^I am logged in as "(.*)"$/ do |name|
    user = User.find_by_name(user)
    post '/session', :openid_url => user.identity_url
    # Some assertions just to make sure our hack in env.rb is still working
    response.should redirect_to('/')
    flash[:notice].should eql('Logged in successfully')
end

I'm just completely clobbering the open id auth for the cucumber features, obviously if I need instances where there is failed login I could do that based on the supplied identity_url.

DEfusion
A: 

DEfusion's answer works except that I needed to normalize the identity_url like:

ActionController::Base.class_eval do

    private

        def begin_open_id_authentication(identity_url, options = {})
            yield OpenIdAuthentication::Result.new(:successful), self.normalize_identifier(identity_url), nil
        end 
end

Thanks

Jamal Hansen
+1  A: 

Bort, a rails skeleton app, has a full set of rspec tests and supports openID login so you may want to take a look and see what they do.

srboisvert
A: 

Thanks for your solution defusion. I also found that I could pass some parameters during the openid authentication by replacing your nil argument by a hashmap :

 private
def begin_open_id_authentication(identity_url, options = {})
    yield OpenIdAuthentication::Result.new(:successful), identity_url, {"fullname" => "My Name"}

end

The problem is that my authentication process always returns me that name. Does anyone know how I could make this value scenario-dependent ?

Chris
+1  A: 

If you want to be able to stub out responses do this:

In features/support/helpers.rb:

ActionController::Base.class_eval do

  private
    def fake_openid_response(identity_url)
      [OpenIdAuthentication::Result.new(:successful), identity_url, nil]
    end

    def begin_open_id_authentication(identity_url, options = {})
        yield fake_openid_response(identity_url)
    end 
end

By moving the response out to a separate method you can now stub the response in your steps if necessary. For example, if I wanted a :missing response and I had a controller GoogleLoginController I could do the following using Mocha:

GoogleLoginController.any_instance.stubs(:fake_openid_response)
    .returns([OpenIdAuthentication::Result.new(:missing), identity_url, nil])
Greg DeVore