Testing signed cookies on rails

I use signed cookies in Rails 3 to enable the Remember Me feature in the application. Everything works, except that I cannot functionally test cookies, because comparing cookies['remember_id'] gives me an encrypted cookie and cookies.signed not defined.

Any clues?

+4
source share
3 answers

The problem (at least on the surface) is that in the context of the functional test (ActionController :: TestCase) the cookie is a hash, while when working with controllers it is ActionDispatch :: cookies :: CookieJar. Therefore, we need to convert it to a CookieJar so that we can use the β€œsigned” method to convert it to a SignedCookieJar.

In your functional tests (after a request for receipt) you can put the following files to convert cookies from a Hash to a CookieJar.

 @request.cookies.merge!(cookies) cookies = ActionDispatch::Cookies::CookieJar.build(@request) 
+6
source

This is a bit off topic, but I was not able to get a Rails 3 solution to work in Rails 5 because ActionDispatch :: Cookies :: CookieJar.build has changed. This did the trick:

 jar = ActionDispatch::Cookies::CookieJar.build(@request, cookies.to_hash) assert_equal jar.signed['remember_token'], ... 
0
source

assert_equal @ controller.send (: cookies) .signed [: remember_id], matching_value

-2
source

Source: https://habr.com/ru/post/1341039/


All Articles