cirandas.net

ref: master

test/unit/environment_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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require_relative "../test_helper"

class EnvironmentMailingTest < ActiveSupport::TestCase

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


  should 'require source_id' do
    mailing = EnvironmentMailing.new
    mailing.valid?
    assert mailing.errors[:source_id].any?

    mailing.source_id = environment.id
    mailing.valid?
    refute mailing.errors[:source_id].any?
  end

  should 'return environment name' do
    mailing = new_mailing(environment)
    assert_equal environment.name, mailing.source.name
  end

  should 'return environment with source_id' do
    mailing = new_mailing(environment)
    assert_equal environment, mailing.source
  end

  should 'return signature message' do
    mailing = new_mailing(environment)
    assert_equal 'Sent by Network.', mailing.signature_message
  end

  should 'return url for environment on url' do
    mailing = new_mailing(environment)
    domain = Domain.new(:name => 'noosfero.net')
    domain.is_default = true
    environment.domains << domain
    assert_equal 'http://noosfero.net', mailing.url
  end

  should 'display name and email on generate_from' do
    mailing = new_mailing(environment).tap do |m|
      m.person = person_1
    end
    assert_equal "#{environment.name} <#{environment.noreply_email}>", mailing.generate_from
  end

  should 'deliver mailing to each recipient after create' do
    mailing = create_mailing(environment, :person => person_1)
    process_delayed_job_queue
    assert_equal 2, ActionMailer::Base.deliveries.count
  end

  should 'create mailing sent to each recipient after delivering mailing' do
    mailing = create_mailing(environment, :person => person_1)
    assert_difference 'MailingSent.count', 2 do
      process_delayed_job_queue
    end
  end

  should 'change locale according to the mailing locale' do
    mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
    Noosfero.expects(:with_locale).with('pt')
    process_delayed_job_queue
  end

  should 'return recipients' do
    mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
    assert_equal [person_1, person_2], mailing.recipients
  end

  should 'return recipients according to limit' do
    mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
    assert_equal [person_1], mailing.recipients(0, 1)
  end

  should 'return all environment admins when recipients_roles is set to environment_administrator' do
    environment.add_admin person_1

    mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
    mailing.recipients_roles = ["environment_administrator"]
    mailing.save

    assert_equivalent(environment.admins, mailing.recipients)
  end

  should 'return all people with role profile_admin when recipients_roles is set to profile_admin' do
    environment.add_admin person_1

    mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
    mailing.recipients_roles = ["profile_admin"]
    mailing.save
    role = Role.find_by(key: 'profile_admin', environment_id: environment)
    profile_admins = Person.by_role(role).where(environment_id: environment)

    assert_equivalent(profile_admins, mailing.recipients)
  end

  should 'return all people when recipients_roles is not set' do
    environment.add_admin person_1

    mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
    mailing.save

    assert_equivalent(environment.people, mailing.recipients)
  end

  should 'return profile_admins and environment admins when both roles are set as recipients' do
    environment.add_admin person_1

    mailing = create_mailing(environment, :locale => 'pt', :person => person_1)
    mailing.recipients_roles = ["profile_admin", "environment_administrator"]
    mailing.save
    role = Role.find_by(key: 'profile_admin', environment_id: environment)
    profile_admins = Person.by_role(role).where(environment_id: environment)

    assert_equivalent(profile_admins+environment.admins, mailing.recipients)
  end

  should 'return true if already sent mailing to a recipient' do
    mailing = create_mailing(environment, :person => person_1)
    process_delayed_job_queue

    assert mailing.mailing_sents.find_by(person_id: person_1.id)
  end

  should 'return false if did not sent mailing to a recipient' do
    recipient = fast_create(Person)
    person = Person['user_one']
    mailing = create_mailing(environment, :person => person_1)
    process_delayed_job_queue

    refute mailing.mailing_sents.find_by(person_id: recipient.id)
  end

  def new_mailing(environment)
    m = EnvironmentMailing.new(:subject => 'Hello', :body => 'We have some news')
    m.source = environment
    m
  end

  def create_mailing(environment, options)
    new_mailing(environment).tap do |m|
      m.locale = options[:locale]
      m.person = options[:person]
      m.save!
    end
  end

end