process_simple_filter_followup_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Channel::EmailParser process with simple filter and followup message', 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(:group_second) { create(:group, name: 'Second group') }
  11. let(:email) { Channel::EmailParser.new.process({ group_id: group_default.id, trusted: false }, data) }
  12. let(:ticket) { email[0] }
  13. let(:article) { email[1] }
  14. let(:filter) do
  15. {
  16. name: 'RSpec: Channel::EmailParser#process',
  17. match: {
  18. from: {
  19. operator: 'contains',
  20. value: 'example.com',
  21. },
  22. },
  23. perform: {
  24. 'X-Zammad-Ticket-group_id' => {
  25. value: group_second.id,
  26. },
  27. },
  28. channel: 'email',
  29. active: true,
  30. created_by_id: 1,
  31. updated_by_id: 1,
  32. }
  33. end
  34. let(:data) do
  35. <<~MAIL
  36. From: Some Body <somebody@example.com>
  37. To: Bob <bod@example.com>
  38. Cc: any@example.com
  39. Subject: follow-up with create post master filter test
  40. Some Text
  41. MAIL
  42. end
  43. let(:modified_ticket) do
  44. ticket.group = group_first
  45. ticket.save
  46. TransactionDispatcher.commit
  47. ticket
  48. end
  49. let(:followup_data) do
  50. <<~MAIL
  51. From: me@example.com
  52. To: customer@example.com
  53. Subject: #{modified_ticket.subject_build('some new subject')}
  54. Some Text
  55. MAIL
  56. end
  57. let(:followup_email) do
  58. Channel::EmailParser.new.process({ group_id: group_default.id, trusted: false }, followup_data)
  59. end
  60. let(:followup_ticket) { followup_email[0] }
  61. let(:followup_article) { followup_email[1] }
  62. it 'modifies followup ticket' do
  63. expect(followup_ticket.id).to eql(ticket.id)
  64. expect(followup_ticket.group.name).to match(group_first.name)
  65. expect(followup_ticket.priority.name).to match('2 normal')
  66. expect(followup_ticket.title).to match('follow-up with create post master filter test')
  67. end
  68. it 'modifies followup article' do
  69. expect(followup_ticket.articles.count).to eq(ticket.articles.count)
  70. expect(article.sender.name).to match('Customer')
  71. expect(article.type.name).to match('email')
  72. expect(article.internal).to be false
  73. end
  74. describe 'Log Trigger and Scheduler in Ticket History #4604' do
  75. it 'add history entries for the postmaster filter manipulation' do
  76. followup_ticket
  77. expect(History.find(followup_ticket.history_get.first['id'])).to have_attributes(
  78. o_id: ticket.id,
  79. sourceable: PostmasterFilter.first,
  80. value_from: '',
  81. value_to: group_second.name,
  82. id_from: nil,
  83. id_to: group_second.id,
  84. )
  85. end
  86. it 'does not have a sourceable for the ticket creation entry' do
  87. followup_ticket
  88. expect(History.find(followup_ticket.history_get.detect { |h| h['type'] == 'created' }['id']).sourceable).to be_nil
  89. end
  90. context 'when filter does add tags' do
  91. let(:tag) { SecureRandom.uuid }
  92. let(:filter) do
  93. {
  94. name: 'RSpec: Channel::EmailParser#process',
  95. match: {
  96. from: {
  97. operator: 'contains',
  98. value: 'example.com',
  99. },
  100. },
  101. perform: {
  102. 'x-zammad-ticket-tags' => { 'operator' => 'add', 'value' => tag }
  103. },
  104. channel: 'email',
  105. active: true,
  106. created_by_id: 1,
  107. updated_by_id: 1,
  108. }
  109. end
  110. it 'does work for tags' do
  111. followup_ticket
  112. history_id = followup_ticket.history_get.detect { |h| h['sourceable_type'] == 'PostmasterFilter' }['id']
  113. expect(History.find(history_id)).to have_attributes(
  114. history_type_id: History::Type.find_by(name: 'added').id,
  115. o_id: ticket.id,
  116. sourceable: PostmasterFilter.first,
  117. value_from: nil,
  118. value_to: tag,
  119. id_from: nil,
  120. id_to: nil,
  121. )
  122. end
  123. end
  124. context 'when filter contains followup headers' do
  125. let(:filter) do
  126. {
  127. name: 'RSpec: Channel::EmailParser#process',
  128. match: {
  129. from: {
  130. operator: 'contains',
  131. value: 'example.com',
  132. },
  133. },
  134. perform: {
  135. 'x-zammad-ticket-followup-group_id' => {
  136. value: group_second.id,
  137. },
  138. },
  139. channel: 'email',
  140. active: true,
  141. created_by_id: 1,
  142. updated_by_id: 1,
  143. }
  144. end
  145. it 'does work for the followup headers' do
  146. followup_ticket
  147. history_id = followup_ticket.history_get.detect { |h| h['sourceable_type'] == 'PostmasterFilter' }['id']
  148. expect(History.find(history_id)).to have_attributes(
  149. o_id: ticket.id,
  150. sourceable: PostmasterFilter.first,
  151. value_from: group_first.name,
  152. value_to: group_second.name,
  153. id_from: group_first.id,
  154. id_to: group_second.id,
  155. )
  156. end
  157. end
  158. end
  159. end