##// END OF EJS Templates
replace "User#before_create" and "User.login" shoulda contexts at unit user test...
Toshi MARUYAMA -
r10329:df44af819c98
parent child
Show More
@@ -1,1077 +1,1071
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 UserTest < ActiveSupport::TestCase
21 21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
22 22 :trackers, :issue_statuses,
23 23 :projects_trackers,
24 24 :watchers,
25 25 :issue_categories, :enumerations, :issues,
26 26 :journals, :journal_details,
27 27 :groups_users,
28 28 :enabled_modules,
29 29 :workflows
30 30
31 31 def setup
32 32 @admin = User.find(1)
33 33 @jsmith = User.find(2)
34 34 @dlopper = User.find(3)
35 35 end
36 36
37 37 def test_generate
38 38 User.generate!(:firstname => 'Testing connection')
39 39 User.generate!(:firstname => 'Testing connection')
40 40 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
41 41 end
42 42
43 43 def test_truth
44 44 assert_kind_of User, @jsmith
45 45 end
46 46
47 47 def test_mail_should_be_stripped
48 48 u = User.new
49 49 u.mail = " foo@bar.com "
50 50 assert_equal "foo@bar.com", u.mail
51 51 end
52 52
53 53 def test_mail_validation
54 54 u = User.new
55 55 u.mail = ''
56 56 assert !u.valid?
57 57 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
58 58 end
59 59
60 60 def test_login_length_validation
61 61 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
62 62 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
63 63 assert !user.valid?
64 64
65 65 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
66 66 assert user.valid?
67 67 assert user.save
68 68 end
69 69
70 70 def test_create
71 71 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
72 72
73 73 user.login = "jsmith"
74 74 user.password, user.password_confirmation = "password", "password"
75 75 # login uniqueness
76 76 assert !user.save
77 77 assert_equal 1, user.errors.count
78 78
79 79 user.login = "newuser"
80 80 user.password, user.password_confirmation = "passwd", "password"
81 81 # password confirmation
82 82 assert !user.save
83 83 assert_equal 1, user.errors.count
84 84
85 85 user.password, user.password_confirmation = "password", "password"
86 86 assert user.save
87 87 end
88 88
89 context "User#before_create" do
90 should "set the mail_notification to the default Setting" do
91 @user1 = User.generate!
92 assert_equal 'only_my_events', @user1.mail_notification
93
94 with_settings :default_notification_option => 'all' do
95 @user2 = User.generate!
96 assert_equal 'all', @user2.mail_notification
97 end
89 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting
90 @user1 = User.generate!
91 assert_equal 'only_my_events', @user1.mail_notification
92 with_settings :default_notification_option => 'all' do
93 @user2 = User.generate!
94 assert_equal 'all', @user2.mail_notification
98 95 end
99 96 end
100 97
101 context "User.login" do
102 should "be case-insensitive." do
103 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
104 u.login = 'newuser'
105 u.password, u.password_confirmation = "password", "password"
106 assert u.save
107
108 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
109 u.login = 'NewUser'
110 u.password, u.password_confirmation = "password", "password"
111 assert !u.save
112 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
113 end
98 def test_user_login_be_case_insensitive
99 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
100 u.login = 'newuser'
101 u.password, u.password_confirmation = "password", "password"
102 assert u.save
103 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
104 u.login = 'NewUser'
105 u.password, u.password_confirmation = "password", "password"
106 assert !u.save
107 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
114 108 end
115 109
116 110 def test_mail_uniqueness_should_not_be_case_sensitive
117 111 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
118 112 u.login = 'newuser1'
119 113 u.password, u.password_confirmation = "password", "password"
120 114 assert u.save
121 115
122 116 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
123 117 u.login = 'newuser2'
124 118 u.password, u.password_confirmation = "password", "password"
125 119 assert !u.save
126 120 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
127 121 end
128 122
129 123 def test_update
130 124 assert_equal "admin", @admin.login
131 125 @admin.login = "john"
132 126 assert @admin.save, @admin.errors.full_messages.join("; ")
133 127 @admin.reload
134 128 assert_equal "john", @admin.login
135 129 end
136 130
137 131 def test_update_should_not_fail_for_legacy_user_with_different_case_logins
138 132 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
139 133 u1.login = 'newuser1'
140 134 assert u1.save
141 135
142 136 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
143 137 u2.login = 'newuser1'
144 138 assert u2.save(:validate => false)
145 139
146 140 user = User.find(u2.id)
147 141 user.firstname = "firstname"
148 142 assert user.save, "Save failed"
149 143 end
150 144
151 145 def test_destroy_should_delete_members_and_roles
152 146 members = Member.find_all_by_user_id(2)
153 147 ms = members.size
154 148 rs = members.collect(&:roles).flatten.size
155 149
156 150 assert_difference 'Member.count', - ms do
157 151 assert_difference 'MemberRole.count', - rs do
158 152 User.find(2).destroy
159 153 end
160 154 end
161 155
162 156 assert_nil User.find_by_id(2)
163 157 assert Member.find_all_by_user_id(2).empty?
164 158 end
165 159
166 160 def test_destroy_should_update_attachments
167 161 attachment = Attachment.create!(:container => Project.find(1),
168 162 :file => uploaded_test_file("testfile.txt", "text/plain"),
169 163 :author_id => 2)
170 164
171 165 User.find(2).destroy
172 166 assert_nil User.find_by_id(2)
173 167 assert_equal User.anonymous, attachment.reload.author
174 168 end
175 169
176 170 def test_destroy_should_update_comments
177 171 comment = Comment.create!(
178 172 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
179 173 :author => User.find(2),
180 174 :comments => 'foo'
181 175 )
182 176
183 177 User.find(2).destroy
184 178 assert_nil User.find_by_id(2)
185 179 assert_equal User.anonymous, comment.reload.author
186 180 end
187 181
188 182 def test_destroy_should_update_issues
189 183 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
190 184
191 185 User.find(2).destroy
192 186 assert_nil User.find_by_id(2)
193 187 assert_equal User.anonymous, issue.reload.author
194 188 end
195 189
196 190 def test_destroy_should_unassign_issues
197 191 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
198 192
199 193 User.find(2).destroy
200 194 assert_nil User.find_by_id(2)
201 195 assert_nil issue.reload.assigned_to
202 196 end
203 197
204 198 def test_destroy_should_update_journals
205 199 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
206 200 issue.init_journal(User.find(2), "update")
207 201 issue.save!
208 202
209 203 User.find(2).destroy
210 204 assert_nil User.find_by_id(2)
211 205 assert_equal User.anonymous, issue.journals.first.reload.user
212 206 end
213 207
214 208 def test_destroy_should_update_journal_details_old_value
215 209 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
216 210 issue.init_journal(User.find(1), "update")
217 211 issue.assigned_to_id = nil
218 212 assert_difference 'JournalDetail.count' do
219 213 issue.save!
220 214 end
221 215 journal_detail = JournalDetail.first(:order => 'id DESC')
222 216 assert_equal '2', journal_detail.old_value
223 217
224 218 User.find(2).destroy
225 219 assert_nil User.find_by_id(2)
226 220 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
227 221 end
228 222
229 223 def test_destroy_should_update_journal_details_value
230 224 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
231 225 issue.init_journal(User.find(1), "update")
232 226 issue.assigned_to_id = 2
233 227 assert_difference 'JournalDetail.count' do
234 228 issue.save!
235 229 end
236 230 journal_detail = JournalDetail.first(:order => 'id DESC')
237 231 assert_equal '2', journal_detail.value
238 232
239 233 User.find(2).destroy
240 234 assert_nil User.find_by_id(2)
241 235 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
242 236 end
243 237
244 238 def test_destroy_should_update_messages
245 239 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
246 240 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
247 241
248 242 User.find(2).destroy
249 243 assert_nil User.find_by_id(2)
250 244 assert_equal User.anonymous, message.reload.author
251 245 end
252 246
253 247 def test_destroy_should_update_news
254 248 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
255 249
256 250 User.find(2).destroy
257 251 assert_nil User.find_by_id(2)
258 252 assert_equal User.anonymous, news.reload.author
259 253 end
260 254
261 255 def test_destroy_should_delete_private_queries
262 256 query = Query.new(:name => 'foo', :is_public => false)
263 257 query.project_id = 1
264 258 query.user_id = 2
265 259 query.save!
266 260
267 261 User.find(2).destroy
268 262 assert_nil User.find_by_id(2)
269 263 assert_nil Query.find_by_id(query.id)
270 264 end
271 265
272 266 def test_destroy_should_update_public_queries
273 267 query = Query.new(:name => 'foo', :is_public => true)
274 268 query.project_id = 1
275 269 query.user_id = 2
276 270 query.save!
277 271
278 272 User.find(2).destroy
279 273 assert_nil User.find_by_id(2)
280 274 assert_equal User.anonymous, query.reload.user
281 275 end
282 276
283 277 def test_destroy_should_update_time_entries
284 278 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
285 279 entry.project_id = 1
286 280 entry.user_id = 2
287 281 entry.save!
288 282
289 283 User.find(2).destroy
290 284 assert_nil User.find_by_id(2)
291 285 assert_equal User.anonymous, entry.reload.user
292 286 end
293 287
294 288 def test_destroy_should_delete_tokens
295 289 token = Token.create!(:user_id => 2, :value => 'foo')
296 290
297 291 User.find(2).destroy
298 292 assert_nil User.find_by_id(2)
299 293 assert_nil Token.find_by_id(token.id)
300 294 end
301 295
302 296 def test_destroy_should_delete_watchers
303 297 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
304 298 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
305 299
306 300 User.find(2).destroy
307 301 assert_nil User.find_by_id(2)
308 302 assert_nil Watcher.find_by_id(watcher.id)
309 303 end
310 304
311 305 def test_destroy_should_update_wiki_contents
312 306 wiki_content = WikiContent.create!(
313 307 :text => 'foo',
314 308 :author_id => 2,
315 309 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
316 310 )
317 311 wiki_content.text = 'bar'
318 312 assert_difference 'WikiContent::Version.count' do
319 313 wiki_content.save!
320 314 end
321 315
322 316 User.find(2).destroy
323 317 assert_nil User.find_by_id(2)
324 318 assert_equal User.anonymous, wiki_content.reload.author
325 319 wiki_content.versions.each do |version|
326 320 assert_equal User.anonymous, version.reload.author
327 321 end
328 322 end
329 323
330 324 def test_destroy_should_nullify_issue_categories
331 325 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
332 326
333 327 User.find(2).destroy
334 328 assert_nil User.find_by_id(2)
335 329 assert_nil category.reload.assigned_to_id
336 330 end
337 331
338 332 def test_destroy_should_nullify_changesets
339 333 changeset = Changeset.create!(
340 334 :repository => Repository::Subversion.create!(
341 335 :project_id => 1,
342 336 :url => 'file:///tmp',
343 337 :identifier => 'tmp'
344 338 ),
345 339 :revision => '12',
346 340 :committed_on => Time.now,
347 341 :committer => 'jsmith'
348 342 )
349 343 assert_equal 2, changeset.user_id
350 344
351 345 User.find(2).destroy
352 346 assert_nil User.find_by_id(2)
353 347 assert_nil changeset.reload.user_id
354 348 end
355 349
356 350 def test_anonymous_user_should_not_be_destroyable
357 351 assert_no_difference 'User.count' do
358 352 assert_equal false, User.anonymous.destroy
359 353 end
360 354 end
361 355
362 356 def test_validate_login_presence
363 357 @admin.login = ""
364 358 assert !@admin.save
365 359 assert_equal 1, @admin.errors.count
366 360 end
367 361
368 362 def test_validate_mail_notification_inclusion
369 363 u = User.new
370 364 u.mail_notification = 'foo'
371 365 u.save
372 366 assert_not_nil u.errors[:mail_notification]
373 367 end
374 368
375 369 context "User#try_to_login" do
376 370 should "fall-back to case-insensitive if user login is not found as-typed." do
377 371 user = User.try_to_login("AdMin", "admin")
378 372 assert_kind_of User, user
379 373 assert_equal "admin", user.login
380 374 end
381 375
382 376 should "select the exact matching user first" do
383 377 case_sensitive_user = User.generate! do |user|
384 378 user.password = "admin"
385 379 end
386 380 # bypass validations to make it appear like existing data
387 381 case_sensitive_user.update_attribute(:login, 'ADMIN')
388 382
389 383 user = User.try_to_login("ADMIN", "admin")
390 384 assert_kind_of User, user
391 385 assert_equal "ADMIN", user.login
392 386
393 387 end
394 388 end
395 389
396 390 def test_password
397 391 user = User.try_to_login("admin", "admin")
398 392 assert_kind_of User, user
399 393 assert_equal "admin", user.login
400 394 user.password = "hello"
401 395 assert user.save
402 396
403 397 user = User.try_to_login("admin", "hello")
404 398 assert_kind_of User, user
405 399 assert_equal "admin", user.login
406 400 end
407 401
408 402 def test_validate_password_length
409 403 with_settings :password_min_length => '100' do
410 404 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
411 405 user.login = "newuser100"
412 406 user.password, user.password_confirmation = "password100", "password100"
413 407 assert !user.save
414 408 assert_equal 1, user.errors.count
415 409 end
416 410 end
417 411
418 412 def test_name_format
419 413 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
420 414 with_settings :user_format => :firstname_lastname do
421 415 assert_equal 'John Smith', @jsmith.reload.name
422 416 end
423 417 with_settings :user_format => :username do
424 418 assert_equal 'jsmith', @jsmith.reload.name
425 419 end
426 420 end
427 421
428 422 def test_today_should_return_the_day_according_to_user_time_zone
429 423 preference = User.find(1).pref
430 424 date = Date.new(2012, 05, 15)
431 425 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
432 426 Date.stubs(:today).returns(date)
433 427 Time.stubs(:now).returns(time)
434 428
435 429 preference.update_attribute :time_zone, 'Baku' # UTC+4
436 430 assert_equal '2012-05-16', User.find(1).today.to_s
437 431
438 432 preference.update_attribute :time_zone, 'La Paz' # UTC-4
439 433 assert_equal '2012-05-15', User.find(1).today.to_s
440 434
441 435 preference.update_attribute :time_zone, ''
442 436 assert_equal '2012-05-15', User.find(1).today.to_s
443 437 end
444 438
445 439 def test_time_to_date_should_return_the_date_according_to_user_time_zone
446 440 preference = User.find(1).pref
447 441 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
448 442
449 443 preference.update_attribute :time_zone, 'Baku' # UTC+4
450 444 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s
451 445
452 446 preference.update_attribute :time_zone, 'La Paz' # UTC-4
453 447 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
454 448
455 449 preference.update_attribute :time_zone, ''
456 450 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
457 451 end
458 452
459 453 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
460 454 with_settings :user_format => 'lastname_coma_firstname' do
461 455 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
462 456 end
463 457 end
464 458
465 459 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
466 460 with_settings :user_format => 'lastname_firstname' do
467 461 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
468 462 end
469 463 end
470 464
471 465 def test_fields_for_order_statement_with_blank_format_should_return_default
472 466 with_settings :user_format => '' do
473 467 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
474 468 end
475 469 end
476 470
477 471 def test_fields_for_order_statement_with_invalid_format_should_return_default
478 472 with_settings :user_format => 'foo' do
479 473 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
480 474 end
481 475 end
482 476
483 477 def test_lock
484 478 user = User.try_to_login("jsmith", "jsmith")
485 479 assert_equal @jsmith, user
486 480
487 481 @jsmith.status = User::STATUS_LOCKED
488 482 assert @jsmith.save
489 483
490 484 user = User.try_to_login("jsmith", "jsmith")
491 485 assert_equal nil, user
492 486 end
493 487
494 488 context ".try_to_login" do
495 489 context "with good credentials" do
496 490 should "return the user" do
497 491 user = User.try_to_login("admin", "admin")
498 492 assert_kind_of User, user
499 493 assert_equal "admin", user.login
500 494 end
501 495 end
502 496
503 497 context "with wrong credentials" do
504 498 should "return nil" do
505 499 assert_nil User.try_to_login("admin", "foo")
506 500 end
507 501 end
508 502 end
509 503
510 504 if ldap_configured?
511 505 context "#try_to_login using LDAP" do
512 506 context "with failed connection to the LDAP server" do
513 507 should "return nil" do
514 508 @auth_source = AuthSourceLdap.find(1)
515 509 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
516 510
517 511 assert_equal nil, User.try_to_login('edavis', 'wrong')
518 512 end
519 513 end
520 514
521 515 context "with an unsuccessful authentication" do
522 516 should "return nil" do
523 517 assert_equal nil, User.try_to_login('edavis', 'wrong')
524 518 end
525 519 end
526 520
527 521 context "binding with user's account" do
528 522 setup do
529 523 @auth_source = AuthSourceLdap.find(1)
530 524 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
531 525 @auth_source.account_password = ''
532 526 @auth_source.save!
533 527
534 528 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
535 529 @ldap_user.login = 'example1'
536 530 @ldap_user.save!
537 531 end
538 532
539 533 context "with a successful authentication" do
540 534 should "return the user" do
541 535 assert_equal @ldap_user, User.try_to_login('example1', '123456')
542 536 end
543 537 end
544 538
545 539 context "with an unsuccessful authentication" do
546 540 should "return nil" do
547 541 assert_nil User.try_to_login('example1', '11111')
548 542 end
549 543 end
550 544 end
551 545
552 546 context "on the fly registration" do
553 547 setup do
554 548 @auth_source = AuthSourceLdap.find(1)
555 549 @auth_source.update_attribute :onthefly_register, true
556 550 end
557 551
558 552 context "with a successful authentication" do
559 553 should "create a new user account if it doesn't exist" do
560 554 assert_difference('User.count') do
561 555 user = User.try_to_login('edavis', '123456')
562 556 assert !user.admin?
563 557 end
564 558 end
565 559
566 560 should "retrieve existing user" do
567 561 user = User.try_to_login('edavis', '123456')
568 562 user.admin = true
569 563 user.save!
570 564
571 565 assert_no_difference('User.count') do
572 566 user = User.try_to_login('edavis', '123456')
573 567 assert user.admin?
574 568 end
575 569 end
576 570 end
577 571
578 572 context "binding with user's account" do
579 573 setup do
580 574 @auth_source = AuthSourceLdap.find(1)
581 575 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
582 576 @auth_source.account_password = ''
583 577 @auth_source.save!
584 578 end
585 579
586 580 context "with a successful authentication" do
587 581 should "create a new user account if it doesn't exist" do
588 582 assert_difference('User.count') do
589 583 user = User.try_to_login('example1', '123456')
590 584 assert_kind_of User, user
591 585 end
592 586 end
593 587 end
594 588
595 589 context "with an unsuccessful authentication" do
596 590 should "return nil" do
597 591 assert_nil User.try_to_login('example1', '11111')
598 592 end
599 593 end
600 594 end
601 595 end
602 596 end
603 597
604 598 else
605 599 puts "Skipping LDAP tests."
606 600 end
607 601
608 602 def test_create_anonymous
609 603 AnonymousUser.delete_all
610 604 anon = User.anonymous
611 605 assert !anon.new_record?
612 606 assert_kind_of AnonymousUser, anon
613 607 end
614 608
615 609 def test_ensure_single_anonymous_user
616 610 AnonymousUser.delete_all
617 611 anon1 = User.anonymous
618 612 assert !anon1.new_record?
619 613 assert_kind_of AnonymousUser, anon1
620 614 anon2 = AnonymousUser.create(
621 615 :lastname => 'Anonymous', :firstname => '',
622 616 :mail => '', :login => '', :status => 0)
623 617 assert_equal 1, anon2.errors.count
624 618 end
625 619
626 620 def test_rss_key
627 621 assert_nil @jsmith.rss_token
628 622 key = @jsmith.rss_key
629 623 assert_equal 40, key.length
630 624
631 625 @jsmith.reload
632 626 assert_equal key, @jsmith.rss_key
633 627 end
634 628
635 629 def test_rss_key_should_not_be_generated_twice
636 630 assert_difference 'Token.count', 1 do
637 631 key1 = @jsmith.rss_key
638 632 key2 = @jsmith.rss_key
639 633 assert_equal key1, key2
640 634 end
641 635 end
642 636
643 637 def test_api_key_should_not_be_generated_twice
644 638 assert_difference 'Token.count', 1 do
645 639 key1 = @jsmith.api_key
646 640 key2 = @jsmith.api_key
647 641 assert_equal key1, key2
648 642 end
649 643 end
650 644
651 645 context "User#api_key" do
652 646 should "generate a new one if the user doesn't have one" do
653 647 user = User.generate!(:api_token => nil)
654 648 assert_nil user.api_token
655 649
656 650 key = user.api_key
657 651 assert_equal 40, key.length
658 652 user.reload
659 653 assert_equal key, user.api_key
660 654 end
661 655
662 656 should "return the existing api token value" do
663 657 user = User.generate!
664 658 token = Token.create!(:action => 'api')
665 659 user.api_token = token
666 660 assert user.save
667 661
668 662 assert_equal token.value, user.api_key
669 663 end
670 664 end
671 665
672 666 context "User#find_by_api_key" do
673 667 should "return nil if no matching key is found" do
674 668 assert_nil User.find_by_api_key('zzzzzzzzz')
675 669 end
676 670
677 671 should "return nil if the key is found for an inactive user" do
678 672 user = User.generate!
679 673 user.status = User::STATUS_LOCKED
680 674 token = Token.create!(:action => 'api')
681 675 user.api_token = token
682 676 user.save
683 677
684 678 assert_nil User.find_by_api_key(token.value)
685 679 end
686 680
687 681 should "return the user if the key is found for an active user" do
688 682 user = User.generate!
689 683 token = Token.create!(:action => 'api')
690 684 user.api_token = token
691 685 user.save
692 686
693 687 assert_equal user, User.find_by_api_key(token.value)
694 688 end
695 689 end
696 690
697 691 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
698 692 user = User.find_by_login("admin")
699 693 user.password = "admin"
700 694 user.save!
701 695
702 696 assert_equal false, User.default_admin_account_changed?
703 697 end
704 698
705 699 def test_default_admin_account_changed_should_return_true_if_password_was_changed
706 700 user = User.find_by_login("admin")
707 701 user.password = "newpassword"
708 702 user.save!
709 703
710 704 assert_equal true, User.default_admin_account_changed?
711 705 end
712 706
713 707 def test_default_admin_account_changed_should_return_true_if_account_is_disabled
714 708 user = User.find_by_login("admin")
715 709 user.password = "admin"
716 710 user.status = User::STATUS_LOCKED
717 711 user.save!
718 712
719 713 assert_equal true, User.default_admin_account_changed?
720 714 end
721 715
722 716 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
723 717 user = User.find_by_login("admin")
724 718 user.destroy
725 719
726 720 assert_equal true, User.default_admin_account_changed?
727 721 end
728 722
729 723 def test_roles_for_project
730 724 # user with a role
731 725 roles = @jsmith.roles_for_project(Project.find(1))
732 726 assert_kind_of Role, roles.first
733 727 assert_equal "Manager", roles.first.name
734 728
735 729 # user with no role
736 730 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
737 731 end
738 732
739 733 def test_projects_by_role_for_user_with_role
740 734 user = User.find(2)
741 735 assert_kind_of Hash, user.projects_by_role
742 736 assert_equal 2, user.projects_by_role.size
743 737 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
744 738 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
745 739 end
746 740
747 741 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
748 742 user = User.find(2)
749 743 assert_equal [], user.projects_by_role[Role.find(3)]
750 744 # should not update the hash
751 745 assert_nil user.projects_by_role.values.detect(&:blank?)
752 746 end
753 747
754 748 def test_projects_by_role_for_user_with_no_role
755 749 user = User.generate!
756 750 assert_equal({}, user.projects_by_role)
757 751 end
758 752
759 753 def test_projects_by_role_for_anonymous
760 754 assert_equal({}, User.anonymous.projects_by_role)
761 755 end
762 756
763 757 def test_valid_notification_options
764 758 # without memberships
765 759 assert_equal 5, User.find(7).valid_notification_options.size
766 760 # with memberships
767 761 assert_equal 6, User.find(2).valid_notification_options.size
768 762 end
769 763
770 764 def test_valid_notification_options_class_method
771 765 assert_equal 5, User.valid_notification_options.size
772 766 assert_equal 5, User.valid_notification_options(User.find(7)).size
773 767 assert_equal 6, User.valid_notification_options(User.find(2)).size
774 768 end
775 769
776 770 def test_mail_notification_all
777 771 @jsmith.mail_notification = 'all'
778 772 @jsmith.notified_project_ids = []
779 773 @jsmith.save
780 774 @jsmith.reload
781 775 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
782 776 end
783 777
784 778 def test_mail_notification_selected
785 779 @jsmith.mail_notification = 'selected'
786 780 @jsmith.notified_project_ids = [1]
787 781 @jsmith.save
788 782 @jsmith.reload
789 783 assert Project.find(1).recipients.include?(@jsmith.mail)
790 784 end
791 785
792 786 def test_mail_notification_only_my_events
793 787 @jsmith.mail_notification = 'only_my_events'
794 788 @jsmith.notified_project_ids = []
795 789 @jsmith.save
796 790 @jsmith.reload
797 791 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
798 792 end
799 793
800 794 def test_comments_sorting_preference
801 795 assert !@jsmith.wants_comments_in_reverse_order?
802 796 @jsmith.pref.comments_sorting = 'asc'
803 797 assert !@jsmith.wants_comments_in_reverse_order?
804 798 @jsmith.pref.comments_sorting = 'desc'
805 799 assert @jsmith.wants_comments_in_reverse_order?
806 800 end
807 801
808 802 def test_find_by_mail_should_be_case_insensitive
809 803 u = User.find_by_mail('JSmith@somenet.foo')
810 804 assert_not_nil u
811 805 assert_equal 'jsmith@somenet.foo', u.mail
812 806 end
813 807
814 808 def test_random_password
815 809 u = User.new
816 810 u.random_password
817 811 assert !u.password.blank?
818 812 assert !u.password_confirmation.blank?
819 813 end
820 814
821 815 context "#change_password_allowed?" do
822 816 should "be allowed if no auth source is set" do
823 817 user = User.generate!
824 818 assert user.change_password_allowed?
825 819 end
826 820
827 821 should "delegate to the auth source" do
828 822 user = User.generate!
829 823
830 824 allowed_auth_source = AuthSource.generate!
831 825 def allowed_auth_source.allow_password_changes?; true; end
832 826
833 827 denied_auth_source = AuthSource.generate!
834 828 def denied_auth_source.allow_password_changes?; false; end
835 829
836 830 assert user.change_password_allowed?
837 831
838 832 user.auth_source = allowed_auth_source
839 833 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
840 834
841 835 user.auth_source = denied_auth_source
842 836 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
843 837 end
844 838 end
845 839
846 840 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
847 841 with_settings :unsubscribe => '1' do
848 842 assert_equal true, User.find(2).own_account_deletable?
849 843 end
850 844 end
851 845
852 846 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
853 847 with_settings :unsubscribe => '0' do
854 848 assert_equal false, User.find(2).own_account_deletable?
855 849 end
856 850 end
857 851
858 852 def test_own_account_deletable_should_be_false_for_a_single_admin
859 853 User.delete_all(["admin = ? AND id <> ?", true, 1])
860 854
861 855 with_settings :unsubscribe => '1' do
862 856 assert_equal false, User.find(1).own_account_deletable?
863 857 end
864 858 end
865 859
866 860 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
867 861 User.generate! do |user|
868 862 user.admin = true
869 863 end
870 864
871 865 with_settings :unsubscribe => '1' do
872 866 assert_equal true, User.find(1).own_account_deletable?
873 867 end
874 868 end
875 869
876 870 context "#allowed_to?" do
877 871 context "with a unique project" do
878 872 should "return false if project is archived" do
879 873 project = Project.find(1)
880 874 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
881 875 assert ! @admin.allowed_to?(:view_issues, Project.find(1))
882 876 end
883 877
884 878 should "return false for write action if project is closed" do
885 879 project = Project.find(1)
886 880 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
887 881 assert ! @admin.allowed_to?(:edit_project, Project.find(1))
888 882 end
889 883
890 884 should "return true for read action if project is closed" do
891 885 project = Project.find(1)
892 886 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
893 887 assert @admin.allowed_to?(:view_project, Project.find(1))
894 888 end
895 889
896 890 should "return false if related module is disabled" do
897 891 project = Project.find(1)
898 892 project.enabled_module_names = ["issue_tracking"]
899 893 assert @admin.allowed_to?(:add_issues, project)
900 894 assert ! @admin.allowed_to?(:view_wiki_pages, project)
901 895 end
902 896
903 897 should "authorize nearly everything for admin users" do
904 898 project = Project.find(1)
905 899 assert ! @admin.member_of?(project)
906 900 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
907 901 assert @admin.allowed_to?(p.to_sym, project)
908 902 end
909 903 end
910 904
911 905 should "authorize normal users depending on their roles" do
912 906 project = Project.find(1)
913 907 assert @jsmith.allowed_to?(:delete_messages, project) #Manager
914 908 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
915 909 end
916 910 end
917 911
918 912 context "with multiple projects" do
919 913 should "return false if array is empty" do
920 914 assert ! @admin.allowed_to?(:view_project, [])
921 915 end
922 916
923 917 should "return true only if user has permission on all these projects" do
924 918 assert @admin.allowed_to?(:view_project, Project.all)
925 919 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
926 920 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
927 921 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
928 922 end
929 923
930 924 should "behave correctly with arrays of 1 project" do
931 925 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
932 926 end
933 927 end
934 928
935 929 context "with options[:global]" do
936 930 should "authorize if user has at least one role that has this permission" do
937 931 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
938 932 @anonymous = User.find(6)
939 933 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
940 934 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
941 935 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
942 936 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
943 937 assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
944 938 end
945 939 end
946 940 end
947 941
948 942 context "User#notify_about?" do
949 943 context "Issues" do
950 944 setup do
951 945 @project = Project.find(1)
952 946 @author = User.generate!
953 947 @assignee = User.generate!
954 948 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
955 949 end
956 950
957 951 should "be true for a user with :all" do
958 952 @author.update_attribute(:mail_notification, 'all')
959 953 assert @author.notify_about?(@issue)
960 954 end
961 955
962 956 should "be false for a user with :none" do
963 957 @author.update_attribute(:mail_notification, 'none')
964 958 assert ! @author.notify_about?(@issue)
965 959 end
966 960
967 961 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
968 962 @user = User.generate!(:mail_notification => 'only_my_events')
969 963 Member.create!(:user => @user, :project => @project, :role_ids => [1])
970 964 assert ! @user.notify_about?(@issue)
971 965 end
972 966
973 967 should "be true for a user with :only_my_events and is the author" do
974 968 @author.update_attribute(:mail_notification, 'only_my_events')
975 969 assert @author.notify_about?(@issue)
976 970 end
977 971
978 972 should "be true for a user with :only_my_events and is the assignee" do
979 973 @assignee.update_attribute(:mail_notification, 'only_my_events')
980 974 assert @assignee.notify_about?(@issue)
981 975 end
982 976
983 977 should "be true for a user with :only_assigned and is the assignee" do
984 978 @assignee.update_attribute(:mail_notification, 'only_assigned')
985 979 assert @assignee.notify_about?(@issue)
986 980 end
987 981
988 982 should "be false for a user with :only_assigned and is not the assignee" do
989 983 @author.update_attribute(:mail_notification, 'only_assigned')
990 984 assert ! @author.notify_about?(@issue)
991 985 end
992 986
993 987 should "be true for a user with :only_owner and is the author" do
994 988 @author.update_attribute(:mail_notification, 'only_owner')
995 989 assert @author.notify_about?(@issue)
996 990 end
997 991
998 992 should "be false for a user with :only_owner and is not the author" do
999 993 @assignee.update_attribute(:mail_notification, 'only_owner')
1000 994 assert ! @assignee.notify_about?(@issue)
1001 995 end
1002 996
1003 997 should "be true for a user with :selected and is the author" do
1004 998 @author.update_attribute(:mail_notification, 'selected')
1005 999 assert @author.notify_about?(@issue)
1006 1000 end
1007 1001
1008 1002 should "be true for a user with :selected and is the assignee" do
1009 1003 @assignee.update_attribute(:mail_notification, 'selected')
1010 1004 assert @assignee.notify_about?(@issue)
1011 1005 end
1012 1006
1013 1007 should "be false for a user with :selected and is not the author or assignee" do
1014 1008 @user = User.generate!(:mail_notification => 'selected')
1015 1009 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1016 1010 assert ! @user.notify_about?(@issue)
1017 1011 end
1018 1012 end
1019 1013
1020 1014 context "other events" do
1021 1015 should 'be added and tested'
1022 1016 end
1023 1017 end
1024 1018
1025 1019 def test_salt_unsalted_passwords
1026 1020 # Restore a user with an unsalted password
1027 1021 user = User.find(1)
1028 1022 user.salt = nil
1029 1023 user.hashed_password = User.hash_password("unsalted")
1030 1024 user.save!
1031 1025
1032 1026 User.salt_unsalted_passwords!
1033 1027
1034 1028 user.reload
1035 1029 # Salt added
1036 1030 assert !user.salt.blank?
1037 1031 # Password still valid
1038 1032 assert user.check_password?("unsalted")
1039 1033 assert_equal user, User.try_to_login(user.login, "unsalted")
1040 1034 end
1041 1035
1042 1036 if Object.const_defined?(:OpenID)
1043 1037
1044 1038 def test_setting_identity_url
1045 1039 normalized_open_id_url = 'http://example.com/'
1046 1040 u = User.new( :identity_url => 'http://example.com/' )
1047 1041 assert_equal normalized_open_id_url, u.identity_url
1048 1042 end
1049 1043
1050 1044 def test_setting_identity_url_without_trailing_slash
1051 1045 normalized_open_id_url = 'http://example.com/'
1052 1046 u = User.new( :identity_url => 'http://example.com' )
1053 1047 assert_equal normalized_open_id_url, u.identity_url
1054 1048 end
1055 1049
1056 1050 def test_setting_identity_url_without_protocol
1057 1051 normalized_open_id_url = 'http://example.com/'
1058 1052 u = User.new( :identity_url => 'example.com' )
1059 1053 assert_equal normalized_open_id_url, u.identity_url
1060 1054 end
1061 1055
1062 1056 def test_setting_blank_identity_url
1063 1057 u = User.new( :identity_url => 'example.com' )
1064 1058 u.identity_url = ''
1065 1059 assert u.identity_url.blank?
1066 1060 end
1067 1061
1068 1062 def test_setting_invalid_identity_url
1069 1063 u = User.new( :identity_url => 'this is not an openid url' )
1070 1064 assert u.identity_url.blank?
1071 1065 end
1072 1066
1073 1067 else
1074 1068 puts "Skipping openid tests."
1075 1069 end
1076 1070
1077 1071 end
General Comments 0
You need to be logged in to leave comments. Login now