使用 open_id_authentication 插件时如何在 RSpec 用户故事/Cucumber 中伪造 OpenID 登录插件、故事、用户、如何在

2023-09-07 23:44:15 作者:_℡↘幽默先生╮

我正在尝试编写一个需要我登录用户的 Cucumber 场景——这通常很简单,但我只使用 OpenID 身份验证(身份验证插件的粗略).然而,在挖掘了 open_id_authentication 插件的胆量后,我不确定如何在 Cucumber 中实现这一点.

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.

推荐答案

我想出了一个办法,如果你把它放在你的 features/support/env.rb 中:

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

我只是完全破坏了黄瓜功能的开放 id 身份验证,显然如果我需要登录失败的实例,我可以根据提供的 identity_url 做到这一点.

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.