email_process_bounce_follow_test.rb 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # rubocop:disable Lint/InterpolationCheck
  2. require 'test_helper'
  3. class EmailProcessBounceFollowUpTest < ActiveSupport::TestCase
  4. test 'process with bounce follow up check' do
  5. ticket = Ticket.create(
  6. title: 'bounce check',
  7. group: Group.lookup(name: 'Users'),
  8. customer_id: 2,
  9. state: Ticket::State.lookup(name: 'new'),
  10. priority: Ticket::Priority.lookup(name: '2 normal'),
  11. updated_by_id: 1,
  12. created_by_id: 1,
  13. )
  14. article = Ticket::Article.create(
  15. ticket_id: ticket.id,
  16. from: 'some_sender@example.com',
  17. to: 'some_recipient@example.com',
  18. subject: 'bounce check',
  19. message_id: '<20150830145601.30.608881@edenhofer.zammad.com>',
  20. body: 'some message bounce check',
  21. internal: false,
  22. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  23. type: Ticket::Article::Type.where(name: 'email').first,
  24. updated_by_id: 1,
  25. created_by_id: 1,
  26. )
  27. travel 1.second
  28. email_raw_string = IO.binread('test/fixtures/mail33-undelivered-mail-returned-to-sender.box')
  29. ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw_string)
  30. assert_equal(ticket.id, ticket_p.id)
  31. assert_equal('new', ticket_p.state.name)
  32. travel_back
  33. ticket.destroy
  34. end
  35. test 'process with bounce trigger email loop check - article based blocker' do
  36. roles = Role.where(name: %w[Customer])
  37. customer1 = User.create_or_update(
  38. login: 'ticket-bounce-trigger1@example.com',
  39. firstname: 'Notification',
  40. lastname: 'Customer1',
  41. email: 'ticket-bounce-trigger1@example.com',
  42. active: true,
  43. roles: roles,
  44. preferences: {},
  45. updated_by_id: 1,
  46. created_by_id: 1,
  47. )
  48. Trigger.create_or_update(
  49. name: 'auto reply new ticket',
  50. condition: {
  51. 'ticket.action' => {
  52. 'operator' => 'is',
  53. 'value' => 'create',
  54. },
  55. },
  56. perform: {
  57. 'notification.email' => {
  58. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  59. 'recipient' => 'ticket_customer',
  60. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  61. },
  62. },
  63. disable_notification: true,
  64. active: true,
  65. created_by_id: 1,
  66. updated_by_id: 1,
  67. )
  68. Trigger.create_or_update(
  69. name: 'auto reply followup',
  70. condition: {
  71. 'ticket.action' => {
  72. 'operator' => 'is',
  73. 'value' => 'update',
  74. },
  75. },
  76. perform: {
  77. 'notification.email' => {
  78. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  79. 'recipient' => 'ticket_customer',
  80. 'subject' => 'Thanks for your follow up (#{ticket.title})!',
  81. },
  82. },
  83. disable_notification: true,
  84. active: true,
  85. created_by_id: 1,
  86. updated_by_id: 1,
  87. )
  88. ticket = Ticket.create(
  89. title: 'bounce check',
  90. group: Group.lookup(name: 'Users'),
  91. customer: customer1,
  92. state: Ticket::State.lookup(name: 'new'),
  93. priority: Ticket::Priority.lookup(name: '2 normal'),
  94. updated_by_id: 1,
  95. created_by_id: 1,
  96. )
  97. article = Ticket::Article.create(
  98. ticket_id: ticket.id,
  99. from: 'some_sender@example.com',
  100. to: 'some_recipient@example.com',
  101. subject: 'bounce check',
  102. message_id: '<20150830145601.30.6088xx@edenhofer.zammad.com>',
  103. body: 'some message bounce check',
  104. internal: false,
  105. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  106. type: Ticket::Article::Type.where(name: 'email').first,
  107. updated_by_id: 1,
  108. created_by_id: 1,
  109. )
  110. Observer::Transaction.commit
  111. assert_equal('new', ticket.state.name)
  112. assert_equal(2, ticket.articles.count)
  113. article = Ticket::Article.create(
  114. ticket_id: ticket.id,
  115. from: 'some_sender@example.com',
  116. to: customer1.email,
  117. subject: 'bounce check 2',
  118. message_id: '<20150830145601.30.608881@edenhofer.zammad.com>',
  119. body: 'some message bounce check 2',
  120. internal: false,
  121. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  122. type: Ticket::Article::Type.where(name: 'email').first,
  123. updated_by_id: 1,
  124. created_by_id: 1,
  125. )
  126. Observer::Transaction.commit
  127. assert_equal(4, ticket.articles.count)
  128. travel 1.second
  129. email_raw_string = IO.binread('test/fixtures/mail33-undelivered-mail-returned-to-sender.box')
  130. ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw_string)
  131. assert_equal(ticket.id, ticket_p.id)
  132. assert_equal('open', ticket_p.state.name)
  133. assert_equal(5, ticket_p.articles.count)
  134. travel_back
  135. ticket.destroy
  136. end
  137. test 'process with bounce trigger email loop check - bounce based blocker' do
  138. roles = Role.where(name: %w[Customer])
  139. customer2 = User.create_or_update(
  140. login: 'ticket-bounce-trigger2@example.com',
  141. firstname: 'Notification',
  142. lastname: 'Customer2',
  143. email: 'ticket-bounce-trigger2@example.com',
  144. active: true,
  145. roles: roles,
  146. preferences: {},
  147. updated_by_id: 1,
  148. created_by_id: 1,
  149. )
  150. Trigger.create_or_update(
  151. name: 'auto reply new ticket',
  152. condition: {
  153. 'ticket.action' => {
  154. 'operator' => 'is',
  155. 'value' => 'create',
  156. },
  157. },
  158. perform: {
  159. 'notification.email' => {
  160. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  161. 'recipient' => 'ticket_customer',
  162. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  163. },
  164. },
  165. disable_notification: true,
  166. active: true,
  167. created_by_id: 1,
  168. updated_by_id: 1,
  169. )
  170. Trigger.create_or_update(
  171. name: 'auto reply followup',
  172. condition: {
  173. 'ticket.action' => {
  174. 'operator' => 'is',
  175. 'value' => 'update',
  176. },
  177. },
  178. perform: {
  179. 'notification.email' => {
  180. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  181. 'recipient' => 'ticket_customer',
  182. 'subject' => 'Thanks for your follow up (#{ticket.title})!',
  183. },
  184. },
  185. disable_notification: true,
  186. active: true,
  187. created_by_id: 1,
  188. updated_by_id: 1,
  189. )
  190. ticket = Ticket.create(
  191. title: 'bounce check',
  192. group: Group.lookup(name: 'Users'),
  193. customer: customer2,
  194. state: Ticket::State.lookup(name: 'new'),
  195. priority: Ticket::Priority.lookup(name: '2 normal'),
  196. updated_by_id: 1,
  197. created_by_id: 1,
  198. )
  199. article = Ticket::Article.create(
  200. ticket_id: ticket.id,
  201. from: 'some_sender@example.com',
  202. to: 'some_recipient@example.com',
  203. subject: 'bounce check',
  204. message_id: '<20150830145601.30.6088xx@edenhofer.zammad.com>',
  205. body: 'some message bounce check',
  206. internal: false,
  207. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  208. type: Ticket::Article::Type.where(name: 'email').first,
  209. updated_by_id: 1,
  210. created_by_id: 1,
  211. )
  212. Observer::Transaction.commit
  213. assert_equal('new', ticket.state.name)
  214. assert_equal(2, ticket.articles.count)
  215. article = Ticket::Article.create(
  216. ticket_id: ticket.id,
  217. from: 'some_sender@example.com',
  218. to: 'some_recipient@example.com',
  219. subject: 'bounce check 2',
  220. message_id: '<20170526150141.232.13312@example.zammad.loc>',
  221. body: 'some message bounce check 2',
  222. internal: false,
  223. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  224. type: Ticket::Article::Type.where(name: 'email').first,
  225. updated_by_id: 1,
  226. created_by_id: 1,
  227. )
  228. Observer::Transaction.commit
  229. assert_equal(4, ticket.articles.count)
  230. travel 1.second
  231. email_raw_string = IO.binread('test/fixtures/mail55.box')
  232. ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw_string)
  233. assert_equal(ticket.id, ticket_p.id)
  234. assert_equal('open', ticket_p.state.name)
  235. assert_equal(5, ticket_p.articles.count)
  236. travel_back
  237. ticket.destroy
  238. end
  239. end