email_keep_on_server_test.rb 6.5 KB

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