To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 2d / 2d6ad8e6f87f6d379c227b9a81e69b5a26399856.svn-base @ 1298:4f746d8966dd
History | View | Annotate | Download (8.24 KB)
| 1 |
# Redmine - project management software |
|---|---|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU General Public License |
| 6 |
# as published by the Free Software Foundation; either version 2 |
| 7 |
# of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
|
| 18 |
require File.expand_path('../../test_helper', __FILE__)
|
| 19 |
require 'account_controller' |
| 20 |
|
| 21 |
# Re-raise errors caught by the controller. |
| 22 |
class AccountController; def rescue_action(e) raise e end; end |
| 23 |
|
| 24 |
class AccountControllerTest < ActionController::TestCase |
| 25 |
fixtures :users, :roles |
| 26 |
|
| 27 |
def setup |
| 28 |
@controller = AccountController.new |
| 29 |
@request = ActionController::TestRequest.new |
| 30 |
@response = ActionController::TestResponse.new |
| 31 |
User.current = nil |
| 32 |
end |
| 33 |
|
| 34 |
def test_get_login |
| 35 |
get :login |
| 36 |
assert_response :success |
| 37 |
assert_template 'login' |
| 38 |
|
| 39 |
assert_select 'input[name=username]' |
| 40 |
assert_select 'input[name=password]' |
| 41 |
end |
| 42 |
|
| 43 |
def test_login_should_redirect_to_back_url_param |
| 44 |
# request.uri is "test.host" in test environment |
| 45 |
post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1' |
| 46 |
assert_redirected_to '/issues/show/1' |
| 47 |
end |
| 48 |
|
| 49 |
def test_login_should_not_redirect_to_another_host |
| 50 |
post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake' |
| 51 |
assert_redirected_to '/my/page' |
| 52 |
end |
| 53 |
|
| 54 |
def test_login_with_wrong_password |
| 55 |
post :login, :username => 'admin', :password => 'bad' |
| 56 |
assert_response :success |
| 57 |
assert_template 'login' |
| 58 |
|
| 59 |
assert_select 'div.flash.error', :text => /Invalid user or password/ |
| 60 |
assert_select 'input[name=username][value=admin]' |
| 61 |
assert_select 'input[name=password]' |
| 62 |
assert_select 'input[name=password][value]', 0 |
| 63 |
end |
| 64 |
|
| 65 |
def test_login_should_rescue_auth_source_exception |
| 66 |
source = AuthSource.create!(:name => 'Test') |
| 67 |
User.find(2).update_attribute :auth_source_id, source.id |
| 68 |
AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
|
| 69 |
|
| 70 |
post :login, :username => 'jsmith', :password => 'jsmith' |
| 71 |
assert_response 500 |
| 72 |
assert_error_tag :content => /Something wrong/ |
| 73 |
end |
| 74 |
|
| 75 |
def test_login_should_reset_session |
| 76 |
@controller.expects(:reset_session).once |
| 77 |
|
| 78 |
post :login, :username => 'jsmith', :password => 'jsmith' |
| 79 |
assert_response 302 |
| 80 |
end |
| 81 |
|
| 82 |
def test_logout |
| 83 |
@request.session[:user_id] = 2 |
| 84 |
get :logout |
| 85 |
assert_redirected_to '/' |
| 86 |
assert_nil @request.session[:user_id] |
| 87 |
end |
| 88 |
|
| 89 |
def test_logout_should_reset_session |
| 90 |
@controller.expects(:reset_session).once |
| 91 |
|
| 92 |
@request.session[:user_id] = 2 |
| 93 |
get :logout |
| 94 |
assert_response 302 |
| 95 |
end |
| 96 |
|
| 97 |
def test_get_register_with_registration_on |
| 98 |
with_settings :self_registration => '3' do |
| 99 |
get :register |
| 100 |
assert_response :success |
| 101 |
assert_template 'register' |
| 102 |
assert_not_nil assigns(:user) |
| 103 |
|
| 104 |
assert_tag 'input', :attributes => {:name => 'user[password]'}
|
| 105 |
assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
|
| 106 |
end |
| 107 |
end |
| 108 |
|
| 109 |
def test_get_register_with_registration_off_should_redirect |
| 110 |
with_settings :self_registration => '0' do |
| 111 |
get :register |
| 112 |
assert_redirected_to '/' |
| 113 |
end |
| 114 |
end |
| 115 |
|
| 116 |
# See integration/account_test.rb for the full test |
| 117 |
def test_post_register_with_registration_on |
| 118 |
with_settings :self_registration => '3' do |
| 119 |
assert_difference 'User.count' do |
| 120 |
post :register, :user => {
|
| 121 |
:login => 'register', |
| 122 |
:password => 'secret123', |
| 123 |
:password_confirmation => 'secret123', |
| 124 |
:firstname => 'John', |
| 125 |
:lastname => 'Doe', |
| 126 |
:mail => 'register@example.com' |
| 127 |
} |
| 128 |
assert_redirected_to '/my/account' |
| 129 |
end |
| 130 |
user = User.first(:order => 'id DESC') |
| 131 |
assert_equal 'register', user.login |
| 132 |
assert_equal 'John', user.firstname |
| 133 |
assert_equal 'Doe', user.lastname |
| 134 |
assert_equal 'register@example.com', user.mail |
| 135 |
assert user.check_password?('secret123')
|
| 136 |
assert user.active? |
| 137 |
end |
| 138 |
end |
| 139 |
|
| 140 |
def test_post_register_with_registration_off_should_redirect |
| 141 |
with_settings :self_registration => '0' do |
| 142 |
assert_no_difference 'User.count' do |
| 143 |
post :register, :user => {
|
| 144 |
:login => 'register', |
| 145 |
:password => 'test', |
| 146 |
:password_confirmation => 'test', |
| 147 |
:firstname => 'John', |
| 148 |
:lastname => 'Doe', |
| 149 |
:mail => 'register@example.com' |
| 150 |
} |
| 151 |
assert_redirected_to '/' |
| 152 |
end |
| 153 |
end |
| 154 |
end |
| 155 |
|
| 156 |
def test_get_lost_password_should_display_lost_password_form |
| 157 |
get :lost_password |
| 158 |
assert_response :success |
| 159 |
assert_select 'input[name=mail]' |
| 160 |
end |
| 161 |
|
| 162 |
def test_lost_password_for_active_user_should_create_a_token |
| 163 |
Token.delete_all |
| 164 |
ActionMailer::Base.deliveries.clear |
| 165 |
assert_difference 'ActionMailer::Base.deliveries.size' do |
| 166 |
assert_difference 'Token.count' do |
| 167 |
with_settings :host_name => 'mydomain.foo', :protocol => 'http' do |
| 168 |
post :lost_password, :mail => 'JSmith@somenet.foo' |
| 169 |
assert_redirected_to '/login' |
| 170 |
end |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
token = Token.order('id DESC').first
|
| 175 |
assert_equal User.find(2), token.user |
| 176 |
assert_equal 'recovery', token.action |
| 177 |
|
| 178 |
assert_select_email do |
| 179 |
assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
|
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
def test_lost_password_for_unknown_user_should_fail |
| 184 |
Token.delete_all |
| 185 |
assert_no_difference 'Token.count' do |
| 186 |
post :lost_password, :mail => 'invalid@somenet.foo' |
| 187 |
assert_response :success |
| 188 |
end |
| 189 |
end |
| 190 |
|
| 191 |
def test_lost_password_for_non_active_user_should_fail |
| 192 |
Token.delete_all |
| 193 |
assert User.find(2).lock! |
| 194 |
|
| 195 |
assert_no_difference 'Token.count' do |
| 196 |
post :lost_password, :mail => 'JSmith@somenet.foo' |
| 197 |
assert_response :success |
| 198 |
end |
| 199 |
end |
| 200 |
|
| 201 |
def test_get_lost_password_with_token_should_display_the_password_recovery_form |
| 202 |
user = User.find(2) |
| 203 |
token = Token.create!(:action => 'recovery', :user => user) |
| 204 |
|
| 205 |
get :lost_password, :token => token.value |
| 206 |
assert_response :success |
| 207 |
assert_template 'password_recovery' |
| 208 |
|
| 209 |
assert_select 'input[type=hidden][name=token][value=?]', token.value |
| 210 |
end |
| 211 |
|
| 212 |
def test_get_lost_password_with_invalid_token_should_redirect |
| 213 |
get :lost_password, :token => "abcdef" |
| 214 |
assert_redirected_to '/' |
| 215 |
end |
| 216 |
|
| 217 |
def test_post_lost_password_with_token_should_change_the_user_password |
| 218 |
user = User.find(2) |
| 219 |
token = Token.create!(:action => 'recovery', :user => user) |
| 220 |
|
| 221 |
post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
| 222 |
assert_redirected_to '/login' |
| 223 |
user.reload |
| 224 |
assert user.check_password?('newpass123')
|
| 225 |
assert_nil Token.find_by_id(token.id), "Token was not deleted" |
| 226 |
end |
| 227 |
|
| 228 |
def test_post_lost_password_with_token_for_non_active_user_should_fail |
| 229 |
user = User.find(2) |
| 230 |
token = Token.create!(:action => 'recovery', :user => user) |
| 231 |
user.lock! |
| 232 |
|
| 233 |
post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' |
| 234 |
assert_redirected_to '/' |
| 235 |
assert ! user.check_password?('newpass123')
|
| 236 |
end |
| 237 |
|
| 238 |
def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form |
| 239 |
user = User.find(2) |
| 240 |
token = Token.create!(:action => 'recovery', :user => user) |
| 241 |
|
| 242 |
post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' |
| 243 |
assert_response :success |
| 244 |
assert_template 'password_recovery' |
| 245 |
assert_not_nil Token.find_by_id(token.id), "Token was deleted" |
| 246 |
|
| 247 |
assert_select 'input[type=hidden][name=token][value=?]', token.value |
| 248 |
end |
| 249 |
|
| 250 |
def test_post_lost_password_with_invalid_token_should_redirect |
| 251 |
post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' |
| 252 |
assert_redirected_to '/' |
| 253 |
end |
| 254 |
end |