Skip to content

Commit

Permalink
cleaning up some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak committed May 4, 2010
1 parent a3c9237 commit 49efd7c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion test/controllers/sessions_controller_test.rb
@@ -1,7 +1,6 @@
require 'test_helper'

class SessionsControllerTest < ActionController::TestCase

tests Clearance::SessionsController

should_filter_params :password
Expand Down Expand Up @@ -46,11 +45,55 @@ class SessionsControllerTest < ActionController::TestCase

should_set_cookie("remember_token", "old-token", Clearance.configuration.cookie_expiration.call)

should "have a default of 1 year from now" do
assert_in_delta Clearance.configuration.cookie_expiration.call, 1.year.from_now, 100
end

should "not change the remember token" do
assert_equal "old-token", @user.reload.remember_token
end
end

context "on POST to #create with good credentials - cookie duration set to 2 weeks" do
custom_duration = 2.weeks.from_now.utc

This comment has been minimized.

Copy link
@andhapp

andhapp May 6, 2010

These two tests are doing the same thing aren't they. The basically test that the value that is supplied via the cookie_expiration gets assigned or the reason to have two tests is because they could lead to fence post errors? Please enlighten.

This comment has been minimized.

Copy link
@rnewman57

rnewman57 May 11, 2010

They are testing that the default (unconfigured) cookie expiration is 1 year from now, and that an explicitly configured cookie expiration is actually respected.


setup do
Clearance.configuration.cookie_expiration = lambda { custom_duration }
@user = Factory(:email_confirmed_user)
@user.update_attribute(:remember_token, "old-token2")
post :create, :session => {
:email => @user.email,
:password => @user.password }
end

should_set_cookie("remember_token", "old-token2", custom_duration)

teardown do
# restore default Clearance configuration
Clearance.configuration = nil
Clearance.configure {}
end
end

context "on POST to #create with good credentials - cookie expiration set to nil (session cookie)" do
setup do
Clearance.configuration.cookie_expiration = lambda { nil }
@user = Factory(:email_confirmed_user)
@user.update_attribute(:remember_token, "old-token3")
post :create, :session => {
:email => @user.email,
:password => @user.password }
end

should_set_cookie("remember_token", "old-token3", nil)

teardown do
# restore default Clearance configuration
Clearance.configuration = nil
Clearance.configure {}
end
end

context "on POST to #create with good credentials and a session return url" do
setup do
@user = Factory(:email_confirmed_user)
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -39,7 +39,7 @@ def self.should_set_cookie(name, value, should_expire_at)
assert_contains cookie, regex, "cookie does not contain an 'expires=' attribute"
cookie =~ regex
expires_at = Time.parse($1)
assert_in_delta should_expire_at, expires_at, 100 # seconds -- arbitrary delta
assert_in_delta should_expire_at, expires_at, 100 # number of seconds we don't expect the test suite to exceed
else
assert_does_not_contain cookie, regex, "cookie contains an 'expires=' attribute but it shouldn't"
end
Expand Down

2 comments on commit 49efd7c

@andhapp
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerning lines 57 - 95
Sorry, I actually meant the tests where custom_duration is set to two different values, 2 weeks and nil.
Reason for two different tests is because nil is a special case, i.e. a session cookie. However, I think these two tests are testing if the remember_token gets assigned the value nil or the custom_duration. And the essence really is if the value gets assigned or not. I am just trying to clarify just for my personal knowledge.

@rnewman57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, three separate things are being tested here:

  • the unconfigured default behavior (which should be a 1 year expiration)
  • an explicitly configured (non-nil) expiration time
  • an explicitly configured nil expiration time (session cookie)

All three tests are important.

Please sign in to comment.