Pat*_*ois 1 rspec ruby-on-rails omniauth strong-parameters
我正在尝试从Rails 4.1.11升级到4.1.16.升级后,我的几个SessionsController规范都失败了.但是当我尝试登录开发环境时,一切似乎都运行正常.
我有以下代码:
# sessions_controller.rb
def create
@identity = Identity.find_or_create(auth)
...
end
protected
def auth
request.env["omniauth.auth"]
end
# identity.rb
class Identity < ActiveRecord::Base
...
def self.find_or_create(auth)
where(auth.slice(:uid, :provider)).first_or_create
end
end
Run Code Online (Sandbox Code Playgroud)
在我的规范中,我使用fixture spec_helper.rb来提供OmniAuth哈希:
# spec_helper.rb
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
:provider => 'facebook',
:uid => '1234567',
:info => {
:email => 'joe@bloggs.com',
...
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用OmniAuth属性来查找用户的身份时,我得到一个ForbiddenAttributesError:
1) SessionsController GET :create not yet signed in sets the user ID in the session hash
Failure/Error: get :create
ActiveModel::ForbiddenAttributesError:
ActiveModel::ForbiddenAttributesError
# /Users/pat/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.6/lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'
Run Code Online (Sandbox Code Playgroud)
我理解params哈希中强参数和白名单属性的想法.但是我不能require在OmniAuth哈希上使用(至少不能直接使用),而且我过去没有这么做.
这是OmniAuth的GitHub页面上提供的示例:
class SessionsController < ApplicationController
def create
@user = User.find_or_create_from_auth_hash(auth_hash)
self.current_user = @user
redirect_to '/'
end
protected
def auth_hash
request.env['omniauth.auth']
end
end
Run Code Online (Sandbox Code Playgroud)
据推测,它适用于最新版本的Rails.
我怎样才能通过测试?如果你想看一下,这是代码的其余部分.
您是否尝试过像这样的参数列入白名单?
def auth
ActionController::Parameters.new(request.env["omniauth.auth"]).permit(...)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
573 次 |
| 最近记录: |