2011-12-07 36 views
4

缩短版本:如何在rspec中为sinatra使用omniauth?

使用了西纳特拉的omniauth宝石,我不能让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请求上的会话变量,但这在重定向的情况下不起作用。


细节:

我试图使用omniauth宝石西纳特拉与以下设置:

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 => "[email protected].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页omniauth文档中,它提到增加了两个环境变量:

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"=>"[email protected]", "name"=>"Someone"}} 

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

<h1>Not Found</h1> 
+0

我发现这个http://stackoverflow.com/a/3892401/111884,这似乎步入正轨,但我无法让它工作。 – zlog

回答

3

部分答案...

回调URL更好地工作,与添加的请求环境变量:

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

last_response.body.should include("Welcome") 

然而,这并不重定向,这对于我的应用程序知道有人登录,需要经过会话工作。更新了问题以反映这一点。

+0

实际上,我需要会话才能登录才能工作,所以答案对我的应用程序并不适用。 – zlog

0

使用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 => "[email protected]", 
    :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 
相关问题