Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fake OpenID login in RSpec user story/Cucumber when using open_id_authentication plugin

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.

like image 348
DEfusion Avatar asked Dec 19 '08 22:12

DEfusion


3 Answers

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.

like image 114
DEfusion Avatar answered Nov 04 '22 20:11

DEfusion


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])
like image 29
Greg DeVore Avatar answered Nov 04 '22 20:11

Greg DeVore


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.

like image 33
srboisvert Avatar answered Nov 04 '22 18:11

srboisvert