简体   繁体   English

使用RSpec测试设计会话超时

[英]Testing Devise session timeout with RSpec

I'd like to customize the time period after which my Rails app will automatically end a user's session. 我想自定义我的Rails应用程序将自动结束用户会话的时间段。

I found this question to give me exactly what I need, but I'd like to approach this through TDD. 我发现这个问题给了我我需要的东西,但是我想通过TDD来解决这个问题。 I've got a relatively solid test suite for the application, I'm just a little lost when it comes to testing if a user's session has expired, other than having a test sit there and wait for the session to expire after x.minutes have elapsed. 我有一个相对坚固的应用程序测试套件,我只是有点迷失测试用户的会话是否已经过期,除了测试坐在那里等待会话在x.minutesx.minutes已经过去了。 Surely there's a better way. 当然有更好的方法。

I checked the Devise and Warden documentation for test helpers dealing with this and came up dry. 我检查了Devise和Warden的文档,找到了处理这个问题的测试助手。 Anyone have any tips? 有人有任何提示吗?

How about stubbing user.timedout? 如何将user.timedout?存根user.timedout? to return true? 归还真的吗? Or, you could pass a custom time in your test that exceeds your custom timeout value, like user.timed_out?(31.minutes.ago) . 或者,您可以在测试中传递超过自定义超时值的自定义时间,例如user.timed_out?(31.minutes.ago)

Instead of playing with exact time elapsed: 而不是玩精确的时间过去:

Since Devise is built on Warden, you can simulate that Devise::SessionsController has timed out the User by way of Warden::Test::Helpers that give access to the Rack proxy. 由于Devise是基于Warden构建的,因此您可以模拟Devise :: SessionsController通过Warden :: Test :: Helpers超时用户,可以访问Rack代理。


# settings_page_spec.rb

include Warden::Test::Helpers

it 'does not allow updating password after timed out' do
  expect(current_path).to eq(user_settings_path(user))
  clink_link 'Manage Password'
    expect(page).to have_button('Update Password')
    Warden.on_next_request do |proxy|
      proxy.set_user(nil)
      click_button 'Update Password'
      expect(current_path).to eq(login_path)
    end
end

Full blog post here: http://www.morphogenic.net/2016/10/devise-test-timeout-user-in-featureintegration-specs-using-warden/ 完整的博客文章: http//www.morphogenic.net/2016/10/devise-test-timeout-user-in-featureintegration-specs-using-warden/

By default device set timeout to 30 minutes. 默认设备设置超时为30分钟。 you can used user.timedout?([specify time in past]) 你可以用user.timedout?([specify time in past])

ex: 例如:

user.timedout?(30.minutes.ago) will return false user.timedout?(30.minutes.ago)将返回false

user.timedout?(10.minutes.ago) will return true user.timedout?(10.minutes.ago)将返回true

you can check user.timedout?() value inside rspec. 你可以检查rspec里面的user.timedout?()值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM