【问题标题】:Oauth minitest error, callback link giving an errorOauth minitest 错误,回调链接给出错误
【发布时间】:2019-02-06 12:43:12
【问题描述】:

我正在尝试为 OmniAuth 用户编写测试,在设置我的 test_helper 后,我遇到了一个错误的 URI 错误。分享以下详细信息:

test_helper.rb

# OmniAuth auth mock for testing
  def setup_omniauth_mock (user)
    OmniAuth.config.test_mode = true
    OmniAuth::AuthHash.new ({
    'provider'            => 'google',
    'uid'                 => '123545',
    'user_id'             => '2',
    'first_name'          => 'X',
    'last_name'           => 'XYZ',
    'email'               => 'xxyz@example.com',
    'image'               => 'https://lh3.googleusercontent.com//photo.jpg',
    'oauth_token'         => 'abcdef12345',
    'oauth_expires_at'    => DateTime.now,
    })
    OmniAuth.config.add_mock(:google, OmniAuth::AuthHash.new)
    get '/auth/":google"/callback'
    Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
    get '/auth/:google/callback'
  end 

我得到的错误:

    test_validating_a_Google_OAuth_user#SessionsControllerTest (0.49s)
URI::InvalidURIError:         URI::InvalidURIError: bad
URI(is not URI?): http://www.example.com:80/auth/":google"/callback
test/test_helper.rb:42:in `setup_omniauth_mock'

现在我按照此处的文档 [Oauth 集成测试][1]

[1]: https://github.com/omniauth/omniauth/wiki/Integration-Testing 但我认为我做错了什么。

有人可以帮我指导一下吗?

谢谢! J.

【问题讨论】:

    标签: ruby-on-rails oauth minitest


    【解决方案1】:

    我实际上通过清理一些东西来解决它。

    我的 test_helper.rb 现在:

     # OmniAuth auth mock setup for testing
      setup do
        OmniAuth.config.test_mode = true
        Rails.application.env_config["omniauth.auth"] =
        OmniAuth.config.mock_auth[:google]
    
      end
    
      #teardown OmniAuth mock setup
      teardown do
        OmniAuth.config.test_mode = false
      end
    
      #Google OAuth mock
      def google_oauth2_mock (user)
        OmniAuth.config.mock_auth[:google]
        OmniAuth::AuthHash.new ({
        'provider'            => 'google_oauth2',
        'uid'                 => '123545',
        'user_id'             => '2',
        'first_name'          => 'X',
        'last_name'           => 'XXYZ',
        'email'               => 'xxyzjam@example.com',
        'image'               => 'https://lh3.googleusercontent.com/photo.jpg',
        'oauth_token'         => 'abcdef12345',
        'refresh_token'       => '12345abcdef',
        'oauth_expires_at'    => DateTime.now,
        })
      end
    

    我将路线放在单独的测试中,这使我能够顺利运行测试套件。

    希望我能够为您节省一些时间和烦恼。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2019-01-01
      • 2013-07-04
      • 1970-01-01
      相关资源
      最近更新 更多