簡體   English   中英

Google Omniauth2 Rails在測試時給我“示例用戶”

[英]Google Omniauth2 Rails Giving me “Example User” when testing

我試圖存根omniauth身份驗證哈希來測試我與RSpec的集成。 由於某種原因,我的用戶模型被喂給“示例用戶”,該用戶沒有常規登錄的Google用戶所擁有的所有信息。

這是給用戶打破測試的參數: {"provider"=>"default", "uid"=>"1234", "info"=>{"name"=>"Example User"}}

這就是應該的樣子,如果我使用pry進入下一個迭代,它會起作用:

{:provider=>"google",
 :uid=>"12345678910",
 :info=>{:email=>"limsammy1@gmail.com", :first_name=>"Sam", :last_name=>"Lim"},
 :credentials=>{:token=>"abcdefg12345", :refresh_token=>"12345abcdefg", :expires_at=>Thu, 16 Nov 2017 15:27:23 -0700}}

這是我的規格:

    require 'rails_helper'

def stub_omniauth
  OmniAuth.config.test_mode = true
  OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new({
    provider: "google_oauth2",
      uid: "12345678910",
      info: {
        email: "limsammy1@gmail.com",
        first_name: "Sam",
        last_name: "Lim"
      },
      credentials: {
        token: "abcdefg12345",
        refresh_token: "abcdefg12345",
        expires_at: DateTime.now,
      }
    })
  end

RSpec.feature "user logs in" do
  scenario "using google oauth2 'omniauth'" do
    stub_omniauth

    visit root_path

    expect(page).to have_link("Sign in with Google")

    click_link "Sign in with Google"

    expect(page).to have_content("Sam Lim")
    expect(page).to have_content("Logout")
  end
end

這是我的用戶模型方法:

def self.update_or_create(auth)
    user = User.find_by(uid: auth[:uid]) || User.new
    binding.pry
    user.attributes = {
      provider: auth[:provider],
      uid: auth[:uid],
      email: auth[:info][:email],
      first_name: auth[:info][:first_name],
      last_name: auth[:info][:last_name],
      token: auth[:credentials][:token],
      refresh_token: auth[:credentials][:refresh_token],
      oauth_expires_at: auth[:credentials][:expires_at]
    }
    user.save!
    user
  end

我在這里的會話控制器中調用該方法:

def create
    user = User.update_or_create(request.env["omniauth.auth"])
    session[:id] = user.id
    redirect_to root_path
  end

我幾天前碰到了這個問題。 問題出在def stub_omniauth 您應該更改OmniAuth.config.mock_auth[:google] => OmniAuth.config.mock_auth[:google_oauth2]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM