email_process_bounce_delivery_permanent_failed_test.rb 6.9 KB

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