email_postmaster_test.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # encoding: utf-8
  2. # rubocop:disable all
  3. require 'test_helper'
  4. class EmailPostmasterTest < ActiveSupport::TestCase
  5. test 'process with postmaster filter' do
  6. group1 = Group.create_if_not_exists(
  7. name: 'Test Group1',
  8. created_by_id: 1,
  9. updated_by_id: 1,
  10. )
  11. group2 = Group.create_if_not_exists(
  12. name: 'Test Group2',
  13. created_by_id: 1,
  14. updated_by_id: 1,
  15. )
  16. PostmasterFilter.destroy_all
  17. PostmasterFilter.create(
  18. name: 'not used',
  19. match: {
  20. from: {
  21. operator: 'contains',
  22. value: 'nobody@example.com',
  23. },
  24. },
  25. perform: {
  26. 'X-Zammad-Ticket-priority' => {
  27. value: '3 high',
  28. },
  29. },
  30. channel: 'email',
  31. active: true,
  32. created_by_id: 1,
  33. updated_by_id: 1,
  34. )
  35. PostmasterFilter.create(
  36. name: 'used',
  37. match: {
  38. from: {
  39. operator: 'contains',
  40. value: 'me@example.com',
  41. },
  42. },
  43. perform: {
  44. 'X-Zammad-Ticket-group_id' => {
  45. value: group1.id,
  46. },
  47. 'x-Zammad-Article-Internal' => {
  48. value: true,
  49. },
  50. },
  51. channel: 'email',
  52. active: true,
  53. created_by_id: 1,
  54. updated_by_id: 1,
  55. )
  56. PostmasterFilter.create(
  57. name: 'used x-any-recipient',
  58. match: {
  59. 'x-any-recipient' => {
  60. operator: 'contains',
  61. value: 'any@example.com',
  62. },
  63. },
  64. perform: {
  65. 'X-Zammad-Ticket-group_id' => {
  66. value: group2.id,
  67. },
  68. 'x-Zammad-Article-Internal' => {
  69. value: true,
  70. },
  71. },
  72. channel: 'email',
  73. active: true,
  74. created_by_id: 1,
  75. updated_by_id: 1,
  76. )
  77. data = 'From: me@example.com
  78. To: customer@example.com
  79. Subject: some subject
  80. Some Text'
  81. parser = Channel::EmailParser.new
  82. ticket, article, user = parser.process( { trusted: false }, data )
  83. assert_equal('Test Group1', ticket.group.name)
  84. assert_equal('2 normal', ticket.priority.name)
  85. assert_equal('some subject', ticket.title)
  86. assert_equal('Customer', article.sender.name)
  87. assert_equal('email', article.type.name)
  88. assert_equal(true, article.internal)
  89. data = 'From: Some Body <somebody@example.com>
  90. To: Bob <bod@example.com>
  91. Cc: any@example.com
  92. Subject: some subject
  93. Some Text'
  94. parser = Channel::EmailParser.new
  95. ticket, article, user = parser.process( { trusted: false }, data )
  96. assert_equal('Test Group2', ticket.group.name)
  97. assert_equal('2 normal', ticket.priority.name)
  98. assert_equal('some subject', ticket.title)
  99. assert_equal('Customer', article.sender.name)
  100. assert_equal('email', article.type.name)
  101. assert_equal(true, article.internal)
  102. PostmasterFilter.create(
  103. name: 'used x-any-recipient',
  104. match: {
  105. 'x-any-recipient' => {
  106. operator: 'contains not',
  107. value: 'any_not@example.com',
  108. },
  109. },
  110. perform: {
  111. 'X-Zammad-Ticket-group_id' => {
  112. value: group2.id,
  113. },
  114. 'X-Zammad-Ticket-priority_id' => {
  115. value: '1',
  116. },
  117. 'x-Zammad-Article-Internal' => {
  118. value: 'false',
  119. },
  120. },
  121. channel: 'email',
  122. active: true,
  123. created_by_id: 1,
  124. updated_by_id: 1,
  125. )
  126. data = 'From: Some Body <somebody@example.com>
  127. To: Bob <bod@example.com>
  128. Cc: any@example.com
  129. Subject: some subject2
  130. Some Text'
  131. parser = Channel::EmailParser.new
  132. ticket, article, user = parser.process( { trusted: false }, data )
  133. assert_equal('Test Group2', ticket.group.name)
  134. assert_equal('1 low', ticket.priority.name)
  135. assert_equal('some subject2', ticket.title)
  136. assert_equal('Customer', article.sender.name)
  137. assert_equal('email', article.type.name)
  138. assert_equal(false, article.internal)
  139. PostmasterFilter.destroy_all
  140. PostmasterFilter.create(
  141. name: 'used - empty selector',
  142. match: {
  143. from: {
  144. operator: 'contains',
  145. value: '',
  146. },
  147. },
  148. perform: {
  149. 'X-Zammad-Ticket-group_id' => {
  150. value: group2.id,
  151. },
  152. 'X-Zammad-Ticket-priority_id' => {
  153. value: '1',
  154. },
  155. 'x-Zammad-Article-Internal' => {
  156. value: true,
  157. },
  158. },
  159. channel: 'email',
  160. active: true,
  161. created_by_id: 1,
  162. updated_by_id: 1,
  163. )
  164. data = 'From: Some Body <somebody@example.com>
  165. To: Bob <bod@example.com>
  166. Cc: any@example.com
  167. Subject: some subject - no selector
  168. Some Text'
  169. parser = Channel::EmailParser.new
  170. ticket, article, user = parser.process( { trusted: false }, data )
  171. assert_equal('Users', ticket.group.name)
  172. assert_equal('2 normal', ticket.priority.name)
  173. assert_equal('some subject - no selector', ticket.title)
  174. assert_equal('Customer', article.sender.name)
  175. assert_equal('email', article.type.name)
  176. assert_equal(false, article.internal)
  177. PostmasterFilter.destroy_all
  178. end
  179. end