email_process_auto_response_test.rb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # rubocop:disable Lint/InterpolationCheck
  2. require 'test_helper'
  3. class EmailProcessAutoResponseTest < ActiveSupport::TestCase
  4. test 'process auto reply check' do
  5. roles = Role.where(name: 'Agent')
  6. agent1 = User.create_or_update(
  7. login: 'ticket-auto-responder-agent1@example.com',
  8. firstname: 'AutoReponder',
  9. lastname: 'Agent1',
  10. email: 'ticket-auto-responder-agent1@example.com',
  11. password: 'agentpw',
  12. active: true,
  13. roles: roles,
  14. groups: Group.all,
  15. updated_by_id: 1,
  16. created_by_id: 1,
  17. )
  18. Trigger.create_or_update(
  19. name: 'auto reply',
  20. condition: {
  21. 'ticket.state_id' => {
  22. 'operator' => 'is',
  23. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  24. }
  25. },
  26. perform: {
  27. 'notification.email' => {
  28. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}',
  29. 'recipient' => 'ticket_customer',
  30. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  31. },
  32. 'ticket.priority_id' => {
  33. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  34. },
  35. 'ticket.tags' => {
  36. 'operator' => 'add',
  37. 'value' => 'aa, kk',
  38. },
  39. },
  40. disable_notification: true,
  41. active: true,
  42. created_by_id: 1,
  43. updated_by_id: 1,
  44. )
  45. email_raw_string = "From: me@example.com
  46. To: customer@example.com
  47. Subject: some new subject
  48. Some Text"
  49. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  50. assert_equal(true, mail['x-zammad-send-auto-response'.to_sym])
  51. Scheduler.worker(true)
  52. assert_equal(2, article_p.ticket.articles.count)
  53. email_raw_string = "From: me@example.com
  54. To: customer@example.com
  55. Subject: some new subject
  56. X-Loop: yes
  57. Some Text"
  58. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  59. assert_equal(false, mail['x-zammad-send-auto-response'.to_sym])
  60. Scheduler.worker(true)
  61. assert_equal(1, article_p.ticket.articles.count)
  62. email_raw_string = "From: me@example.com
  63. To: customer@example.com
  64. Subject: some new subject
  65. Precedence: Bulk
  66. Some Text"
  67. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  68. assert_equal(false, mail['x-zammad-send-auto-response'.to_sym])
  69. email_raw_string = "From: me@example.com
  70. To: customer@example.com
  71. Subject: some new subject
  72. Auto-Submitted: auto-generated
  73. Some Text"
  74. Scheduler.worker(true)
  75. assert_equal(1, article_p.ticket.articles.count)
  76. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  77. assert_equal(false, mail['x-zammad-send-auto-response'.to_sym])
  78. email_raw_string = "From: me@example.com
  79. To: customer@example.com
  80. Subject: some new subject
  81. X-Auto-Response-Suppress: All
  82. Some Text"
  83. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  84. assert_equal(false, mail['x-zammad-send-auto-response'.to_sym])
  85. Scheduler.worker(true)
  86. assert_equal(1, article_p.ticket.articles.count)
  87. email_raw_string = "From: me@example.com
  88. To: customer@example.com
  89. Subject: some new subject
  90. List-Unsubscribe: <mailto:somebody@example.com>
  91. Some Text"
  92. fqdn = Setting.get('fqdn')
  93. email_raw_string = "From: me@example.com
  94. To: customer@example.com
  95. Subject: some new subject
  96. Message-ID: <1234@#{fqdn}>
  97. Some Text"
  98. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  99. assert_equal(false, mail['x-zammad-send-auto-response'.to_sym])
  100. Scheduler.worker(true)
  101. assert_equal(1, article_p.ticket.articles.count)
  102. fqdn = Setting.get('fqdn')
  103. email_raw_string = "From: me@example.com
  104. To: customer@example.com
  105. Subject: some new subject
  106. Message-ID: <1234@not_matching.#{fqdn}>
  107. Some Text"
  108. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  109. assert_equal(true, mail['x-zammad-send-auto-response'.to_sym])
  110. Scheduler.worker(true)
  111. assert_equal(2, article_p.ticket.articles.count)
  112. email_raw_string = "Return-Path: <XX@XX.XX>
  113. X-Original-To: sales@znuny.com
  114. Received: from mail-qk0-f170.example.com (mail-qk0-f170.example.com [209.1.1.1])
  115. by arber.znuny.com (Postfix) with ESMTPS id C3AED5FE2E
  116. for <sales@znuny.com>; Mon, 22 Aug 2016 19:03:15 +0200 (CEST)
  117. Received: by mail-qk0-f170.example.com with SMTP id t7so87721720qkh.1
  118. for <sales@znuny.com>; Mon, 22 Aug 2016 10:03:15 -0700 (PDT)
  119. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
  120. d=XX.XX; s=example;
  121. h=to:from:date:message-id:subject:mime-version:precedence
  122. :auto-submitted:content-transfer-encoding:content-disposition;
  123. bh=SL5tTVvGdxsKjLic38irxzlP439P3jixJH0QTG1HJ5I=;
  124. b=CIk3PLELgjOCagyiFFbd6rlb8ZRDGYRUrg5Dntxa7e5X+PT4cgL+IE13N9TFkK8ZUJ
  125. GohlaPLGiBymIYLTtYMKUpcf22oiX8ZgGiSu1aEMC1Gsa1ZDf+vpy4kd4+7EecRT3IWF
  126. 4RafQxeaqe67budhQpO1Z6UAel6BdJj0xguKM=
  127. X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
  128. d=1e100.net; s=20130820;
  129. h=x-gm-message-state:to:from:date:message-id:subject:mime-version
  130. :precedence:auto-submitted:content-transfer-encoding
  131. :content-disposition;
  132. bh=SL5tTVvGdxsKjLic38irxzlP439P3jixJH0QTG1HJ5I=;
  133. b=PYULo3xigc4O/cuNZ79OathQ5HDMFWWIwUxz6CHbpXDQR5k3EPy/skJU1992hVz9Rl
  134. xiGwScBCkMqOjlxHjQSWhFJIxNtdvMk4m0bixBZ79IEvRuQa9cEbqjf6efnV58br5ftQ
  135. 2osHrtQczoSqLE/d61/o102RfQ0avVyX8XNJik0iepg8MiCY7LTOE9hrbnuDDLxgQecH
  136. rMEfkR7bafcUj1YEto5Vd7uV11cVZYx8UIQqVAVbfygv8dTSFeOzz3NyM0M41rRexfYH
  137. 79Yi5i7z/Wk6q2427wkJ3FIR1B7VQVQEmcq/Texbch+gAXPGBNPUHdg2WHt7NXGktrHL
  138. d3DA==
  139. X-Gm-Message-State: AE9vXwMCTnihGiG/tc7xNNlhFLcEK6DPp7otypJg5e4alD3xGK2R707BP29druIi/mcdNyaHg1vP5lSZ8EvrwvOF8iA0HNFhECGjBTJ40YrSJAR8E89xVwxFv/er+U3vEpqmPmt+hL4QhxK/+D2gKOcHSxku
  140. X-Received: by 10.1.1.1 with SMTP id 17mr25015996qkf.279.1471885393931;
  141. Mon, 22 Aug 2016 10:03:13 -0700 (PDT)
  142. To: sales@znuny.com
  143. From: \"XXX\" <XX@XX.XX>
  144. Date: Mon, 22 Aug 2016 10:03:13 -0700
  145. Message-ID: <CA+kqV8PH1DU+zcSx3M00Hrm_oJedRLjbgAUdoi9p0+sMwYsyUg@mail.gmail.com>
  146. Subject: XX PieroXXway - vacation response RE: Callback Request: XX XX [Ticket#1118974]
  147. MIME-Version: 1.0
  148. Precedence: bulk
  149. X-Autoreply: yes
  150. Auto-Submitted: auto-replied
  151. Content-Type: text/html; charset=UTF-8
  152. Content-Transfer-Encoding: quoted-printable
  153. Content-Disposition: inline
  154. test"
  155. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  156. assert_equal(false, mail['x-zammad-send-auto-response'.to_sym])
  157. Scheduler.worker(true)
  158. assert_equal(1, article_p.ticket.articles.count)
  159. # add an agent notification
  160. Trigger.create_or_update(
  161. name: 'additional agent notification',
  162. condition: {
  163. 'ticket.state_id' => {
  164. 'operator' => 'is',
  165. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  166. }
  167. },
  168. perform: {
  169. 'notification.email' => {
  170. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}',
  171. 'recipient' => 'ticket_agents',
  172. 'subject' => 'New Ticket add. info (#{ticket.title})!',
  173. },
  174. 'ticket.priority_id' => {
  175. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  176. },
  177. 'ticket.tags' => {
  178. 'operator' => 'add',
  179. 'value' => 'aa, kk',
  180. },
  181. },
  182. disable_notification: true,
  183. active: true,
  184. created_by_id: 1,
  185. updated_by_id: 1,
  186. )
  187. email_raw_string = "From: me@example.com
  188. To: customer@example.com
  189. Subject: some new subject
  190. X-Loop: yes
  191. Some Text"
  192. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
  193. assert_equal(false, mail['x-zammad-send-auto-response'.to_sym])
  194. Scheduler.worker(true)
  195. assert_equal(2, article_p.ticket.articles.count)
  196. article_customer = article_p.ticket.articles.first
  197. assert_equal('me@example.com', article_customer.from)
  198. assert_equal('customer@example.com', article_customer.to)
  199. assert_equal('Customer', article_customer.sender.name)
  200. assert_equal('email', article_customer.type.name)
  201. article_notification = article_p.ticket.articles.last
  202. assert_match(/New Ticket add. info/, article_notification.subject)
  203. assert_no_match(/me@example.com/, article_notification.to)
  204. assert_match(/#{agent1.email}/, article_notification.to)
  205. assert_equal('System', article_notification.sender.name)
  206. assert_equal('email', article_notification.type.name)
  207. Trigger.destroy_all
  208. end
  209. end