From 7c8e3b6858c950b237146db765c6da4281209166 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Fri, 6 Sep 2013 00:00:17 +0000 Subject: [PATCH] rails3: fix mailing tests --- app/mailers/mailing.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/models/mailing.rb | 63 --------------------------------------------------------------- app/views/mailing/sender/mail.html.erb | 17 ----------------- app/views/mailing/sender/notification.html.erb | 17 +++++++++++++++++ test/unit/mailing_test.rb | 20 ++++++++++---------- 5 files changed, 92 insertions(+), 90 deletions(-) create mode 100644 app/mailers/mailing.rb delete mode 100644 app/models/mailing.rb delete mode 100644 app/views/mailing/sender/mail.html.erb create mode 100644 app/views/mailing/sender/notification.html.erb diff --git a/app/mailers/mailing.rb b/app/mailers/mailing.rb new file mode 100644 index 0000000..0442dbd --- /dev/null +++ b/app/mailers/mailing.rb @@ -0,0 +1,65 @@ +require 'mailing_job' + +class Mailing < ActiveRecord::Base + + attr_accessible :subject, :body + validates_presence_of :source_id, :subject, :body + belongs_to :source, :foreign_key => :source_id, :polymorphic => true + belongs_to :person + + has_many :mailing_sents + + xss_terminate :only => [ :subject, :body ], :with => 'white_list', :on => 'validation' + + after_create do |mailing| + mailing.schedule + end + + def schedule + Delayed::Job.enqueue MailingJob.new(self.id) + end + + def generate_from + "#{source.name} <#{source.contact_email}>" + end + + def generate_subject + '[%s] %s' % [source.name, subject] + end + + def signature_message + _('Sent by Noosfero.') + end + + def url + '' + end + + def deliver + each_recipient do |recipient| + begin + Mailing::Sender.notification(self, recipient.email).deliver + self.mailing_sents.create(:person => recipient) + rescue Exception + # FIXME should not discard errors silently. An idea is to collect all + # errors and generate a task (notification) for the +source+ + # (environment/organization) listing these errors. + end + end + end + + class Sender < ActionMailer::Base + def notification(mailing, recipient) + @message = mailing.body + @signature_message = mailing.signature_message + @url = mailing.url + mail( + :content_type => 'text/html', + :recipients => recipient, + :from => mailing.generate_from, + :reply_to => mailing.person.email, + :subject => mailing.generate_subject + ) + end + end +end diff --git a/app/models/mailing.rb b/app/models/mailing.rb deleted file mode 100644 index 9d06851..0000000 --- a/app/models/mailing.rb +++ /dev/null @@ -1,63 +0,0 @@ -require 'mailing_job' - -class Mailing < ActiveRecord::Base - - attr_accessible :subject, :body - validates_presence_of :source_id, :subject, :body - belongs_to :source, :foreign_key => :source_id, :polymorphic => true - belongs_to :person - - has_many :mailing_sents - - xss_terminate :only => [ :subject, :body ], :with => 'white_list', :on => 'validation' - - after_create do |mailing| - mailing.schedule - end - - def schedule - Delayed::Job.enqueue MailingJob.new(self.id) - end - - def generate_from - "#{source.name} <#{source.contact_email}>" - end - - def generate_subject - '[%s] %s' % [source.name, subject] - end - - def signature_message - _('Sent by Noosfero.') - end - - def url - '' - end - - def deliver - each_recipient do |recipient| - begin - Mailing::Sender.deliver_mail(self, recipient.email) - self.mailing_sents.create(:person => recipient) - rescue Exception - # FIXME should not discard errors silently. An idea is to collect all - # errors and generate a task (notification) for the +source+ - # (environment/organization) listing these errors. - end - end - end - - class Sender < ActionMailer::Base - def mail(mailing, recipient) - content_type 'text/html' - recipients recipient - from mailing.generate_from - reply_to mailing.person.email - subject mailing.generate_subject - body :message => mailing.body, - :signature_message => mailing.signature_message, - :url => mailing.url - end - end -end diff --git a/app/views/mailing/sender/mail.html.erb b/app/views/mailing/sender/mail.html.erb deleted file mode 100644 index dc92944..0000000 --- a/app/views/mailing/sender/mail.html.erb +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - <%= word_wrap(@message) %> -

- --
- <%= @signature_message %>
- <%= @url %> -

- - - - - diff --git a/app/views/mailing/sender/notification.html.erb b/app/views/mailing/sender/notification.html.erb new file mode 100644 index 0000000..dc92944 --- /dev/null +++ b/app/views/mailing/sender/notification.html.erb @@ -0,0 +1,17 @@ + + + + + + + <%= word_wrap(@message) %> +

+ --
+ <%= @signature_message %>
+ <%= @url %> +

+ + + + + diff --git a/test/unit/mailing_test.rb b/test/unit/mailing_test.rb index 510c1b3..2d97959 100644 --- a/test/unit/mailing_test.rb +++ b/test/unit/mailing_test.rb @@ -51,50 +51,50 @@ class MailingTest < ActiveSupport::TestCase end should 'return source' do - mailing = Mailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news') + mailing = create(Mailing, :source => environment, :subject => 'Hello', :body => 'We have some news') assert_equal environment, Mailing.find(mailing.id).source end should 'return source name' do - mailing = Mailing.new(:source => environment) + mailing = build(Mailing, :source => environment) assert_equal environment.name, mailing.source.name end should 'return source with source_id' do - mailing = Mailing.new(:source => environment) + mailing = build(Mailing, :source => environment) assert_equal environment, mailing.source end should 'return person with person_id' do person = Person['user_one'] - mailing = Mailing.new(:source => environment, :person => person) + mailing = build(Mailing, :source => environment, :person => person) assert_equal person, mailing.person end should 'display name and email on generate_from' do person = Person['user_one'] - mailing = Mailing.new(:source => environment, :person => person) + mailing = build(Mailing, :source => environment, :person => person) assert_equal "#{environment.name} <#{environment.contact_email}>", mailing.generate_from end should 'generate subject' do - mailing = Mailing.new(:source => environment, :subject => 'Hello :)') + mailing = build(Mailing, :source => environment, :subject => 'Hello :)') assert_equal "[#{environment.name}] #{mailing.subject}", mailing.generate_subject end should 'return signature message' do - mailing = Mailing.new(:source => environment) + mailing = build(Mailing, :source => environment) assert_equal 'Sent by Noosfero.', mailing.signature_message end should 'return blank string on url' do - mailing = Mailing.new(:source => environment) - environment.domains << Domain.create(:name => 'noosfero.net', :is_default => true) + mailing = build(Mailing, :source => environment) + environment.domains << create(Domain, :name => 'noosfero.net', :is_default => true) assert_equal '', mailing.url end should 'process the entire batch even if individual emails crash' do - mailing = Mailing.new(:source => environment, :person => Person['user_one'], :body => 'test', :subject => 'test') + mailing = build(Mailing, :source => environment, :person => Person['user_one'], :body => 'test', :subject => 'test') def mailing.each_recipient user_one = Person['user_one'] user_two = Person['user_two'] -- libgit2 0.21.2