email_deliver_test.rb 9.9 KB

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