##// END OF EJS Templates
Missing fixtures....
Jean-Philippe Lang -
r13507:193bb9362de0
parent child
Show More
@@ -1,424 +1,424
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class AccountControllerTest < ActionController::TestCase
21 fixtures :users, :roles
21 fixtures :users, :email_addresses, :roles
22 22
23 23 def setup
24 24 User.current = nil
25 25 end
26 26
27 27 def test_get_login
28 28 get :login
29 29 assert_response :success
30 30 assert_template 'login'
31 31
32 32 assert_select 'input[name=username]'
33 33 assert_select 'input[name=password]'
34 34 end
35 35
36 36 def test_get_login_while_logged_in_should_redirect_to_back_url_if_present
37 37 @request.session[:user_id] = 2
38 38 @request.env["HTTP_REFERER"] = 'http://test.host/issues/show/1'
39 39
40 40 get :login, :back_url => 'http://test.host/issues/show/1'
41 41 assert_redirected_to '/issues/show/1'
42 42 assert_equal 2, @request.session[:user_id]
43 43 end
44 44
45 45 def test_get_login_while_logged_in_should_redirect_to_referer_without_back_url
46 46 @request.session[:user_id] = 2
47 47 @request.env["HTTP_REFERER"] = 'http://test.host/issues/show/1'
48 48
49 49 get :login
50 50 assert_redirected_to '/issues/show/1'
51 51 assert_equal 2, @request.session[:user_id]
52 52 end
53 53
54 54 def test_get_login_while_logged_in_should_redirect_to_home_by_default
55 55 @request.session[:user_id] = 2
56 56
57 57 get :login
58 58 assert_redirected_to '/'
59 59 assert_equal 2, @request.session[:user_id]
60 60 end
61 61
62 62 def test_login_should_redirect_to_back_url_param
63 63 # request.uri is "test.host" in test environment
64 64 back_urls = [
65 65 'http://test.host/issues/show/1',
66 66 '/'
67 67 ]
68 68 back_urls.each do |back_url|
69 69 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
70 70 assert_redirected_to back_url
71 71 end
72 72 end
73 73
74 74 def test_login_with_suburi_should_redirect_to_back_url_param
75 75 @relative_url_root = Redmine::Utils.relative_url_root
76 76 Redmine::Utils.relative_url_root = '/redmine'
77 77
78 78 back_urls = [
79 79 'http://test.host/redmine/issues/show/1',
80 80 '/redmine'
81 81 ]
82 82 back_urls.each do |back_url|
83 83 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
84 84 assert_redirected_to back_url
85 85 end
86 86 ensure
87 87 Redmine::Utils.relative_url_root = @relative_url_root
88 88 end
89 89
90 90 def test_login_should_not_redirect_to_another_host
91 91 back_urls = [
92 92 'http://test.foo/fake',
93 93 '//test.foo/fake'
94 94 ]
95 95 back_urls.each do |back_url|
96 96 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
97 97 assert_redirected_to '/my/page'
98 98 end
99 99 end
100 100
101 101 def test_login_with_suburi_should_not_redirect_to_another_suburi
102 102 @relative_url_root = Redmine::Utils.relative_url_root
103 103 Redmine::Utils.relative_url_root = '/redmine'
104 104
105 105 back_urls = [
106 106 'http://test.host/',
107 107 'http://test.host/fake',
108 108 'http://test.host/fake/issues',
109 109 'http://test.host/redmine/../fake',
110 110 'http://test.host/redmine/../fake/issues',
111 111 'http://test.host/redmine/%2e%2e/fake'
112 112 ]
113 113 back_urls.each do |back_url|
114 114 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => back_url
115 115 assert_redirected_to '/my/page'
116 116 end
117 117 ensure
118 118 Redmine::Utils.relative_url_root = @relative_url_root
119 119 end
120 120
121 121 def test_login_with_wrong_password
122 122 post :login, :username => 'admin', :password => 'bad'
123 123 assert_response :success
124 124 assert_template 'login'
125 125
126 126 assert_select 'div.flash.error', :text => /Invalid user or password/
127 127 assert_select 'input[name=username][value=admin]'
128 128 assert_select 'input[name=password]'
129 129 assert_select 'input[name=password][value]', 0
130 130 end
131 131
132 132 def test_login_with_locked_account_should_fail
133 133 User.find(2).update_attribute :status, User::STATUS_LOCKED
134 134
135 135 post :login, :username => 'jsmith', :password => 'jsmith'
136 136 assert_redirected_to '/login'
137 137 assert_include 'locked', flash[:error]
138 138 assert_nil @request.session[:user_id]
139 139 end
140 140
141 141 def test_login_as_registered_user_with_manual_activation_should_inform_user
142 142 User.find(2).update_attribute :status, User::STATUS_REGISTERED
143 143
144 144 with_settings :self_registration => '2', :default_language => 'en' do
145 145 post :login, :username => 'jsmith', :password => 'jsmith'
146 146 assert_redirected_to '/login'
147 147 assert_include 'pending administrator approval', flash[:error]
148 148 end
149 149 end
150 150
151 151 def test_login_as_registered_user_with_email_activation_should_propose_new_activation_email
152 152 User.find(2).update_attribute :status, User::STATUS_REGISTERED
153 153
154 154 with_settings :self_registration => '1', :default_language => 'en' do
155 155 post :login, :username => 'jsmith', :password => 'jsmith'
156 156 assert_redirected_to '/login'
157 157 assert_equal 2, @request.session[:registered_user_id]
158 158 assert_include 'new activation email', flash[:error]
159 159 end
160 160 end
161 161
162 162 def test_login_should_rescue_auth_source_exception
163 163 source = AuthSource.create!(:name => 'Test')
164 164 User.find(2).update_attribute :auth_source_id, source.id
165 165 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
166 166
167 167 post :login, :username => 'jsmith', :password => 'jsmith'
168 168 assert_response 500
169 169 assert_select_error /Something wrong/
170 170 end
171 171
172 172 def test_login_should_reset_session
173 173 @controller.expects(:reset_session).once
174 174
175 175 post :login, :username => 'jsmith', :password => 'jsmith'
176 176 assert_response 302
177 177 end
178 178
179 179 def test_get_logout_should_not_logout
180 180 @request.session[:user_id] = 2
181 181 get :logout
182 182 assert_response :success
183 183 assert_template 'logout'
184 184
185 185 assert_equal 2, @request.session[:user_id]
186 186 end
187 187
188 188 def test_get_logout_with_anonymous_should_redirect
189 189 get :logout
190 190 assert_redirected_to '/'
191 191 end
192 192
193 193 def test_logout
194 194 @request.session[:user_id] = 2
195 195 post :logout
196 196 assert_redirected_to '/'
197 197 assert_nil @request.session[:user_id]
198 198 end
199 199
200 200 def test_logout_should_reset_session
201 201 @controller.expects(:reset_session).once
202 202
203 203 @request.session[:user_id] = 2
204 204 post :logout
205 205 assert_response 302
206 206 end
207 207
208 208 def test_get_register_with_registration_on
209 209 with_settings :self_registration => '3' do
210 210 get :register
211 211 assert_response :success
212 212 assert_template 'register'
213 213 assert_not_nil assigns(:user)
214 214
215 215 assert_select 'input[name=?]', 'user[password]'
216 216 assert_select 'input[name=?]', 'user[password_confirmation]'
217 217 end
218 218 end
219 219
220 220 def test_get_register_should_detect_user_language
221 221 with_settings :self_registration => '3' do
222 222 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
223 223 get :register
224 224 assert_response :success
225 225 assert_not_nil assigns(:user)
226 226 assert_equal 'fr', assigns(:user).language
227 227 assert_select 'select[name=?]', 'user[language]' do
228 228 assert_select 'option[value=fr][selected=selected]'
229 229 end
230 230 end
231 231 end
232 232
233 233 def test_get_register_with_registration_off_should_redirect
234 234 with_settings :self_registration => '0' do
235 235 get :register
236 236 assert_redirected_to '/'
237 237 end
238 238 end
239 239
240 240 # See integration/account_test.rb for the full test
241 241 def test_post_register_with_registration_on
242 242 with_settings :self_registration => '3' do
243 243 assert_difference 'User.count' do
244 244 post :register, :user => {
245 245 :login => 'register',
246 246 :password => 'secret123',
247 247 :password_confirmation => 'secret123',
248 248 :firstname => 'John',
249 249 :lastname => 'Doe',
250 250 :mail => 'register@example.com'
251 251 }
252 252 assert_redirected_to '/my/account'
253 253 end
254 254 user = User.order('id DESC').first
255 255 assert_equal 'register', user.login
256 256 assert_equal 'John', user.firstname
257 257 assert_equal 'Doe', user.lastname
258 258 assert_equal 'register@example.com', user.mail
259 259 assert user.check_password?('secret123')
260 260 assert user.active?
261 261 end
262 262 end
263 263
264 264 def test_post_register_with_registration_off_should_redirect
265 265 with_settings :self_registration => '0' do
266 266 assert_no_difference 'User.count' do
267 267 post :register, :user => {
268 268 :login => 'register',
269 269 :password => 'test',
270 270 :password_confirmation => 'test',
271 271 :firstname => 'John',
272 272 :lastname => 'Doe',
273 273 :mail => 'register@example.com'
274 274 }
275 275 assert_redirected_to '/'
276 276 end
277 277 end
278 278 end
279 279
280 280 def test_get_lost_password_should_display_lost_password_form
281 281 get :lost_password
282 282 assert_response :success
283 283 assert_select 'input[name=mail]'
284 284 end
285 285
286 286 def test_lost_password_for_active_user_should_create_a_token
287 287 Token.delete_all
288 288 ActionMailer::Base.deliveries.clear
289 289 assert_difference 'ActionMailer::Base.deliveries.size' do
290 290 assert_difference 'Token.count' do
291 291 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
292 292 post :lost_password, :mail => 'JSmith@somenet.foo'
293 293 assert_redirected_to '/login'
294 294 end
295 295 end
296 296 end
297 297
298 298 token = Token.order('id DESC').first
299 299 assert_equal User.find(2), token.user
300 300 assert_equal 'recovery', token.action
301 301
302 302 assert_select_email do
303 303 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
304 304 end
305 305 end
306 306
307 307 def test_lost_password_using_additional_email_address_should_send_email_to_the_address
308 308 EmailAddress.create!(:user_id => 2, :address => 'anotherAddress@foo.bar')
309 309 Token.delete_all
310 310
311 311 assert_difference 'ActionMailer::Base.deliveries.size' do
312 312 assert_difference 'Token.count' do
313 313 post :lost_password, :mail => 'ANOTHERaddress@foo.bar'
314 314 assert_redirected_to '/login'
315 315 end
316 316 end
317 317 mail = ActionMailer::Base.deliveries.last
318 318 assert_equal ['anotherAddress@foo.bar'], mail.bcc
319 319 end
320 320
321 321 def test_lost_password_for_unknown_user_should_fail
322 322 Token.delete_all
323 323 assert_no_difference 'Token.count' do
324 324 post :lost_password, :mail => 'invalid@somenet.foo'
325 325 assert_response :success
326 326 end
327 327 end
328 328
329 329 def test_lost_password_for_non_active_user_should_fail
330 330 Token.delete_all
331 331 assert User.find(2).lock!
332 332
333 333 assert_no_difference 'Token.count' do
334 334 post :lost_password, :mail => 'JSmith@somenet.foo'
335 335 assert_redirected_to '/account/lost_password'
336 336 end
337 337 end
338 338
339 339 def test_lost_password_for_user_who_cannot_change_password_should_fail
340 340 User.any_instance.stubs(:change_password_allowed?).returns(false)
341 341
342 342 assert_no_difference 'Token.count' do
343 343 post :lost_password, :mail => 'JSmith@somenet.foo'
344 344 assert_response :success
345 345 end
346 346 end
347 347
348 348 def test_get_lost_password_with_token_should_display_the_password_recovery_form
349 349 user = User.find(2)
350 350 token = Token.create!(:action => 'recovery', :user => user)
351 351
352 352 get :lost_password, :token => token.value
353 353 assert_response :success
354 354 assert_template 'password_recovery'
355 355
356 356 assert_select 'input[type=hidden][name=token][value=?]', token.value
357 357 end
358 358
359 359 def test_get_lost_password_with_invalid_token_should_redirect
360 360 get :lost_password, :token => "abcdef"
361 361 assert_redirected_to '/'
362 362 end
363 363
364 364 def test_post_lost_password_with_token_should_change_the_user_password
365 365 user = User.find(2)
366 366 token = Token.create!(:action => 'recovery', :user => user)
367 367
368 368 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
369 369 assert_redirected_to '/login'
370 370 user.reload
371 371 assert user.check_password?('newpass123')
372 372 assert_nil Token.find_by_id(token.id), "Token was not deleted"
373 373 end
374 374
375 375 def test_post_lost_password_with_token_for_non_active_user_should_fail
376 376 user = User.find(2)
377 377 token = Token.create!(:action => 'recovery', :user => user)
378 378 user.lock!
379 379
380 380 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
381 381 assert_redirected_to '/'
382 382 assert ! user.check_password?('newpass123')
383 383 end
384 384
385 385 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
386 386 user = User.find(2)
387 387 token = Token.create!(:action => 'recovery', :user => user)
388 388
389 389 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
390 390 assert_response :success
391 391 assert_template 'password_recovery'
392 392 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
393 393
394 394 assert_select 'input[type=hidden][name=token][value=?]', token.value
395 395 end
396 396
397 397 def test_post_lost_password_with_invalid_token_should_redirect
398 398 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
399 399 assert_redirected_to '/'
400 400 end
401 401
402 402 def test_activation_email_should_send_an_activation_email
403 403 User.find(2).update_attribute :status, User::STATUS_REGISTERED
404 404 @request.session[:registered_user_id] = 2
405 405
406 406 with_settings :self_registration => '1' do
407 407 assert_difference 'ActionMailer::Base.deliveries.size' do
408 408 get :activation_email
409 409 assert_redirected_to '/login'
410 410 end
411 411 end
412 412 end
413 413
414 414 def test_activation_email_without_session_data_should_fail
415 415 User.find(2).update_attribute :status, User::STATUS_REGISTERED
416 416
417 417 with_settings :self_registration => '1' do
418 418 assert_no_difference 'ActionMailer::Base.deliveries.size' do
419 419 get :activation_email
420 420 assert_redirected_to '/'
421 421 end
422 422 end
423 423 end
424 424 end
@@ -1,64 +1,64
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class CommentsControllerTest < ActionController::TestCase
21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
21 fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles, :enabled_modules, :news, :comments
22 22
23 23 def setup
24 24 User.current = nil
25 25 end
26 26
27 27 def test_add_comment
28 28 @request.session[:user_id] = 2
29 29 post :create, :id => 1, :comment => { :comments => 'This is a test comment' }
30 30 assert_redirected_to '/news/1'
31 31
32 32 comment = News.find(1).comments.last
33 33 assert_not_nil comment
34 34 assert_equal 'This is a test comment', comment.comments
35 35 assert_equal User.find(2), comment.author
36 36 end
37 37
38 38 def test_empty_comment_should_not_be_added
39 39 @request.session[:user_id] = 2
40 40 assert_no_difference 'Comment.count' do
41 41 post :create, :id => 1, :comment => { :comments => '' }
42 42 assert_response :redirect
43 43 assert_redirected_to '/news/1'
44 44 end
45 45 end
46 46
47 47 def test_create_should_be_denied_if_news_is_not_commentable
48 48 News.any_instance.stubs(:commentable?).returns(false)
49 49 @request.session[:user_id] = 2
50 50 assert_no_difference 'Comment.count' do
51 51 post :create, :id => 1, :comment => { :comments => 'This is a test comment' }
52 52 assert_response 403
53 53 end
54 54 end
55 55
56 56 def test_destroy_comment
57 57 comments_count = News.find(1).comments.size
58 58 @request.session[:user_id] = 2
59 59 delete :destroy, :id => 1, :comment_id => 2
60 60 assert_redirected_to '/news/1'
61 61 assert_nil Comment.find_by_id(2)
62 62 assert_equal comments_count - 1, News.find(1).comments.size
63 63 end
64 64 end
General Comments 0
You need to be logged in to leave comments. Login now