加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何在rspec中使用omniauth用于sinatra?

发布时间:2020-12-17 02:16:35 所属栏目:百科 来源:网络整理
导读:缩短版: 使用omniauth gem for sinatra,我无法让rspec登录工作并保持我的会话以用于后续请求. 根据http://benprew.posterous.com/testing-sessions-with-sinatra的建议,并关闭会话,我已将问题与此隔离: app.send(:set,:sessions,false) # From http://benp
缩短版:

使用omniauth gem for sinatra,我无法让rspec登录工作并保持我的会话以用于后续请求.

根据http://benprew.posterous.com/testing-sessions-with-sinatra的建议,并关闭会话,我已将问题与此隔离:

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遵循重定向并保留会话变量?这在Sinatra有可能吗?

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

细节:

我正在尝试使用以下设置在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>响应.我错过了什么?

在omniauth docs的Integration testing页面上,它提到添加两个环境变量:

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

但是我补充说,似乎只适用于铁路

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

在我的规范中我的前一块,我收到此错误:

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

编辑:

打电话来

get '/auth/google_oauth2/',{"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>

解决方法

部分答案……

使用添加的请求环境变量,回调网址效果更好:

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

last_response.body.should include("Welcome")

但是,这不适用于重定向后的会话,这是我的应用程序知道某人已登录所必需的.更新了问题以反映这一点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读