Browse Source

Follow-up ef47d6c - Maintenance: Improves email drivers.

Mantas Masalskis 1 month ago
parent
commit
4055631920

+ 1 - 1
app/models/channel/driver/base_email_outbound.rb

@@ -20,7 +20,7 @@ class Channel::Driver::BaseEmailOutbound
     prepare_idn_outbound(attr)
   end
 
-  def deliver_mail(attr, notification, method, options = nil)
+  def deliver_mail(attr, notification, method, options = {})
     mail = Channel::EmailBuild.build(attr, notification)
     mail.delivery_method method, options
     mail.deliver

+ 1 - 1
app/models/channel/driver/sendmail.rb

@@ -26,7 +26,7 @@ class Channel::Driver::Sendmail < Channel::Driver::BaseEmailOutbound
   def deliver_mail(attr, notification)
     if ENV['ZAMMAD_MAIL_TO_FILE'].present?
       super(attr, notification, :file, { location: Rails.root.join('tmp/mails'), extension: '.eml' })
-    elsif Rails.env.test?
+    elsif Rails.env.test? && ENV['ZAMMAD_MAIL_PRETEND_NOT_TEST'] != '1'
       # test
       super(attr, notification, :test)
     else

+ 27 - 0
spec/models/channel/driver/sendmail_spec.rb

@@ -25,4 +25,31 @@ RSpec.describe Channel::Driver::Sendmail do
       expect(content).to match(%r{#{address}})
     end
   end
+
+  context 'with regular Sendmail usage' do
+    let(:address) { Faker::Internet.email }
+    let(:body)    { Faker::Lorem.sentence(word_count: 3) }
+
+    let(:mocked_sendmail) do
+      instance_double(IO).tap do |dbl|
+        allow(dbl).to receive(:puts)
+        allow(dbl).to receive(:flush)
+      end
+    end
+
+    around do |example|
+      ENV['ZAMMAD_MAIL_PRETEND_NOT_TEST'] = '1'
+      example.run
+      ENV.delete('ZAMMAD_MAIL_PRETEND_NOT_TEST')
+    end
+
+    it 'sends mail', :aggregate_failures do
+      allow_any_instance_of(Mail::Sendmail).to receive(:popen).and_yield(mocked_sendmail)
+
+      described_class.new.deliver({}, { to: address, from: address, body: body })
+
+      expect(mocked_sendmail).to have_received(:puts).with(include(address).and(include(body)))
+      expect(mocked_sendmail).to have_received(:flush)
+    end
+  end
 end