Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i test cookie expiry in rails rspec

There is a lot of confusion about setting cookies in rspec http://relishapp.com/rspec/rspec-rails/v/2-6/dir/controller-specs/file/cookies

in your controller, normally you can write

cookies['transaction_code'] = { :expires => 300.seconds.from_now, :value => c }

but in rspec i can only write

request.cookies['transaction_code'] = transaction_code

if i say

request.cookies['transaction_code'] = { :expires => 300.seconds.from_now, :value => c }

i get the hash back as value of cookies['transaction_code'] in my controller.

Now my question is: how do i set/test cookie expiry then in an rspec controller test example?

UPDATE: On seconds thought: What i mean is: how do i test if the controller is reacts to an expired cookie as expected, but in fact an expired cookie is just like no cookie if i trust cookie implementation, which i should do, so after all maybe my question makes no sense. If this is the case, i need to test if (another) controller action sets an expiring cookie correctly, but how do i do it if cookies['transaction_code'] in the test only gives the value back?

like image 466
Viktor Trón Avatar asked Jun 27 '11 12:06

Viktor Trón


1 Answers

Browsers do not send cookie attributes back to the server. This is why you can only send the key-value pair to the action.

Since you can assume that Rails, Rack and browsers do the right thing with the arguments, all you really need to test is the arguments your code is passing to the CookieJar.

To test that expiry is being set properly in the controller setting the cookie, you could stub out the #cookies method and make sure the right settings are being passed to it.

# app/controllers/widget_controller.rb
...
def index
    cookies[:expiring_cookie] = { :value   => 'All that we see or seem...', 
                                  :expires => 1.hour.from_now }
end
...

# spec/controllers/widget_controller_spec.rb
...
it "sets the cookie" do
  get :index
  response.cookies['expiring_cookie'].should eq('All that we see or seem...')
                                               # is but a dream within a dream.
                                               #                - Edgar Allan Poe
end

it "sets the cookie expiration" do
  stub_cookie_jar = HashWithIndifferentAccess.new
  controller.stub(:cookies) { stub_cookie_jar }

  get :index
  expiring_cookie = stub_cookie_jar['expiring_cookie']
  expiring_cookie[:expires].to_i.should be_within(1).of(1.hour.from_now.to_i)
end
...

Testing much more than this is boiling the ocean. At some point, you have to assume that the stack you are sitting on (e.g., Rails, Rack, the web server, TCP/IP, OS, web browsers, etc) works properly and focus on the code you control.

like image 147
krohrbaugh Avatar answered Sep 19 '22 22:09

krohrbaugh