email_postmaster_test.rb 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. # rubocop:disable all
  2. require 'test_helper'
  3. class EmailPostmasterTest < ActiveSupport::TestCase
  4. test 'valid/invalid postmaster filter' do
  5. PostmasterFilter.create!(
  6. name: 'not used',
  7. match: {
  8. from: {
  9. operator: 'contains',
  10. value: 'nobody@example.com',
  11. },
  12. },
  13. perform: {
  14. 'X-Zammad-Ticket-priority' => {
  15. value: '3 high',
  16. },
  17. },
  18. channel: 'email',
  19. active: true,
  20. created_by_id: 1,
  21. updated_by_id: 1,
  22. )
  23. assert_raises(Exceptions::UnprocessableEntity) {
  24. PostmasterFilter.create!(
  25. name: 'empty filter should not work',
  26. match: {},
  27. perform: {
  28. 'X-Zammad-Ticket-priority' => {
  29. value: '3 high',
  30. },
  31. },
  32. channel: 'email',
  33. active: true,
  34. created_by_id: 1,
  35. updated_by_id: 1,
  36. )
  37. }
  38. assert_raises(Exceptions::UnprocessableEntity) {
  39. PostmasterFilter.create!(
  40. name: 'empty filter should not work',
  41. match: {
  42. from: {
  43. operator: 'contains',
  44. value: '',
  45. },
  46. },
  47. perform: {
  48. 'X-Zammad-Ticket-priority' => {
  49. value: '3 high',
  50. },
  51. },
  52. channel: 'email',
  53. active: true,
  54. created_by_id: 1,
  55. updated_by_id: 1,
  56. )
  57. }
  58. assert_raises(Exceptions::UnprocessableEntity) {
  59. PostmasterFilter.create!(
  60. name: 'invalid regex',
  61. match: {
  62. from: {
  63. operator: 'contains',
  64. value: 'regex:[]',
  65. },
  66. },
  67. perform: {
  68. 'X-Zammad-Ticket-priority' => {
  69. value: '3 high',
  70. },
  71. },
  72. channel: 'email',
  73. active: true,
  74. created_by_id: 1,
  75. updated_by_id: 1,
  76. )
  77. }
  78. assert_raises(Exceptions::UnprocessableEntity) {
  79. PostmasterFilter.create!(
  80. name: 'invalid regex',
  81. match: {
  82. from: {
  83. operator: 'contains',
  84. value: 'regex:??',
  85. },
  86. },
  87. perform: {
  88. 'X-Zammad-Ticket-priority' => {
  89. value: '3 high',
  90. },
  91. },
  92. channel: 'email',
  93. active: true,
  94. created_by_id: 1,
  95. updated_by_id: 1,
  96. )
  97. }
  98. assert_raises(Exceptions::UnprocessableEntity) {
  99. PostmasterFilter.create!(
  100. name: 'invalid regex',
  101. match: {
  102. from: {
  103. operator: 'contains',
  104. value: 'regex:*',
  105. },
  106. },
  107. perform: {
  108. 'X-Zammad-Ticket-priority' => {
  109. value: '3 high',
  110. },
  111. },
  112. channel: 'email',
  113. active: true,
  114. created_by_id: 1,
  115. updated_by_id: 1,
  116. )
  117. }
  118. PostmasterFilter.create!(
  119. name: 'use .*',
  120. match: {
  121. from: {
  122. operator: 'contains',
  123. value: '*',
  124. },
  125. },
  126. perform: {
  127. 'X-Zammad-Ticket-priority' => {
  128. value: '3 high',
  129. },
  130. },
  131. channel: 'email',
  132. active: true,
  133. created_by_id: 1,
  134. updated_by_id: 1,
  135. )
  136. end
  137. test 'process with postmaster filter with regex' do
  138. group_default = Group.lookup(name: 'Users')
  139. group1 = Group.create_if_not_exists(
  140. name: 'Test Group1',
  141. created_by_id: 1,
  142. updated_by_id: 1,
  143. )
  144. group2 = Group.create_if_not_exists(
  145. name: 'Test Group2',
  146. created_by_id: 1,
  147. updated_by_id: 1,
  148. )
  149. PostmasterFilter.destroy_all
  150. PostmasterFilter.create!(
  151. name: 'used - empty selector',
  152. match: {
  153. from: {
  154. operator: 'contains',
  155. value: 'regex:.*',
  156. },
  157. },
  158. perform: {
  159. 'X-Zammad-Ticket-group_id' => {
  160. value: group2.id,
  161. },
  162. 'X-Zammad-Ticket-priority_id' => {
  163. value: '1',
  164. },
  165. 'x-Zammad-Article-Internal' => {
  166. value: true,
  167. },
  168. },
  169. channel: 'email',
  170. active: true,
  171. created_by_id: 1,
  172. updated_by_id: 1,
  173. )
  174. data = 'From: Some Body <somebody@example.com>
  175. To: Bob <bod@example.com>
  176. Cc: any@example.com
  177. Subject: some subject - no selector
  178. Some Text'
  179. parser = Channel::EmailParser.new
  180. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  181. assert_equal('Test Group2', ticket.group.name)
  182. assert_equal('1 low', ticket.priority.name)
  183. assert_equal('some subject - no selector', ticket.title)
  184. assert_equal('Customer', article.sender.name)
  185. assert_equal('email', article.type.name)
  186. assert_equal(true, article.internal)
  187. PostmasterFilter.destroy_all
  188. PostmasterFilter.create!(
  189. name: 'used - empty selector',
  190. match: {
  191. from: {
  192. operator: 'contains',
  193. value: '*',
  194. },
  195. },
  196. perform: {
  197. 'X-Zammad-Ticket-group_id' => {
  198. value: group2.id,
  199. },
  200. 'X-Zammad-Ticket-priority_id' => {
  201. value: '1',
  202. },
  203. 'x-Zammad-Article-Internal' => {
  204. value: true,
  205. },
  206. },
  207. channel: 'email',
  208. active: true,
  209. created_by_id: 1,
  210. updated_by_id: 1,
  211. )
  212. data = 'From: Some Body <somebody@example.com>
  213. To: Bob <bod@example.com>
  214. Cc: any@example.com
  215. Subject: some subject - no selector
  216. Some Text'
  217. parser = Channel::EmailParser.new
  218. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  219. assert_equal('Test Group2', ticket.group.name)
  220. assert_equal('1 low', ticket.priority.name)
  221. assert_equal('some subject - no selector', ticket.title)
  222. assert_equal('Customer', article.sender.name)
  223. assert_equal('email', article.type.name)
  224. assert_equal(true, article.internal)
  225. PostmasterFilter.destroy_all
  226. PostmasterFilter.create!(
  227. name: 'used - empty selector',
  228. match: {
  229. subject: {
  230. operator: 'contains',
  231. value: '*me*',
  232. },
  233. },
  234. perform: {
  235. 'X-Zammad-Ticket-group_id' => {
  236. value: group2.id,
  237. },
  238. 'X-Zammad-Ticket-priority_id' => {
  239. value: '1',
  240. },
  241. 'x-Zammad-Article-Internal' => {
  242. value: true,
  243. },
  244. },
  245. channel: 'email',
  246. active: true,
  247. created_by_id: 1,
  248. updated_by_id: 1,
  249. )
  250. data = 'From: Some Body <somebody@example.com>
  251. To: Bob <bod@example.com>
  252. Cc: any@example.com
  253. Subject: *me*
  254. Some Text'
  255. parser = Channel::EmailParser.new
  256. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  257. assert_equal('Test Group2', ticket.group.name)
  258. assert_equal('1 low', ticket.priority.name)
  259. assert_equal('*me*', ticket.title)
  260. assert_equal('Customer', article.sender.name)
  261. assert_equal('email', article.type.name)
  262. assert_equal(true, article.internal)
  263. PostmasterFilter.destroy_all
  264. PostmasterFilter.create!(
  265. name: 'used - empty selector',
  266. match: {
  267. subject: {
  268. operator: 'contains not',
  269. value: '*me*',
  270. },
  271. },
  272. perform: {
  273. 'X-Zammad-Ticket-group_id' => {
  274. value: group2.id,
  275. },
  276. 'X-Zammad-Ticket-priority_id' => {
  277. value: '1',
  278. },
  279. 'x-Zammad-Article-Internal' => {
  280. value: true,
  281. },
  282. },
  283. channel: 'email',
  284. active: true,
  285. created_by_id: 1,
  286. updated_by_id: 1,
  287. )
  288. data = 'From: Some Body <somebody@example.com>
  289. To: Bob <bod@example.com>
  290. Cc: any@example.com
  291. Subject: *mo*
  292. Some Text'
  293. parser = Channel::EmailParser.new
  294. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  295. assert_equal('Test Group2', ticket.group.name)
  296. assert_equal('1 low', ticket.priority.name)
  297. assert_equal('*mo*', ticket.title)
  298. assert_equal('Customer', article.sender.name)
  299. assert_equal('email', article.type.name)
  300. assert_equal(true, article.internal)
  301. PostmasterFilter.destroy_all
  302. PostmasterFilter.create!(
  303. name: 'used - empty selector',
  304. match: {
  305. subject: {
  306. operator: 'contains not',
  307. value: '*me*',
  308. },
  309. },
  310. perform: {
  311. 'X-Zammad-Ticket-group_id' => {
  312. value: group2.id,
  313. },
  314. 'X-Zammad-Ticket-priority_id' => {
  315. value: '1',
  316. },
  317. 'x-Zammad-Article-Internal' => {
  318. value: true,
  319. },
  320. },
  321. channel: 'email',
  322. active: true,
  323. created_by_id: 1,
  324. updated_by_id: 1,
  325. )
  326. data = 'From: Some Body <somebody@example.com>
  327. To: Bob <bod@example.com>
  328. Cc: any@example.com
  329. Subject: *me*
  330. Some Text'
  331. parser = Channel::EmailParser.new
  332. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  333. assert_equal('Users', ticket.group.name)
  334. assert_equal('2 normal', ticket.priority.name)
  335. assert_equal('*me*', ticket.title)
  336. assert_equal('Customer', article.sender.name)
  337. assert_equal('email', article.type.name)
  338. assert_equal(false, article.internal)
  339. end
  340. test 'process with postmaster filter - message-id as match condition' do
  341. PostmasterFilter.create!(
  342. name: 'used - message-id',
  343. match: {
  344. 'message-id': {
  345. operator: 'contains',
  346. value: '@sombody.domain>',
  347. },
  348. },
  349. perform: {
  350. 'X-Zammad-Ticket-priority_id' => {
  351. value: '1',
  352. },
  353. 'x-Zammad-Article-Internal' => {
  354. value: true,
  355. },
  356. },
  357. channel: 'email',
  358. active: true,
  359. created_by_id: 1,
  360. updated_by_id: 1,
  361. )
  362. data = 'From: Some Body <somebody@example.com>
  363. To: Bob <bod@example.com>
  364. Cc: any@example.com
  365. Subject: *me*
  366. Message-Id: <1520781034.17887@sombody.domain>
  367. Some Text'
  368. parser = Channel::EmailParser.new
  369. ticket, article, user = parser.process({}, data)
  370. assert_equal('Users', ticket.group.name)
  371. assert_equal('1 low', ticket.priority.name)
  372. assert_equal('*me*', ticket.title)
  373. assert_equal('Customer', article.sender.name)
  374. assert_equal('email', article.type.name)
  375. assert_equal(true, article.internal)
  376. end
  377. test 'process with postmaster filter' do
  378. group_default = Group.lookup(name: 'Users')
  379. group1 = Group.create_if_not_exists(
  380. name: 'Test Group1',
  381. created_by_id: 1,
  382. updated_by_id: 1,
  383. )
  384. group2 = Group.create_if_not_exists(
  385. name: 'Test Group2',
  386. created_by_id: 1,
  387. updated_by_id: 1,
  388. )
  389. PostmasterFilter.destroy_all
  390. PostmasterFilter.create!(
  391. name: 'not used',
  392. match: {
  393. from: {
  394. operator: 'contains',
  395. value: 'nobody@example.com',
  396. },
  397. },
  398. perform: {
  399. 'X-Zammad-Ticket-priority' => {
  400. value: '3 high',
  401. },
  402. },
  403. channel: 'email',
  404. active: true,
  405. created_by_id: 1,
  406. updated_by_id: 1,
  407. )
  408. PostmasterFilter.create!(
  409. name: 'used',
  410. match: {
  411. from: {
  412. operator: 'contains',
  413. value: 'me@example.com',
  414. },
  415. },
  416. perform: {
  417. 'X-Zammad-Ticket-group_id' => {
  418. value: group1.id,
  419. },
  420. 'x-Zammad-Article-Internal' => {
  421. value: true,
  422. },
  423. },
  424. channel: 'email',
  425. active: true,
  426. created_by_id: 1,
  427. updated_by_id: 1,
  428. )
  429. PostmasterFilter.create!(
  430. name: 'used x-any-recipient',
  431. match: {
  432. 'x-any-recipient' => {
  433. operator: 'contains',
  434. value: 'any@example.com',
  435. },
  436. },
  437. perform: {
  438. 'X-Zammad-Ticket-group_id' => {
  439. value: group2.id,
  440. },
  441. 'x-Zammad-Article-Internal' => {
  442. value: true,
  443. },
  444. },
  445. channel: 'email',
  446. active: true,
  447. created_by_id: 1,
  448. updated_by_id: 1,
  449. )
  450. data = 'From: me@example.com
  451. To: customer@example.com
  452. Subject: some subject
  453. Some Text'
  454. parser = Channel::EmailParser.new
  455. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  456. assert_equal('Test Group1', ticket.group.name)
  457. assert_equal('2 normal', ticket.priority.name)
  458. assert_equal('some subject', ticket.title)
  459. assert_equal('Customer', article.sender.name)
  460. assert_equal('email', article.type.name)
  461. assert_equal(true, article.internal)
  462. data = 'From: Some Body <somebody@example.com>
  463. To: Bob <bod@example.com>
  464. Cc: any@example.com
  465. Subject: some subject
  466. Some Text'
  467. parser = Channel::EmailParser.new
  468. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  469. assert_equal('Test Group2', ticket.group.name)
  470. assert_equal('2 normal', ticket.priority.name)
  471. assert_equal('some subject', ticket.title)
  472. assert_equal('Customer', article.sender.name)
  473. assert_equal('email', article.type.name)
  474. assert_equal(true, article.internal)
  475. PostmasterFilter.create!(
  476. name: 'used x-any-recipient 2',
  477. match: {
  478. 'x-any-recipient' => {
  479. operator: 'contains not',
  480. value: 'any_not@example.com',
  481. },
  482. },
  483. perform: {
  484. 'X-Zammad-Ticket-group_id' => {
  485. value: group2.id,
  486. },
  487. 'X-Zammad-Ticket-priority_id' => {
  488. value: '1',
  489. },
  490. 'x-Zammad-Article-Internal' => {
  491. value: 'false',
  492. },
  493. },
  494. channel: 'email',
  495. active: true,
  496. created_by_id: 1,
  497. updated_by_id: 1,
  498. )
  499. data = 'From: Some Body <somebody@example.com>
  500. To: Bob <bod@example.com>
  501. Cc: any@example.com
  502. Subject: some subject2
  503. Some Text'
  504. parser = Channel::EmailParser.new
  505. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  506. assert_equal('Test Group2', ticket.group.name)
  507. assert_equal('1 low', ticket.priority.name)
  508. assert_equal('some subject2', ticket.title)
  509. assert_equal('Customer', article.sender.name)
  510. assert_equal('email', article.type.name)
  511. assert_equal(false, article.internal)
  512. PostmasterFilter.destroy_all
  513. # follow-up with create post master filter test
  514. PostmasterFilter.create!(
  515. name: 'used - empty selector',
  516. match: {
  517. from: {
  518. operator: 'contains',
  519. value: 'example.com',
  520. },
  521. },
  522. perform: {
  523. 'X-Zammad-Ticket-group_id' => {
  524. value: group2.id,
  525. },
  526. },
  527. channel: 'email',
  528. active: true,
  529. created_by_id: 1,
  530. updated_by_id: 1,
  531. )
  532. data = 'From: Some Body <somebody@example.com>
  533. To: Bob <bod@example.com>
  534. Cc: any@example.com
  535. Subject: follow-up with create post master filter test
  536. Some Text'
  537. parser = Channel::EmailParser.new
  538. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  539. assert_equal(group2.name, ticket.group.name)
  540. assert_equal('2 normal', ticket.priority.name)
  541. assert_equal('follow-up with create post master filter test', ticket.title)
  542. assert_equal('Customer', article.sender.name)
  543. assert_equal('email', article.type.name)
  544. assert_equal(false, article.internal)
  545. # move ticket
  546. ticket.group = group1
  547. ticket.save
  548. Observer::Transaction.commit
  549. data = "From: me@example.com
  550. To: customer@example.com
  551. Subject: #{ticket.subject_build('some new subject')}
  552. Some Text"
  553. article_count = ticket.articles.count
  554. parser = Channel::EmailParser.new
  555. ticket_followup, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  556. # check if group is still the old one
  557. assert_equal(ticket.id, ticket_followup.id)
  558. assert_equal(group1.name, ticket_followup.group.name)
  559. assert_equal(group1.name, ticket_followup.group.name)
  560. assert_equal('2 normal', ticket_followup.priority.name)
  561. assert_equal('follow-up with create post master filter test', ticket_followup.title)
  562. assert_equal('Customer', article.sender.name)
  563. assert_equal('email', article.type.name)
  564. assert_equal(false, article.internal)
  565. assert_equal(article_count+1, ticket_followup.articles.count)
  566. PostmasterFilter.destroy_all
  567. PostmasterFilter.create!(
  568. name: 'used',
  569. match: {
  570. from: {
  571. operator: 'contains',
  572. value: 'me@example.com',
  573. },
  574. },
  575. perform: {
  576. 'X-Zammad-Ticket-group_id' => {
  577. value: group1.id,
  578. },
  579. 'x-Zammad-Article-Internal' => {
  580. value: true,
  581. },
  582. 'x-Zammad-Ticket-customer_id' => {
  583. value: '',
  584. value_completion: '',
  585. },
  586. },
  587. channel: 'email',
  588. active: true,
  589. created_by_id: 1,
  590. updated_by_id: 1,
  591. )
  592. data = 'From: ME Bob <me@example.com>
  593. To: customer@example.com
  594. Subject: some subject
  595. Some Text'
  596. parser = Channel::EmailParser.new
  597. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  598. assert_equal(group1.name, ticket.group.name)
  599. assert_equal('2 normal', ticket.priority.name)
  600. assert_equal('some subject', ticket.title)
  601. assert_equal('me@example.com', ticket.customer.email)
  602. PostmasterFilter.destroy_all
  603. PostmasterFilter.create!(
  604. name: 'used',
  605. match: {
  606. from: {
  607. operator: 'contains',
  608. value: 'me@example.com',
  609. },
  610. },
  611. perform: {
  612. 'X-Zammad-Ticket-group_id' => {
  613. value: group1.id,
  614. },
  615. 'x-Zammad-Article-Internal' => {
  616. value: true,
  617. },
  618. 'x-Zammad-Ticket-customer_id' => {
  619. value: 999_999,
  620. value_completion: 'xxx',
  621. },
  622. },
  623. channel: 'email',
  624. active: true,
  625. created_by_id: 1,
  626. updated_by_id: 1,
  627. )
  628. data = 'From: ME Bob <me@example.com>
  629. To: customer@example.com
  630. Subject: some subject
  631. Some Text'
  632. parser = Channel::EmailParser.new
  633. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  634. assert_equal(group1.name, ticket.group.name)
  635. assert_equal('2 normal', ticket.priority.name)
  636. assert_equal('some subject', ticket.title)
  637. assert_equal('me@example.com', ticket.customer.email)
  638. PostmasterFilter.destroy_all
  639. PostmasterFilter.create!(
  640. name: 'used',
  641. match: {
  642. from: {
  643. operator: 'contains',
  644. value: 'me@example.com',
  645. },
  646. },
  647. perform: {
  648. 'X-Zammad-Ticket-group_id' => {
  649. value: group1.id,
  650. },
  651. 'X-Zammad-Ticket-priority_id' => {
  652. value: 888_888,
  653. },
  654. 'x-Zammad-Article-Internal' => {
  655. value: true,
  656. },
  657. 'x-Zammad-Ticket-customer_id' => {
  658. value: 999_999,
  659. value_completion: 'xxx',
  660. },
  661. },
  662. channel: 'email',
  663. active: true,
  664. created_by_id: 1,
  665. updated_by_id: 1,
  666. )
  667. data = 'From: ME Bob <me@example.com>
  668. To: customer@example.com
  669. Subject: some subject
  670. Some Text'
  671. parser = Channel::EmailParser.new
  672. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  673. assert_equal(group1.name, ticket.group.name)
  674. assert_equal('2 normal', ticket.priority.name)
  675. assert_equal('some subject', ticket.title)
  676. assert_equal('me@example.com', ticket.customer.email)
  677. assert_equal('2 normal', ticket.priority.name)
  678. PostmasterFilter.destroy_all
  679. PostmasterFilter.create!(
  680. name: 'Autoresponder',
  681. match: {
  682. 'auto-submitted' => {
  683. 'operator' => 'contains not',
  684. 'value' => 'auto-generated',
  685. },
  686. 'from' => {
  687. 'operator' => 'contains',
  688. 'value' => '@example.com',
  689. }
  690. },
  691. perform: {
  692. 'x-zammad-article-internal' => {
  693. 'value' => 'true',
  694. },
  695. 'x-zammad-article-type_id' => {
  696. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  697. },
  698. 'x-zammad-ignore' => {
  699. 'value' => 'false',
  700. },
  701. },
  702. channel: 'email',
  703. active: true,
  704. created_by_id: 1,
  705. updated_by_id: 1,
  706. )
  707. data = 'From: ME Bob <me@example.com>
  708. To: customer@example.com
  709. Subject: some subject
  710. Some Text'
  711. parser = Channel::EmailParser.new
  712. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  713. assert_equal('Users', ticket.group.name)
  714. assert_equal('2 normal', ticket.priority.name)
  715. assert_equal('some subject', ticket.title)
  716. assert_equal('me@example.com', ticket.customer.email)
  717. assert_equal('2 normal', ticket.priority.name)
  718. assert_equal('Customer', article.sender.name)
  719. assert_equal('note', article.type.name)
  720. assert_equal(true, article.internal)
  721. PostmasterFilter.destroy_all
  722. PostmasterFilter.create!(
  723. name: 'Autoresponder',
  724. match: {
  725. 'auto-submitted' => {
  726. 'operator' => 'contains',
  727. 'value' => 'auto-generated',
  728. },
  729. 'from' => {
  730. 'operator' => 'contains',
  731. 'value' => '@example.com',
  732. }
  733. },
  734. perform: {
  735. 'x-zammad-article-internal' => {
  736. 'value' => 'true',
  737. },
  738. 'x-zammad-article-type_id' => {
  739. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  740. },
  741. 'x-zammad-ignore' => {
  742. 'value' => 'false',
  743. },
  744. },
  745. channel: 'email',
  746. active: true,
  747. created_by_id: 1,
  748. updated_by_id: 1,
  749. )
  750. data = 'From: ME Bob <me@example.com>
  751. To: customer@example.com
  752. Subject: some subject
  753. Some Text'
  754. parser = Channel::EmailParser.new
  755. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  756. assert_equal('Users', ticket.group.name)
  757. assert_equal('2 normal', ticket.priority.name)
  758. assert_equal('some subject', ticket.title)
  759. assert_equal('me@example.com', ticket.customer.email)
  760. assert_equal('2 normal', ticket.priority.name)
  761. assert_equal('Customer', article.sender.name)
  762. assert_equal('email', article.type.name)
  763. assert_equal(false, article.internal)
  764. PostmasterFilter.destroy_all
  765. PostmasterFilter.create!(
  766. name: 'Autoresponder',
  767. match: {
  768. 'auto-submitted' => {
  769. 'operator' => 'contains not',
  770. 'value' => 'auto-generated',
  771. },
  772. 'to' => {
  773. 'operator' => 'contains',
  774. 'value' => 'customer@example.com',
  775. },
  776. 'from' => {
  777. 'operator' => 'contains',
  778. 'value' => '@example.com',
  779. }
  780. },
  781. perform: {
  782. 'x-zammad-article-internal' => {
  783. 'value' => 'true',
  784. },
  785. 'x-zammad-article-type_id' => {
  786. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  787. },
  788. 'x-zammad-ignore' => {
  789. 'value' => 'false',
  790. },
  791. },
  792. channel: 'email',
  793. active: true,
  794. created_by_id: 1,
  795. updated_by_id: 1,
  796. )
  797. data = 'From: ME Bob <me@example.com>
  798. To: customer@example.com
  799. Subject: some subject
  800. Some Text'
  801. parser = Channel::EmailParser.new
  802. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  803. assert_equal('Users', ticket.group.name)
  804. assert_equal('2 normal', ticket.priority.name)
  805. assert_equal('some subject', ticket.title)
  806. assert_equal('me@example.com', ticket.customer.email)
  807. assert_equal('2 normal', ticket.priority.name)
  808. assert_equal('Customer', article.sender.name)
  809. assert_equal('note', article.type.name)
  810. assert_equal(true, article.internal)
  811. PostmasterFilter.destroy_all
  812. PostmasterFilter.create!(
  813. name: 'Autoresponder',
  814. match: {
  815. 'auto-submitted' => {
  816. 'operator' => 'contains',
  817. 'value' => 'auto-generated',
  818. },
  819. 'to' => {
  820. 'operator' => 'contains',
  821. 'value' => 'customer1@example.com',
  822. },
  823. 'from' => {
  824. 'operator' => 'contains',
  825. 'value' => '@example.com',
  826. }
  827. },
  828. perform: {
  829. 'x-zammad-article-internal' => {
  830. 'value' => 'true',
  831. },
  832. 'x-zammad-article-type_id' => {
  833. 'value' => Ticket::Article::Type.find_by(name: 'note').id.to_s,
  834. },
  835. 'x-zammad-ignore' => {
  836. 'value' => 'false',
  837. },
  838. },
  839. channel: 'email',
  840. active: true,
  841. created_by_id: 1,
  842. updated_by_id: 1,
  843. )
  844. data = 'From: ME Bob <me@example.com>
  845. To: customer@example.com
  846. Subject: some subject
  847. Some Text'
  848. parser = Channel::EmailParser.new
  849. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  850. assert_equal('Users', ticket.group.name)
  851. assert_equal('2 normal', ticket.priority.name)
  852. assert_equal('some subject', ticket.title)
  853. assert_equal('me@example.com', ticket.customer.email)
  854. assert_equal('2 normal', ticket.priority.name)
  855. assert_equal('Customer', article.sender.name)
  856. assert_equal('email', article.type.name)
  857. assert_equal(false, article.internal)
  858. end
  859. test 'tags in postmaster filter' do
  860. group_default = Group.lookup(name: 'Users')
  861. PostmasterFilter.create!(
  862. name: '01 set tag for email',
  863. match: {
  864. from: {
  865. operator: 'contains',
  866. value: 'nobody@example.com',
  867. },
  868. },
  869. perform: {
  870. 'x-zammad-ticket-tags' => {
  871. operator: 'add',
  872. value: 'test1, test2, test3',
  873. },
  874. },
  875. channel: 'email',
  876. active: true,
  877. created_by_id: 1,
  878. updated_by_id: 1,
  879. )
  880. PostmasterFilter.create!(
  881. name: '02 set tag for email',
  882. match: {
  883. from: {
  884. operator: 'contains',
  885. value: 'nobody@example.com',
  886. },
  887. },
  888. perform: {
  889. 'x-zammad-ticket-tags' => {
  890. operator: 'remove',
  891. value: 'test2, test3',
  892. },
  893. },
  894. channel: 'email',
  895. active: true,
  896. created_by_id: 1,
  897. updated_by_id: 1,
  898. )
  899. PostmasterFilter.create!(
  900. name: '03 set tag for email',
  901. match: {
  902. from: {
  903. operator: 'contains',
  904. value: 'nobody@example.com',
  905. },
  906. },
  907. perform: {
  908. 'x-zammad-ticket-tags' => {
  909. operator: 'add',
  910. value: 'test3',
  911. },
  912. },
  913. channel: 'email',
  914. active: true,
  915. created_by_id: 1,
  916. updated_by_id: 1,
  917. )
  918. PostmasterFilter.create!(
  919. name: '04 set tag for email',
  920. match: {
  921. from: {
  922. operator: 'contains',
  923. value: 'nobody@example.com',
  924. },
  925. },
  926. perform: {
  927. 'x-zammad-ticket-tags' => {
  928. operator: 'add',
  929. value: 'abc1 , abc2 ',
  930. },
  931. },
  932. channel: 'email',
  933. active: true,
  934. created_by_id: 1,
  935. updated_by_id: 1,
  936. )
  937. data = 'From: ME Bob <nobody@example.com>
  938. To: customer@example.com
  939. Subject: some subject
  940. Some Text'
  941. parser = Channel::EmailParser.new
  942. ticket, article, user = parser.process({ group_id: group_default.id, trusted: false }, data)
  943. tags = Tag.tag_list(object: 'Ticket', o_id: ticket.id)
  944. assert_equal('Users', ticket.group.name)
  945. assert_equal('2 normal', ticket.priority.name)
  946. assert_equal('some subject', ticket.title)
  947. assert_equal('nobody@example.com', ticket.customer.email)
  948. assert_equal(4, tags.count)
  949. assert(tags.include?('test1'))
  950. assert(tags.include?('test3'))
  951. assert(tags.include?('abc1'))
  952. assert(tags.include?('abc2'))
  953. end
  954. end