【问题标题】:How do I use omniauth in rspec for sinatra?如何在 rspec 中为 sinatra 使用omniauth?
【发布时间】:2012-01-15 04:46:06
【问题描述】:

缩短版:

使用 sinatra 的 omniauth gem,我无法让 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 文档的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] 

到我的规范中的 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>

【问题讨论】:

标签: ruby testing rspec sinatra omniauth


【解决方案1】:

部分答案...

回调 url 效果更好,添加了请求环境变量:

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

last_response.body.should include("Welcome")

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

【讨论】:

  • 实际上,我需要会话才能登录才能工作,所以答案对我的应用程序并不适用。
【解决方案2】:

使用this gist(源自https://stackoverflow.com/a/3892401/111884)存储会话数据,我得到了存储会话的测试,允许我将会话传递给进一步的请求。

不过可能有更简单的方法。

设置代码:

# Omniauth settings
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:google_oauth2, {
  :uid => '222222222222222222222',
  :info => {
    :email => "someone@example.com",
    :name => 'Someone'
  }
})


# Based on https://gist.github.com/375973 (from https://stackoverflow.com/a/3892401/111884)
class SessionData
  def initialize(cookies)
    @cookies = cookies
    @data = cookies['rack.session']
    if @data
      @data = @data.unpack("m*").first
      @data = Marshal.load(@data)
    else
      @data = {}
    end
  end

  def [](key)
    @data[key]
  end

  def []=(key, value)
    @data[key] = value
    session_data = Marshal.dump(@data)
    session_data = [session_data].pack("m*")
    @cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
    raise "session variable not set" unless @cookies['rack.session'] == session_data
  end
end

def login!(session)
  get '/auth/google_oauth2/callback', nil, { "omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2] }
  session['uid'] = last_request.session['uid']

  # Logged in user should have the same uid as login credentials
  session['uid'].should == OmniAuth.config.mock_auth[:google_oauth2]['uid']
end

# Based on Rack::Test::Session::follow_redirect!
def follow_redirect_with_session_login!(session)
  unless last_response.redirect?
    raise Error.new("Last response was not a redirect. Cannot follow_redirect!")
  end

  get(last_response["Location"], {}, { "HTTP_REFERER" => last_request.url, "rack.session" => {"uid" => session['uid']} })
end

def get_with_session_login(path)
  get path, nil, {"rack.session" => {"uid" => session['uid']}}
end

示例 rspec 代码:

describe "Authentication:" do

  def session
    SessionData.new(rack_test_session.instance_variable_get(:@rack_mock_session).cookie_jar)
  end

  describe "Logging in as a new user" do
    it "should create a new account with the user's name" do
      login!(session)
      last_request.session[:flash][:success].should include("Welcome")

      get_with_session_login "/"
      follow_redirect_with_session_login!(session)
      last_response.body.should include("Someone")
    end
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-22
    • 2011-11-17
    • 1970-01-01
    • 2012-06-28
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    相关资源
    最近更新 更多