process_multiple_filter_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Channel::EmailParser process with multiple filter', aggregate_failures: true, type: :model do
  4. before do
  5. PostmasterFilter.destroy_all
  6. filters.push(extra_filter) if !extra_filter.empty?
  7. filters.each do |filter|
  8. PostmasterFilter.create!(filter)
  9. end
  10. end
  11. let(:extra_filter) { [] }
  12. let(:group_default) { Group.lookup(name: 'Users') }
  13. let(:group_first) { create(:group, name: 'First group') }
  14. let(:group_second) { create(:group, name: 'Second group') }
  15. let(:email) { Channel::EmailParser.new.process({ group_id: group_default.id, trusted: false }, data) }
  16. let(:ticket) { email[0] }
  17. let(:article) { email[1] }
  18. shared_examples 'filtered' do |params|
  19. it 'modifies ticket' do
  20. expect(ticket.group.name).to match(group.name)
  21. expect(ticket.priority.name).to match(params[:priority])
  22. expect(ticket.title).to match(params[:title])
  23. end
  24. it 'modifies article' do
  25. expect(article.sender.name).to match('Customer')
  26. expect(article.type.name).to match('email')
  27. expect(article.internal).to be params[:internal]
  28. end
  29. end
  30. context 'with multiple filters' do
  31. let(:filters) do
  32. [
  33. {
  34. name: 'RSpec: Channel::EmailParser#process',
  35. match: {
  36. from: {
  37. operator: 'contains',
  38. value: 'nobody@example.com',
  39. },
  40. },
  41. perform: {
  42. 'X-Zammad-Ticket-priority' => {
  43. value: '3 high',
  44. },
  45. },
  46. channel: 'email',
  47. active: true,
  48. created_by_id: 1,
  49. updated_by_id: 1,
  50. },
  51. {
  52. name: 'RSpec: Channel::EmailParser#process',
  53. match: {
  54. from: {
  55. operator: 'contains',
  56. value: 'me@example.com',
  57. },
  58. },
  59. perform: {
  60. 'X-Zammad-Ticket-group_id' => {
  61. value: group_first.id,
  62. },
  63. 'x-Zammad-Article-Internal' => {
  64. value: true,
  65. },
  66. },
  67. channel: 'email',
  68. active: true,
  69. created_by_id: 1,
  70. updated_by_id: 1,
  71. },
  72. {
  73. name: 'x-any-recipient match value any@example.com',
  74. match: {
  75. 'x-any-recipient' => {
  76. operator: 'contains',
  77. value: 'any@example.com',
  78. },
  79. },
  80. perform: {
  81. 'X-Zammad-Ticket-group_id' => {
  82. value: group_second.id,
  83. },
  84. 'x-Zammad-Article-Internal' => {
  85. value: true,
  86. },
  87. },
  88. channel: 'email',
  89. active: true,
  90. created_by_id: 1,
  91. updated_by_id: 1,
  92. }
  93. ]
  94. end
  95. context 'with from match value me@example.com' do
  96. let(:data) do
  97. <<~MAIL
  98. From: me@example.com
  99. To: customer@example.com
  100. Subject: some subject
  101. Some Text
  102. MAIL
  103. end
  104. let(:group) { group_first }
  105. include_examples('filtered', title: 'some subject',
  106. priority: '2 normal',
  107. internal: true)
  108. end
  109. context 'with x-any-recipient-match value any@example.com' do
  110. let(:data) do
  111. <<~MAIL
  112. From: Some Body <somebody@example.com>
  113. To: Bob <bob@example.com>
  114. To: any@example.com
  115. Subject: some subject
  116. Some Text
  117. MAIL
  118. end
  119. let(:group) { group_second }
  120. include_examples('filtered', title: 'some subject',
  121. priority: '2 normal',
  122. internal: true)
  123. end
  124. context 'with additional not x-any-recipient-match value any_not@example.com' do
  125. let(:extra_filter) do
  126. {
  127. name: 'x-any-recipient not match value any_not@example.com',
  128. match: {
  129. 'x-any-recipient' => {
  130. operator: 'contains not',
  131. value: 'any_not@example.com',
  132. },
  133. },
  134. perform: {
  135. 'X-Zammad-Ticket-group_id' => {
  136. value: group_second.id,
  137. },
  138. 'X-Zammad-Ticket-priority_id' => {
  139. value: '1',
  140. },
  141. 'x-Zammad-Article-Internal' => {
  142. value: 'false',
  143. },
  144. },
  145. channel: 'email',
  146. active: true,
  147. created_by_id: 1,
  148. updated_by_id: 1,
  149. }
  150. end
  151. let(:data) do
  152. <<~MAIL
  153. From: Some Body <somebody@example.com>
  154. To: Bob <bob@example.com>
  155. Cc: any@example.com
  156. Subject: some subject2
  157. Some Text
  158. MAIL
  159. end
  160. let(:group) { group_second }
  161. include_examples('filtered', title: 'some subject',
  162. priority: '1 low',
  163. internal: false)
  164. end
  165. end
  166. end