cirandas.net

ref: master

test/unit/mail_queuer_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
ENV['MAIL_QUEUER'] = '1'
require_relative "../test_helper"

class MailQueuerTest < ActiveSupport::TestCase

  def setup
    MailSchedule.delete_all
    Delayed::Job.delete_all

    ENV['MAIL_QUEUER_LIMIT'] = '100'

    MailSchedule.create dest_count: 0, scheduled_to: Time.now
    ApplicationMailer.deliveries = []
  end

  class Mailer < ApplicationMailer
    def test to:[], cc:[], bcc:[], from: Environment.default.noreply_email
      mail to: to, cc: cc, bcc: bcc, from: from,
        subject: "test", body: 'test'
    end
  end

  should 'fit the available limit' do
    to  = 80.times.map{ |i| "b#{i}@example.com" }
    cc  = 10.times.map{ |i| "b#{i}@example.com" }
    bcc =  9.times.map{ |i| "b#{i}@example.com" }
    Mailer.test(to: to, cc: cc, bcc: bcc).deliver

    Delayed::Worker.new.work_off
    message = ApplicationMailer.deliveries.last
    assert_equal 99, MailSchedule.first.dest_count
    assert_equal to, message.to
    assert_equal cc, message.cc
    assert_equal bcc, message.bcc
  end


  should 'break the mail when :to is bigger than limit' do
    to  = 100.times.map{ |i| "b#{i}@example.com" }
    cc  = ["cc@example.com"]
    bcc = ["bcc@example.com"]
    Mailer.test(to: to, cc: cc, bcc: bcc).deliver

    Delayed::Worker.new.work_off
    assert_equal 1, ApplicationMailer.deliveries.count
    message = ApplicationMailer.deliveries.first
    assert_equal 99, MailSchedule.first.dest_count
    assert_equal to+cc, message.reply_to
    assert_equal to.first(99), message.to
    assert message.cc.blank?
    assert message.bcc.blank?

    message = job_message
    assert_equal 3, MailSchedule.last.dest_count
    assert_equal to+cc, message.reply_to
    assert_equal [to.last], message.to
    assert_equal cc, message.cc
    assert_equal bcc, message.bcc
  end

  should 'break the mail with cc is bigger than limit' do
    to  = 10.times.map{ |i| "b#{i}@example.com" }
    cc  = 100.times.map{ |i| "b#{i}@example.com" }
    bcc = ["bcc@example.com"]
    Mailer.test(to: to, cc: cc, bcc: bcc).deliver

    Delayed::Worker.new.work_off
    assert_equal 1, ApplicationMailer.deliveries.count
    message = ApplicationMailer.deliveries.first
    assert_equal 99, MailSchedule.first.dest_count
    assert_equal to+cc, message.reply_to
    assert_equal to, message.to
    assert_equal cc.first(89), message.cc
    assert message.bcc.blank?

    message = job_message
    assert_equal 13, MailSchedule.last.dest_count
    assert_equal to+cc, message.reply_to
    assert_equal [to.first], message.to
    assert_equal cc[89..-1], message.cc
    assert_equal bcc, message.bcc
  end

  should 'break the mail with bcc is bigger than limit' do
    to  = 10.times.map{ |i| "b#{i}@example.com" }
    cc  = 10.times.map{ |i| "b#{i}@example.com" }
    bcc = 80.times.map{ |i| "b#{i}@example.com" }
    Mailer.test(to: to, cc: cc, bcc: bcc).deliver

    Delayed::Worker.new.work_off
    assert_equal 1, ApplicationMailer.deliveries.count
    message = ApplicationMailer.deliveries.first
    assert_equal 99, MailSchedule.first.dest_count
    assert_equal to+cc, message.reply_to
    assert_equal to, message.to
    assert_equal cc, message.cc
    assert_equal bcc.first(79), message.bcc

    message = job_message
    assert_equal 2, MailSchedule.last.dest_count
    assert_equal to+cc, message.reply_to
    assert_equal [to.first], message.to
    assert message.cc.blank?
    assert_equal [bcc.last], message.bcc
  end

  should 'send in the next hour if available_limit < dest_count < limit' do
    MailSchedule.last.update dest_count: 90

    to  = 10.times.map{ |i| "b#{i}@example.com" }
    cc  = 10.times.map{ |i| "b#{i}@example.com" }
    bcc =  9.times.map{ |i| "b#{i}@example.com" }
    Mailer.test(to: to, cc: cc, bcc: bcc).deliver

    assert_equal 2, MailSchedule.count
    assert_equal 90, MailSchedule.first.dest_count
    assert_equal 29, MailSchedule.last.dest_count
    assert_equal MailSchedule.last.scheduled_to, MailSchedule.first.scheduled_to + 1.hour

    Delayed::Worker.new.work_off
    assert_equal nil, ApplicationMailer.deliveries.last

    message = job_message
    assert_equal to, message.to
    assert_equal cc, message.cc
    assert_equal bcc, message.bcc
  end

  protected

  def job_message job=Delayed::Job.last
    y   = YAML.load job.handler
    l   = y.job_data['arguments']
    m   = l.first
    bcc = l.second

    msg = Mail.read_from_string m
    msg.bcc = bcc
    msg
  end

end