在与 Rails 3.1/Authlogic/ActiveRecord 会话存储的集成测试中无法获得授权用户

发布于 2024-12-15 09:28:40 字数 1107 浏览 5 评论 0原文

我试图在使用 Authlogic 和 ActiveRecord SessionStore 的 Rails 3.1 应用程序上编写一个简单的集成测试,但我遇到了困难。

首先,我尝试了一种经典方法:在确保 require "authlogic/test_case" 行已经在我们的 test_helper.rb 中之后,我编写了一个调用 的设置方法activate_authlogic 然后使用 UserSession(@user) 创建会话。

这确实创建了一个会话(正如 UserSession.find 所证明的那样),但是当对受保护资源执行 get 请求时,响应将重定向到登录表单和会话被杀死(即 UserSession.find 将返回 nil

我也尝试过 POST-ing 电子邮件/密码,但这似乎仅有效 如果我将会话存储更改回 cookie_store (我从 这条评论中发现的)。

将会话存储切换到 CookieStore 仅用于测试是一种选择,但有些测试已经依赖于 ActiveRecord 存储。

有没有办法只为了一次测试而切换会话存储?我缺少的这个问题还有其他解决方案吗?

require 'test_helper'

class ProtectedTest < ActionController::IntegrationTest
  def setup
    activate_authlogic
    @user = Factory(:user, :roles => 'user')
    UserSession.create(@user)
  end

  def test_protected
    https!
    get '/protected'
    assert_response :success
  end
end

I am trying to write a simple integration test on a Rails 3.1 application that uses Authlogic and ActiveRecord SessionStore, but I'm hitting the wall.

First, I tried a classic approach: after ensuring that require "authlogic/test_case" line is already in our test_helper.rb I then wrote a setup method that calls activate_authlogic and then uses UserSession(@user) to create a session.

This did create a session (as proved by UserSession.find), but when doing a get request on protected resource, the response would be a redirect to the login form and session was killed (ie. UserSession.find would return nil)

I tried POST-ing the email/password as well, but that seems to work only if I change the session store back to cookie_store (something I found out from this comment).

Switching session store to CookieStore just for test would be an option, but there are some tests that already depend on ActiveRecord store.

Is there any way to switch the session store just for one test? Is there any other solution to this problem that I'm missing?

require 'test_helper'

class ProtectedTest < ActionController::IntegrationTest
  def setup
    activate_authlogic
    @user = Factory(:user, :roles => 'user')
    UserSession.create(@user)
  end

  def test_protected
    https!
    get '/protected'
    assert_response :success
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2024-12-22 09:28:41

亲爱的未来人士:

我想出了一个解决方法,通过在不同的环境中启动需要授权用户的测试在包含“test_helper”之前设置 ENV["RAILS_ENV"]。然后我将该环境中的 session_store 更改为 cookie_store。

ENV["RAILS_ENV"] = "test_cs"
require 'test_helper'
end

class ProtectedTest < ActionController::IntegrationTest
  # ...
end

Dear people from the future:

I figured a workaround to this by starting tests that needed authorized users in a different environment by setting ENV["RAILS_ENV"] before including 'test_helper'. Then I changed session_store in that environment to cookie_store.

ENV["RAILS_ENV"] = "test_cs"
require 'test_helper'
end

class ProtectedTest < ActionController::IntegrationTest
  # ...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文