email_deliver_test.rb 9.9 KB

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