使用Rails 4,我在让Authlogic看到我伪造的UserSession时遇到了问题。
我已经设置了pages#whoami来呈现当前用户的电子邮件地址,作为一个简单的测试。
class PagesController < ApplicationController
# before_filter :require_user
def whoami
render :text => current_user.try(:email) || 'anonymous'
end
end在spec/spec_helper.rb中:
require "authlogic/test_case"
include Authlogic::TestCase我的rspec测试:
require 'spec_helper'
describe '/whoami' do
setup :activate_authlogic
it "should tell me who I am" do
user = FactoryGirl.create(:user)
user.should be_valid
session = UserSession.create(user)
session.should be_valid
get '/whoami'
response.body.should == user.email
end
end我更新了应用程序控制器以显示当前会话:
def require_user
unless current_user
raise "Current User Session is: #{ current_user_session.inspect}"
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end有了before_filter :require_user的评论,我正确地得到了“匿名”。当我取消注释时,我看到我的用户会话为零。我试着看了一下授权代码,但在Authlogic::Session:Persistence::InstanceMethods#persisting?中迷路了。
我在试着调试。这就是我目前所处的位置。
在这里,我们尝试将Authlogic::Session::Base.controller设置为测试的模拟控制器:case.rb#L109
在我的规范中,我看到@控制器是一个Authlogic::TestCase::MockController
在我的规范中,我看到Authlogic::Session::Base.controller设置为模拟控制器。
但是,我随后检查了以下内容:
class ApplicationController < ActionController::Base
...
def current_user_session
raise Authlogic::Session::Base.controller.inspect
...
end
end我看到了Authlogic::ControllerAdapters::RailsAdapter .因此,控制器正在被设置,但没有持续。我想知道这是否与从Rails3到Rails4的切换有关?
如对此有任何见解,将不胜感激。
针对感兴趣者的Gem版本: gem rspec-core (2.14.5)宝石创作逻辑(3.3.0)创业板rails (4.0.0)
发布于 2013-09-12 17:49:13
按照https://stackoverflow.com/a/5803121,请求规范只是ActionDispatch::IntegrationTest的一个薄包装器。因此,与普通控制器规范不同,不存在对session的直接访问。
由于这一点,无法直接使用AuthLogic登录用户,后者确实依赖于会话和cookie:
它首先进行身份验证,然后设置适当的会话值和cookie来持久化会话。
对于request/integration/api/feature规范,直接到登录路径的请求将是必要的,以在幕后设置适当的会话/cookie。然后将集成会话用适当的值发送回(就像正常的web请求一样)。
为了使生活更简单,您可以添加一个助手方法,您可以为请求/集成/api/特性规范包括该方法:
# spec/support/auth_logic_helpers.rb
module Authlogic
module TestHelper
# You can call this anything you want, I chose this name as it was similar
# to how AuthLogic calls it's objects and methods
def create_user_session(user)
# Assuming you have this defined in your routes, otherwise just use:
# '/your_login_path'
post user_session_path, login: user.login, password: user.password
end
end
end
# Make this available to just the request and feature specs
RSpec.configure do |config|
config.include Authlogic::TestHelper, type: :request
config.include Authlogic::TestHelper, type: :feature
endhttps://stackoverflow.com/questions/18751297
复制相似问题