简体   繁体   中英

current_user set to nil on redirect (session lost?)

Configuration

Webpacker application using Rails 5.1 with Vue.js 2.5

I've been trying to diagnose this bug for 3 days now and I am at my wits end. I've started to add system tests using Capybara using standard selenium-webdriver .

Problems occur when I am trying to access authenticatable routes, for example, user settings:

RSpec.describe 'home page', type: :system do
  before(:context) do
    WebMock.disable_net_connect!(allow_localhost: true)
  end

  after do
    Warden.test_reset!
  end

  context 'when logged in' do
    context 'as an average user' do

      before do
        @profile = create(:profile)
        @user = @profile.user
        @user.update(confirmed_at: Time.now)

        login_as(@user, scope: :user)
      end

      it 'redirects to Account Settings page after pressing Account Settings' do
        visit '/'

        find('.profile-menu').hover
        find('.settings').click

        expect(page).to have_content 'Account Settings'
      end
    end
  end
end

this is not a link and is handled in my .vue file, which just redirects to the route I need location.href = "/users/edit";

When I am on the root page, I am signed in, current_user returns the user and all is great. But as soon as I press that link, at some point it gets reset as I get redirected to the standard Devise Login page.

If I directly go to that page visit '/users/edit , but current_user is set and its all fine.

This is my test devise setup:

require 'devise'

RSpec.configure do |config|
  config.include Warden::Test::Helpers
end

Let me know if you need some extra information, I think I've provided all the crucial bits but could have missed something.

I seem to have fixed this issue. I've tried a couple things mentioned in this SO answer . As far as I can tell, what fixed it was fixing my configuration from:

 RSpec.configure do |config|
   config.include Warden::Test::Helpers
 end

to

 RSpec.configure do |config|
   config.include Devise::Test::IntegrationHelpers, type: :system
 end

This also allowed me to drop:

  after(:each) do
    Warden.test_reset!
  end

in all my tests. I've run quite a few tests and they don't fail anymore, so I am for now assuming this has been indeed fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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