email_deliver_test.rb 9.2 KB

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