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_response :success
32 def test_should_allow_signup
33 assert_difference User, :count do
35 assert_response :redirect
39 def test_should_require_login_on_signup
40 assert_no_difference User, :count do
41 create_user(:login => nil)
42 assert assigns(:user).errors.on(:login)
43 assert_response :success
47 def test_should_require_password_on_signup
48 assert_no_difference User, :count do
49 create_user(:password => nil)
50 assert assigns(:user).errors.on(:password)
51 assert_response :success
55 def test_should_require_password_confirmation_on_signup
56 assert_no_difference User, :count do
57 create_user(:password_confirmation => nil)
58 assert assigns(:user).errors.on(:password_confirmation)
59 assert_response :success
63 def test_should_require_email_on_signup
64 assert_no_difference User, :count do
65 create_user(:email => nil)
66 assert assigns(:user).errors.on(:email)
67 assert_response :success
71 def test_should_logout
74 assert_nil session[:user]
75 assert_response :redirect
78 def test_should_remember_me
79 post :login, :login => 'quentin', :password => 'test', :remember_me => "1"
80 assert_not_nil @response.cookies["auth_token"]
83 def test_should_not_remember_me
84 post :login, :login => 'quentin', :password => 'test', :remember_me => "0"
85 assert_nil @response.cookies["auth_token"]
88 def test_should_delete_token_on_logout
91 assert_equal @response.cookies["auth_token"], []
94 def test_should_login_with_cookie
95 users(:quentin).remember_me
96 @request.cookies["auth_token"] = cookie_for(:quentin)
98 assert @controller.send(:logged_in?)
101 def test_should_fail_expired_cookie_login
102 users(:quentin).remember_me
103 users(:quentin).update_attribute :remember_token_expires_at, 5.minutes.ago
104 @request.cookies["auth_token"] = cookie_for(:quentin)
106 assert !@controller.send(:logged_in?)
109 def test_should_fail_cookie_login
110 users(:quentin).remember_me
111 @request.cookies["auth_token"] = auth_token('invalid_auth_token')
113 assert !@controller.send(:logged_in?)
117 def create_user(options = {})
118 post :signup, :user => { :login => 'quire', :email => 'quire@example.com',
119 :password => 'quire', :password_confirmation => 'quire' }.merge(options)
122 def auth_token(token)
123 CGI::Cookie.new('name' => 'auth_token', 'value' => token)
127 auth_token users(user).remember_token