jonleighton / focused_controller

MIT License
468 stars 27 forks source link

Session values accessible, Cookies private? #25

Open JeanMertz opened 11 years ago

JeanMertz commented 11 years ago

See the below specification:

context 'For valid authentication' do
  Given { subject.params = { authentication: { email: 'my@mail.com' } } }
  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'
JeanMertz commented 11 years ago

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.

JeanMertz commented 11 years ago

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