I have trouble undestanding the following situation:
I am logging in and remembering my users by doing the following
class SessionsController < ApplicationController
def create
user=User.find_by(email: params[:session][:email])
if user && user.authenticate(params[:session][:password])
log_in(user)
params[:session][:remember_me]=="1" ? remember(user) : forget(user)
redirect_to user
else
flash.now[:danger]="Invalid email/password combination"
render "new"
end
end
and
module SessionsHelper
def remember(user)
user.remember
cookies.permanent.signed[:user_id]=user.id
cookies.permanent[:remember_token]=user.remember_token
end
all of which works fine. The thing that I don't understand though, is that in an integration test I have to use cookies["remember_token"] because in the following situation cookies[:remember_token] will return nil
test "login with remember_me" do
log_in_as(@user)
assert_not_nil cookies["remember_token"]
end
Why does it return nil here and why does it not do that, if I use it for example as a parameter in a function inside of the Sessionhelper ( for example user.authenticated?(cookies[:remember_token]) )
P.S.: All of the code above comes from the Ruby on Rails Tutorial by Michael Hartl https://www.railstutorial.org/book (chap. 9.3.1)
This is because in normal application cookies method returns ActionDispatch::Cookies::CookieJar object which always converts cookie names to string. In integration test, cookies method returns Rack::Test::CookieJar object which doesn't do such conversion. So if you set your cookie in tests via symbol you need to access it via symbol. The same goes if you set your cookie via string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With