123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- RSpec.describe 'Channel::EmailParser process with simple filter', aggregate_failures: true, type: :model do
- before do
- PostmasterFilter.destroy_all
- PostmasterFilter.create!(filter)
- end
- let(:group_default) { Group.lookup(name: 'Users') }
- let(:group_first) { create(:group, name: 'First group') }
- let(:email) { Channel::EmailParser.new.process({ group_id: group_default.id, trusted: false }, data) }
- let(:ticket) { email[0] }
- let(:article) { email[1] }
- let(:filter) do
- {
- name: 'RSpec: Channel::EmailParser#process',
- match: matcher,
- perform: {
- 'X-Zammad-Ticket-group_id' => {
- value: group.id,
- },
- 'X-Zammad-Ticket-priority_id' => {
- value: '1',
- },
- 'x-Zammad-Article-Internal' => {
- value: true,
- },
- },
- channel: 'email',
- active: true,
- created_by_id: 1,
- updated_by_id: 1,
- }
- end
- shared_examples 'filtered' do |params|
- it 'modifies ticket' do
- expect(ticket.group.name).to match(group.name)
- expect(ticket.priority.name).to match(params[:priority])
- expect(ticket.title).to match(params[:title])
- end
- it 'modifies article' do
- expect(article.sender.name).to match('Customer')
- expect(article.type.name).to match('email')
- expect(article.internal).to be true
- end
- end
- shared_examples 'not_filtered' do |params|
- it 'not modifies ticket' do
- expect(ticket.group.name).to match(group_default.name)
- expect(ticket.priority.name).to match('2 normal')
- expect(ticket.title).to match(params[:title])
- end
- it 'not modifies article' do
- expect(article.sender.name).to match('Customer')
- expect(article.type.name).to match('email')
- expect(article.internal).to be false
- end
- end
- context 'when from matches regex value: .*' do
- let(:data) do
- <<~MAIL
- From: Some Body <somebody@example.com>
- To: Bob <bod@example.com>
- Cc: any@example.com
- Subject: some subject - no selector
- Some Text
- MAIL
- end
- let(:group) { group_first }
- let(:matcher) do
- {
- from: {
- operator: 'matches regex',
- value: '.*',
- }
- }
- end
- include_examples('filtered', title: 'some subject - no selector',
- priority: '1 low')
- end
- context 'when from match value: *' do
- let(:data) do
- <<~MAIL
- From: Some Body <somebody@example.com>
- To: Bob <bod@example.com>
- Cc: any@example.com
- Subject: some subject - no selector
- Some Text
- MAIL
- end
- let(:group) { group_first }
- let(:matcher) do
- {
- from: {
- operator: 'contains',
- value: '*',
- }
- }
- end
- include_examples('filtered',
- title: 'some subject - no selector',
- priority: '1 low')
- end
- context 'when subject match value: *me*' do
- let(:data) do
- <<~MAIL
- From: Some Body <somebody@example.com>
- To: Bob <bod@example.com>
- Cc: any@example.com
- Subject: *me*
- Some Text
- MAIL
- end
- let(:group) { group_first }
- let(:matcher) do
- {
- subject: {
- operator: 'contains',
- value: '*me*',
- }
- }
- end
- include_examples('filtered', title: '*me*',
- priority: '1 low')
- end
- context 'when not subject match value: *me* and email subject: *mo*' do
- let(:data) do
- <<~MAIL
- From: Some Body <somebody@example.com>
- To: Bob <bod@example.com>
- Cc: any@example.com
- Subject: *mo*
- Some Text
- MAIL
- end
- let(:group) { group_first }
- let(:matcher) do
- {
- subject: {
- operator: 'contains not',
- value: '*me*',
- }
- }
- end
- include_examples('filtered', title: '*mo*',
- priority: '1 low')
- end
- context 'when not subject match value: *me* and email subject: *me*' do
- let(:data) do
- <<~MAIL
- From: Some Body <somebody@example.com>
- To: Bob <bod@example.com>
- Cc: any@example.com
- Subject: *me*
- Some Text
- MAIL
- end
- let(:group) { group_first }
- let(:matcher) do
- {
- subject: {
- operator: 'contains not',
- value: '*me*',
- }
- }
- end
- include_examples('not_filtered', title: '*me*')
- end
- context 'when message-id match value: @sombody.domain' do
- let(:filter) do
- {
- name: 'RSpec: Channel::EmailParser#process',
- match: {
- 'message-id': {
- operator: 'contains',
- value: '@sombody.domain>',
- },
- },
- perform: {
- 'X-Zammad-Ticket-priority_id' => {
- value: '1',
- },
- 'x-Zammad-Article-Internal' => {
- value: true,
- },
- },
- channel: 'email',
- active: true,
- created_by_id: 1,
- updated_by_id: 1,
- }
- end
- let(:data) do
- <<~MAIL
- From: Some Body <somebody@example.com>
- To: Bob <bod@example.com>
- Cc: any@example.com
- Subject: *me*
- Message-Id: <1520781034.17887@sombody.domain>
- Some Text
- MAIL
- end
- let(:group) { group_default }
- include_examples('filtered', title: '*me*',
- priority: '1 low')
- end
- context 'when perform customer_id empty and from match value: me@example.com' do
- let(:filter) do
- {
- name: 'RSpec: Channel::EmailParser#process',
- match: {
- from: {
- operator: 'contains',
- value: 'me@example.com',
- },
- },
- perform: {
- 'X-Zammad-Ticket-group_id' => {
- value: group.id,
- },
- 'x-Zammad-Article-Internal' => {
- value: true,
- },
- 'x-Zammad-Ticket-customer_id' => {
- value: '',
- value_completion: '',
- },
- },
- channel: 'email',
- active: true,
- created_by_id: 1,
- updated_by_id: 1,
- }
- end
- let(:data) do
- <<~MAIL
- From: ME Bob <me@example.com>
- To: customer@example.com
- Subject: some subject
- Some Text
- MAIL
- end
- let(:group) { group_first }
- include_examples('filtered', title: 'some subject',
- priority: '2 normal')
- it 'modifies customer email' do
- expect(ticket.customer.email).to match('me@example.com')
- end
- end
- context 'when perform customer_id has high value and from match value: me@example.com' do
- let(:filter) do
- {
- name: 'RSpec: Channel::EmailParser#process',
- match: {
- from: {
- operator: 'contains',
- value: 'me@example.com',
- },
- },
- perform: {
- 'X-Zammad-Ticket-group_id' => {
- value: group.id,
- },
- 'x-Zammad-Article-Internal' => {
- value: true,
- },
- 'x-Zammad-Ticket-customer_id' => {
- value: 999_999,
- value_completion: 'xxx',
- },
- },
- channel: 'email',
- active: true,
- created_by_id: 1,
- updated_by_id: 1,
- }
- end
- let(:data) do
- <<~MAIL
- From: ME Bob <me@example.com>
- To: customer@example.com
- Subject: some subject
- Some Text
- MAIL
- end
- let(:group) { group_first }
- include_examples('filtered', title: 'some subject',
- priority: '2 normal')
- it 'modifies customer email' do
- expect(ticket.customer.email).to match('me@example.com')
- end
- end
- context 'when perform customer_id and priority_id has high value and from match value: me@example.com' do
- let(:filter) do
- {
- name: 'RSpec: Channel::EmailParser#process',
- match: {
- from: {
- operator: 'contains',
- value: 'me@example.com',
- },
- },
- perform: {
- 'X-Zammad-Ticket-group_id' => {
- value: group.id,
- },
- 'X-Zammad-Ticket-priority_id' => {
- value: 888_888,
- },
- 'x-Zammad-Article-Internal' => {
- value: true,
- },
- 'x-Zammad-Ticket-customer_id' => {
- value: 999_999,
- value_completion: 'xxx',
- },
- },
- channel: 'email',
- active: true,
- created_by_id: 1,
- updated_by_id: 1,
- }
- end
- let(:data) do
- <<~MAIL
- From: ME Bob <me@example.com>
- To: customer@example.com
- Subject: some subject
- Some Text
- MAIL
- end
- let(:group) { group_first }
- include_examples('filtered', title: 'some subject',
- priority: '2 normal')
- it 'modifies customer email' do
- expect(ticket.customer.email).to match('me@example.com')
- end
- end
- end
|