Commit 7c8e3b6858c950b237146db765c6da4281209166
1 parent
6d2a0edf
Exists in
master
and in
29 other branches
rails3: fix mailing tests
Showing
5 changed files
with
92 additions
and
90 deletions
Show diff stats
@@ -0,0 +1,65 @@ | @@ -0,0 +1,65 @@ | ||
1 | +require 'mailing_job' | ||
2 | + | ||
3 | +class Mailing < ActiveRecord::Base | ||
4 | + | ||
5 | + attr_accessible :subject, :body | ||
6 | + validates_presence_of :source_id, :subject, :body | ||
7 | + belongs_to :source, :foreign_key => :source_id, :polymorphic => true | ||
8 | + belongs_to :person | ||
9 | + | ||
10 | + has_many :mailing_sents | ||
11 | + | ||
12 | + xss_terminate :only => [ :subject, :body ], :with => 'white_list', :on => 'validation' | ||
13 | + | ||
14 | + after_create do |mailing| | ||
15 | + mailing.schedule | ||
16 | + end | ||
17 | + | ||
18 | + def schedule | ||
19 | + Delayed::Job.enqueue MailingJob.new(self.id) | ||
20 | + end | ||
21 | + | ||
22 | + def generate_from | ||
23 | + "#{source.name} <#{source.contact_email}>" | ||
24 | + end | ||
25 | + | ||
26 | + def generate_subject | ||
27 | + '[%s] %s' % [source.name, subject] | ||
28 | + end | ||
29 | + | ||
30 | + def signature_message | ||
31 | + _('Sent by Noosfero.') | ||
32 | + end | ||
33 | + | ||
34 | + def url | ||
35 | + '' | ||
36 | + end | ||
37 | + | ||
38 | + def deliver | ||
39 | + each_recipient do |recipient| | ||
40 | + begin | ||
41 | + Mailing::Sender.notification(self, recipient.email).deliver | ||
42 | + self.mailing_sents.create(:person => recipient) | ||
43 | + rescue Exception | ||
44 | + # FIXME should not discard errors silently. An idea is to collect all | ||
45 | + # errors and generate a task (notification) for the +source+ | ||
46 | + # (environment/organization) listing these errors. | ||
47 | + end | ||
48 | + end | ||
49 | + end | ||
50 | + | ||
51 | + class Sender < ActionMailer::Base | ||
52 | + def notification(mailing, recipient) | ||
53 | + @message = mailing.body | ||
54 | + @signature_message = mailing.signature_message | ||
55 | + @url = mailing.url | ||
56 | + mail( | ||
57 | + :content_type => 'text/html', | ||
58 | + :recipients => recipient, | ||
59 | + :from => mailing.generate_from, | ||
60 | + :reply_to => mailing.person.email, | ||
61 | + :subject => mailing.generate_subject | ||
62 | + ) | ||
63 | + end | ||
64 | + end | ||
65 | +end |
app/models/mailing.rb
@@ -1,63 +0,0 @@ | @@ -1,63 +0,0 @@ | ||
1 | -require 'mailing_job' | ||
2 | - | ||
3 | -class Mailing < ActiveRecord::Base | ||
4 | - | ||
5 | - attr_accessible :subject, :body | ||
6 | - validates_presence_of :source_id, :subject, :body | ||
7 | - belongs_to :source, :foreign_key => :source_id, :polymorphic => true | ||
8 | - belongs_to :person | ||
9 | - | ||
10 | - has_many :mailing_sents | ||
11 | - | ||
12 | - xss_terminate :only => [ :subject, :body ], :with => 'white_list', :on => 'validation' | ||
13 | - | ||
14 | - after_create do |mailing| | ||
15 | - mailing.schedule | ||
16 | - end | ||
17 | - | ||
18 | - def schedule | ||
19 | - Delayed::Job.enqueue MailingJob.new(self.id) | ||
20 | - end | ||
21 | - | ||
22 | - def generate_from | ||
23 | - "#{source.name} <#{source.contact_email}>" | ||
24 | - end | ||
25 | - | ||
26 | - def generate_subject | ||
27 | - '[%s] %s' % [source.name, subject] | ||
28 | - end | ||
29 | - | ||
30 | - def signature_message | ||
31 | - _('Sent by Noosfero.') | ||
32 | - end | ||
33 | - | ||
34 | - def url | ||
35 | - '' | ||
36 | - end | ||
37 | - | ||
38 | - def deliver | ||
39 | - each_recipient do |recipient| | ||
40 | - begin | ||
41 | - Mailing::Sender.deliver_mail(self, recipient.email) | ||
42 | - self.mailing_sents.create(:person => recipient) | ||
43 | - rescue Exception | ||
44 | - # FIXME should not discard errors silently. An idea is to collect all | ||
45 | - # errors and generate a task (notification) for the +source+ | ||
46 | - # (environment/organization) listing these errors. | ||
47 | - end | ||
48 | - end | ||
49 | - end | ||
50 | - | ||
51 | - class Sender < ActionMailer::Base | ||
52 | - def mail(mailing, recipient) | ||
53 | - content_type 'text/html' | ||
54 | - recipients recipient | ||
55 | - from mailing.generate_from | ||
56 | - reply_to mailing.person.email | ||
57 | - subject mailing.generate_subject | ||
58 | - body :message => mailing.body, | ||
59 | - :signature_message => mailing.signature_message, | ||
60 | - :url => mailing.url | ||
61 | - end | ||
62 | - end | ||
63 | -end |
app/views/mailing/sender/mail.html.erb
@@ -1,17 +0,0 @@ | @@ -1,17 +0,0 @@ | ||
1 | -<!DOCTYPE html> | ||
2 | -<html> | ||
3 | - <head> | ||
4 | - <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
5 | - </head> | ||
6 | - <body> | ||
7 | - <%= word_wrap(@message) %> | ||
8 | - <p> | ||
9 | - --<br/> | ||
10 | - <%= @signature_message %><br/> | ||
11 | - <%= @url %> | ||
12 | - </p> | ||
13 | - </body> | ||
14 | -</html> | ||
15 | - | ||
16 | - | ||
17 | - |
@@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
1 | +<!DOCTYPE html> | ||
2 | +<html> | ||
3 | + <head> | ||
4 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
5 | + </head> | ||
6 | + <body> | ||
7 | + <%= word_wrap(@message) %> | ||
8 | + <p> | ||
9 | + --<br/> | ||
10 | + <%= @signature_message %><br/> | ||
11 | + <%= @url %> | ||
12 | + </p> | ||
13 | + </body> | ||
14 | +</html> | ||
15 | + | ||
16 | + | ||
17 | + |
test/unit/mailing_test.rb
@@ -51,50 +51,50 @@ class MailingTest < ActiveSupport::TestCase | @@ -51,50 +51,50 @@ class MailingTest < ActiveSupport::TestCase | ||
51 | end | 51 | end |
52 | 52 | ||
53 | should 'return source' do | 53 | should 'return source' do |
54 | - mailing = Mailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news') | 54 | + mailing = create(Mailing, :source => environment, :subject => 'Hello', :body => 'We have some news') |
55 | assert_equal environment, Mailing.find(mailing.id).source | 55 | assert_equal environment, Mailing.find(mailing.id).source |
56 | end | 56 | end |
57 | 57 | ||
58 | should 'return source name' do | 58 | should 'return source name' do |
59 | - mailing = Mailing.new(:source => environment) | 59 | + mailing = build(Mailing, :source => environment) |
60 | assert_equal environment.name, mailing.source.name | 60 | assert_equal environment.name, mailing.source.name |
61 | end | 61 | end |
62 | 62 | ||
63 | should 'return source with source_id' do | 63 | should 'return source with source_id' do |
64 | - mailing = Mailing.new(:source => environment) | 64 | + mailing = build(Mailing, :source => environment) |
65 | assert_equal environment, mailing.source | 65 | assert_equal environment, mailing.source |
66 | end | 66 | end |
67 | 67 | ||
68 | should 'return person with person_id' do | 68 | should 'return person with person_id' do |
69 | person = Person['user_one'] | 69 | person = Person['user_one'] |
70 | - mailing = Mailing.new(:source => environment, :person => person) | 70 | + mailing = build(Mailing, :source => environment, :person => person) |
71 | assert_equal person, mailing.person | 71 | assert_equal person, mailing.person |
72 | end | 72 | end |
73 | 73 | ||
74 | should 'display name and email on generate_from' do | 74 | should 'display name and email on generate_from' do |
75 | person = Person['user_one'] | 75 | person = Person['user_one'] |
76 | - mailing = Mailing.new(:source => environment, :person => person) | 76 | + mailing = build(Mailing, :source => environment, :person => person) |
77 | assert_equal "#{environment.name} <#{environment.contact_email}>", mailing.generate_from | 77 | assert_equal "#{environment.name} <#{environment.contact_email}>", mailing.generate_from |
78 | end | 78 | end |
79 | 79 | ||
80 | should 'generate subject' do | 80 | should 'generate subject' do |
81 | - mailing = Mailing.new(:source => environment, :subject => 'Hello :)') | 81 | + mailing = build(Mailing, :source => environment, :subject => 'Hello :)') |
82 | assert_equal "[#{environment.name}] #{mailing.subject}", mailing.generate_subject | 82 | assert_equal "[#{environment.name}] #{mailing.subject}", mailing.generate_subject |
83 | end | 83 | end |
84 | 84 | ||
85 | should 'return signature message' do | 85 | should 'return signature message' do |
86 | - mailing = Mailing.new(:source => environment) | 86 | + mailing = build(Mailing, :source => environment) |
87 | assert_equal 'Sent by Noosfero.', mailing.signature_message | 87 | assert_equal 'Sent by Noosfero.', mailing.signature_message |
88 | end | 88 | end |
89 | 89 | ||
90 | should 'return blank string on url' do | 90 | should 'return blank string on url' do |
91 | - mailing = Mailing.new(:source => environment) | ||
92 | - environment.domains << Domain.create(:name => 'noosfero.net', :is_default => true) | 91 | + mailing = build(Mailing, :source => environment) |
92 | + environment.domains << create(Domain, :name => 'noosfero.net', :is_default => true) | ||
93 | assert_equal '', mailing.url | 93 | assert_equal '', mailing.url |
94 | end | 94 | end |
95 | 95 | ||
96 | should 'process the entire batch even if individual emails crash' do | 96 | should 'process the entire batch even if individual emails crash' do |
97 | - mailing = Mailing.new(:source => environment, :person => Person['user_one'], :body => 'test', :subject => 'test') | 97 | + mailing = build(Mailing, :source => environment, :person => Person['user_one'], :body => 'test', :subject => 'test') |
98 | def mailing.each_recipient | 98 | def mailing.each_recipient |
99 | user_one = Person['user_one'] | 99 | user_one = Person['user_one'] |
100 | user_two = Person['user_two'] | 100 | user_two = Person['user_two'] |