email_deliver_test.rb 9.8 KB

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