首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用MiniTest时,当尝试测试``omniauth Google-oauth2‘gem时,我一直得到一个302重定向到我的sign_in路径

使用MiniTest时,当尝试测试``omniauth Google-oauth2‘gem时,我一直得到一个302重定向到我的sign_in路径
EN

Stack Overflow用户
提问于 2018-09-04 17:18:27
回答 1查看 899关注 0票数 0

我已经用我的rails应用程序设置了omniauth-google-oauth2 gem。这是有效的,但当我试图写我的测试,我不断得到302重定向。我看到的许多网站结果都提到了如何在Rspec中测试它,但我尝试使用Rails附带的MiniTest进行测试。因为我是新手,所以我一直在纠结于把事情搞砸的地方。

routes.rb

代码语言:javascript
运行
复制
Rails.application.routes.draw do

  devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }

  devise_scope :user do
      get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
      get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
    end

  root to: "home#index"

end

test_helper.rb

代码语言:javascript
运行
复制
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'mocha/minitest'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  def setup_omniauth_mock(user)
    OmniAuth.config.test_mode = true
      OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
        provider: "google_oauth2",
        email: "#{user.email}",
        first_name: "#{user.first_name}",
        last_name: "#{user.last_name}"
      })
      Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
    Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
  end

  def login_with_user(user)
    setup_omniauth_mock(user)
    get user_google_oauth2_omniauth_authorize_path
  end

end

module ActionController
    class TestCase
        include Devise::Test::ControllerHelpers
    end
end

module ActionDispatch
    class IntegrationTest
        include Devise::Test::IntegrationHelpers
    end
end

test/integration/login_with_google_oauth2_test.rb

代码语言:javascript
运行
复制
require 'test_helper'

class LoginWithGoogleOauth2Test < ActionDispatch::IntegrationTest
  setup do
    @user = users(:one)
  end

  test "allows a logged-in user to view the home page" do 
    login_with_user(@user)
    get root_path
    assert_redirected_to root_path
    assert_selector "h1", text: "#{user.full_name}"
  end

end

运行rails test错误

代码语言:javascript
运行
复制
daveomcd@mcdonald-PC9020:~/rails_projects/haystack_scout$ rails test
Running via Spring preloader in process 24855
/home/daveomcd/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/application.rb:185: warning: Insecure world writable dir /home/daveomcd/.rvm/gems/ruby-2.5.1/bin in PATH, mode 040777
Run options: --seed 55919

# Running:

.....F

Failure:
LoginWithGoogleOauth2Test#test_allows_a_logged-in_user_to_view_the_home_page [/mnt/c/Users/mcdonaldd/Documents/Rails Projects/haystack_scout/test/integration/login_with_google_oauth2_test.rb:20]:
Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/sign_in>.
Expected "http://www.example.com/" to be === "http://www.example.com/sign_in".


bin/rails test test/integration/login_with_google_oauth2_test.rb:17



Finished in 0.238680s, 25.1383 runs/s, 50.2766 assertions/s.
6 runs, 12 assertions, 1 failures, 0 errors, 0 skips
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-25 15:23:19

所以我想出了一个测试omniauth-google-oauth2的方法。我会尽我最大的努力把它展示在下面。感谢那些花时间调查我问题的人。

test\integration\authorization_integration_test.rb

代码语言:javascript
运行
复制
require 'test_helper'

class AuthorizationIntegrationTest < ActionDispatch::IntegrationTest
  include Capybara::DSL
  include Capybara::Minitest::Assertions
  include Devise::Test::IntegrationHelpers

    setup do
        OmniAuth.config.test_mode = true
        Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
        Rails.application.env_config["omniauth.auth"]  = google_oauth2_mock
    end

    teardown do
        OmniAuth.config.test_mode = false
    end

    test "authorizes and sets user currently in database with Google OAuth" do
        visit root_path
        assert page.has_content? "Sign in with Google"  
        click_link "Sign in with Google"
        assert page.has_content? "Successfully authenticated from Google account."
    end

    private

        def google_oauth2_mock
            OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
            provider: "google_oauth2",
            uid: "12345678910",
            info: { 
                email: "fakeemail@gmail-fake.com",
                first_name: "David",
                last_name: "McDonald"
            },
            credentials: {
                token: "abcdefgh12345",
                refresh_token: "12345abcdefgh",
                expires_at: DateTime.now
            }
          })
        end

end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52171212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档