email_postmaster_to_sender.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. require 'test_helper'
  2. require 'net/imap'
  3. class EmailPostmasterToSender < ActiveSupport::TestCase
  4. setup do
  5. Setting.set('postmaster_max_size', 0.1)
  6. @test_id = rand(999_999_999)
  7. # setup the IMAP account info for Zammad
  8. if ENV['MAIL_SERVER'].blank?
  9. raise "Need MAIL_SERVER as ENV variable like export MAIL_SERVER='mx.example.com'"
  10. end
  11. if ENV['MAIL_SERVER_ACCOUNT'].blank?
  12. raise "Need MAIL_SERVER_ACCOUNT as ENV variable like export MAIL_SERVER_ACCOUNT='user:somepass'"
  13. end
  14. @server_address = ENV['MAIL_SERVER']
  15. @server_login = ENV['MAIL_SERVER_ACCOUNT'].split(':')[0]
  16. @server_password = ENV['MAIL_SERVER_ACCOUNT'].split(':')[1]
  17. @folder = "postmaster_to_sender_#{@test_id}"
  18. if ENV['MAIL_SERVER_EMAIL'].blank?
  19. raise "Need MAIL_SERVER_EMAIL as ENV variable like export MAIL_SERVER_EMAIL='master@example.com'"
  20. end
  21. @sender_email_address = ENV['MAIL_SERVER_EMAIL']
  22. @email_address = EmailAddress.create!(
  23. realname: 'me Helpdesk',
  24. email: "some-zammad-#{ENV['MAIL_SERVER_EMAIL']}",
  25. updated_by_id: 1,
  26. created_by_id: 1,
  27. )
  28. group = Group.create_or_update(
  29. name: 'PostmasterToSenderTest',
  30. email_address_id: @email_address.id,
  31. updated_by_id: 1,
  32. created_by_id: 1,
  33. )
  34. @channel = Channel.create!(
  35. area: 'Email::Account',
  36. group_id: group.id,
  37. options: {
  38. inbound: {
  39. adapter: 'imap',
  40. options: {
  41. host: @server_address,
  42. user: @server_login,
  43. password: @server_password,
  44. ssl: true,
  45. folder: @folder,
  46. keep_on_server: false,
  47. }
  48. },
  49. outbound: {
  50. adapter: 'smtp',
  51. options: {
  52. host: @server_address,
  53. port: 25,
  54. start_tls: true,
  55. user: @server_login,
  56. password: @server_password,
  57. email: @email_address.email
  58. },
  59. },
  60. },
  61. active: true,
  62. updated_by_id: 1,
  63. created_by_id: 1,
  64. )
  65. @email_address.channel_id = @channel.id
  66. @email_address.save!
  67. end
  68. test 'postmaster reply with email on oversized incoming emails' do
  69. imap = Net::IMAP.new(@server_address, 993, true, nil, false)
  70. imap.login(@server_login, @server_password)
  71. imap.create(@folder)
  72. imap.select(@folder)
  73. # put a very large message in it
  74. large_message = "Subject: Oversized Email Message
  75. From: Max Mustermann <#{@sender_email_address}>
  76. To: shugo@example.com
  77. Message-ID: <#{@test_id}@zammad.test.com>
  78. Oversized Email Message Body #{'#' * 120_000}
  79. ".gsub(%r{\n}, "\r\n")
  80. large_message_md5 = Digest::MD5.hexdigest(large_message)
  81. large_message_size = format('%<MB>.2f', MB: large_message.size.to_f / 1024 / 1024)
  82. imap.append(@folder, large_message, [], Time.zone.now)
  83. @channel.fetch(true)
  84. # 1. verify that the oversized email has been saved locally to:
  85. # /tmp/oversized_mail/yyyy-mm-ddThh:mm:ss-:md5.eml
  86. path = Rails.root.join('tmp/oversized_mail')
  87. target_files = Dir.entries(path).select do |filename|
  88. filename =~ %r{^#{large_message_md5}\.eml$}
  89. end
  90. assert(target_files.present?, 'Large message .eml log file must be present.')
  91. # pick the latest file that matches the criteria
  92. target_file = target_files.max
  93. # verify that the file is byte for byte identical to the sent message
  94. file_path = Rails.root.join('tmp/oversized_mail', target_file)
  95. eml_data = File.read(file_path)
  96. assert_equal(large_message, eml_data)
  97. # 2. verify that a postmaster response email has been sent to the sender
  98. message_ids = nil
  99. 5.times do |sleep_offset|
  100. imap.select('inbox')
  101. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  102. break if message_ids.count.positive?
  103. # send mail hasn't arrived yet in the inbox
  104. sleep sleep_offset
  105. end
  106. assert(message_ids.count.positive?, 'Must have received a reply from the postmaster')
  107. imap_message_id = message_ids.last
  108. msg = imap.fetch(imap_message_id, 'RFC822')[0].attr['RFC822']
  109. assert(msg.present?, 'Must have received a reply from the postmaster')
  110. imap.store(imap_message_id, '+FLAGS', [:Deleted])
  111. imap.expunge()
  112. # parse the reply mail and verify the various headers
  113. parser = Channel::EmailParser.new
  114. mail = parser.parse(msg)
  115. assert_equal(mail[:from_email], @email_address.email)
  116. assert_equal(mail[:subject], '[undeliverable] Message too large')
  117. assert_equal("<#{@test_id}@zammad.test.com>",
  118. mail['references'],
  119. 'Reply\'s Referecnes header must match the send message ID')
  120. assert_equal("<#{@test_id}@zammad.test.com>",
  121. mail['in-reply-to'],
  122. 'Reply\'s In-Reply-To header must match the send message ID')
  123. # verify the reply mail body content
  124. body = mail[:body]
  125. assert(body.start_with?('Dear Max Mustermann'), 'Body must contain sender name')
  126. assert(body.include?('Oversized Email Message'), 'Body must contain original subject')
  127. assert(body.include?('0.1 MB'), 'Body must contain max allowed message size')
  128. assert(body.include?("#{large_message_size} MB"), 'Body must contain the original message size')
  129. assert(body.include?(Setting.get('fqdn')), 'Body must contain the Zammad instance name')
  130. # 3. check if original mail got removed
  131. imap.select(@folder)
  132. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  133. assert_equal(message_ids.count, 0, 'Original customer mail must be deleted.')
  134. # final clean up
  135. imap.delete(@folder)
  136. @channel.destroy!
  137. end
  138. test 'postmaster reply with no email on oversized incoming emails' do
  139. Setting.set('postmaster_send_reject_if_mail_too_large', false)
  140. imap = Net::IMAP.new(@server_address, 993, true, nil, false)
  141. imap.login(@server_login, @server_password)
  142. imap.select('inbox')
  143. message_count = imap.sort(['DATE'], ['ALL'], 'US-ASCII').count
  144. imap.create(@folder)
  145. imap.select(@folder)
  146. # put a very large message in it
  147. large_message = "Subject: Oversized Email Message
  148. From: Max Mustermann <#{@sender_email_address}>
  149. To: shugo@example.com
  150. Message-ID: <#{@test_id}@zammad.test.com>
  151. Oversized Email Message Body #{'#' * 120_000}
  152. ".gsub(%r{\n}, "\r\n")
  153. imap.append(@folder, large_message, [], Time.zone.now)
  154. @channel.fetch(true)
  155. # 1. verify that the oversized email has been saved locally to:
  156. # /tmp/oversized_mail/yyyy-mm-ddThh:mm:ss-:md5.eml
  157. path = Rails.root.join('tmp/oversized_mail')
  158. target_files = Dir.entries(path).select do |filename|
  159. filename =~ %r{^.+?\.eml$}
  160. end
  161. assert_not(target_files.blank?, 'Large message .eml log file must be blank.')
  162. # 2. verify that a postmaster response email has been sent to the sender
  163. imap.select('inbox')
  164. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  165. assert_equal(message_ids.count, message_count, 'Must not have received a reply from the postmaster')
  166. # 3. check if original mail got removed
  167. imap.select(@folder)
  168. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  169. imap_message_id = message_ids.last
  170. msg = imap.fetch(imap_message_id, 'RFC822')[0].attr['RFC822']
  171. imap.store(imap_message_id, '+FLAGS', [:Deleted])
  172. imap.expunge()
  173. assert(msg.present?, 'Oversized Email Message')
  174. assert_equal(message_ids.count, 1, 'Original customer mail must be deleted.')
  175. # final clean up
  176. imap.delete(@folder)
  177. @channel.destroy!
  178. end
  179. teardown do
  180. Setting.set('postmaster_max_size', 10)
  181. end
  182. end