focused_controller
focused_controller copied to clipboard
Session values accessible, Cookies private?
See the below specification:
context 'For valid authentication' do
Given { subject.params = { authentication: { email: '[email protected]' } } }
Given { subject.authentication.stub(:legitimate?).and_return(true) }
Given { Authentication.any_instance.stub_chain(:user, :id).and_return(1) }
When { subject.call }
context 'deletes signup session' do
Then { subject.session[:signup].should be_nil }
end
context 'sets authentication cookie' do
Then { subject.send(:cookies)[:authentication].should_not be_nil }
end
context 'sets authentication cookie' do
Then { subject.cookies[:authentication].should_not be_nil }
end
end
The first two pass, the last one fails. I was under the impression that I should be able to access cookie data in my FocusedController
specs (I have included FocusedController::RSpecHelper
) but only session
seems to be available here?
Here is the error stacktrace:
1) SessionsController SessionsController::Create For valid authentication sets authentication cookie
Failure/Error: Then { subject.cookies[:authentication].should_not be_nil }
NoMethodError:
private method `cookies' called for #<SessionsController::Create:0x007fc818852f90>
# ./spec/unit/controllers/sessions_controller_spec.rb:47:in `block (5 levels) in <top (required)>'
# ./spec/unit/controllers/sessions_controller_spec.rb:47:in `block in Then'
Another issue, which I believe to be related to this, is the fact that cookies.signed
causes errors when using with FocusedControllers:
1) SessionsController SessionsController::Create For valid authentication deletes signup session
Failure/Error: When { subject.call }
ArgumentError:
A secret is required to generate an integrity hash for cookie session data. Use config.secret_token = "some secret phrase of at least 30 characters"in config/initializers/secret_token.rb
The secret token is set, and works both in the development environment as well as integration tests, it only fails on FocusedController unit tests.
I solved the second error (was caused by using Rails'4.0.0.beta1
).
See: https://github.com/JeanMertz/focused_controller/commit/15816665c52818ef5f21dde75911850ed6ada105
def cookie_jar
- @cookie_jar ||= ActionDispatch::Cookies::CookieJar.new
+ key = ActionDispatch::Cookies::GENERATOR_KEY
+ @cookie_jar ||= ActionDispatch::Cookies::CookieJar.new(key)
end