mailer_spec.rb 3.8 KB

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