leftbrained-ar_mailer 2.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,576 @@
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
+ assert_equal "", @sm.worker
80
+
81
+ @sm = ActionMailer::ARSendmail.new :Delay => 75, :Verbose => true,
82
+ :Once => true, :BatchSize => 1000, :Worker=>"Pat"
83
+
84
+ assert_equal 75, @sm.delay
85
+ assert_equal true, @sm.once
86
+ assert_equal true, @sm.verbose
87
+ assert_equal 1000, @sm.batch_size
88
+ assert_equal "Pat", @sm.worker
89
+ end
90
+
91
+ def test_class_parse_args_batch_size
92
+ options = ActionMailer::ARSendmail.process_args %w[-b 500]
93
+
94
+ assert_equal 500, options[:BatchSize]
95
+
96
+ options = ActionMailer::ARSendmail.process_args %w[--batch-size 500]
97
+
98
+ assert_equal 500, options[:BatchSize]
99
+ end
100
+
101
+ def test_class_parse_args_chdir
102
+ argv = %w[-c /tmp]
103
+
104
+ options = ActionMailer::ARSendmail.process_args argv
105
+
106
+ assert_equal '/tmp', options[:Chdir]
107
+
108
+ argv = %w[--chdir /tmp]
109
+
110
+ options = ActionMailer::ARSendmail.process_args argv
111
+
112
+ assert_equal '/tmp', options[:Chdir]
113
+
114
+ argv = %w[-c /nonexistent]
115
+
116
+ out, err = capture_io do
117
+ assert_raises SystemExit do
118
+ ActionMailer::ARSendmail.process_args argv
119
+ end
120
+ end
121
+ end
122
+
123
+ def test_class_parse_args_daemon
124
+ argv = %w[-d]
125
+
126
+ options = ActionMailer::ARSendmail.process_args argv
127
+
128
+ assert_equal true, options[:Daemon]
129
+
130
+ argv = %w[--daemon]
131
+
132
+ options = ActionMailer::ARSendmail.process_args argv
133
+
134
+ assert_equal true, options[:Daemon]
135
+ end
136
+
137
+ def test_class_parse_args_pidfile
138
+ argv = %w[-p ./log/ar_sendmail.pid]
139
+
140
+ options = ActionMailer::ARSendmail.process_args argv
141
+
142
+ assert_equal './log/ar_sendmail.pid', options[:Pidfile]
143
+
144
+ argv = %w[--pidfile ./log/ar_sendmail.pid]
145
+
146
+ options = ActionMailer::ARSendmail.process_args argv
147
+
148
+ assert_equal './log/ar_sendmail.pid', options[:Pidfile]
149
+ end
150
+
151
+ def test_class_parse_args_delay
152
+ argv = %w[--delay 75]
153
+
154
+ options = ActionMailer::ARSendmail.process_args argv
155
+
156
+ assert_equal 75, options[:Delay]
157
+ end
158
+
159
+ def test_class_parse_args_environment
160
+ assert_equal nil, ENV['RAILS_ENV']
161
+
162
+ argv = %w[-e production]
163
+
164
+ options = ActionMailer::ARSendmail.process_args argv
165
+
166
+ assert_equal 'production', options[:RailsEnv]
167
+
168
+ assert_equal 'production', ENV['RAILS_ENV']
169
+
170
+ argv = %w[--environment production]
171
+
172
+ options = ActionMailer::ARSendmail.process_args argv
173
+
174
+ assert_equal 'production', options[:RailsEnv]
175
+ end
176
+
177
+ def test_class_parse_args_mailq
178
+ options = ActionMailer::ARSendmail.process_args []
179
+ refute_includes options, :MailQ
180
+
181
+ argv = %w[--mailq]
182
+
183
+ options = ActionMailer::ARSendmail.process_args argv
184
+
185
+ assert_equal true, options[:MailQ]
186
+ end
187
+
188
+ def test_class_parse_args_max_age
189
+ options = ActionMailer::ARSendmail.process_args []
190
+ assert_equal 86400 * 7, options[:MaxAge]
191
+
192
+ argv = %w[--max-age 86400]
193
+
194
+ options = ActionMailer::ARSendmail.process_args argv
195
+
196
+ assert_equal 86400, options[:MaxAge]
197
+ end
198
+
199
+ def test_class_parse_args_no_config_environment
200
+ $".delete 'config/environment.rb'
201
+
202
+ out, err = capture_io do
203
+ assert_raises SystemExit do
204
+ ActionMailer::ARSendmail.process_args []
205
+ end
206
+ end
207
+
208
+ ensure
209
+ $" << 'config/environment.rb' if @include_c_e
210
+ end
211
+
212
+ def test_class_parse_args_once
213
+ argv = %w[-o]
214
+
215
+ options = ActionMailer::ARSendmail.process_args argv
216
+
217
+ assert_equal true, options[:Once]
218
+
219
+ argv = %w[--once]
220
+
221
+ options = ActionMailer::ARSendmail.process_args argv
222
+
223
+ assert_equal true, options[:Once]
224
+ end
225
+
226
+ def test_class_parse_args_queue
227
+ options = ActionMailer::ARSendmail.process_args %w[-w pat]
228
+
229
+ assert_equal "pat", options[:Worker]
230
+
231
+ options = ActionMailer::ARSendmail.process_args %w[--worker pat]
232
+
233
+ assert_equal "pat", options[:Worker]
234
+ end
235
+
236
+ def test_class_usage
237
+ out, err = capture_io do
238
+ assert_raises SystemExit do
239
+ ActionMailer::ARSendmail.usage 'opts'
240
+ end
241
+ end
242
+
243
+ assert_equal '', out
244
+ assert_equal "opts\n", err
245
+
246
+ out, err = capture_io do
247
+ assert_raises SystemExit do
248
+ ActionMailer::ARSendmail.usage 'opts', 'hi'
249
+ end
250
+ end
251
+
252
+ assert_equal '', out
253
+ assert_equal "hi\n\nopts\n", err
254
+ end
255
+
256
+ def test_cleanup
257
+ e1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
258
+ e1.created_on = Time.now
259
+ e2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
260
+ e3 = Email.create :mail => 'body', :to => 'to', :from => 'from'
261
+ e3.last_send_attempt = Time.now
262
+
263
+ out, err = capture_io do
264
+ @sm.cleanup
265
+ end
266
+
267
+ assert_equal '', out
268
+ assert_equal "expired 1 emails from the queue\n", err
269
+ assert_equal 2, Email.records.length
270
+
271
+ assert_equal [e1, e2], Email.records
272
+ end
273
+
274
+ def test_cleanup_disabled
275
+ e1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
276
+ e1.created_on = Time.now
277
+ e2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
278
+
279
+ @sm.max_age = 0
280
+
281
+ out, err = capture_io do
282
+ @sm.cleanup
283
+ end
284
+
285
+ assert_equal '', out
286
+ assert_equal 2, Email.records.length
287
+ end
288
+
289
+ def test_deliver
290
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
291
+
292
+ out, err = capture_io do
293
+ @sm.deliver [email]
294
+ end
295
+
296
+ assert_equal 1, Net::SMTP.deliveries.length
297
+ assert_equal ['body', 'from', 'to'], Net::SMTP.deliveries.first
298
+ assert_equal 0, Email.records.length
299
+ assert_equal 0, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
300
+
301
+ assert_equal '', out
302
+ assert_equal "sent email 00000000001 from from to to: \"queued\"\n", err
303
+ end
304
+
305
+ def test_deliver_not_called_when_no_emails
306
+ sm = ActionMailer::ARSendmail.new({:Once => true})
307
+ sm.expects(:deliver).never
308
+ sm.run
309
+ end
310
+
311
+ def test_deliver_auth_error
312
+ Net::SMTP.on_start do
313
+ e = Net::SMTPAuthenticationError.new 'try again'
314
+ e.set_backtrace %w[one two three]
315
+ raise e
316
+ end
317
+
318
+ now = Time.now.to_i
319
+
320
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
321
+
322
+ out, err = capture_io do
323
+ @sm.deliver [email]
324
+ end
325
+
326
+ assert_equal 0, Net::SMTP.deliveries.length
327
+ assert_equal 1, Email.records.length
328
+ assert_equal 0, Email.records.first.last_send_attempt
329
+ assert_equal 0, Net::SMTP.reset_called
330
+ assert_equal 1, @sm.failed_auth_count
331
+ assert_equal [60], @sm.slept
332
+
333
+ assert_equal '', out
334
+ assert_equal "authentication error, retrying: try again\n", err
335
+ end
336
+
337
+ def test_deliver_auth_error_recover
338
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
339
+ @sm.failed_auth_count = 1
340
+
341
+ out, err = capture_io do @sm.deliver [email] end
342
+
343
+ assert_equal 0, @sm.failed_auth_count
344
+ assert_equal 1, Net::SMTP.deliveries.length
345
+ end
346
+
347
+ def test_deliver_auth_error_twice
348
+ Net::SMTP.on_start do
349
+ e = Net::SMTPAuthenticationError.new 'try again'
350
+ e.set_backtrace %w[one two three]
351
+ raise e
352
+ end
353
+
354
+ @sm.failed_auth_count = 1
355
+
356
+ out, err = capture_io do
357
+ assert_raises Net::SMTPAuthenticationError do
358
+ @sm.deliver []
359
+ end
360
+ end
361
+
362
+ assert_equal 2, @sm.failed_auth_count
363
+ assert_equal "authentication error, giving up: try again\n", err
364
+ end
365
+
366
+ def test_deliver_4xx_error
367
+ Net::SMTP.on_send_message do
368
+ e = Net::SMTPSyntaxError.new 'try again'
369
+ e.set_backtrace %w[one two three]
370
+ raise e
371
+ end
372
+
373
+ now = Time.now.to_i
374
+
375
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
376
+
377
+ out, err = capture_io do
378
+ @sm.deliver [email]
379
+ end
380
+
381
+ assert_equal 0, Net::SMTP.deliveries.length
382
+ assert_equal 1, Email.records.length
383
+ assert_operator now, :<=, Email.records.first.last_send_attempt
384
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
385
+
386
+ assert_equal '', out
387
+ assert_equal "error sending email 1: \"try again\"(Net::SMTPSyntaxError):\n\tone\n\ttwo\n\tthree\n", err
388
+ end
389
+
390
+ def test_deliver_5xx_error
391
+ Net::SMTP.on_send_message do
392
+ e = Net::SMTPFatalError.new 'unknown recipient'
393
+ e.set_backtrace %w[one two three]
394
+ raise e
395
+ end
396
+
397
+ now = Time.now.to_i
398
+
399
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
400
+
401
+ out, err = capture_io do
402
+ @sm.deliver [email]
403
+ end
404
+
405
+ assert_equal 0, Net::SMTP.deliveries.length
406
+ assert_equal 0, Email.records.length
407
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
408
+
409
+ assert_equal '', out
410
+ assert_equal "5xx error sending email 1, removing from queue: \"unknown recipient\"(Net::SMTPFatalError):\n\tone\n\ttwo\n\tthree\n", err
411
+ end
412
+
413
+ def test_deliver_errno_epipe
414
+ Net::SMTP.on_send_message do
415
+ raise Errno::EPIPE
416
+ end
417
+
418
+ now = Time.now.to_i
419
+
420
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
421
+
422
+ out, err = capture_io do
423
+ @sm.deliver [email]
424
+ end
425
+
426
+ assert_equal 0, Net::SMTP.deliveries.length
427
+ assert_equal 1, Email.records.length
428
+ assert_operator now, :>=, Email.records.first.last_send_attempt
429
+ assert_equal 0, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
430
+
431
+ assert_equal '', out
432
+ assert_equal '', err
433
+ end
434
+
435
+ def test_deliver_syntax_error
436
+ Net::SMTP.on_send_message do
437
+ Net::SMTP.on_send_message # clear
438
+ e = Net::SMTPSyntaxError.new 'blah blah blah'
439
+ e.set_backtrace %w[one two three]
440
+ raise e
441
+ end
442
+
443
+ now = Time.now.to_i
444
+
445
+ email1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
446
+ email2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
447
+
448
+ out, err = capture_io do
449
+ @sm.deliver [email1, email2]
450
+ end
451
+
452
+ assert_equal 1, Net::SMTP.deliveries.length, 'delivery count'
453
+ assert_equal 1, Email.records.length
454
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
455
+ assert_operator now, :<=, Email.records.first.last_send_attempt
456
+
457
+ assert_equal '', out
458
+ 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
459
+ end
460
+
461
+ def test_deliver_timeout
462
+ Net::SMTP.on_send_message do
463
+ e = Timeout::Error.new 'timed out'
464
+ e.set_backtrace %w[one two three]
465
+ raise e
466
+ end
467
+
468
+ now = Time.now.to_i
469
+
470
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
471
+
472
+ out, err = capture_io do
473
+ @sm.deliver [email]
474
+ end
475
+
476
+ assert_equal 0, Net::SMTP.deliveries.length
477
+ assert_equal 1, Email.records.length
478
+ assert_operator now, :>=, Email.records.first.last_send_attempt
479
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on Timeout'
480
+
481
+ assert_equal '', out
482
+ assert_equal "error sending email 1: \"timed out\"(Timeout::Error):\n\tone\n\ttwo\n\tthree\n", err
483
+ end
484
+
485
+ def test_do_exit
486
+ out, err = capture_io do
487
+ assert_raises SystemExit do
488
+ @sm.do_exit
489
+ end
490
+ end
491
+
492
+ assert_equal '', out
493
+ assert_equal "caught signal, shutting down\n", err
494
+ end
495
+
496
+ def test_log
497
+ out, err = capture_io do
498
+ @sm.log 'hi'
499
+ end
500
+
501
+ assert_equal "hi\n", err
502
+ end
503
+
504
+ def test_find_emails
505
+ email_data = [
506
+ { :mail => 'body0', :to => 'recip@h1.example.com', :from => nobody },
507
+ { :mail => 'body1', :to => 'recip@h1.example.com', :from => nobody },
508
+ { :mail => 'body2', :to => 'recip@h2.example.com', :from => nobody },
509
+ ]
510
+
511
+ emails = email_data.map do |email_data| Email.create email_data end
512
+
513
+ tried = Email.create :mail => 'body3', :to => 'recip@h3.example.com',
514
+ :from => nobody
515
+
516
+ tried.last_send_attempt = Time.now.to_i - 258
517
+
518
+ found_emails = []
519
+
520
+ out, err = capture_io do
521
+ found_emails = @sm.find_emails
522
+ end
523
+
524
+ assert_equal emails, found_emails
525
+
526
+ assert_equal '', out
527
+ assert_equal "found 3 emails to send\n", err
528
+ end
529
+
530
+ def test_find_emails_with_specific_worker
531
+ pats_email = [ { :mail => 'Pat1', :to => 'recip@h1.example.com', :from => nobody, :worker=>"Pat" },
532
+ { :mail => 'Pat2', :to => 'recip@h1.example.com', :from => nobody, :worker=>"Pat" },
533
+ { :mail => 'Pat3', :to => 'recip@h1.example.com', :from => nobody, :worker=>"Pat" }].map do |data|
534
+ Email.create data
535
+ end
536
+
537
+ dhls_email = [{ :mail => 'DHL1', :to => 'recip@h1.example.com', :from => nobody, :worker=>"DHL" },
538
+ { :mail => 'DHL2', :to => 'recip@h1.example.com', :from => nobody, :worker=>"DHL" }].map do |data|
539
+ Email.create data
540
+ end
541
+
542
+ general_email = Email.create(:mail => 'No', :to => 'recip@h1.example.com', :from => nobody)
543
+
544
+ # require "ruby-debug"
545
+ # debugger
546
+
547
+ out, err = capture_io do
548
+ found_emails = ActionMailer::ARSendmail.new.find_emails
549
+ assert_equal found_emails, [general_email]
550
+ end
551
+
552
+
553
+ out, err = capture_io do
554
+ found_emails = ActionMailer::ARSendmail.new(:Worker=>"Pat").find_emails
555
+ assert_equal found_emails, pats_email
556
+ end
557
+
558
+ out, err = capture_io do
559
+ found_emails = ActionMailer::ARSendmail.new(:Worker=>"DHL").find_emails
560
+ assert_equal found_emails, dhls_email
561
+ end
562
+
563
+
564
+ end
565
+
566
+ def test_smtp_settings
567
+ ActionMailer::Base.server_settings[:address] = 'localhost'
568
+
569
+ assert_equal 'localhost', @sm.smtp_settings[:address]
570
+ end
571
+
572
+ def nobody
573
+ 'nobody@example.com'
574
+ end
575
+
576
+ end