我试图删除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
答案 0 :(得分:1)
几天前,我碰到了这个问题。
问题出在def stub_omniauth
中。
您应该更改OmniAuth.config.mock_auth[:google]
=> OmniAuth.config.mock_auth[:google_oauth2]