mailer_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. require 'rails_helper'
  2. require 'ostruct'
  3. RSpec.describe NotificationFactory::Mailer do
  4. describe '#template' do
  5. context 'for postmaster oversized mail' do
  6. let(:raw_incoming_mail) { File.read(Rails.root.join('test/data/mail/mail010.box')) }
  7. let(:parsed_incoming_mail) { Channel::EmailParser.new.parse raw_incoming_mail }
  8. let(:incoming_mail) do
  9. mail = OpenStruct.new
  10. mail.from_display_name = parsed_incoming_mail[:from_display_name]
  11. mail.subject = parsed_incoming_mail[:subject]
  12. mail.msg_size = format('%<MB>.2f', MB: raw_incoming_mail.size.to_f / 1024 / 1024)
  13. mail
  14. end
  15. let(:en_expected_subject) { '[undeliverable] Message too large' }
  16. let(:en_expected_body) do
  17. <<~BODY
  18. Dear Smith Sepp,
  19. Unfortunately your email titled \"Gruß aus Oberalteich\" could not be delivered to one or more recipients.
  20. Your message was 0.01 MB but we only accept messages up to 10 MB.
  21. Please reduce the message size and try again. Thank you for your understanding.
  22. Regretfully,
  23. Postmaster of zammad.example.com
  24. BODY
  25. end
  26. shared_examples 'plaintext mail templating' do
  27. it 'templates correctly' do
  28. result = described_class.template(
  29. template: 'email_oversized',
  30. locale: locale,
  31. format: 'txt',
  32. objects: {
  33. mail: incoming_mail,
  34. },
  35. raw: true, # will not add application template
  36. standalone: true, # default: false - will send header & footer
  37. )
  38. expect(result[:subject]).to eq(expected_subject)
  39. expect(result[:body]).to eq(expected_body)
  40. end
  41. end
  42. context 'English locale (en)' do
  43. include_examples 'plaintext mail templating' do
  44. let(:locale) { 'en' }
  45. let(:expected_subject) { en_expected_subject }
  46. let(:expected_body) { en_expected_body }
  47. end
  48. end
  49. context 'German locale (de)' do
  50. include_examples 'plaintext mail templating' do
  51. let(:locale) { 'de' }
  52. let(:expected_subject) { '[Unzustellbar] Nachricht zu groß' }
  53. let(:expected_body) do
  54. <<~BODY
  55. Hallo Smith Sepp,
  56. Ihre E-Mail mit dem Betreff \"Gruß aus Oberalteich\" konnte nicht an einen oder mehrere Empfänger zugestellt werden.
  57. Die Nachricht hatte eine Größe von 0.01 MB, wir akzeptieren jedoch nur E-Mails mit einer Größe von bis zu 10 MB.
  58. Bitte reduzieren Sie die Größe Ihrer Nachricht und versuchen Sie es erneut. Vielen Dank für Ihr Verständnis.
  59. Mit freundlichen Grüßen
  60. Postmaster von zammad.example.com
  61. BODY
  62. end
  63. end
  64. end
  65. context 'unsupported locale, which defaults back to English locale (en)' do
  66. include_examples 'plaintext mail templating' do
  67. let(:locale) { 'UNSUPPORTED_LOCALE' }
  68. let(:expected_subject) { en_expected_subject }
  69. let(:expected_body) { en_expected_body }
  70. end
  71. end
  72. end
  73. end
  74. describe '#send' do
  75. subject(:result) do
  76. described_class.send(
  77. recipient: user,
  78. subject: 'some subject',
  79. body: 'some body',
  80. )
  81. end
  82. context 'recipient with email address' do
  83. let(:user) { create(:agent, email: 'somebody@example.com') }
  84. it 'returns a Mail::Message' do
  85. expect( result ).to be_kind_of(Mail::Message)
  86. end
  87. end
  88. context 'recipient without email address' do
  89. let(:user) { create(:agent, email: '') }
  90. it 'raises Exceptions::UnprocessableEntity' do
  91. expect { result }.to raise_error(Exceptions::UnprocessableEntity)
  92. end
  93. end
  94. end
  95. end