mailer_spec.rb 4.7 KB

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