email_process_bounce_follow_test.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. require 'test_helper'
  2. class EmailProcessBounceFollowUpTest < ActiveSupport::TestCase
  3. test 'process with bounce follow up check' do
  4. ticket = Ticket.create(
  5. title: 'bounce check',
  6. group: Group.lookup(name: 'Users'),
  7. customer_id: 2,
  8. state: Ticket::State.lookup(name: 'new'),
  9. priority: Ticket::Priority.lookup(name: '2 normal'),
  10. updated_by_id: 1,
  11. created_by_id: 1,
  12. )
  13. article = Ticket::Article.create(
  14. ticket_id: ticket.id,
  15. from: 'some_sender@example.com',
  16. to: 'some_recipient@example.com',
  17. subject: 'bounce check',
  18. message_id: '<20150830145601.30.608881@edenhofer.zammad.com>',
  19. body: 'some message bounce check',
  20. internal: false,
  21. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  22. type: Ticket::Article::Type.where(name: 'email').first,
  23. updated_by_id: 1,
  24. created_by_id: 1,
  25. )
  26. travel 1.second
  27. email_file_path = Rails.root.join('test', 'data', 'mail', 'mail033-undelivered-mail-returned-to-sender.box')
  28. email_raw_string = File.read(email_file_path)
  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_file_path = Rails.root.join('test', 'data', 'mail', 'mail033-undelivered-mail-returned-to-sender.box')
  130. email_raw_string = File.read(email_file_path)
  131. ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw_string)
  132. assert_equal(ticket.id, ticket_p.id)
  133. assert_equal('open', ticket_p.state.name)
  134. assert_equal(5, ticket_p.articles.count)
  135. travel_back
  136. ticket.destroy
  137. end
  138. test 'process with bounce trigger email loop check - bounce based blocker' do
  139. roles = Role.where(name: %w[Customer])
  140. customer2 = User.create_or_update(
  141. login: 'ticket-bounce-trigger2@example.com',
  142. firstname: 'Notification',
  143. lastname: 'Customer2',
  144. email: 'ticket-bounce-trigger2@example.com',
  145. active: true,
  146. roles: roles,
  147. preferences: {},
  148. updated_by_id: 1,
  149. created_by_id: 1,
  150. )
  151. Trigger.create_or_update(
  152. name: 'auto reply new ticket',
  153. condition: {
  154. 'ticket.action' => {
  155. 'operator' => 'is',
  156. 'value' => 'create',
  157. },
  158. },
  159. perform: {
  160. 'notification.email' => {
  161. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  162. 'recipient' => 'ticket_customer',
  163. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  164. },
  165. },
  166. disable_notification: true,
  167. active: true,
  168. created_by_id: 1,
  169. updated_by_id: 1,
  170. )
  171. Trigger.create_or_update(
  172. name: 'auto reply followup',
  173. condition: {
  174. 'ticket.action' => {
  175. 'operator' => 'is',
  176. 'value' => 'update',
  177. },
  178. },
  179. perform: {
  180. 'notification.email' => {
  181. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  182. 'recipient' => 'ticket_customer',
  183. 'subject' => 'Thanks for your follow up (#{ticket.title})!',
  184. },
  185. },
  186. disable_notification: true,
  187. active: true,
  188. created_by_id: 1,
  189. updated_by_id: 1,
  190. )
  191. ticket = Ticket.create(
  192. title: 'bounce check',
  193. group: Group.lookup(name: 'Users'),
  194. customer: customer2,
  195. state: Ticket::State.lookup(name: 'new'),
  196. priority: Ticket::Priority.lookup(name: '2 normal'),
  197. updated_by_id: 1,
  198. created_by_id: 1,
  199. )
  200. article = Ticket::Article.create(
  201. ticket_id: ticket.id,
  202. from: 'some_sender@example.com',
  203. to: 'some_recipient@example.com',
  204. subject: 'bounce check',
  205. message_id: '<20150830145601.30.6088xx@edenhofer.zammad.com>',
  206. body: 'some message bounce check',
  207. internal: false,
  208. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  209. type: Ticket::Article::Type.where(name: 'email').first,
  210. updated_by_id: 1,
  211. created_by_id: 1,
  212. )
  213. Observer::Transaction.commit
  214. assert_equal('new', ticket.state.name)
  215. assert_equal(2, ticket.articles.count)
  216. article = Ticket::Article.create(
  217. ticket_id: ticket.id,
  218. from: 'some_sender@example.com',
  219. to: 'some_recipient@example.com',
  220. subject: 'bounce check 2',
  221. message_id: '<20170526150141.232.13312@example.zammad.loc>',
  222. body: 'some message bounce check 2',
  223. internal: false,
  224. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  225. type: Ticket::Article::Type.where(name: 'email').first,
  226. updated_by_id: 1,
  227. created_by_id: 1,
  228. )
  229. Observer::Transaction.commit
  230. assert_equal(4, ticket.articles.count)
  231. travel 1.second
  232. email_file_path = Rails.root.join('test', 'data', 'mail', 'mail055.box')
  233. email_raw_string = File.read(email_file_path)
  234. ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw_string)
  235. assert_equal(ticket.id, ticket_p.id)
  236. assert_equal('open', ticket_p.state.name)
  237. assert_equal(5, ticket_p.articles.count)
  238. travel_back
  239. ticket.destroy
  240. end
  241. end