process_auto_submitted_filter_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Channel::EmailParser process with auto-submitted filter', aggregate_failures: true, type: :model do
  4. before do
  5. PostmasterFilter.destroy_all
  6. PostmasterFilter.create!(filter)
  7. end
  8. let(:group_default) { Group.lookup(name: 'Users') }
  9. let(:email) { Channel::EmailParser.new.process({ group_id: group_default.id, trusted: false }, data) }
  10. let(:ticket) { email[0] }
  11. let(:article) { email[1] }
  12. shared_examples 'modifies ticket' do
  13. it 'modifies ticket' do
  14. expect(ticket.group.name).to match(group_default.name)
  15. expect(ticket.priority.name).to match('2 normal')
  16. expect(ticket.title).to match('some subject')
  17. expect(ticket.customer.email).to match('me@example.com')
  18. end
  19. end
  20. shared_examples 'article is note' do
  21. it 'modifies article note' do
  22. expect(article.sender.name).to match('Customer')
  23. expect(article.type.name).to match('note')
  24. expect(article.internal).to be true
  25. end
  26. end
  27. shared_examples 'article is email' do
  28. it 'modifies article email' do
  29. expect(article.sender.name).to match('Customer')
  30. expect(article.type.name).to match('email')
  31. expect(article.internal).to be false
  32. end
  33. end
  34. context 'when from match value: @example.com and auto-submitted contains not: auto-generated' do
  35. let(:filter) do
  36. {
  37. name: 'RSpec: Channel::EmailParser#process',
  38. match: {
  39. 'auto-submitted' => {
  40. 'operator' => 'contains not',
  41. 'value' => 'auto-generated',
  42. },
  43. 'from' => {
  44. 'operator' => 'contains',
  45. 'value' => '@example.com',
  46. }
  47. },
  48. perform: {
  49. 'x-zammad-article-internal' => {
  50. 'value' => 'true',
  51. },
  52. 'x-zammad-article-type_id' => {
  53. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  54. },
  55. 'x-zammad-ignore' => {
  56. 'value' => 'false',
  57. },
  58. },
  59. channel: 'email',
  60. active: true,
  61. created_by_id: 1,
  62. updated_by_id: 1,
  63. }
  64. end
  65. let(:data) do
  66. <<~MAIL
  67. From: ME Bob <me@example.com>
  68. To: customer@example.com
  69. Subject: some subject
  70. Some Text
  71. MAIL
  72. end
  73. include_examples 'modifies ticket'
  74. include_examples 'article is note'
  75. end
  76. context 'when from match value: @example.com and auto-submitted contains: auto-generated' do
  77. let(:filter) do
  78. {
  79. name: 'RSpec: Channel::EmailParser#process',
  80. match: {
  81. 'auto-submitted' => {
  82. 'operator' => 'contains',
  83. 'value' => 'auto-generated',
  84. },
  85. 'from' => {
  86. 'operator' => 'contains',
  87. 'value' => '@example.com',
  88. }
  89. },
  90. perform: {
  91. 'x-zammad-article-internal' => {
  92. 'value' => 'true',
  93. },
  94. 'x-zammad-article-type_id' => {
  95. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  96. },
  97. 'x-zammad-ignore' => {
  98. 'value' => 'false',
  99. },
  100. },
  101. channel: 'email',
  102. active: true,
  103. created_by_id: 1,
  104. updated_by_id: 1,
  105. }
  106. end
  107. let(:data) do
  108. <<~MAIL
  109. From: ME Bob <me@example.com>
  110. To: customer@example.com
  111. Subject: some subject
  112. Some Text
  113. MAIL
  114. end
  115. include_examples 'modifies ticket'
  116. include_examples 'article is email'
  117. end
  118. context 'when to match value: customer@example.com and auto-submitted contains not: auto-generated' do
  119. let(:filter) do
  120. {
  121. name: 'RSpec: Channel::EmailParser#process',
  122. match: {
  123. 'auto-submitted' => {
  124. 'operator' => 'contains not',
  125. 'value' => 'auto-generated',
  126. },
  127. 'to' => {
  128. 'operator' => 'contains',
  129. 'value' => 'customer@example.com',
  130. },
  131. 'from' => {
  132. 'operator' => 'contains',
  133. 'value' => '@example.com',
  134. }
  135. },
  136. perform: {
  137. 'x-zammad-article-internal' => {
  138. 'value' => 'true',
  139. },
  140. 'x-zammad-article-type_id' => {
  141. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  142. },
  143. 'x-zammad-ignore' => {
  144. 'value' => 'false',
  145. },
  146. },
  147. channel: 'email',
  148. active: true,
  149. created_by_id: 1,
  150. updated_by_id: 1,
  151. }
  152. end
  153. let(:data) do
  154. <<~MAIL
  155. From: ME Bob <me@example.com>
  156. To: customer@example.com
  157. Subject: some subject
  158. Some Text
  159. MAIL
  160. end
  161. include_examples 'modifies ticket'
  162. include_examples 'article is note'
  163. end
  164. context 'when from match value: @example.com, to match value: customer1@example.com and auto-submitted contains: auto-generated' do
  165. let(:filter) do
  166. {
  167. name: 'RSpec: Channel::EmailParser#process',
  168. match: {
  169. 'auto-submitted' => {
  170. 'operator' => 'contains',
  171. 'value' => 'auto-generated',
  172. },
  173. 'to' => {
  174. 'operator' => 'contains',
  175. 'value' => 'customer1@example.com',
  176. },
  177. 'from' => {
  178. 'operator' => 'contains',
  179. 'value' => '@example.com',
  180. }
  181. },
  182. perform: {
  183. 'x-zammad-article-internal' => {
  184. 'value' => 'true',
  185. },
  186. 'x-zammad-article-type_id' => {
  187. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  188. },
  189. 'x-zammad-ignore' => {
  190. 'value' => 'false',
  191. },
  192. },
  193. channel: 'email',
  194. active: true,
  195. created_by_id: 1,
  196. updated_by_id: 1,
  197. }
  198. end
  199. let(:data) do
  200. <<~MAIL
  201. From: ME Bob <me@example.com>
  202. To: customer@example.com
  203. Subject: some subject
  204. Some Text
  205. MAIL
  206. end
  207. include_examples 'modifies ticket'
  208. include_examples 'article is email'
  209. end
  210. end