gpt4 book ai didi

ruby - 我如何在 rspec for sinatra 中使用 omniauth?

转载 作者:数据小太阳 更新时间:2023-10-29 07:34:48 27 4
gpt4 key购买 nike

简化版:

使用 sinatra 的 omniauth gem,我无法让 rspec 登录工作并为后续请求保留我的 session 。

基于 http://benprew.posterous.com/testing-sessions-with-sinatra 的建议,并关闭 session ,我已将问题隔离到此:

  app.send(:set, :sessions, false)    # From http://benprew.posterous.com/testing-sessions-with-sinatra
get '/auth/google_oauth2/callback', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2] }
# last_request.session => {"uid"=>"222222222222222222222", :flash=>{:success=>"Welcome"}}
# last_response.body => ""

follow_redirect!
# last_request.session => {:flash=>{}}
# last_response.body => Html for the homepage, which is what I want

如何让 rspec 遵循重定向并保留 session 变量?这在 Sinatra 中可能吗?

来自 http://benprew.posterous.com/testing-sessions-with-sinatra ,似乎我必须在每个需要登录的 get/post 请求上发送 session 变量,但这在重定向的情况下不起作用。


详情:

我正在尝试通过以下设置在 sinatra 中使用 omniauth gem:

spec_helper.rb

ENV['RACK_ENV'] = 'test'

# Include web.rb file
require_relative '../web'
# Include factories.rb file
require_relative '../test/factories.rb'

require 'rspec'
require 'rack/test'
require 'factory_girl'
require 'ruby-debug'

# Include Rack::Test in all rspec tests
RSpec.configure do |conf|
conf.include Rack::Test::Methods
conf.mock_with :rspec
end

web_spec.rb

describe "Authentication:" do
before do
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:google_oauth2, {
:uid => '222222222222222222222',
:info => {
:email => "someone@example.com",
:name => 'Someone'
}
})
end

describe "Logging in as a new user" do
it "should work" do
get '/auth/google_oauth2/'

last_response.body.should include("Welcome")
end
end
end

尝试进行身份验证时,我得到一个 <h1>Not Found</h1>回复。我错过了什么?

关于 Integration testing omn​​iauth 文档的页面,它提到添加两个环境变量:

before do 
request.env["devise.mapping"] = Devise.mappings[:user]
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end

但正如我所补充的那样,似乎仅适用于 rails

request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2] 

到我的before阻止我的规范,我得到这个错误:

Failure/Error: request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
ArgumentError:
wrong number of arguments (0 for 1)

编辑:

调用 get

get '/auth/google_oauth2/', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2]}

似乎给我last_request.env["omniauth.auth"]等于

 {"provider"=>"google_oauth2", "uid"=>"222222222222222222222", "info"=>{"email"=>"someone@example.com", "name"=>"Someone"}}

这似乎是对的,但是 last_response.body仍然返回

<h1>Not Found</h1>

最佳答案

部分答案...

回调 url 工作得更好,添加了请求环境变量:

get '/auth/google_oauth2/callback', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2]}
follow_redirect!

last_response.body.should include("Welcome")

但是,这不适用于重定向后的 session ,这是我的应用知道有人登录所必需的。更新了问题以反射(reflect)这一点。

关于ruby - 我如何在 rspec for sinatra 中使用 omniauth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8419286/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com