Integration Test Session not loading Session Cookie - ruby-on-rails

Rails version: 5.1.1, upgrading from 4.2.7
I have an integration test (inherting from ActionDispatch::IntegrationTest) which hits a URL. The controller at that URL puts some stuff into the session. The test then tries to assert that certain values are now present in the session.
My session_store.rb:
Rails.application.config.session_store :cookie_store, key: '_companySite_session', expire_after: 1.year
What I'm finding is that the session variable, called from the integration test as 'session', is always an empty hash ({}).
If I look at request.host in the test, I see "www.example.com". If I look at response.cookies or cookies, I see:
#<Rack::Test::Cookie:0x00000006037108
#default_host="www.example.com",
#name="_companySite_session",
#name_value_raw=
"_companySite_session=a3RnYjRvWDlUYzRZaVlaTnlsK2RkeXBWcWFNYnQ1RVo4azNqdXNOanVWWWZVT2o5Vk0wTkVhY1dFOTBxdWNzMU5sY3c1cXhXRm0xT3JZWWZGUnluSnRuYTVoaWQ1S3ZZRXVMa1YyQ2pUMjEvOExaWkcybTdGT3lUZnZ0STRSSGVpc0dPU1VONWNwTEZxQlRvODN3eXdKZTYreVIvNDR4Y0IxMms2djlPWUZCcktYWmo1SElSZ0ttaHA5UWk5MDA5aVJzaGt3cUJ4V2tFY295ZGo4ak8ydUI3OWRnTUdkcmRpanVML3phalJtZ2c1OXByeExiRDhlcy9rR1RYUGh6NlkxV2xqNWl2VjlCMm45WXo0ajBKN0VTQ29CVGRQRVErOVIrNFZwdEZBc01oL2RaSGltUjdleGlWN3YySTdJUzcvbVlJbWdwWVNUVkw4K1pPc3JsUWxOR1BZNFVrUXFtYVVObmNlYlZrSklEbk5aU3VTck5MNm12a0NBMVF2aG5xLS1pSEJTVTVLZzJ2ZVA2M1IzTmJ4Njh3PT0%3D--f253c4e1678e53896a247b087e741cf006c5874f",
#options={"path"=>"/", "expires"=>"Fri, 08 Jun 2018 19:46:22 -0000", "HttpOnly"=>nil, "domain"=>"www.example.com"},
#value=
"a3RnYjRvWDlUYzRZaVlaTnlsK2RkeXBWcWFNYnQ1RVo4azNqdXNOanVWWWZVT2o5Vk0wTkVhY1dFOTBxdWNzMU5sY3c1cXhXRm0xT3JZWWZGUnluSnRuYTVoaWQ1S3ZZRXVMa1YyQ2pUMjEvOExaWkcybTdGT3lUZnZ0STRSSGVpc0dPU1VONWNwTEZxQlRvODN3eXdKZTYreVIvNDR4Y0IxMms2djlPWUZCcktYWmo1SElSZ0ttaHA5UWk5MDA5aVJzaGt3cUJ4V2tFY295ZGo4ak8ydUI3OWRnTUdkcmRpanVML3phalJtZ2c1OXByeExiRDhlcy9rR1RYUGh6NlkxV2xqNWl2VjlCMm45WXo0ajBKN0VTQ29CVGRQRVErOVIrNFZwdEZBc01oL2RaSGltUjdleGlWN3YySTdJUzcvbVlJbWdwWVNUVkw4K1pPc3JsUWxOR1BZNFVrUXFtYVVObmNlYlZrSklEbk5aU3VTck5MNm12a0NBMVF2aG5xLS1pSEJTVTVLZzJ2ZVA2M1IzTmJ4Njh3PT0=--f253c4e1678e53896a247b087e741cf006c5874f">
In other words, the session cookie seems to be present, and its domain seems to match the request's host. I've also used the code from How to decrypt a Rails 5 session cookie manually? to print out the value of this cookie, and the value is correct.
My test environment's config specifies config.action_controller.allow_forgery_protection = false
At this point, I'm out of ideas. The cookie is there, it seems fine, its set to the domain of the request, its not expired, but session remains an empty hash. Is it not getting updated somehow? Is it trying to update but failing silently? I've run out of things to try.
This test works fine under Rails 4.2.7.
While I haven't dived into it as fully, I've also been finding that tests' flash variable always seems to be empty, too. Again, tests that worked fine in Rails 4 are breaking because the flash always seems to be empty. I assume the flash and the session are related, so maybe the same problem affects them both.

The solution was that we had this setting in our test.rb environment file:
config.allow_concurrency = false
For some reason, after making a request in the test, this setting causes the session to be replaced with an empty hash. Rails' docs suggest that you not set this flag directly: http://guides.rubyonrails.org/v3.2/configuring.html.
I think this is something we had set in Rails 2, then got carried into Rails 4 when we upgraded, and now breaks in Rails 5.
I've logged it as an issue in the Rails repo: https://github.com/rails/rails/issues/29608

Related

rails session values disapear after current page

rails version is 5.0.4
To simplify the question, I will use an example which I had tested on my website.
I saved some values in session in http://myhome/a
http://myhome/a only had 2 lines code.
session[:asdf] = 1
render plain: "#{session.to_hash}"
The string I got is {"session_id"=>"9913d09d270a4dd67c8782836d1aaaaa", "asdf"=>1}
After that, I turn to http://myhome/b instantly
render plain: "#{session.to_hash}"
The value I got is only {}. I cannot even got session_id from it!!
myhome is fully copied from myhome_old
I had change the production value in secrets.yml
The same code worked find on myhome_old. Therefore, I think that might not be the problem of setting of my browser.
Is there something would effect the mechanism of session of rails?
Finally, I solve this problem by deleting config/initializers/session_store.rb.
It works fine now.

Rails.cache.clear returns nil

I have this setup
config.cache_store = :redis_store, ENV['REDIS_CACHE_URL']
$ redis-cli
127.0.0.1:6379> set random_key 1
OK
Now I go to the console and do Rails.cache.clear which returns nil
And I am still able to access the key random_key in the redis-cli. It did not clear the cache.
I could not read what Rails.cache returns here too ruby/2.3.4/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails.rb
Is Rails.cache.clear is supposed to return true?
Can someone please help me out if my understanding is wrong?
redis-cache stores data under a particular namespace.
For example, if you've configured redis-store according to Documentation, then cache keys will be stored under cache namespace. That means, that when you Rails.cache.write("random_key", "key") a key cache:random_key will appear in the Redis. Therefore, when you Rails.cache.clear, only keys under cache namespace will be deleted.
Hence, if you manually create random_key in Redis, Rails.cache.clear won't remove it. But if you manually create cache:random_key, it will.
Be careful when using Rails.cache.clear it will invalidate all the keys for the application (source)
[~Not sure if this is the best place for this answer~]
This helpful article was a great way for me to understand caching when changing versions of Rails from 5.1+ to Rails 6.1+. The article talks about options for generating a cache key with or without versioning.
In the instance of my application, versioning was needed but not turned on when upgraded to Rails 6.1:
#in application.rb
config.active_record.collection_cache_versioning = true
Then within the application code where object.cache_key is called, I had to change it to object.cache_key_with_version (source)

Ruby on rails : memcached read value is nil

I've inherited a Rails project that looks like it's using memcached.
I have a controller, in one method I'm storing a value in the cache with:
Rails.cache.write("key", #var)
in another method subsequently called in the same controller I read it back with:
#var = Rails.cache.read("key")
and the variable is nil. Why is caching not working?
I've just started using ROR so there's a good chance it's something pretty basic.
Just a couple of things to check:
Have you enabled caching in your development environment?
You didn't say which version of RoR you're using, however cache is not enabled by default on development environment.
To enable it, set config.action_controller.perform_caching = true in config/environments/development.rb file.
Have you started memcached server?
If not, start it with memcached -vv
If your memcached server is down when you try to write into cache, the call to Rails.cache.write will return false and the subsequent call to Rails.cache.read will therefore return nil.

In Rails 3.1, how can I cache the results of a AR query to a cache?

For an AR query like this:
#users = User.find(some_conditions_here)
then #users is an AR array and I want to cache this.
If I do, in one controller call, a
Rails.cache.write('foo',#users)
it doesn't complain or error out and I can even see a 'foo' under /tmp/cache with a non-zero size but a subsequent controller call to
Rails.cache.read('foo')
returns a nil. When I do both the write and read from a Rails console, it works as expected. What is it about doing it via a controller that causes this problem?
This used to work before in Rails 2... what am I missing?
Check that config.action_controller.perform_caching is set to true.
To quote the Rails caching guide,
caching is disabled by default for development and test, and enabled for production

Rails.cache.read returns nil in rails, but not in console

I'm attempting to cache and read a user object via its api key. The use gets cached fine, and I can read the cache in the rails console, but for whatever reason doing the same exact Rails.cache.read in the rails app always returns nil.
Heres an example of what I'm doing. This is in a before_filter function.
def authKey
#?api=ce6f95a8bf7f9861330ede58f8972981
key = params[:api]
cu = Rails.cache.read(key)
#<do some logic>
logger.debug("CACHING USER #{key}")
Rails.cache.write(key, user)
The cu will always be nil, but the object will exist in memcache. Has anyone else run into this sort of problem? I'm using the dalli gem with compression enabled.
I believe that rails caching is off by default for development and on in test and production. Check in config/environments/development.rb and see what config.cache_classes is set to...
Caching is turned off by default for development environment.
Adding 'config.action_controller.perform_caching = true' to config/environments/development.rb should fix the problem.
http://guides.rubyonrails.org/caching_with_rails.html#basic-caching
In dev mode you can toggle caching by running rails dev:cache

Resources