1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'account_controller'
4 # Re-raise errors caught by the controller.
5 class AccountController; def rescue_action(e) raise e end; end
7 class AccountControllerTest < Test::Unit::TestCase
8 # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead
9 # Then, you can remove it from this and the units test.
10 include AuthenticatedTestHelper
15 @controller = AccountController.new
16 @request = ActionController::TestRequest.new
17 @response = ActionController::TestResponse.new
20 def test_should_login_and_redirect
21 post :login, :login => 'quentin', :password => 'test'
23 assert_response :redirect
26 def test_should_fail_login_and_not_redirect
27 post :login, :login => 'quentin', :password => 'bad password'
28 assert_nil session[:user]
29 assert_nil session[:user_id]
30 assert_response :success
33 def test_should_allow_signup
34 assert_difference User, :count do
36 assert_response :redirect
40 def test_should_require_login_on_signup
41 assert_no_difference User, :count do
42 create_user(:login => nil)
43 assert assigns(:user).errors.on(:login)
44 assert_response :success
48 def test_should_require_password_on_signup
49 assert_no_difference User, :count do
50 create_user(:password => nil)
51 assert assigns(:user).errors.on(:password)
52 assert_response :success
56 def test_should_require_password_confirmation_on_signup
57 assert_no_difference User, :count do
58 create_user(:password_confirmation => nil)
59 assert assigns(:user).errors.on(:password_confirmation)
60 assert_response :success
64 def test_should_require_email_on_signup
65 assert_no_difference User, :count do
66 create_user(:email => nil)
67 assert assigns(:user).errors.on(:email)
68 assert_response :success
72 def test_should_logout
75 assert_nil session[:user]
76 assert_nil session[:user_id]
77 assert_response :redirect
80 def test_should_remember_me
81 post :login, :login => 'quentin', :password => 'test', :remember_me => "1"
82 assert_not_nil @response.cookies["auth_token"]
85 def test_should_not_remember_me
86 post :login, :login => 'quentin', :password => 'test', :remember_me => "0"
87 assert_nil @response.cookies["auth_token"]
90 def test_should_delete_token_on_logout
93 assert_equal @response.cookies["auth_token"], []
96 def test_should_login_with_cookie
97 users(:quentin).remember_me
98 @request.cookies["auth_token"] = cookie_for(:quentin)
100 assert @controller.send(:logged_in?)
103 def test_should_fail_expired_cookie_login
104 users(:quentin).remember_me
105 users(:quentin).update_attribute :remember_token_expires_at, 5.minutes.ago
106 @request.cookies["auth_token"] = cookie_for(:quentin)
108 assert !@controller.send(:logged_in?)
111 def test_should_fail_cookie_login
112 users(:quentin).remember_me
113 @request.cookies["auth_token"] = auth_token('invalid_auth_token')
115 assert !@controller.send(:logged_in?)
119 def create_user(options = {})
120 post :signup, :user => { :login => 'quire', :email => 'quire@example.com',
121 :password => 'quire', :password_confirmation => 'quire' }.merge(options)
124 def auth_token(token)
125 CGI::Cookie.new('name' => 'auth_token', 'value' => token)
129 auth_token users(user).remember_token