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