使用 Cucumber 时,在 OmniAuth 中打开测试模式不会重定向请求

发布于 2024-12-26 02:51:52 字数 2004 浏览 2 评论 0 原文

我正在尝试通过在向 OmniAuth 登录过程>/auth/facebook,如此处此处。问题是,当我打开测试模式时,请求作为错误返回,这与未打开测试模式时的行为相同。

user_management.feature

Feature: User management
    @omniauth_test
    Scenario: Login
        Given a user exists
        And that user is signed in

web_steps.rbomniauth.rb

...
And /^that user is signed in$/ do
  visit "/auth/facebook"
end
...

Before('@omniauth_test') do
  OmniAuth.config.test_mode = true
  p "OmniAuth.config.test_mode is #{OmniAuth.config.test_mode}"
  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
      "provider"=>"facebook",
      "uid"=>"uid",
      "user_info"=>{"email"=>"[email protected]", "first_name"=>"Test", "last_name"=>"User", "name"=>"Test User"}
  }
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end

结果

Feature: User management

  @omniauth_test
  Scenario: Login                # features/user_management.feature:3
"OmniAuth.config.test_mode is true"
    Given a user exists     # features/step_definitions/pickle_steps.rb:4
    And that user is signed in # features/step_definitions/web_steps.rb:40
      No route matches [GET] "/auth/facebook" (ActionController::RoutingError)
      ./features/step_definitions/web_steps.rb:41:in `/^that user is signed in$/'
      features/testing.feature:5:in `And that user is signed in'

I am trying to test my OmniAuth login process by providing a faked authentication hash when a request is made to /auth/facebook, as described here and here. The problem is, when I turn test mode on, the request returns as an error, which is the same behaviour as when test mode is not turned on.

user_management.feature

Feature: User management
    @omniauth_test
    Scenario: Login
        Given a user exists
        And that user is signed in

web_steps.rb

...
And /^that user is signed in$/ do
  visit "/auth/facebook"
end
...

omniauth.rb

Before('@omniauth_test') do
  OmniAuth.config.test_mode = true
  p "OmniAuth.config.test_mode is #{OmniAuth.config.test_mode}"
  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
      "provider"=>"facebook",
      "uid"=>"uid",
      "user_info"=>{"email"=>"[email protected]", "first_name"=>"Test", "last_name"=>"User", "name"=>"Test User"}
  }
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end

Outcome

Feature: User management

  @omniauth_test
  Scenario: Login                # features/user_management.feature:3
"OmniAuth.config.test_mode is true"
    Given a user exists     # features/step_definitions/pickle_steps.rb:4
    And that user is signed in # features/step_definitions/web_steps.rb:40
      No route matches [GET] "/auth/facebook" (ActionController::RoutingError)
      ./features/step_definitions/web_steps.rb:41:in `/^that user is signed in$/'
      features/testing.feature:5:in `And that user is signed in'

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

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

发布评论

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

评论(3

弥繁 2025-01-02 02:51:52

您应该将这两个放入测试初始值设定项中:

request.env["devise.mapping"] = Devise.mappings[:user] 
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]

You should throw these two in an test initializer:

request.env["devise.mapping"] = Devise.mappings[:user] 
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
薆情海 2025-01-02 02:51:52

在 features/support 下的omniauth.rb中添加以下内容,并标记需要使用@omniauth_test进行fb登录的场景

Before('@omniauth_test') do  
  OmniAuth.config.test_mode = true  

  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
   :provider => 'facebook',
   :uid => '1234567',
   :info => {
     :nickname => 'test',
     :email => '[email protected]',
     :name => 'Test User',
     :first_name => 'Test',
     :last_name => 'User',
     :location => 'California',
     :verified => true
    }.stringify_keys!
  }.stringify_keys!
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end

Throw the following in omniauth.rb under features/support and mark your scenario which requires fb login with @omniauth_test

Before('@omniauth_test') do  
  OmniAuth.config.test_mode = true  

  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
   :provider => 'facebook',
   :uid => '1234567',
   :info => {
     :nickname => 'test',
     :email => '[email protected]',
     :name => 'Test User',
     :first_name => 'Test',
     :last_name => 'User',
     :location => 'California',
     :verified => true
    }.stringify_keys!
  }.stringify_keys!
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end
随心而道 2025-01-02 02:51:52

问题不在于你的测试。它与您的路由有关,或更具体地说与omniauths 路由有关。

您确定在 config/initializers/omniauth.rb 中为 facebook 设置了策略吗?

你可以用 gemform 获取它 https://github.com/mkdynamic/omniauth-facebook

另外,添加新策略后请记住重新启动网络服务器。 (这让我有一次;))

The problem is not with your tests. It's with your routing, or more specifically with omniauths routing.

Are you sure that you have a strategy set up in config/initializers/omniauth.rb for facebook?

You can get it in gemform https://github.com/mkdynamic/omniauth-facebook

Also, remember to restart your webserver after adding a new strategy. (That got me once ;))

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