email_keep_on_server_test.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. require 'net/imap'
  4. class EmailKeepOnServerTest < ActiveSupport::TestCase
  5. setup do
  6. if ENV['KEEP_ON_MAIL_SERVER'].blank?
  7. raise "Need KEEP_ON_MAIL_SERVER as ENV variable like export KEEP_ON_MAIL_SERVER='mx.example.com'"
  8. end
  9. if ENV['KEEP_ON_MAIL_SERVER_ACCOUNT'].blank?
  10. raise "Need KEEP_ON_MAIL_SERVER_ACCOUNT as ENV variable like export KEEP_ON_MAIL_SERVER_ACCOUNT='user:somepass'"
  11. end
  12. @server_login = ENV['KEEP_ON_MAIL_SERVER_ACCOUNT'].split(':')[0]
  13. @server_password = ENV['KEEP_ON_MAIL_SERVER_ACCOUNT'].split(':')[1]
  14. @folder = "keep_on_mail_server_#{SecureRandom.uuid}"
  15. email_address = EmailAddress.create!(
  16. realname: 'me Helpdesk',
  17. email: "me#{SecureRandom.uuid}@example.com",
  18. updated_by_id: 1,
  19. created_by_id: 1,
  20. )
  21. group = Group.create_or_update(
  22. name: 'KeepOnServerTest',
  23. email_address_id: email_address.id,
  24. updated_by_id: 1,
  25. created_by_id: 1,
  26. )
  27. @channel = Channel.create!(
  28. area: 'Email::Account',
  29. group_id: group.id,
  30. options: {
  31. inbound: {
  32. adapter: 'imap',
  33. options: {
  34. host: ENV['KEEP_ON_MAIL_SERVER'],
  35. user: @server_login,
  36. password: @server_password,
  37. ssl: true,
  38. folder: @folder,
  39. # keep_on_server: true,
  40. }
  41. },
  42. outbound: {
  43. adapter: 'sendmail'
  44. }
  45. },
  46. active: true,
  47. updated_by_id: 1,
  48. created_by_id: 1,
  49. )
  50. email_address.channel_id = @channel.id
  51. email_address.save!
  52. end
  53. test 'keep on server' do
  54. @channel.options[:inbound][:options][:keep_on_server] = true
  55. @channel.save!
  56. # clean mailbox
  57. imap = Net::IMAP.new(ENV['KEEP_ON_MAIL_SERVER'], 993, true, nil, false)
  58. imap.login(@server_login, @server_password)
  59. imap.create(@folder)
  60. imap.select(@folder)
  61. # put unseen message in it
  62. imap.append(@folder, "Subject: hello1
  63. From: shugo@example.com
  64. To: shugo@example.com
  65. Message-ID: <some1@example_keep_on_server>
  66. hello world
  67. ".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
  68. # verify if message is still on server
  69. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  70. assert_equal(1, message_ids.count)
  71. message_meta = imap.fetch(1, ['FLAGS'])[0].attr
  72. assert_not(message_meta['FLAGS'].include?(:Seen))
  73. # fetch messages
  74. article_count = Ticket::Article.count
  75. @channel.fetch(true)
  76. assert_equal(article_count + 1, Ticket::Article.count)
  77. # verify if message is still on server
  78. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  79. assert_equal(1, message_ids.count)
  80. message_meta = imap.fetch(1, ['RFC822.HEADER', 'FLAGS'])[0].attr
  81. assert(message_meta['FLAGS'].include?(:Seen))
  82. # fetch messages
  83. article_count = Ticket::Article.count
  84. @channel.fetch(true)
  85. assert_equal(article_count, Ticket::Article.count)
  86. # verify if message is still on server
  87. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  88. assert_equal(1, message_ids.count)
  89. # put unseen message in it
  90. imap.append(@folder, "Subject: hello2
  91. From: shugo@example.com
  92. To: shugo@example.com
  93. Message-ID: <some2@example_keep_on_server>
  94. hello world
  95. ".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
  96. message_meta = imap.fetch(1, ['FLAGS'])[0].attr
  97. assert(message_meta['FLAGS'].include?(:Seen))
  98. message_meta = imap.fetch(2, ['FLAGS'])[0].attr
  99. assert_not(message_meta['FLAGS'].include?(:Seen))
  100. # fetch messages
  101. article_count = Ticket::Article.count
  102. @channel.fetch(true)
  103. assert_equal(article_count + 1, Ticket::Article.count)
  104. # verify if message is still on server
  105. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  106. assert_equal(2, message_ids.count)
  107. message_meta = imap.fetch(1, ['FLAGS'])[0].attr
  108. assert(message_meta['FLAGS'].include?(:Seen))
  109. message_meta = imap.fetch(2, ['FLAGS'])[0].attr
  110. assert(message_meta['FLAGS'].include?(:Seen))
  111. # set messages to not seen
  112. imap.store(1, '-FLAGS', [:Seen])
  113. imap.store(2, '-FLAGS', [:Seen])
  114. # fetch messages
  115. article_count = Ticket::Article.count
  116. @channel.fetch(true)
  117. assert_equal(article_count, Ticket::Article.count)
  118. imap.delete(@folder)
  119. @channel.destroy!
  120. end
  121. test 'keep not on server' do
  122. @channel.options[:inbound][:options][:keep_on_server] = false
  123. @channel.save!
  124. # clean mailbox
  125. imap = Net::IMAP.new(ENV['KEEP_ON_MAIL_SERVER'], 993, true, nil, false)
  126. imap.login(@server_login, @server_password)
  127. imap.create(@folder)
  128. imap.select(@folder)
  129. # put unseen message in it
  130. imap.append(@folder, "Subject: hello1
  131. From: shugo@example.com
  132. To: shugo@example.com
  133. Message-ID: <some1@example_remove_from_server>
  134. hello world
  135. ".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
  136. # verify if message is still on server
  137. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  138. assert_equal(1, message_ids.count)
  139. message_meta = imap.fetch(1, ['FLAGS'])[0].attr
  140. assert_not(message_meta['FLAGS'].include?(:Seen))
  141. # fetch messages
  142. article_count = Ticket::Article.count
  143. @channel.fetch(true)
  144. assert_equal(article_count + 1, Ticket::Article.count)
  145. # verify if message is still on server
  146. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  147. assert_equal(1, message_ids.count)
  148. # put unseen message in it
  149. imap.append(@folder, "Subject: hello2
  150. From: shugo@example.com
  151. To: shugo@example.com
  152. Message-ID: <some2@example_remove_from_server>
  153. hello world
  154. ".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
  155. # verify if message is still on server
  156. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  157. assert_equal(1, message_ids.count)
  158. message_meta = imap.fetch(1, ['FLAGS'])[0].attr
  159. assert_not(message_meta['FLAGS'].include?(:Seen))
  160. # fetch messages
  161. article_count = Ticket::Article.count
  162. @channel.fetch(true)
  163. assert_equal(article_count + 1, Ticket::Article.count)
  164. # verify if message is still on server
  165. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  166. assert_equal(1, message_ids.count)
  167. # put unseen message in it
  168. imap.append(@folder, "Subject: hello2
  169. From: shugo@example.com
  170. To: shugo@example.com
  171. Message-ID: <some2@example_remove_from_server>
  172. hello world
  173. ".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
  174. # verify if message is still on server
  175. message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
  176. assert_equal(1, message_ids.count)
  177. # fetch messages
  178. article_count = Ticket::Article.count
  179. @channel.fetch(true)
  180. assert_equal(article_count + 1, Ticket::Article.count)
  181. imap.delete(@folder)
  182. @channel.destroy!
  183. end
  184. end