##// END OF EJS Templates
Validates user's mail_notification and turn options into strings (the attribute type)....
Jean-Philippe Lang -
r4380:e4f319fe6122
parent child
Show More
@@ -35,12 +35,12 class User < Principal
35 35 }
36 36
37 37 MAIL_NOTIFICATION_OPTIONS = [
38 [:all, :label_user_mail_option_all],
39 [:selected, :label_user_mail_option_selected],
40 [:none, :label_user_mail_option_none],
41 [:only_my_events, :label_user_mail_option_only_my_events],
42 [:only_assigned, :label_user_mail_option_only_assigned],
43 [:only_owner, :label_user_mail_option_only_owner]
38 ['all', :label_user_mail_option_all],
39 ['selected', :label_user_mail_option_selected],
40 ['only_my_events', :label_user_mail_option_only_my_events],
41 ['only_assigned', :label_user_mail_option_only_assigned],
42 ['only_owner', :label_user_mail_option_only_owner],
43 ['none', :label_user_mail_option_none]
44 44 ]
45 45
46 46 has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)},
@@ -73,6 +73,7 class User < Principal
73 73 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
74 74 validates_length_of :mail, :maximum => 60, :allow_nil => true
75 75 validates_confirmation_of :password, :allow_nil => true
76 validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
76 77
77 78 def before_create
78 79 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
@@ -265,7 +266,7 class User < Principal
265 266 # Note that @user.membership.size would fail since AR ignores
266 267 # :include association option when doing a count
267 268 if memberships.length < 1
268 MAIL_NOTIFICATION_OPTIONS.delete_if {|option| option.first == :selected}
269 MAIL_NOTIFICATION_OPTIONS.delete_if {|option| option.first == 'selected'}
269 270 else
270 271 MAIL_NOTIFICATION_OPTIONS
271 272 end
@@ -411,26 +412,26 class User < Principal
411 412 #
412 413 # TODO: only supports Issue events currently
413 414 def notify_about?(object)
414 case mail_notification.to_sym
415 when :all
415 case mail_notification
416 when 'all'
416 417 true
417 when :selected
418 when 'selected'
418 419 # Handled by the Project
419 when :none
420 when 'none'
420 421 false
421 when :only_my_events
422 when 'only_my_events'
422 423 if object.is_a?(Issue) && (object.author == self || object.assigned_to == self)
423 424 true
424 425 else
425 426 false
426 427 end
427 when :only_assigned
428 when 'only_assigned'
428 429 if object.is_a?(Issue) && object.assigned_to == self
429 430 true
430 431 else
431 432 false
432 433 end
433 when :only_owner
434 when 'only_owner'
434 435 if object.is_a?(Issue) && object.author == self
435 436 true
436 437 else
@@ -115,12 +115,19 class UserTest < ActiveSupport::TestCase
115 115 assert Member.find_all_by_user_id(2).empty?
116 116 end
117 117
118 def test_validate
118 def test_validate_login_presence
119 119 @admin.login = ""
120 120 assert !@admin.save
121 121 assert_equal 1, @admin.errors.count
122 122 end
123 123
124 def test_validate_mail_notification_inclusion
125 u = User.new
126 u.mail_notification = 'foo'
127 u.save
128 assert_not_nil u.errors.on(:mail_notification)
129 end
130
124 131 context "User#try_to_login" do
125 132 should "fall-back to case-insensitive if user login is not found as-typed." do
126 133 user = User.try_to_login("AdMin", "admin")
@@ -437,55 +444,49 class UserTest < ActiveSupport::TestCase
437 444 end
438 445
439 446 should "be true for a user with :all" do
440 @author.update_attribute(:mail_notification, :all)
447 @author.update_attribute(:mail_notification, 'all')
441 448 assert @author.notify_about?(@issue)
442 449 end
443 450
444 451 should "be false for a user with :none" do
445 @author.update_attribute(:mail_notification, :none)
452 @author.update_attribute(:mail_notification, 'none')
446 453 assert ! @author.notify_about?(@issue)
447 454 end
448 455
449 456 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
450 @user = User.generate_with_protected!(:mail_notification => :only_my_events)
457 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
451 458 assert ! @user.notify_about?(@issue)
452 459 end
453 460
454 461 should "be true for a user with :only_my_events and is the author" do
455 @author.update_attribute(:mail_notification, :only_my_events)
462 @author.update_attribute(:mail_notification, 'only_my_events')
456 463 assert @author.notify_about?(@issue)
457 464 end
458 465
459 466 should "be true for a user with :only_my_events and is the assignee" do
460 @assignee.update_attribute(:mail_notification, :only_my_events)
467 @assignee.update_attribute(:mail_notification, 'only_my_events')
461 468 assert @assignee.notify_about?(@issue)
462 469 end
463 470
464 471 should "be true for a user with :only_assigned and is the assignee" do
465 @assignee.update_attribute(:mail_notification, :only_assigned)
472 @assignee.update_attribute(:mail_notification, 'only_assigned')
466 473 assert @assignee.notify_about?(@issue)
467 474 end
468 475
469 476 should "be false for a user with :only_assigned and is not the assignee" do
470 @author.update_attribute(:mail_notification, :only_assigned)
477 @author.update_attribute(:mail_notification, 'only_assigned')
471 478 assert ! @author.notify_about?(@issue)
472 479 end
473 480
474 481 should "be true for a user with :only_owner and is the author" do
475 @author.update_attribute(:mail_notification, :only_owner)
482 @author.update_attribute(:mail_notification, 'only_owner')
476 483 assert @author.notify_about?(@issue)
477 484 end
478 485
479 486 should "be false for a user with :only_owner and is not the author" do
480 @assignee.update_attribute(:mail_notification, :only_owner)
487 @assignee.update_attribute(:mail_notification, 'only_owner')
481 488 assert ! @assignee.notify_about?(@issue)
482 489 end
483
484 should "be false if the mail_notification is anything else" do
485 @assignee.update_attribute(:mail_notification, :somthing_else)
486 assert ! @assignee.notify_about?(@issue)
487 end
488
489 490 end
490 491
491 492 context "other events" do
General Comments 0
You need to be logged in to leave comments. Login now