cirandas.net

ref: master

test/unit/mailing_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
require_relative "../test_helper"

class MailingTest < ActiveSupport::TestCase

  def setup
    ActionMailer::Base.deliveries = []
    @environment = fast_create(Environment, :name => 'Network')
    create_user('user_one', :environment_id => @environment.id)
    create_user('user_two', :environment_id => @environment.id)
  end
  attr_reader :environment

  should 'source be able to polymorphic relationship' do
    m = Mailing.new
    assert_nothing_raised do
      m.source = Environment.new
    end
    assert_nothing_raised do
      m.source = Profile.new
    end
  end

  should 'require source_id' do
    mailing = Mailing.new
    mailing.valid?
    assert mailing.errors[:source_id.to_s].present?

    mailing.source_id = Environment.default.id
    mailing.valid?
    refute mailing.errors[:source_id.to_s].present?
  end

  should 'require subject' do
    mailing = Mailing.new
    mailing.valid?
    assert mailing.errors[:subject.to_s].present?

    mailing.subject = 'Hello :)'
    mailing.valid?
    refute mailing.errors[:subject.to_s].present?
  end

  should 'require body' do
    mailing = Mailing.new
    mailing.valid?
    assert mailing.errors[:body.to_s].present?

    mailing.body = 'We have some news!'
    mailing.valid?
    refute mailing.errors[:body.to_s].present?
  end

  should 'return source' do
    mailing = create(Mailing, :source => environment, :subject => 'Hello', :body => 'We have some news')
    assert_equal environment, Mailing.find(mailing.id).source
  end

  should 'return source name' do
    mailing = build(Mailing, :source => environment)
    assert_equal environment.name, mailing.source.name
  end

  should 'return source with source_id' do
    mailing = build(Mailing, :source => environment)
    assert_equal environment, mailing.source
  end

  should 'return person with person_id' do
    person = Person['user_one']
    mailing = build(Mailing, :source => environment, :person => person)
    assert_equal person, mailing.person
  end

  should 'display name and email on generate_from' do
    person = Person['user_one']
    mailing = build(Mailing, :source => environment, :person => person)
    assert_equal "#{environment.name} <#{environment.noreply_email}>", mailing.generate_from
  end

  should 'generate subject' do
    mailing = build(Mailing, :source => environment, :subject => 'Hello :)')
    assert_equal "[#{environment.name}] #{mailing.subject}", mailing.generate_subject
  end

  should 'return signature message' do
    mailing = build(Mailing, :source => environment)
    assert_equal 'Sent by Noosfero.', mailing.signature_message
  end

  should 'return blank string on url' do
    mailing = build(Mailing, :source => environment)
    environment.domains << create(Domain, :name => 'noosfero.net', :is_default => true)
    assert_equal '', mailing.url
  end

  should 'process the entire batch even if individual emails crash' do
    mailing = build(Mailing, :source => environment, :person => Person['user_one'], :body => 'test', :subject => 'test')
    def mailing.each_recipient
      user_one = Person['user_one']
      user_two = Person['user_two']
      user_one.stubs(:email).raises(RuntimeError.new)
      [user_one, user_two].each do |p|
        yield p
      end
    end
    mailing.stubs(:schedule)
    mailing.save!
    mailing.deliver

    assert_equal 1, ActionMailer::Base.deliveries.size
  end

end