Juzer Shakir
Juzer Shakir

Reputation: 75

How to set a user session in RSpec feature test in Rails

Initially I had setup a simple custom login functionality for my application which used session and now migrated it to use logic of authentication provided by Rails that uses cookies for creating sign-in session.

This was my code for signing in users using session:

  def create
    if user = User.authenticate_by(user_params)
      session[:user_id] = user.id

      respond_to do |format|
      ...
      end
    else
    ..
    end
  end

And this is how I created session for a user in feature tests to test authenticated views using rack_session_access gem:

  before do
    page.set_rack_session(user_id: user.id)
    visit new_sabeel_path
  end

This used to work perfectly fine and pass all the tests before migrating the new sign-in logic.

This is my new/current logic for signing in users using cookies:

  def create
    if user ||= User.authenticate_by(user_params)
      start_new_session_for user

      respond_to do |format|
        ...
      end
    else
      ...
    end
  end
  def start_new_session_for(user)
    user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
      Current.session = session
      cookies.signed.permanent[:session_id] = {value: session.id, httponly: true, same_site: :lax}
    end
  end

After these changes, the tests now fail because we are no longer using session based sign-in.

How do I test my authenticated views if I am unable to mimic the sign-in logic as before?

Expected It should set the cookies properly in tests and be able to visit authenticated views.

Current Behaviour In tests, when visiting authenticated views it redirects back to sign-in page.

I have tried to google this but all I get is how to test in the controllers and not for feature tests.

I also tried a library called show_me_the_cookies for creating cookies in my tests but it's not able to sign-in with that, it redirect to sign-in page.

  before do
    create_cookie("session_id", session.id)
    visit new_sabeel_path
  end

I tried creating cookies using cookies method (just as I have implemented in login logic) in my test suite but the method is not automatically loaded by rails. Seems like it's a part of ActionController module.

Has anyone here used Rails new authentication and tested authenticated views in feature tests? Or has anyone have the experience in created signed-in user in feature tests using cookies? If yes, I would love to hear about your solution for this!

FYI these are the dependencies I am using in my tests environment:

group :development, :test do
  gem "factory_bot_rails", "~> 6.4.0"
  gem "rspec-rails", "~> 7.1.0"
end

group :test do
  gem "capybara", "~> 3.40.0"
  gem "rack_session_access"
  gem "rails-controller-testing", "~> 1.0.5"
  gem "selenium-webdriver", "~> 4.27.0"
  gem "shoulda-callback-matchers", "~> 1.1.4"
  gem "shoulda-matchers", "~> 6.4.0"
  gem "simplecov", require: false
  gem "show_me_the_cookies", "~> 6.0"
end

Upvotes: 0

Views: 68

Answers (0)

Related Questions