process_simple_filter_spec.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Channel::EmailParser process with simple 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(:group_first) { create(:group, name: 'First group') }
  10. let(:email) { Channel::EmailParser.new.process({ group_id: group_default.id, trusted: false }, data) }
  11. let(:ticket) { email[0] }
  12. let(:article) { email[1] }
  13. let(:filter) do
  14. {
  15. name: 'RSpec: Channel::EmailParser#process',
  16. match: matcher,
  17. perform: {
  18. 'X-Zammad-Ticket-group_id' => {
  19. value: group.id,
  20. },
  21. 'X-Zammad-Ticket-priority_id' => {
  22. value: '1',
  23. },
  24. 'x-Zammad-Article-Internal' => {
  25. value: true,
  26. },
  27. },
  28. channel: 'email',
  29. active: true,
  30. created_by_id: 1,
  31. updated_by_id: 1,
  32. }
  33. end
  34. shared_examples 'filtered' do |params|
  35. it 'modifies ticket' do
  36. expect(ticket.group.name).to match(group.name)
  37. expect(ticket.priority.name).to match(params[:priority])
  38. expect(ticket.title).to match(params[:title])
  39. end
  40. it 'modifies article' do
  41. expect(article.sender.name).to match('Customer')
  42. expect(article.type.name).to match('email')
  43. expect(article.internal).to be true
  44. end
  45. end
  46. shared_examples 'not_filtered' do |params|
  47. it 'not modifies ticket' do
  48. expect(ticket.group.name).to match(group_default.name)
  49. expect(ticket.priority.name).to match('2 normal')
  50. expect(ticket.title).to match(params[:title])
  51. end
  52. it 'not modifies article' do
  53. expect(article.sender.name).to match('Customer')
  54. expect(article.type.name).to match('email')
  55. expect(article.internal).to be false
  56. end
  57. end
  58. context 'when from matches regex value: .*' do
  59. let(:data) do
  60. <<~MAIL
  61. From: Some Body <somebody@example.com>
  62. To: Bob <bod@example.com>
  63. Cc: any@example.com
  64. Subject: some subject - no selector
  65. Some Text
  66. MAIL
  67. end
  68. let(:group) { group_first }
  69. let(:matcher) do
  70. {
  71. from: {
  72. operator: 'matches regex',
  73. value: '.*',
  74. }
  75. }
  76. end
  77. include_examples('filtered', title: 'some subject - no selector',
  78. priority: '1 low')
  79. end
  80. context 'when from match value: *' do
  81. let(:data) do
  82. <<~MAIL
  83. From: Some Body <somebody@example.com>
  84. To: Bob <bod@example.com>
  85. Cc: any@example.com
  86. Subject: some subject - no selector
  87. Some Text
  88. MAIL
  89. end
  90. let(:group) { group_first }
  91. let(:matcher) do
  92. {
  93. from: {
  94. operator: 'contains',
  95. value: '*',
  96. }
  97. }
  98. end
  99. include_examples('filtered',
  100. title: 'some subject - no selector',
  101. priority: '1 low')
  102. end
  103. context 'when subject match value: *me*' do
  104. let(:data) do
  105. <<~MAIL
  106. From: Some Body <somebody@example.com>
  107. To: Bob <bod@example.com>
  108. Cc: any@example.com
  109. Subject: *me*
  110. Some Text
  111. MAIL
  112. end
  113. let(:group) { group_first }
  114. let(:matcher) do
  115. {
  116. subject: {
  117. operator: 'contains',
  118. value: '*me*',
  119. }
  120. }
  121. end
  122. include_examples('filtered', title: '*me*',
  123. priority: '1 low')
  124. end
  125. context 'when not subject match value: *me* and email subject: *mo*' do
  126. let(:data) do
  127. <<~MAIL
  128. From: Some Body <somebody@example.com>
  129. To: Bob <bod@example.com>
  130. Cc: any@example.com
  131. Subject: *mo*
  132. Some Text
  133. MAIL
  134. end
  135. let(:group) { group_first }
  136. let(:matcher) do
  137. {
  138. subject: {
  139. operator: 'contains not',
  140. value: '*me*',
  141. }
  142. }
  143. end
  144. include_examples('filtered', title: '*mo*',
  145. priority: '1 low')
  146. end
  147. context 'when not subject match value: *me* and email subject: *me*' do
  148. let(:data) do
  149. <<~MAIL
  150. From: Some Body <somebody@example.com>
  151. To: Bob <bod@example.com>
  152. Cc: any@example.com
  153. Subject: *me*
  154. Some Text
  155. MAIL
  156. end
  157. let(:group) { group_first }
  158. let(:matcher) do
  159. {
  160. subject: {
  161. operator: 'contains not',
  162. value: '*me*',
  163. }
  164. }
  165. end
  166. include_examples('not_filtered', title: '*me*')
  167. end
  168. context 'when message-id match value: @sombody.domain' do
  169. let(:filter) do
  170. {
  171. name: 'RSpec: Channel::EmailParser#process',
  172. match: {
  173. 'message-id': {
  174. operator: 'contains',
  175. value: '@sombody.domain>',
  176. },
  177. },
  178. perform: {
  179. 'X-Zammad-Ticket-priority_id' => {
  180. value: '1',
  181. },
  182. 'x-Zammad-Article-Internal' => {
  183. value: true,
  184. },
  185. },
  186. channel: 'email',
  187. active: true,
  188. created_by_id: 1,
  189. updated_by_id: 1,
  190. }
  191. end
  192. let(:data) do
  193. <<~MAIL
  194. From: Some Body <somebody@example.com>
  195. To: Bob <bod@example.com>
  196. Cc: any@example.com
  197. Subject: *me*
  198. Message-Id: <1520781034.17887@sombody.domain>
  199. Some Text
  200. MAIL
  201. end
  202. let(:group) { group_default }
  203. include_examples('filtered', title: '*me*',
  204. priority: '1 low')
  205. end
  206. context 'when perform customer_id empty and from match value: me@example.com' do
  207. let(:filter) do
  208. {
  209. name: 'RSpec: Channel::EmailParser#process',
  210. match: {
  211. from: {
  212. operator: 'contains',
  213. value: 'me@example.com',
  214. },
  215. },
  216. perform: {
  217. 'X-Zammad-Ticket-group_id' => {
  218. value: group.id,
  219. },
  220. 'x-Zammad-Article-Internal' => {
  221. value: true,
  222. },
  223. 'x-Zammad-Ticket-customer_id' => {
  224. value: '',
  225. value_completion: '',
  226. },
  227. },
  228. channel: 'email',
  229. active: true,
  230. created_by_id: 1,
  231. updated_by_id: 1,
  232. }
  233. end
  234. let(:data) do
  235. <<~MAIL
  236. From: ME Bob <me@example.com>
  237. To: customer@example.com
  238. Subject: some subject
  239. Some Text
  240. MAIL
  241. end
  242. let(:group) { group_first }
  243. include_examples('filtered', title: 'some subject',
  244. priority: '2 normal')
  245. it 'modifies customer email' do
  246. expect(ticket.customer.email).to match('me@example.com')
  247. end
  248. end
  249. context 'when perform customer_id has high value and from match value: me@example.com' do
  250. let(:filter) do
  251. {
  252. name: 'RSpec: Channel::EmailParser#process',
  253. match: {
  254. from: {
  255. operator: 'contains',
  256. value: 'me@example.com',
  257. },
  258. },
  259. perform: {
  260. 'X-Zammad-Ticket-group_id' => {
  261. value: group.id,
  262. },
  263. 'x-Zammad-Article-Internal' => {
  264. value: true,
  265. },
  266. 'x-Zammad-Ticket-customer_id' => {
  267. value: 999_999,
  268. value_completion: 'xxx',
  269. },
  270. },
  271. channel: 'email',
  272. active: true,
  273. created_by_id: 1,
  274. updated_by_id: 1,
  275. }
  276. end
  277. let(:data) do
  278. <<~MAIL
  279. From: ME Bob <me@example.com>
  280. To: customer@example.com
  281. Subject: some subject
  282. Some Text
  283. MAIL
  284. end
  285. let(:group) { group_first }
  286. include_examples('filtered', title: 'some subject',
  287. priority: '2 normal')
  288. it 'modifies customer email' do
  289. expect(ticket.customer.email).to match('me@example.com')
  290. end
  291. end
  292. context 'when perform customer_id and priority_id has high value and from match value: me@example.com' do
  293. let(:filter) do
  294. {
  295. name: 'RSpec: Channel::EmailParser#process',
  296. match: {
  297. from: {
  298. operator: 'contains',
  299. value: 'me@example.com',
  300. },
  301. },
  302. perform: {
  303. 'X-Zammad-Ticket-group_id' => {
  304. value: group.id,
  305. },
  306. 'X-Zammad-Ticket-priority_id' => {
  307. value: 888_888,
  308. },
  309. 'x-Zammad-Article-Internal' => {
  310. value: true,
  311. },
  312. 'x-Zammad-Ticket-customer_id' => {
  313. value: 999_999,
  314. value_completion: 'xxx',
  315. },
  316. },
  317. channel: 'email',
  318. active: true,
  319. created_by_id: 1,
  320. updated_by_id: 1,
  321. }
  322. end
  323. let(:data) do
  324. <<~MAIL
  325. From: ME Bob <me@example.com>
  326. To: customer@example.com
  327. Subject: some subject
  328. Some Text
  329. MAIL
  330. end
  331. let(:group) { group_first }
  332. include_examples('filtered', title: 'some subject',
  333. priority: '2 normal')
  334. it 'modifies customer email' do
  335. expect(ticket.customer.email).to match('me@example.com')
  336. end
  337. end
  338. end