无法模仿Omniauth谷歌

时间:2017-07-05 07:42:37

标签: mocking omniauth-google-oauth2

我正在使用rails应用程序,并想在我的rails应用程序中自动执行"使用google" 登录。

这是我在google搜索后到目前为止写的测试用例。 但不是将我带到我的主页,而是将我重定向到其他网址。不要理解为什么会发生这种情况。

 test 'z' do
    Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
    Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
    stub_omniauth
    get '/users/auth/google_oauth2_login'
    print @response.body
    # it prints: Redirecting to http://www.example.com/users/auth/google_oauth2_login/callback...nil
  end

这是方法

  def stub_omniauth
    # first, set OmniAuth to run in test mode
    OmniAuth.config.test_mode = true
    # then, provide a set of fake oauth data that
    # omniauth will use when a user tries to authenticate:
    OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new({
                                                                    provider: "google",
                                                                    uid: "12345678910",
                                                                    info: {
                                                                        email: "jesse@mountainmantechnologies.com",
                                                                        first_name: "Jesse",
                                                                        last_name: "Spevack"
                                                                    },
                                                                    credentials: {
                                                                        token: "abcdefg12345",
                                                                        refresh_token: "12345abcdefg",
                                                                        expires_at: DateTime.now,
                                                                    }
                                                                })
  end

重定向到http://www.example.com/users/auth/google_oauth2_login/callback...nil

0 个答案:

没有答案