email_deliver_test.rb 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class EmailDeliverTest < ActiveSupport::TestCase
  4. test 'basic check' do
  5. if !ENV['MAIL_SERVER']
  6. raise "Need MAIL_SERVER as ENV variable like export MAIL_SERVER='mx.example.com'"
  7. end
  8. if !ENV['MAIL_SERVER_ACCOUNT']
  9. raise "Need MAIL_SERVER_ACCOUNT as ENV variable like export MAIL_SERVER_ACCOUNT='user:somepass'"
  10. end
  11. server_login = ENV['MAIL_SERVER_ACCOUNT'].split(':')[0]
  12. server_password = ENV['MAIL_SERVER_ACCOUNT'].split(':')[1]
  13. email_address = EmailAddress.create(
  14. realname: 'me Helpdesk',
  15. email: "me#{rand(999_999_999)}@example.com",
  16. updated_by_id: 1,
  17. created_by_id: 1,
  18. )
  19. group = Group.create_or_update(
  20. name: 'DeliverTest',
  21. email_address_id: email_address.id,
  22. updated_by_id: 1,
  23. created_by_id: 1,
  24. )
  25. channel = Channel.create(
  26. area: 'Email::Account',
  27. group_id: group.id,
  28. options: {
  29. inbound: {
  30. adapter: 'imap',
  31. options: {
  32. host: 'mx1.example.com',
  33. user: 'example',
  34. password: 'some_pw',
  35. ssl: true,
  36. }
  37. },
  38. outbound: {
  39. adapter: 'sendmail'
  40. }
  41. },
  42. active: true,
  43. updated_by_id: 1,
  44. created_by_id: 1,
  45. )
  46. email_address.channel_id = channel.id
  47. email_address.save
  48. ticket1 = Ticket.create(
  49. title: 'some delivery test',
  50. group: group,
  51. customer_id: 2,
  52. state: Ticket::State.lookup(name: 'new'),
  53. priority: Ticket::Priority.lookup(name: '2 normal'),
  54. updated_by_id: 1,
  55. created_by_id: 1,
  56. )
  57. assert(ticket1, 'ticket created')
  58. article1 = Ticket::Article.create(
  59. ticket_id: ticket1.id,
  60. to: 'some_recipient@example_not_existing_what_ever.com',
  61. subject: 'some subject',
  62. message_id: 'some@id',
  63. body: 'some message delivery test',
  64. internal: false,
  65. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  66. type: Ticket::Article::Type.find_by(name: 'email'),
  67. updated_by_id: 1,
  68. created_by_id: 1,
  69. )
  70. assert_equal(nil, article1.preferences['delivery_retry'])
  71. assert_equal(nil, article1.preferences['delivery_status'])
  72. assert_equal(nil, article1.preferences['delivery_status_date'])
  73. assert_equal(nil, article1.preferences['delivery_status_message'])
  74. result = Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new(article1.id)
  75. assert(result.perform)
  76. article1_lookup = Ticket::Article.find(article1.id)
  77. assert_equal(1, article1_lookup.preferences['delivery_retry'])
  78. assert_equal('success', article1_lookup.preferences['delivery_status'])
  79. assert(article1_lookup.preferences['delivery_status_date'])
  80. assert_equal(nil, article1_lookup.preferences['delivery_status_message'])
  81. # send with invalid smtp settings
  82. channel.update_attributes(
  83. options: {
  84. inbound: {
  85. adapter: 'imap',
  86. options: {
  87. host: 'mx1.example.com',
  88. user: 'example',
  89. password: 'some_pw',
  90. ssl: true,
  91. }
  92. },
  93. outbound: {
  94. adapter: 'smtp',
  95. options: {
  96. host: 'mx1.example.com',
  97. port: 25,
  98. start_tls: true,
  99. user: 'not_existing',
  100. password: 'not_existing',
  101. },
  102. },
  103. },
  104. )
  105. assert_raises(RuntimeError) {
  106. result = Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new(article1.id)
  107. assert_not(result.perform)
  108. }
  109. article1_lookup = Ticket::Article.find(article1.id)
  110. assert_equal(2, article1_lookup.preferences['delivery_retry'])
  111. assert_equal('fail', article1_lookup.preferences['delivery_status'])
  112. assert(article1_lookup.preferences['delivery_status_date'])
  113. assert(article1_lookup.preferences['delivery_status_message'])
  114. # send with correct smtp settings
  115. channel.update_attributes(
  116. options: {
  117. inbound: {
  118. adapter: 'imap',
  119. options: {
  120. host: 'mx1.example.com',
  121. user: 'example',
  122. password: 'some_pw',
  123. ssl: true,
  124. }
  125. },
  126. outbound: {
  127. adapter: 'smtp',
  128. options: {
  129. host: ENV['MAIL_SERVER'],
  130. port: 25,
  131. start_tls: true,
  132. user: server_login,
  133. password: server_password,
  134. },
  135. },
  136. },
  137. )
  138. result = Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new(article1.id)
  139. assert(result.perform)
  140. article1_lookup = Ticket::Article.find(article1.id)
  141. assert_equal(3, article1_lookup.preferences['delivery_retry'])
  142. assert_equal('success', article1_lookup.preferences['delivery_status'])
  143. assert(article1_lookup.preferences['delivery_status_date'])
  144. assert_nil(article1_lookup.preferences['delivery_status_message'])
  145. # check retry jobs
  146. # remove background jobs
  147. Delayed::Job.destroy_all
  148. # send with invalid smtp settings
  149. channel.update_attributes(
  150. options: {
  151. inbound: {
  152. adapter: 'imap',
  153. options: {
  154. host: 'mx1.example.com',
  155. user: 'example',
  156. password: 'some_pw',
  157. ssl: true,
  158. }
  159. },
  160. outbound: {
  161. adapter: 'smtp',
  162. options: {
  163. host: 'mx1.example.com',
  164. port: 25,
  165. start_tls: true,
  166. user: 'not_existing',
  167. password: 'not_existing',
  168. },
  169. },
  170. },
  171. )
  172. # remove background jobs
  173. Delayed::Job.destroy_all
  174. article2 = Ticket::Article.create(
  175. ticket_id: ticket1.id,
  176. to: 'some_recipient@example_not_existing_what_ever.com',
  177. subject: 'some subject2',
  178. message_id: 'some@id',
  179. body: 'some message delivery test2',
  180. internal: false,
  181. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  182. type: Ticket::Article::Type.find_by(name: 'email'),
  183. updated_by_id: 1,
  184. created_by_id: 1,
  185. )
  186. assert_raises(RuntimeError) {
  187. Scheduler.worker(true)
  188. }
  189. article2_lookup = Ticket::Article.find(article2.id)
  190. assert_equal(2, ticket1.articles.count)
  191. assert_equal(1, article2_lookup.preferences['delivery_retry'])
  192. assert_equal('fail', article2_lookup.preferences['delivery_status'])
  193. assert(article2_lookup.preferences['delivery_status_date'])
  194. assert(article2_lookup.preferences['delivery_status_message'])
  195. Scheduler.worker(true)
  196. article2_lookup = Ticket::Article.find(article2.id)
  197. assert_equal(2, ticket1.articles.count)
  198. assert_equal(1, article2_lookup.preferences['delivery_retry'])
  199. assert_equal('fail', article2_lookup.preferences['delivery_status'])
  200. assert(article2_lookup.preferences['delivery_status_date'])
  201. assert(article2_lookup.preferences['delivery_status_message'])
  202. sleep 6
  203. assert_raises(RuntimeError) {
  204. Scheduler.worker(true)
  205. }
  206. article2_lookup = Ticket::Article.find(article2.id)
  207. assert_equal(2, ticket1.articles.count)
  208. assert_equal(2, article2_lookup.preferences['delivery_retry'])
  209. assert_equal('fail', article2_lookup.preferences['delivery_status'])
  210. assert(article2_lookup.preferences['delivery_status_date'])
  211. assert(article2_lookup.preferences['delivery_status_message'])
  212. Scheduler.worker(true)
  213. article2_lookup = Ticket::Article.find(article2.id)
  214. assert_equal(2, ticket1.articles.count)
  215. assert_equal(2, article2_lookup.preferences['delivery_retry'])
  216. assert_equal('fail', article2_lookup.preferences['delivery_status'])
  217. assert(article2_lookup.preferences['delivery_status_date'])
  218. assert(article2_lookup.preferences['delivery_status_message'])
  219. sleep 11
  220. assert_raises(RuntimeError) {
  221. Scheduler.worker(true)
  222. }
  223. article2_lookup = Ticket::Article.find(article2.id)
  224. assert_equal(2, ticket1.articles.count)
  225. assert_equal(3, article2_lookup.preferences['delivery_retry'])
  226. assert_equal('fail', article2_lookup.preferences['delivery_status'])
  227. assert(article2_lookup.preferences['delivery_status_date'])
  228. assert(article2_lookup.preferences['delivery_status_message'])
  229. sleep 16
  230. assert_raises(RuntimeError) {
  231. Scheduler.worker(true)
  232. }
  233. article2_lookup = Ticket::Article.find(article2.id)
  234. article_delivery_system = ticket1.articles.last
  235. assert_equal(3, ticket1.articles.count)
  236. assert_equal(4, article2_lookup.preferences['delivery_retry'])
  237. assert_equal('fail', article2_lookup.preferences['delivery_status'])
  238. assert(article2_lookup.preferences['delivery_status_date'])
  239. assert(article2_lookup.preferences['delivery_status_message'])
  240. assert_equal('System', article_delivery_system.sender.name)
  241. assert_equal(true, article_delivery_system.preferences['delivery_message'])
  242. assert_equal(article2.id, article_delivery_system.preferences['delivery_article_id_related'])
  243. end
  244. end