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

发布于 2024-07-10 05:56:12 字数 148 浏览 4 评论 0原文

我正在尝试编写一个 Cucumber 场景,要求我有一个登录用户 - 这通常非常简单,但我只使用 OpenID 身份验证(身份验证插件的curtosy)。 然而,在深入研究 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眸 2024-07-17 05:56:12

我已经找到了一种方法,如果你将其放在 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

那么你可以在适当的步骤中执行类似的操作:

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

寄与心 2024-07-17 05:56:12

如果您希望能够存根响应,请执行以下操作:

在 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

通过将响应移至单独的方法,您现在可以在必要时在步骤中存根响应。 例如,如果我想要一个 :missing 响应,并且我有一个控制器 GoogleLoginController,我可以使用 Mocha 执行以下操作:

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

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])
想念有你 2024-07-17 05:56:12

Bort,一个rails骨架应用,有全套的rspec测试,支持openID登录,所以你可以想看看他们做了什么。

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.

记忆之渊 2024-07-17 05:56:12

DEfusion 的答案是有效的,只是我需要将 Identity_url 规范化,例如:

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

谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文