diff --git a/app/models/add_friend.rb b/app/models/add_friend.rb index 25cef81..5995638 100644 --- a/app/models/add_friend.rb +++ b/app/models/add_friend.rb @@ -23,8 +23,12 @@ class AddFriend < Task :manage_friends end + def target_notification_description + _('%{requestor} wants to be your friend.') % {:requestor => requestor.name} + end + def target_notification_message - _('%{requestor} wants to be your friend.') % {:requestor => requestor.name} + "\n\n" + + target_notification_description + "\n\n" + _('You need to login to %{system} in order to accept %{requestor} as your friend.') % { :system => target.environment.name, :requestor => requestor.name } end diff --git a/app/models/add_member.rb b/app/models/add_member.rb index 406d653..9879e29 100644 --- a/app/models/add_member.rb +++ b/app/models/add_member.rb @@ -35,8 +35,12 @@ class AddMember < Task :manage_memberships end + def target_notification_description + _('%{requestor} wants to be a member of this community.') % {:requestor => requestor.name} + end + def target_notification_message - _('%{requestor} wants to be a member of this community.') % {:requestor => requestor.name} + "\n\n" + + target_notification_description + "\n\n" + _('You will need login to %{system} in order to accept or reject %{requestor} as a member of %{organization}.') % { :system => target.environment.name, :requestor => requestor.name, :organization => organization.name } end diff --git a/app/models/approve_article.rb b/app/models/approve_article.rb index fb9afaf..0e35ebb 100644 --- a/app/models/approve_article.rb +++ b/app/models/approve_article.rb @@ -117,9 +117,13 @@ class ApproveArticle < Task article.blank? end + def target_notification_description + _('%{requestor} wants to publish the article: %{article}.') % {:requestor => requestor.name, :article => article.name} + end + def target_notification_message return nil if target.organization? && !target.moderated_articles? - _('%{requestor} wants to publish the article: %{article}.') % {:requestor => requestor.name, :article => article.name} + "\n\n" + target_notification_description + "\n\n" + _('You need to login on %{system} in order to approve or reject this article.') % { :system => target.environment.name } end diff --git a/app/models/create_community.rb b/app/models/create_community.rb index 8f5c050..d6246ed 100644 --- a/app/models/create_community.rb +++ b/app/models/create_community.rb @@ -88,6 +88,10 @@ class CreateCommunity < Task self.status == Task::Status::FINISHED end + def target_notification_description + _('%{requestor} wants to create community %{subject}') % {:requestor => requestor.name, :subject => subject} + end + def target_notification_message _("User \"%{user}\" just requested to create community %{community}. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :user => self.requestor.name, :community => self.name } end diff --git a/app/models/create_enterprise.rb b/app/models/create_enterprise.rb index dc9f8f3..e9a9ff3 100644 --- a/app/models/create_enterprise.rb +++ b/app/models/create_enterprise.rb @@ -210,6 +210,10 @@ class CreateEnterprise < Task msg end + def target_notification_description + _('%{requestor} wants to create enterprise %{subject}.') % {:requestor => requestor.name, :subject => subject} + end + def permission :validate_enterprise end diff --git a/app/models/suggest_article.rb b/app/models/suggest_article.rb index 35cb2dd..cd5e57b 100644 --- a/app/models/suggest_article.rb +++ b/app/models/suggest_article.rb @@ -56,9 +56,13 @@ class SuggestArticle < Task result = {:type => :defined_image, :src => '/images/icons-app/article-minor.png', :name => article_name} end - def target_notification_message + def target_notification_description _('%{sender} suggested the publication of the article: %{article}.') % - {:sender => sender, :article => article_name} + "\n\n" + + {:sender => sender, :article => article_name} + end + + def target_notification_message + target_notification_description + "\n\n" + _('You need to login on %{system} in order to approve or reject this article.') % { :system => target.environment.name } end diff --git a/app/models/task.rb b/app/models/task.rb index a6fd5f0..0fae74c 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -186,6 +186,10 @@ class Task < ActiveRecord::Base raise NotImplementedError, "#{self} does not implement #target_notification_message" end + def target_notification_description + '' + end + # What permission is required to perform task? def permission :perform_task diff --git a/app/models/task_mailer.rb b/app/models/task_mailer.rb index 0af6991..693f4a2 100644 --- a/app/models/task_mailer.rb +++ b/app/models/task_mailer.rb @@ -17,7 +17,7 @@ class TaskMailer < ActionMailer::Base url_for_tasks_list = task.target.kind_of?(Environment) ? '' : url_for(task.target.url.merge(:controller => 'tasks', :action => 'index')) from self.class.generate_from(task) - subject '[%s] %s' % [task.environment.name, task.information] + subject '[%s] %s' % [task.environment.name, task.target_notification_description] body :target => task.target.name, :message => msg, :environment => task.environment.name, diff --git a/test/unit/add_friend_test.rb b/test/unit/add_friend_test.rb index 7799614..435bba7 100644 --- a/test/unit/add_friend_test.rb +++ b/test/unit/add_friend_test.rb @@ -2,31 +2,32 @@ require File.dirname(__FILE__) + '/../test_helper' class AddFriendTest < ActiveSupport::TestCase + def setup + @person1 = create_user('testuser1').person + @person2 = create_user('testuser2').person + end + attr_reader :person1, :person2 + should 'be a task' do ok { AddFriend.new.kind_of?(Task) } end should 'actually create friendships (two way) when confirmed' do - p1 = create_user('testuser1').person - p2 = create_user('testuser2').person - task = fast_create(AddFriend, :requestor_id => p1.id, :target_id => p2.id, :target_type => 'Person') + task = fast_create(AddFriend, :requestor_id => person1.id, :target_id => person2.id, :target_type => 'Person') assert_difference Friendship, :count, 2 do task.finish end - p1.friends.reload - p2.friends.reload + person1.friends.reload + person2.friends.reload - ok('p1 should have p2 as friend') { p1.friends.include?(p2) } - ok('p2 should have p1 as friend') { p2.friends.include?(p1) } + ok('person1 should have person2 as friend') { person1.friends.include?(person2) } + ok('person2 should have person1 as friend') { person2.friends.include?(person1) } end should 'put friendships in the right groups' do - p1 = create_user('testuser1').person - p2 = create_user('testuser2').person - - task = fast_create(AddFriend, :requestor_id => p1, :target_id => p2.id, :target_type => 'Person') + task = fast_create(AddFriend, :requestor_id => person1, :target_id => person2.id, :target_type => 'Person') task.group_for_person = 'friend1' task.group_for_friend = 'friend2' assert task.save @@ -35,8 +36,8 @@ class AddFriendTest < ActiveSupport::TestCase task.finish end - ok('p1 should list p2 as friend1') { p1.friendships.first.group == 'friend1' } - ok('p2 should have p1 as friend2') { p2.friendships.first.group == 'friend2' } + ok('person1 should list person2 as friend1') { person1.friendships.first.group == 'friend1' } + ok('person2 should have person1 as friend2') { person2.friendships.first.group == 'friend2' } end should 'require requestor (person adding other as friend)' do @@ -63,12 +64,9 @@ class AddFriendTest < ActiveSupport::TestCase end should 'send e-mails' do - p1 = create_user('testuser1').person - p2 = create_user('testuser2').person - TaskMailer.expects(:deliver_target_notification).at_least_once - task = AddFriend.create!(:person => p1, :friend => p2) + task = AddFriend.create!(:person => person1, :friend => person2) end should 'has permission to manage friends' do @@ -77,18 +75,14 @@ class AddFriendTest < ActiveSupport::TestCase end should 'not add friend twice' do - p1 = create_user('testuser1').person - p2 = create_user('testuser2').person - fast_create(AddFriend, :requestor_id => p1.id, :target_id => p2.id) + fast_create(AddFriend, :requestor_id => person1.id, :target_id => person2.id) assert_raise ActiveRecord::RecordInvalid do - AddFriend.create!(:person => p1, :friend => p2) + AddFriend.create!(:person => person1, :friend => person2) end end should 'override target notification message method from Task' do - p1 = create_user('testuser1').person - p2 = create_user('testuser2').person - task = AddFriend.new(:person => p1, :friend => p2) + task = AddFriend.new(:person => person1, :friend => person2) assert_nothing_raised NotImplementedError do task.target_notification_message end @@ -122,5 +116,23 @@ class AddFriendTest < ActiveSupport::TestCase assert !task.errors[:group_for_friend] end + should 'have target notification message if is organization and not moderated' do + task = AddFriend.new(:person => person1, :friend => person2) + + assert_match(/wants to be your friend.*[\n]*.*to accept/, task.target_notification_message) + end + + should 'have target notification description' do + task = AddFriend.new(:person => person1, :friend => person2) + + assert_match(/#{task.requestor.name} wants to be your friend/, task.target_notification_description) + end + + should 'deliver target notification message' do + task = AddFriend.new(:person => person1, :friend => person2) + + email = TaskMailer.deliver_target_notification(task, task.target_notification_message) + assert_match(/#{task.requestor.name} wants to be your friend/, email.subject) + end end diff --git a/test/unit/add_member_test.rb b/test/unit/add_member_test.rb index 7d09b40..6880a98 100644 --- a/test/unit/add_member_test.rb +++ b/test/unit/add_member_test.rb @@ -2,19 +2,24 @@ require File.dirname(__FILE__) + '/../test_helper' class AddMemberTest < ActiveSupport::TestCase + def setup + @person = fast_create(Person) + @community = fast_create(Community) + end + attr_reader :person, :community + + should 'be a task' do ok { AddMember.new.kind_of?(Task) } end should 'actually add memberships when confirmed' do - p = create_user('testuser1').person - c = fast_create(Community, :name => 'closed community') - c.update_attribute(:closed, true) + community.update_attribute(:closed, true) TaskMailer.stubs(:deliver_target_notification) - task = fast_create(AddMember, :requestor_id => p.id, :target_id => c.id, :target_type => 'Community') + task = fast_create(AddMember, :requestor_id => person.id, :target_id => community.id, :target_type => 'Community') task.finish - assert_equal [p], c.members + assert_equal [person], community.members end should 'require requestor' do @@ -40,13 +45,11 @@ class AddMemberTest < ActiveSupport::TestCase end should 'send e-mails' do - p = create_user('testuser1').person - c = fast_create(Community, :name => 'closed community') - c.update_attribute(:closed, true) + community.update_attribute(:closed, true) TaskMailer.expects(:deliver_target_notification).at_least_once - task = AddMember.create!(:person => p, :organization => c) + task = AddMember.create!(:person => person, :organization => community) end should 'has permission to manage members' do @@ -55,47 +58,56 @@ class AddMemberTest < ActiveSupport::TestCase end should 'have roles' do - p = create_user('testuser1').person - c = fast_create(Community, :name => 'community_test') TaskMailer.stubs(:deliver_target_notification) - task = AddMember.create!(:roles => [1,2,3], :person => p, :organization => c) + task = AddMember.create!(:roles => [1,2,3], :person => person, :organization => community) assert_equal [1,2,3], task.roles end should 'put member with the right roles' do - p = create_user('testuser1').person - c = fast_create(Community, :name => 'community_test') - - roles = [Profile::Roles.member(c.environment.id), Profile::Roles.admin(c.environment.id)] + roles = [Profile::Roles.member(community.environment.id), Profile::Roles.admin(community.environment.id)] TaskMailer.stubs(:deliver_target_notification) - task = AddMember.create!(:roles => roles.map(&:id), :person => p, :organization => c) + task = AddMember.create!(:roles => roles.map(&:id), :person => person, :organization => community) task.finish - current_roles = p.find_roles(c).map(&:role) + current_roles = person.find_roles(community).map(&:role) assert_includes current_roles, roles[0] assert_includes current_roles, roles[1] end should 'override target notification message method from Task' do - p1 = create_user('testuser1').person - p2 = create_user('testuser2').person - task = AddFriend.new(:person => p1, :friend => p2) + task = AddMember.new(:person => person, :organization => community) assert_nothing_raised NotImplementedError do task.target_notification_message end end should 'ignore roles with id zero' do - p = create_user('testuser1').person - c = fast_create(Community, :name => 'community_test') - - role = Profile::Roles.member(c.environment.id) + role = Profile::Roles.member(community.environment.id) TaskMailer.stubs(:deliver_target_notification) - task = AddMember.create!(:roles => ["0", role.id, nil], :person => p, :organization => c) + task = AddMember.create!(:roles => ["0", role.id, nil], :person => person, :organization => community) task.finish - current_roles = p.find_roles(c).map(&:role) + current_roles = person.find_roles(community).map(&:role) assert_includes current_roles, role end + should 'have target notification message' do + task = AddMember.new(:person => person, :organization => community) + + assert_match(/#{person.name} wants to be a member of this community.*[\n]*.*to accept or reject/, task.target_notification_message) + end + + should 'have target notification description' do + task = AddMember.new(:person => person, :organization => community) + + assert_match(/#{task.requestor.name} wants to be a member of this community/, task.target_notification_description) + end + + should 'deliver target notification message' do + task = AddMember.new(:person => person, :organization => community) + + email = TaskMailer.deliver_target_notification(task, task.target_notification_message) + assert_match(/#{task.requestor.name} wants to be a member of this community/, email.subject) + end + end diff --git a/test/unit/approve_article_test.rb b/test/unit/approve_article_test.rb index e5f17ed..a835628 100644 --- a/test/unit/approve_article_test.rb +++ b/test/unit/approve_article_test.rb @@ -330,5 +330,36 @@ class ApproveArticleTest < ActiveSupport::TestCase assert_equal article.is_trackable?, article.class.last.is_trackable? end + should 'not have target notification message if it is not a moderated oganization' do + community.moderated_articles = false; community.save + task = ApproveArticle.new(:article => article, :target => community, :requestor => profile) + + assert_nil task.target_notification_message + end + + should 'have target notification message if is organization and not moderated' do + task = ApproveArticle.new(:article => article, :target => community, :requestor => profile) + + community.expects(:moderated_articles?).returns(['true']) + + assert_match(/wants to publish the article.*[\n]*.*to approve or reject/, task.target_notification_message) + end + + should 'have target notification description' do + community.moderated_articles = false; community.save + task = ApproveArticle.new(:article => article, :target => community, :requestor => profile) + + assert_match(/#{task.requestor.name} wants to publish the article: #{article.name}/, task.target_notification_description) + end + + should 'deliver target notification message' do + task = ApproveArticle.new(:article => article, :target => community, :requestor => profile) + + community.expects(:notification_emails).returns(['target@example.com']) + community.expects(:moderated_articles?).returns(['true']) + + email = TaskMailer.deliver_target_notification(task, task.target_notification_message) + assert_match(/#{task.requestor.name} wants to publish the article: #{article.name}/, email.subject) + end end diff --git a/test/unit/create_community_test.rb b/test/unit/create_community_test.rb index ec2e433..002f93d 100644 --- a/test/unit/create_community_test.rb +++ b/test/unit/create_community_test.rb @@ -83,4 +83,23 @@ class CreateCommunityTest < Test::Unit::TestCase assert_equal 'rails.png', Community['my-new-community'].image.filename end + should 'have target notification message' do + task = CreateCommunity.new(:name => 'community test', :target => Environment.default, :requestor => person) + + assert_match(/requested to create community.*to approve or reject/, task.target_notification_message) + end + + should 'have target notification description' do + task = CreateCommunity.new(:name => 'community test', :target => Environment.default, :requestor => person) + + assert_match(/#{task.requestor.name} wants to create community #{task.subject}/, task.target_notification_description) + end + + should 'deliver target notification message' do + task = CreateCommunity.new(:name => 'community test', :target => Environment.default, :requestor => person) + + email = TaskMailer.deliver_target_notification(task, task.target_notification_message) + assert_match(/#{task.requestor.name} wants to create community #{task.subject}/, email.subject) + end + end diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 1375875..c812bf7 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -2,6 +2,11 @@ require File.dirname(__FILE__) + '/../test_helper' class CreateEnterpriseTest < Test::Unit::TestCase + def setup + @person = fast_create(Person) + end + attr_reader :person + should 'provide needed data' do task = CreateEnterprise.new @@ -253,4 +258,25 @@ class CreateEnterpriseTest < Test::Unit::TestCase t = CreateEnterprise.new assert_equal :validate_enterprise, t.permission end + + should 'have target notification message' do + task = CreateEnterprise.new(:name => 'My enterprise', :requestor => person, :target => Environment.default) + + assert_match(/#{task.name}.*requested to enter #{person.environment}.*approve or reject/, task.target_notification_message) + end + + should 'have target notification description' do + task = CreateEnterprise.new(:name => 'My enterprise', :requestor => person, :target => Environment.default) + + assert_match(/#{task.requestor.name} wants to create enterprise #{task.subject}/, task.target_notification_description) + end + + should 'deliver target notification message' do + task = CreateEnterprise.new(:name => 'My enterprise', :requestor => person, :target => Environment.default) + + email = TaskMailer.deliver_target_notification(task, task.target_notification_message) + + assert_match(/#{task.requestor.name} wants to create enterprise #{task.subject}/, email.subject) + end + end diff --git a/test/unit/suggest_article_test.rb b/test/unit/suggest_article_test.rb index aeb2c17..b43ca69 100644 --- a/test/unit/suggest_article_test.rb +++ b/test/unit/suggest_article_test.rb @@ -134,4 +134,25 @@ class SuggestArticleTest < ActiveSupport::TestCase assert_equal 'some name', article.author_name end + should 'have target notification message' do + task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe') + + assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}.*[\n]*.*to approve or reject/, task.target_notification_message) + end + + should 'have target notification description' do + task = build(SuggestArticle,:target => @profile, :article_name => 'suggested article', :name => 'johndoe') + + assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, task.target_notification_description) + end + + should 'deliver target notification message' do + task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com') + + email = TaskMailer.deliver_target_notification(task, task.target_notification_message) + + assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, email.subject) + end + + end diff --git a/test/unit/task_mailer_test.rb b/test/unit/task_mailer_test.rb index d593a4d..ebed475 100644 --- a/test/unit/task_mailer_test.rb +++ b/test/unit/task_mailer_test.rb @@ -85,7 +85,7 @@ class TaskMailerTest < Test::Unit::TestCase should 'be able to send a "target notification" message' do task = Task.new - task.expects(:information).returns('the task') + task.expects(:target_notification_description).returns('the task') target = mock() target.expects(:notification_emails).returns(['target@example.com']) diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 4af6436..6eacfde 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -232,6 +232,11 @@ class TaskTest < Test::Unit::TestCase assert_equal task.environment, nil end + should 'have blank string on target_notification_description in Task base class' do + task = Task.new + assert_equal '', task.target_notification_description + end + protected def sample_user -- libgit2 0.21.2