flipstone-ar_mailer 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ # This is a configuration file for ar_sendmail daemon init.d script
2
+ #
3
+ # Define the settings for each app which has a mailer that you want start
4
+ # automatically on startup.
5
+ #
6
+ # You can define an optional defaults section which will apply to all
7
+ # applications unless the setting is specified under the applications specific
8
+ # config.
9
+ #
10
+ # Settings not specified in either the defaults or app config section will
11
+ # be implied by the gem binary defaults. The option names are the same as the
12
+ # long format binary option switches. Run 'ar_sendmail -h' to see option switches
13
+ # and default values.
14
+ #
15
+ # Copy this file to /etc/ar_sendmail.conf and it will be read by the init.d
16
+ # script.
17
+ #
18
+
19
+ ## Demo app config
20
+ #
21
+ #defaults:
22
+ # batch-size: 10
23
+ # max-age: 0
24
+ # delay: 60
25
+ #
26
+ #app_name:
27
+ # chdir: /var/www/apps/app_name
28
+ # environment: production
29
+ # pidfile: ./log/ar_sendmail.pid
30
+
@@ -0,0 +1,218 @@
1
+ require 'net/smtp'
2
+ require 'smtp_tls' unless Net::SMTP.instance_methods.include?("enable_starttls_auto")
3
+ require 'time'
4
+
5
+ class Net::SMTP
6
+
7
+ @reset_called = 0
8
+
9
+ @deliveries = []
10
+
11
+ @send_message_block = nil
12
+
13
+ @start_block = nil
14
+
15
+ class << self
16
+
17
+ attr_reader :deliveries
18
+ attr_reader :send_message_block
19
+ attr_accessor :reset_called
20
+
21
+ # send :remove_method, :start
22
+ end
23
+
24
+ def self.on_send_message(&block)
25
+ @send_message_block = block
26
+ end
27
+
28
+ def self.on_start(&block)
29
+ if block_given?
30
+ @start_block = block
31
+ else
32
+ @start_block
33
+ end
34
+ end
35
+
36
+ def self.clear_on_start
37
+ @start_block = nil
38
+ end
39
+
40
+ def self.reset
41
+ deliveries.clear
42
+ on_start
43
+ on_send_message
44
+ @reset_called = 0
45
+ end
46
+
47
+ def start(*args)
48
+ self.class.on_start.call if self.class.on_start
49
+ yield self
50
+ end
51
+
52
+ alias test_old_reset reset if instance_methods.include? 'reset'
53
+
54
+ def reset
55
+ self.class.reset_called += 1
56
+ end
57
+
58
+ alias test_old_send_message send_message
59
+
60
+ def send_message(mail, to, from)
61
+ return self.class.send_message_block.call(mail, to, from) unless
62
+ self.class.send_message_block.nil?
63
+ self.class.deliveries << [mail, to, from]
64
+ return "queued"
65
+ end
66
+
67
+ end
68
+
69
+ ##
70
+ # Stub for ActionMailer::Base
71
+
72
+ module ActionMailer; end
73
+
74
+ class ActionMailer::Base
75
+
76
+ @server_settings = {}
77
+
78
+ class << self
79
+ cattr_accessor :email_class
80
+ attr_accessor :delivery_method
81
+ end
82
+
83
+ def self.logger
84
+ o = Object.new
85
+ def o.info(arg) end
86
+ return o
87
+ end
88
+
89
+ def self.method_missing(meth, *args)
90
+ meth.to_s =~ /deliver_(.*)/
91
+ super unless $1
92
+ new($1, *args).deliver!
93
+ end
94
+
95
+ def self.reset
96
+ server_settings.clear
97
+ self.email_class = Email
98
+ end
99
+
100
+ def self.server_settings
101
+ @server_settings
102
+ end
103
+
104
+ def initialize(meth = nil)
105
+ send meth if meth
106
+ end
107
+
108
+ def deliver!
109
+ ArMailer::ActiveRecord.new.deliver!(@mail)
110
+ end
111
+
112
+ def self.add_delivery_method(*args)
113
+
114
+ end
115
+
116
+ end
117
+
118
+ module Mail
119
+ class Message
120
+
121
+ end
122
+ end
123
+
124
+ ##
125
+ # Stub for an ActiveRecord model
126
+
127
+ class Email
128
+
129
+ START = Time.parse 'Thu Aug 10 2006 11:19:48'
130
+
131
+ attr_accessor :from, :to, :mail, :last_send_attempt, :created_on, :id,
132
+ :failed_at, :failure_message
133
+
134
+ @records = []
135
+ @id = 0
136
+
137
+ class << self; attr_accessor :records, :id; end
138
+
139
+ def self.create(record)
140
+ record = new record[:from], record[:to], record[:mail],
141
+ record[:last_send_attempt],
142
+ record[:failed_at],
143
+ record[:failure_message]
144
+
145
+ records << record
146
+ return record
147
+ end
148
+
149
+ def self.destroy_all(conditions)
150
+ timeout = conditions.last
151
+ found = []
152
+
153
+ records.each do |record|
154
+ next if record.last_send_attempt == 0
155
+ next if record.created_on == 0
156
+ next unless record.created_on < timeout
157
+ record.destroy
158
+ found << record
159
+ end
160
+
161
+ found
162
+ end
163
+
164
+ def self.find(_, options = nil)
165
+ return records if options.nil?
166
+ if options[:conditions] &&
167
+ options[:conditions].first == 'failed_at IS NULL AND last_send_attempt < ?'
168
+ now = options[:conditions].last
169
+ return records.select do |r|
170
+ r.failed_at.nil? && r.last_send_attempt < now
171
+ end
172
+ else
173
+ raise "Query not handled by mock :#{options.inspect}. Beware that your sql is not actually tested"
174
+ end
175
+ end
176
+
177
+ def self.reset
178
+ @id = 0
179
+ records.clear
180
+ end
181
+
182
+ def initialize(from, to, mail, last_send_attempt = nil, failed_at = nil, failure_message = nil)
183
+ @from = from
184
+ @to = to
185
+ @mail = mail
186
+ @id = self.class.id += 1
187
+ @created_on = START + @id
188
+ @last_send_attempt = last_send_attempt || 0
189
+ @failed_at = failed_at
190
+ @failure_message = failure_message
191
+ end
192
+
193
+ def destroy
194
+ self.class.records.delete self
195
+ self.freeze
196
+ end
197
+
198
+ def ==(other)
199
+ other.id == id
200
+ end
201
+
202
+ def save
203
+ end
204
+
205
+ end
206
+
207
+ Newsletter = Email
208
+
209
+ class String
210
+ def classify
211
+ self
212
+ end
213
+
214
+ def tableize
215
+ self.downcase
216
+ end
217
+
218
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class Mailer < ActionMailer::Base
4
+ self.delivery_method = :activerecord
5
+
6
+ def mail
7
+ @mail = Mail::Message.new
8
+ def @mail.encoded() 'email' end
9
+ def @mail.from() ['nobody@example.com'] end
10
+ def @mail.[](key) {'return-path' => $return_path, 'from' => 'nobody@example.com'}[key] end
11
+ def @mail.destinations() %w[user1@example.com user2@example.com] end
12
+ def @mail.ready_to_send() end
13
+ end
14
+
15
+ end
16
+
17
+ class TestARMailer < Test::Unit::TestCase
18
+
19
+ def setup
20
+ $return_path = nil
21
+ Mailer.email_class = Email
22
+
23
+ Email.records.clear
24
+ Newsletter.records.clear
25
+ end
26
+
27
+ def test_self_email_class_equals
28
+ Mailer.email_class = Newsletter
29
+
30
+ Mailer.deliver_mail
31
+
32
+ assert_equal 2, Newsletter.records.length
33
+ end
34
+
35
+ def test_perform_delivery_activerecord_when_return_path_is_present
36
+ $return_path = stub(:spec => 'return-path@example.com')
37
+ Mailer.deliver_mail
38
+
39
+ assert_equal 2, Email.records.length
40
+ record = Email.records.first
41
+ assert_equal 'return-path@example.com', record.from
42
+ end
43
+
44
+ def test_perform_delivery_activerecord
45
+ Mailer.deliver_mail
46
+
47
+ assert_equal 2, Email.records.length
48
+
49
+ record = Email.records.first
50
+ assert_equal 'email', record.mail
51
+ assert_equal 'user1@example.com', record.to
52
+ assert_equal 'nobody@example.com', record.from
53
+
54
+ assert_equal 'user2@example.com', Email.records.last.to
55
+ end
56
+
57
+ end
58
+