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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> def create
if user = User.authenticate_by(user_params)
session[:user_id] = user.id
respond_to do |format|
...
end
else
..
end
end
</code>
<code> def create if user = User.authenticate_by(user_params) session[:user_id] = user.id respond_to do |format| ... end else .. end end </code>
  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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> before do
page.set_rack_session(user_id: user.id)
visit new_sabeel_path
end
</code>
<code> before do page.set_rack_session(user_id: user.id) visit new_sabeel_path end </code>
  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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> def create
if user ||= User.authenticate_by(user_params)
start_new_session_for user
respond_to do |format|
...
end
else
...
end
end
</code>
<code> def create if user ||= User.authenticate_by(user_params) start_new_session_for user respond_to do |format| ... end else ... end end </code>
  def create
    if user ||= User.authenticate_by(user_params)
      start_new_session_for user

      respond_to do |format|
        ...
      end
    else
      ...
    end
  end
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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
</code>
<code> 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 </code>
  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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> before do
create_cookie("session_id", session.id)
visit new_sabeel_path
end
</code>
<code> before do create_cookie("session_id", session.id) visit new_sabeel_path end </code>
  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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật