New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Signed cookies not available in controller tests #27145
Comments
Looks like this was introduced via ae29142, cc @eileencodes @tenderlove Relevant commits: |
I think I have a fix for this, just adding a regression test to the test suite. |
Opened #27146 with a possible solution to this ticket. |
Closing since #27146 was merged. |
@maclover7 @sgrif I think the problem is still alive. I left a comment on that page. Am I missing something ? |
@devohh this is still an issue that needs to be fixed. |
Hrm, the original issue was about controller tests, and @devohh said the problem is with integration tests? Can you send a test script demonstrating the problem? |
Oh sorry @rafaelfranca I didn't see you reverted the original fix. Nevermind :) |
@eileencodes @rafaelfranca The same issue occurs during integration test AND also during controller test. |
This issue has been fixed in Rails 5.1 & master by 78c6c4b from @maclover7. To double check, I also verified it with the original reproduction app in the RSpec issue, and it passed with an upgrade to 5.1.0. 👍 |
That commit from Jon was merged but there was a second that was related and reverted, but never fixed. See #27586 |
Sorry about that, I did not run the script from #27586. Thanks @eileencodes. Here's the second script mentioned for reference on this issue. begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "rails", github: "rails/rails", branch: "5-0-stable"
gem "pry-byebug"
end
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_token = "secret_token"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
resources :users, only: [:index]
end
end
class UsersController < ActionController::Base
def index
unless cookies[:name].present?
cookies[:name] = "Alice"
end
render plain: "Home"
end
end
require "minitest/autorun"
require "rack/test"
class UsersControllerTest < ActionController::TestCase
setup do
@controller = UsersController.new
@routes = Rails.application.routes
end
test '#index sets a cookie if it is not already present' do
get :index
assert_response :ok
assert_equal "Alice", cookies[:name]
cookies[:name] = "Bob"
get :index
assert_response :ok
assert_equal "Bob", cookies[:name]
end
end |
This issue is still valid? |
On a fresh Rails 5.2 install, I still can't use require 'test_helper'
class MyControllerTest < ActionDispatch::IntegrationTest
test "displays current user's first name" do
user = users(:alice)
cookies.encrypted[:current_user] = user.id
get '/'
assert_select "p", user.first_name
end
end Results in:
|
Can confirm too, I guess an alt is to log in or fetch a token for an API first instead of trying to stub the cookie before the test. |
This issue is fixed for |
I thought |
This issue has been ported from the report at rspec/rspec-rails#1658, as it can be replicated purely with Rails using public API. This test passes using Rails 4.2
Steps to reproduce
Reproduction Script
Expected behavior
Changes to
cookies.encrypted
in the test setup should appear in the controller's cookiesActual behavior
Changes to
cookies.encrypted
in the test setup do not affect the controller's cookiesSystem configuration
Rails version: 5.0.0, 5.0.0.1, master
Ruby version: 2.3.3
The text was updated successfully, but these errors were encountered: