sendmail_spec.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Channel::Driver::Sendmail do
  4. context 'with env var ZAMMAD_MAIL_TO_FILE present' do
  5. let(:address) { Faker::Internet.email }
  6. let(:body) { Faker::Lorem.sentence(word_count: 3) }
  7. let(:file) { Rails.root.join("tmp/mails/#{address}.eml") }
  8. around do |example|
  9. ENV['ZAMMAD_MAIL_TO_FILE'] = '1'
  10. FileUtils.rm_f(file)
  11. example.run
  12. FileUtils.rm_f(file)
  13. ENV.delete('ZAMMAD_MAIL_TO_FILE')
  14. end
  15. it 'creates mail file', :aggregate_failures do
  16. described_class.new.deliver({}, { to: address, from: address, body: body })
  17. expect(file).to exist
  18. content = File.read(file)
  19. expect(content).to match(%r{#{body}})
  20. expect(content).to match(%r{#{address}})
  21. end
  22. end
  23. context 'with regular Sendmail usage' do
  24. let(:address) { Faker::Internet.email }
  25. let(:body) { Faker::Lorem.sentence(word_count: 3) }
  26. let(:mocked_sendmail) do
  27. instance_double(IO).tap do |dbl|
  28. allow(dbl).to receive(:puts)
  29. allow(dbl).to receive(:flush)
  30. end
  31. end
  32. around do |example|
  33. ENV['ZAMMAD_MAIL_PRETEND_NOT_TEST'] = '1'
  34. example.run
  35. ENV.delete('ZAMMAD_MAIL_PRETEND_NOT_TEST')
  36. end
  37. it 'sends mail', :aggregate_failures do
  38. allow_any_instance_of(Mail::Sendmail).to receive(:popen).and_yield(mocked_sendmail)
  39. described_class.new.deliver({}, { to: address, from: address, body: body })
  40. expect(mocked_sendmail).to have_received(:puts).with(include(address).and(include(body)))
  41. expect(mocked_sendmail).to have_received(:flush)
  42. end
  43. end
  44. end