##// END OF EJS Templates
Added User#notify_about? to check when a user should be notified about an event...
Eric Davis -
r4104:c059300d99aa
parent child
Show More
@@ -415,9 +415,10 class Issue < ActiveRecord::Base
415 # Returns the mail adresses of users that should be notified
415 # Returns the mail adresses of users that should be notified
416 def recipients
416 def recipients
417 notified = project.notified_users
417 notified = project.notified_users
418 # Author and assignee are always notified unless they have been locked
418 # Author and assignee are always notified unless they have been
419 notified << author if author && author.active?
419 # locked or don't want to be notified
420 notified << assigned_to if assigned_to && assigned_to.active?
420 notified << author if author && author.active? && author.notify_about?(self)
421 notified << assigned_to if assigned_to && assigned_to.active? && assigned_to.notify_about?(self)
421 notified.uniq!
422 notified.uniq!
422 # Remove users that can not view the issue
423 # Remove users that can not view the issue
423 notified.reject! {|user| !visible?(user)}
424 notified.reject! {|user| !visible?(user)}
@@ -368,6 +368,41 class User < Principal
368 allowed_to?(action, nil, options.reverse_merge(:global => true))
368 allowed_to?(action, nil, options.reverse_merge(:global => true))
369 end
369 end
370
370
371 # Utility method to help check if a user should be notified about an
372 # event.
373 #
374 # TODO: only supports Issue events currently
375 def notify_about?(object)
376 case mail_notification.to_sym
377 when :all
378 true
379 when :selected
380 # Handled by the Project
381 when :none
382 false
383 when :only_my_events
384 if object.is_a?(Issue) && (object.author == self || object.assigned_to == self)
385 true
386 else
387 false
388 end
389 when :only_assigned
390 if object.is_a?(Issue) && object.assigned_to == self
391 true
392 else
393 false
394 end
395 when :only_owner
396 if object.is_a?(Issue) && object.author == self
397 true
398 else
399 false
400 end
401 else
402 false
403 end
404 end
405
371 def self.current=(user)
406 def self.current=(user)
372 @current_user = user
407 @current_user = user
373 end
408 end
@@ -3,7 +3,7 class User < Principal
3 generator_for :mail, :method => :next_email
3 generator_for :mail, :method => :next_email
4 generator_for :firstname, :method => :next_firstname
4 generator_for :firstname, :method => :next_firstname
5 generator_for :lastname, :method => :next_lastname
5 generator_for :lastname, :method => :next_lastname
6
6
7 def self.next_login
7 def self.next_login
8 @gen_login ||= 'user1'
8 @gen_login ||= 'user1'
9 @gen_login.succ!
9 @gen_login.succ!
@@ -742,7 +742,9 class IssueTest < ActiveSupport::TestCase
742 context "Issue#recipients" do
742 context "Issue#recipients" do
743 setup do
743 setup do
744 @project = Project.find(1)
744 @project = Project.find(1)
745 @issue = Issue.generate_for_project!(@project, :assigned_to => User.generate_with_protected!)
745 @author = User.generate_with_protected!
746 @assignee = User.generate_with_protected!
747 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
746 end
748 end
747
749
748 should "include project recipients" do
750 should "include project recipients" do
@@ -761,5 +763,24 class IssueTest < ActiveSupport::TestCase
761 assert @issue.assigned_to, "No assigned_to set for Issue"
763 assert @issue.assigned_to, "No assigned_to set for Issue"
762 assert @issue.recipients.include?(@issue.assigned_to.mail)
764 assert @issue.recipients.include?(@issue.assigned_to.mail)
763 end
765 end
766
767 should "not include users who opt out of all email" do
768 @author.update_attribute(:mail_notification, :none)
769
770 assert !@issue.recipients.include?(@issue.author.mail)
771 end
772
773 should "not include the issue author if they are only notified of assigned issues" do
774 @author.update_attribute(:mail_notification, :only_assigned)
775
776 assert !@issue.recipients.include?(@issue.author.mail)
777 end
778
779 should "not include the assigned user if they are only notified of owned issues" do
780 @assignee.update_attribute(:mail_notification, :only_owner)
781
782 assert !@issue.recipients.include?(@issue.assigned_to.mail)
783 end
784
764 end
785 end
765 end
786 end
@@ -152,7 +152,7 class MailHandlerTest < ActiveSupport::TestCase
152 assert !issue.new_record?
152 assert !issue.new_record?
153 issue.reload
153 issue.reload
154 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
154 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
155 assert_equal 1, issue.watchers.size
155 assert_equal 1, issue.watcher_user_ids.size
156 end
156 end
157
157
158 def test_add_issue_by_unknown_user
158 def test_add_issue_by_unknown_user
@@ -398,6 +398,72 class UserTest < ActiveSupport::TestCase
398 end
398 end
399 end
399 end
400
400
401 context "User#notify_about?" do
402 context "Issues" do
403 setup do
404 @project = Project.find(1)
405 @author = User.generate_with_protected!
406 @assignee = User.generate_with_protected!
407 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
408 end
409
410 should "be true for a user with :all" do
411 @author.update_attribute(:mail_notification, :all)
412 assert @author.notify_about?(@issue)
413 end
414
415 should "be false for a user with :none" do
416 @author.update_attribute(:mail_notification, :none)
417 assert ! @author.notify_about?(@issue)
418 end
419
420 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
421 @user = User.generate_with_protected!(:mail_notification => :only_my_events)
422 assert ! @user.notify_about?(@issue)
423 end
424
425 should "be true for a user with :only_my_events and is the author" do
426 @author.update_attribute(:mail_notification, :only_my_events)
427 assert @author.notify_about?(@issue)
428 end
429
430 should "be true for a user with :only_my_events and is the assignee" do
431 @assignee.update_attribute(:mail_notification, :only_my_events)
432 assert @assignee.notify_about?(@issue)
433 end
434
435 should "be true for a user with :only_assigned and is the assignee" do
436 @assignee.update_attribute(:mail_notification, :only_assigned)
437 assert @assignee.notify_about?(@issue)
438 end
439
440 should "be false for a user with :only_assigned and is not the assignee" do
441 @author.update_attribute(:mail_notification, :only_assigned)
442 assert ! @author.notify_about?(@issue)
443 end
444
445 should "be true for a user with :only_owner and is the author" do
446 @author.update_attribute(:mail_notification, :only_owner)
447 assert @author.notify_about?(@issue)
448 end
449
450 should "be false for a user with :only_owner and is not the author" do
451 @assignee.update_attribute(:mail_notification, :only_owner)
452 assert ! @assignee.notify_about?(@issue)
453 end
454
455 should "be false if the mail_notification is anything else" do
456 @assignee.update_attribute(:mail_notification, :somthing_else)
457 assert ! @assignee.notify_about?(@issue)
458 end
459
460 end
461
462 context "other events" do
463 should 'be added and tested'
464 end
465 end
466
401 if Object.const_defined?(:OpenID)
467 if Object.const_defined?(:OpenID)
402
468
403 def test_setting_identity_url
469 def test_setting_identity_url
General Comments 0
You need to be logged in to leave comments. Login now