foresth-ar_mailer 2.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 = Object.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
+
@@ -0,0 +1,528 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class ActionMailer::ARSendmail
4
+ attr_accessor :slept
5
+ def sleep(secs)
6
+ @slept ||= []
7
+ @slept << secs
8
+ end
9
+ end
10
+
11
+ class TestARSendmail < MiniTest::Unit::TestCase
12
+
13
+ def setup
14
+ ActionMailer::Base.reset
15
+ Email.reset
16
+ Net::SMTP.reset
17
+
18
+ @sm = ActionMailer::ARSendmail.new
19
+ @sm.verbose = true
20
+
21
+ Net::SMTP.clear_on_start
22
+
23
+ @include_c_e = ! $".grep(/config\/environment.rb/).empty?
24
+ $" << 'config/environment.rb' unless @include_c_e
25
+ end
26
+
27
+ def teardown
28
+ $".delete 'config/environment.rb' unless @include_c_e
29
+ end
30
+
31
+ def test_class_mailq
32
+ Email.create :from => nobody, :to => 'recip@h1.example.com',
33
+ :mail => 'body0'
34
+ Email.create :from => nobody, :to => 'recip@h1.example.com',
35
+ :mail => 'body1'
36
+ last = Email.create :from => nobody, :to => 'recip@h2.example.com',
37
+ :mail => 'body2'
38
+ last_attempt_time = Time.parse('Thu Aug 10 2006 11:40:05')
39
+ last.last_send_attempt = last_attempt_time.to_i
40
+
41
+ out, err = capture_io do
42
+ ActionMailer::ARSendmail.mailq
43
+ end
44
+
45
+ expected = <<-EOF
46
+ -Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
47
+ 1 5 Thu Aug 10 11:19:49 nobody@example.com
48
+ recip@h1.example.com
49
+
50
+ 2 5 Thu Aug 10 11:19:50 nobody@example.com
51
+ recip@h1.example.com
52
+
53
+ 3 5 Thu Aug 10 11:19:51 nobody@example.com
54
+ Last send attempt: Thu Aug 10 11:40:05 %s 2006
55
+ recip@h2.example.com
56
+
57
+ -- 0 Kbytes in 3 Requests.
58
+ EOF
59
+
60
+ expected = expected % last_attempt_time.strftime('%z')
61
+ assert_equal expected, out
62
+ end
63
+
64
+ def test_class_mailq_empty
65
+ out, err = capture_io do
66
+ ActionMailer::ARSendmail.mailq
67
+ end
68
+
69
+ assert_equal "Mail queue is empty\n", out
70
+ end
71
+
72
+ def test_class_new
73
+ @sm = ActionMailer::ARSendmail.new
74
+
75
+ assert_equal 60, @sm.delay
76
+ assert_equal nil, @sm.once
77
+ assert_equal nil, @sm.verbose
78
+ assert_equal nil, @sm.batch_size
79
+
80
+ @sm = ActionMailer::ARSendmail.new :Delay => 75, :Verbose => true,
81
+ :Once => true, :BatchSize => 1000
82
+
83
+ assert_equal 75, @sm.delay
84
+ assert_equal true, @sm.once
85
+ assert_equal true, @sm.verbose
86
+ assert_equal 1000, @sm.batch_size
87
+ end
88
+
89
+ def test_class_parse_args_batch_size
90
+ options = ActionMailer::ARSendmail.process_args %w[-b 500]
91
+
92
+ assert_equal 500, options[:BatchSize]
93
+
94
+ options = ActionMailer::ARSendmail.process_args %w[--batch-size 500]
95
+
96
+ assert_equal 500, options[:BatchSize]
97
+ end
98
+
99
+ def test_class_parse_args_chdir
100
+ argv = %w[-c /tmp]
101
+
102
+ options = ActionMailer::ARSendmail.process_args argv
103
+
104
+ assert_equal '/tmp', options[:Chdir]
105
+
106
+ argv = %w[--chdir /tmp]
107
+
108
+ options = ActionMailer::ARSendmail.process_args argv
109
+
110
+ assert_equal '/tmp', options[:Chdir]
111
+
112
+ argv = %w[-c /nonexistent]
113
+
114
+ out, err = capture_io do
115
+ assert_raises SystemExit do
116
+ ActionMailer::ARSendmail.process_args argv
117
+ end
118
+ end
119
+ end
120
+
121
+ def test_class_parse_args_daemon
122
+ argv = %w[-d]
123
+
124
+ options = ActionMailer::ARSendmail.process_args argv
125
+
126
+ assert_equal true, options[:Daemon]
127
+
128
+ argv = %w[--daemon]
129
+
130
+ options = ActionMailer::ARSendmail.process_args argv
131
+
132
+ assert_equal true, options[:Daemon]
133
+ end
134
+
135
+ def test_class_parse_args_pidfile
136
+ argv = %w[-p ./log/ar_sendmail.pid]
137
+
138
+ options = ActionMailer::ARSendmail.process_args argv
139
+
140
+ assert_equal './log/ar_sendmail.pid', options[:Pidfile]
141
+
142
+ argv = %w[--pidfile ./log/ar_sendmail.pid]
143
+
144
+ options = ActionMailer::ARSendmail.process_args argv
145
+
146
+ assert_equal './log/ar_sendmail.pid', options[:Pidfile]
147
+ end
148
+
149
+ def test_class_parse_args_delay
150
+ argv = %w[--delay 75]
151
+
152
+ options = ActionMailer::ARSendmail.process_args argv
153
+
154
+ assert_equal 75, options[:Delay]
155
+ end
156
+
157
+ def test_class_parse_args_environment
158
+ assert_equal nil, ENV['RAILS_ENV']
159
+
160
+ argv = %w[-e production]
161
+
162
+ options = ActionMailer::ARSendmail.process_args argv
163
+
164
+ assert_equal 'production', options[:RailsEnv]
165
+
166
+ assert_equal 'production', ENV['RAILS_ENV']
167
+
168
+ argv = %w[--environment production]
169
+
170
+ options = ActionMailer::ARSendmail.process_args argv
171
+
172
+ assert_equal 'production', options[:RailsEnv]
173
+ end
174
+
175
+ def test_class_parse_args_mailq
176
+ options = ActionMailer::ARSendmail.process_args []
177
+ refute_includes options, :MailQ
178
+
179
+ argv = %w[--mailq]
180
+
181
+ options = ActionMailer::ARSendmail.process_args argv
182
+
183
+ assert_equal true, options[:MailQ]
184
+ end
185
+
186
+ def test_class_parse_args_max_age
187
+ options = ActionMailer::ARSendmail.process_args []
188
+ assert_equal 86400 * 7, options[:MaxAge]
189
+
190
+ argv = %w[--max-age 86400]
191
+
192
+ options = ActionMailer::ARSendmail.process_args argv
193
+
194
+ assert_equal 86400, options[:MaxAge]
195
+ end
196
+
197
+ def test_class_parse_args_no_config_environment
198
+ $".delete 'config/environment.rb'
199
+
200
+ out, err = capture_io do
201
+ assert_raises SystemExit do
202
+ ActionMailer::ARSendmail.process_args []
203
+ end
204
+ end
205
+
206
+ ensure
207
+ $" << 'config/environment.rb' if @include_c_e
208
+ end
209
+
210
+ def test_class_parse_args_once
211
+ argv = %w[-o]
212
+
213
+ options = ActionMailer::ARSendmail.process_args argv
214
+
215
+ assert_equal true, options[:Once]
216
+
217
+ argv = %w[--once]
218
+
219
+ options = ActionMailer::ARSendmail.process_args argv
220
+
221
+ assert_equal true, options[:Once]
222
+ end
223
+
224
+ def test_class_usage
225
+ out, err = capture_io do
226
+ assert_raises SystemExit do
227
+ ActionMailer::ARSendmail.usage 'opts'
228
+ end
229
+ end
230
+
231
+ assert_equal '', out
232
+ assert_equal "opts\n", err
233
+
234
+ out, err = capture_io do
235
+ assert_raises SystemExit do
236
+ ActionMailer::ARSendmail.usage 'opts', 'hi'
237
+ end
238
+ end
239
+
240
+ assert_equal '', out
241
+ assert_equal "hi\n\nopts\n", err
242
+ end
243
+
244
+ def test_cleanup
245
+ e1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
246
+ e1.created_on = Time.now
247
+ e2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
248
+ e3 = Email.create :mail => 'body', :to => 'to', :from => 'from'
249
+ e3.last_send_attempt = Time.now
250
+
251
+ out, err = capture_io do
252
+ @sm.cleanup
253
+ end
254
+
255
+ assert_equal '', out
256
+ assert_equal "expired 1 emails from the queue\n", err
257
+ assert_equal 2, Email.records.length
258
+
259
+ assert_equal [e1, e2], Email.records
260
+ end
261
+
262
+ def test_cleanup_disabled
263
+ e1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
264
+ e1.created_on = Time.now
265
+ e2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
266
+
267
+ @sm.max_age = 0
268
+
269
+ out, err = capture_io do
270
+ @sm.cleanup
271
+ end
272
+
273
+ assert_equal '', out
274
+ assert_equal 2, Email.records.length
275
+ end
276
+
277
+ def test_deliver
278
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
279
+
280
+ out, err = capture_io do
281
+ @sm.deliver [email]
282
+ end
283
+
284
+ assert_equal 1, Net::SMTP.deliveries.length
285
+ assert_equal ['body', 'from', 'to'], Net::SMTP.deliveries.first
286
+ assert_equal 0, Email.records.length
287
+ assert_equal 0, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
288
+
289
+ assert_equal '', out
290
+ assert_equal "sent email 00000000001 from from to to: \"queued\"\n", err
291
+ end
292
+
293
+ def test_deliver_not_called_when_no_emails
294
+ sm = ActionMailer::ARSendmail.new({:Once => true})
295
+ sm.expects(:deliver).never
296
+ sm.run
297
+ end
298
+
299
+ def test_deliver_auth_error
300
+ Net::SMTP.on_start do
301
+ e = Net::SMTPAuthenticationError.new 'try again'
302
+ e.set_backtrace %w[one two three]
303
+ raise e
304
+ end
305
+
306
+ now = Time.now.to_i
307
+
308
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
309
+
310
+ out, err = capture_io do
311
+ @sm.deliver [email]
312
+ end
313
+
314
+ assert_equal 0, Net::SMTP.deliveries.length
315
+ assert_equal 1, Email.records.length
316
+ assert_equal 0, Email.records.first.last_send_attempt
317
+ assert_equal 0, Net::SMTP.reset_called
318
+ assert_equal 1, @sm.failed_auth_count
319
+ assert_equal [60], @sm.slept
320
+
321
+ assert_equal '', out
322
+ assert_equal "authentication error, retrying: try again\n", err
323
+ end
324
+
325
+ def test_deliver_auth_error_recover
326
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
327
+ @sm.failed_auth_count = 1
328
+
329
+ out, err = capture_io do @sm.deliver [email] end
330
+
331
+ assert_equal 0, @sm.failed_auth_count
332
+ assert_equal 1, Net::SMTP.deliveries.length
333
+ end
334
+
335
+ def test_deliver_auth_error_twice
336
+ Net::SMTP.on_start do
337
+ e = Net::SMTPAuthenticationError.new 'try again'
338
+ e.set_backtrace %w[one two three]
339
+ raise e
340
+ end
341
+
342
+ @sm.failed_auth_count = 1
343
+
344
+ out, err = capture_io do
345
+ assert_raises Net::SMTPAuthenticationError do
346
+ @sm.deliver []
347
+ end
348
+ end
349
+
350
+ assert_equal 2, @sm.failed_auth_count
351
+ assert_equal "authentication error, giving up: try again\n", err
352
+ end
353
+
354
+ def test_deliver_4xx_error
355
+ Net::SMTP.on_send_message do
356
+ e = Net::SMTPSyntaxError.new 'try again'
357
+ e.set_backtrace %w[one two three]
358
+ raise e
359
+ end
360
+
361
+ now = Time.now.to_i
362
+
363
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
364
+
365
+ out, err = capture_io do
366
+ @sm.deliver [email]
367
+ end
368
+
369
+ assert_equal 0, Net::SMTP.deliveries.length
370
+ assert_equal 1, Email.records.length
371
+ assert_operator now, :<=, Email.records.first.last_send_attempt
372
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
373
+
374
+ assert_equal '', out
375
+ assert_equal "error sending email 1: \"try again\"(Net::SMTPSyntaxError):\n\tone\n\ttwo\n\tthree\n", err
376
+ end
377
+
378
+ def test_deliver_5xx_error
379
+ Net::SMTP.on_send_message do
380
+ e = Net::SMTPFatalError.new 'unknown recipient'
381
+ e.set_backtrace %w[one two three]
382
+ raise e
383
+ end
384
+
385
+ now = Time.now.to_i
386
+
387
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
388
+
389
+ out, err = capture_io do
390
+ @sm.deliver [email]
391
+ end
392
+
393
+ assert_equal 0, Net::SMTP.deliveries.length
394
+ assert_equal 0, Email.records.length
395
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
396
+
397
+ assert_equal '', out
398
+ assert_equal "5xx error sending email 1, removing from queue: \"unknown recipient\"(Net::SMTPFatalError):\n\tone\n\ttwo\n\tthree\n", err
399
+ end
400
+
401
+ def test_deliver_errno_epipe
402
+ Net::SMTP.on_send_message do
403
+ raise Errno::EPIPE
404
+ end
405
+
406
+ now = Time.now.to_i
407
+
408
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
409
+
410
+ out, err = capture_io do
411
+ @sm.deliver [email]
412
+ end
413
+
414
+ assert_equal 0, Net::SMTP.deliveries.length
415
+ assert_equal 1, Email.records.length
416
+ assert_operator now, :>=, Email.records.first.last_send_attempt
417
+ assert_equal 0, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
418
+
419
+ assert_equal '', out
420
+ assert_equal '', err
421
+ end
422
+
423
+ def test_deliver_syntax_error
424
+ Net::SMTP.on_send_message do
425
+ Net::SMTP.on_send_message # clear
426
+ e = Net::SMTPSyntaxError.new 'blah blah blah'
427
+ e.set_backtrace %w[one two three]
428
+ raise e
429
+ end
430
+
431
+ now = Time.now.to_i
432
+
433
+ email1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
434
+ email2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
435
+
436
+ out, err = capture_io do
437
+ @sm.deliver [email1, email2]
438
+ end
439
+
440
+ assert_equal 1, Net::SMTP.deliveries.length, 'delivery count'
441
+ assert_equal 1, Email.records.length
442
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
443
+ assert_operator now, :<=, Email.records.first.last_send_attempt
444
+
445
+ assert_equal '', out
446
+ assert_equal "error sending email 1: \"blah blah blah\"(Net::SMTPSyntaxError):\n\tone\n\ttwo\n\tthree\nsent email 00000000002 from from to to: \"queued\"\n", err
447
+ end
448
+
449
+ def test_deliver_timeout
450
+ Net::SMTP.on_send_message do
451
+ e = Timeout::Error.new 'timed out'
452
+ e.set_backtrace %w[one two three]
453
+ raise e
454
+ end
455
+
456
+ now = Time.now.to_i
457
+
458
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
459
+
460
+ out, err = capture_io do
461
+ @sm.deliver [email]
462
+ end
463
+
464
+ assert_equal 0, Net::SMTP.deliveries.length
465
+ assert_equal 1, Email.records.length
466
+ assert_operator now, :>=, Email.records.first.last_send_attempt
467
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on Timeout'
468
+
469
+ assert_equal '', out
470
+ assert_equal "error sending email 1: \"timed out\"(Timeout::Error):\n\tone\n\ttwo\n\tthree\n", err
471
+ end
472
+
473
+ def test_do_exit
474
+ out, err = capture_io do
475
+ assert_raises SystemExit do
476
+ @sm.do_exit
477
+ end
478
+ end
479
+
480
+ assert_equal '', out
481
+ assert_equal "caught signal, shutting down\n", err
482
+ end
483
+
484
+ def test_log
485
+ out, err = capture_io do
486
+ @sm.log 'hi'
487
+ end
488
+
489
+ assert_equal "hi\n", err
490
+ end
491
+
492
+ def test_find_emails
493
+ email_data = [
494
+ { :mail => 'body0', :to => 'recip@h1.example.com', :from => nobody },
495
+ { :mail => 'body1', :to => 'recip@h1.example.com', :from => nobody },
496
+ { :mail => 'body2', :to => 'recip@h2.example.com', :from => nobody },
497
+ ]
498
+
499
+ emails = email_data.map do |email_data| Email.create email_data end
500
+
501
+ tried = Email.create :mail => 'body3', :to => 'recip@h3.example.com',
502
+ :from => nobody
503
+
504
+ tried.last_send_attempt = Time.now.to_i - 258
505
+
506
+ found_emails = []
507
+
508
+ out, err = capture_io do
509
+ found_emails = @sm.find_emails
510
+ end
511
+
512
+ assert_equal emails, found_emails
513
+
514
+ assert_equal '', out
515
+ assert_equal "found 3 emails to send\n", err
516
+ end
517
+
518
+ def test_smtp_settings
519
+ ActionMailer::Base.server_settings[:address] = 'localhost'
520
+
521
+ assert_equal 'localhost', @sm.smtp_settings[:address]
522
+ end
523
+
524
+ def nobody
525
+ 'nobody@example.com'
526
+ end
527
+
528
+ end