email_process_bounce_delivery_permanent_failed_test.rb 6.9 KB

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