Commit f13cebfe5318ab22858a3383d6e7ffb7cbbf5555
1 parent
0aa7c21a
Exists in
master
and in
29 other branches
ActionItem96: implementing mailing in task finish/cancel
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@621 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
8 changed files
with
76 additions
and
11 deletions
Show diff stats
app/models/task.rb
| @@ -38,7 +38,7 @@ class Task < ActiveRecord::Base | @@ -38,7 +38,7 @@ class Task < ActiveRecord::Base | ||
| 38 | self.end_date = Time.now | 38 | self.end_date = Time.now |
| 39 | self.save! | 39 | self.save! |
| 40 | self.perform | 40 | self.perform |
| 41 | - self.notify_requestor(self.finish_message) | 41 | + TaskMailer.deliver_task_finished(self) |
| 42 | end | 42 | end |
| 43 | end | 43 | end |
| 44 | 44 | ||
| @@ -49,7 +49,7 @@ class Task < ActiveRecord::Base | @@ -49,7 +49,7 @@ class Task < ActiveRecord::Base | ||
| 49 | self.status = Task::Status::CANCELLED | 49 | self.status = Task::Status::CANCELLED |
| 50 | self.end_date = Time.now | 50 | self.end_date = Time.now |
| 51 | self.save! | 51 | self.save! |
| 52 | - self.notify_requestor(self.cancel_message) | 52 | + TaskMailer.deliver_task_cancelled(self) |
| 53 | end | 53 | end |
| 54 | end | 54 | end |
| 55 | 55 | ||
| @@ -65,11 +65,6 @@ class Task < ActiveRecord::Base | @@ -65,11 +65,6 @@ class Task < ActiveRecord::Base | ||
| 65 | def perform | 65 | def perform |
| 66 | end | 66 | end |
| 67 | 67 | ||
| 68 | - # sends a message to the requestor | ||
| 69 | - def notify_requestor(msg) | ||
| 70 | - # TODO: implement message sending | ||
| 71 | - end | ||
| 72 | - | ||
| 73 | # The message that will be sent to the requestor of the task when its | 68 | # The message that will be sent to the requestor of the task when its |
| 74 | # finished. | 69 | # finished. |
| 75 | def finish_message | 70 | def finish_message |
app/models/task_mailer.rb
| 1 | class TaskMailer < ActionMailer::Base | 1 | class TaskMailer < ActionMailer::Base |
| 2 | + | ||
| 3 | + def task_finished(task) | ||
| 4 | + recipients task.requestor.email | ||
| 5 | + from task.requestor.environment.contact_email | ||
| 6 | + subject task | ||
| 7 | + body :requestor => task.requestor.name, | ||
| 8 | + :message => task.finish_message, | ||
| 9 | + :environment => task.requestor.environment.name, | ||
| 10 | + :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + def task_cancelled(task) | ||
| 14 | + recipients task.requestor.email | ||
| 15 | + from task.requestor.environment.contact_email | ||
| 16 | + subject task | ||
| 17 | + body :requestor => task.requestor.name, | ||
| 18 | + :message => task.cancel_message, | ||
| 19 | + :environment => task.requestor.environment.name, | ||
| 20 | + :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') | ||
| 21 | + end | ||
| 22 | + | ||
| 2 | end | 23 | end |
app/models/user.rb
| @@ -26,6 +26,7 @@ class User < ActiveRecord::Base | @@ -26,6 +26,7 @@ class User < ActiveRecord::Base | ||
| 26 | validates_length_of :email, :within => 3..100 | 26 | validates_length_of :email, :within => 3..100 |
| 27 | validates_uniqueness_of :login, :email, :case_sensitive => false | 27 | validates_uniqueness_of :login, :email, :case_sensitive => false |
| 28 | before_save :encrypt_password | 28 | before_save :encrypt_password |
| 29 | + validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT | ||
| 29 | 30 | ||
| 30 | validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.') | 31 | validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.') |
| 31 | 32 |
test/unit/environment_test.rb
| @@ -153,4 +153,12 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -153,4 +153,12 @@ class EnvironmentTest < Test::Unit::TestCase | ||
| 153 | assert !env.errors.invalid?(:contact_email) | 153 | assert !env.errors.invalid?(:contact_email) |
| 154 | end | 154 | end |
| 155 | 155 | ||
| 156 | + should 'provide a default hostname' do | ||
| 157 | + env = Environment.create!(:name => 'test environment') | ||
| 158 | + assert_nil env.default_hostname | ||
| 159 | + | ||
| 160 | + env.domains << Domain.create(:name => 'example.com') | ||
| 161 | + assert_equal 'example.com', env.default_hostname | ||
| 162 | + end | ||
| 163 | + | ||
| 156 | end | 164 | end |
test/unit/task_mailer_test.rb
| @@ -16,6 +16,26 @@ class TaskMailerTest < Test::Unit::TestCase | @@ -16,6 +16,26 @@ class TaskMailerTest < Test::Unit::TestCase | ||
| 16 | @expected.mime_version = '1.0' | 16 | @expected.mime_version = '1.0' |
| 17 | end | 17 | end |
| 18 | 18 | ||
| 19 | + should 'be able to send a "task finished" message' do | ||
| 20 | + | ||
| 21 | + task = mock() | ||
| 22 | + task.expects(:finish_message).returns('the message') | ||
| 23 | + | ||
| 24 | + requestor = mock() | ||
| 25 | + requestor.expects(:email).returns('requestor@example.com') | ||
| 26 | + requestor.expects(:name).returns('my name') | ||
| 27 | + | ||
| 28 | + environment = mock() | ||
| 29 | + environment.expects(:contact_email).returns('sender@example.com') | ||
| 30 | + environment.expects(:default_hostname).returns('example.com') | ||
| 31 | + environment.expects(:name).returns('example') | ||
| 32 | + | ||
| 33 | + task.expects(:requestor).returns(requestor).at_least_once | ||
| 34 | + requestor.expects(:environment).returns(environment).at_least_once | ||
| 35 | + | ||
| 36 | + TaskMailer.deliver_task_finished(task) | ||
| 37 | + end | ||
| 38 | + | ||
| 19 | private | 39 | private |
| 20 | def read_fixture(action) | 40 | def read_fixture(action) |
| 21 | IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}") | 41 | IO.readlines("#{FIXTURES_PATH}/task_mailer/#{action}") |
test/unit/task_test.rb
| @@ -2,6 +2,12 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -2,6 +2,12 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | 2 | ||
| 3 | class TaskTest < Test::Unit::TestCase | 3 | class TaskTest < Test::Unit::TestCase |
| 4 | 4 | ||
| 5 | + def setup | ||
| 6 | + ActionMailer::Base.delivery_method = :test | ||
| 7 | + ActionMailer::Base.perform_deliveries = true | ||
| 8 | + ActionMailer::Base.deliveries = [] | ||
| 9 | + end | ||
| 10 | + | ||
| 5 | def test_relationship_with_requestor | 11 | def test_relationship_with_requestor |
| 6 | t = Task.create | 12 | t = Task.create |
| 7 | assert_raise ActiveRecord::AssociationTypeMismatch do | 13 | assert_raise ActiveRecord::AssociationTypeMismatch do |
| @@ -23,6 +29,7 @@ class TaskTest < Test::Unit::TestCase | @@ -23,6 +29,7 @@ class TaskTest < Test::Unit::TestCase | ||
| 23 | end | 29 | end |
| 24 | 30 | ||
| 25 | def test_should_call_perform_in_finish | 31 | def test_should_call_perform_in_finish |
| 32 | + TaskMailer.expects(:deliver_task_finished) | ||
| 26 | t = Task.create | 33 | t = Task.create |
| 27 | t.expects(:perform) | 34 | t.expects(:perform) |
| 28 | t.finish | 35 | t.finish |
| @@ -30,6 +37,7 @@ class TaskTest < Test::Unit::TestCase | @@ -30,6 +37,7 @@ class TaskTest < Test::Unit::TestCase | ||
| 30 | end | 37 | end |
| 31 | 38 | ||
| 32 | def test_should_have_cancelled_status_after_cancel | 39 | def test_should_have_cancelled_status_after_cancel |
| 40 | + TaskMailer.expects(:deliver_task_cancelled) | ||
| 33 | t = Task.create | 41 | t = Task.create |
| 34 | t.cancel | 42 | t.cancel |
| 35 | assert_equal Task::Status::CANCELLED, t.status | 43 | assert_equal Task::Status::CANCELLED, t.status |
| @@ -41,16 +49,14 @@ class TaskTest < Test::Unit::TestCase | @@ -41,16 +49,14 @@ class TaskTest < Test::Unit::TestCase | ||
| 41 | end | 49 | end |
| 42 | 50 | ||
| 43 | def test_should_notify_finish | 51 | def test_should_notify_finish |
| 52 | + TaskMailer.expects(:deliver_task_finished) | ||
| 44 | t = Task.create | 53 | t = Task.create |
| 45 | - t.expects(:notify_requestor) | ||
| 46 | - t.expects(:finish_message) | ||
| 47 | t.finish | 54 | t.finish |
| 48 | end | 55 | end |
| 49 | 56 | ||
| 50 | def test_should_notify_cancel | 57 | def test_should_notify_cancel |
| 58 | + TaskMailer.expects(:deliver_task_cancelled) | ||
| 51 | t = Task.create | 59 | t = Task.create |
| 52 | - t.expects(:notify_requestor) | ||
| 53 | - t.expects(:cancel_message) | ||
| 54 | t.cancel | 60 | t.cancel |
| 55 | end | 61 | end |
| 56 | 62 |