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