From c346d8b66b0dd34b5ae1726f2c401ae4f39b5afd Mon Sep 17 00:00:00 2001 From: Dominik Klein Date: Mon, 26 Jul 2021 12:30:56 +0200 Subject: [PATCH] Fixes #3667 - No possibility to enforce auto response if one of the blocking auto response mail header exists. --- .../channel/filter/auto_response_check.rb | 23 ++-- .../filter/auto_response_check_spec.rb | 112 ++++++++++++++++++ 2 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 spec/models/channel/filter/auto_response_check_spec.rb diff --git a/app/models/channel/filter/auto_response_check.rb b/app/models/channel/filter/auto_response_check.rb index dcf73810f..94633bce1 100644 --- a/app/models/channel/filter/auto_response_check.rb +++ b/app/models/channel/filter/auto_response_check.rb @@ -4,15 +4,18 @@ module Channel::Filter::AutoResponseCheck def self.run(_channel, mail, _transaction_params) - # if header is available, do not generate auto response - mail[ :'x-zammad-send-auto-response' ] = false - mail[ :'x-zammad-is-auto-response' ] = true + header_is_auto_response_exists = mail.key?(:'x-zammad-is-auto-response') + mail[ :'x-zammad-is-auto-response' ] = header_is_auto_response_exists ? ActiveModel::Type::Boolean.new.cast(mail[ :'x-zammad-is-auto-response' ]) : true - if !mail[ :'x-zammad-article-preferences' ] - mail[ :'x-zammad-article-preferences' ] = {} - end - mail[ :'x-zammad-article-preferences' ]['send-auto-response'] = false - mail[ :'x-zammad-article-preferences' ]['is-auto-response'] = true + header_send_auto_response_exists = mail.key?(:'x-zammad-send-auto-response') + mail[ :'x-zammad-send-auto-response' ] = header_send_auto_response_exists ? ActiveModel::Type::Boolean.new.cast(mail[ :'x-zammad-send-auto-response' ]) : !mail[ :'x-zammad-is-auto-response' ] + + mail[ :'x-zammad-article-preferences' ] ||= {} + mail[ :'x-zammad-article-preferences' ]['send-auto-response'] = mail[ :'x-zammad-send-auto-response' ] + mail[ :'x-zammad-article-preferences' ]['is-auto-response'] = mail[ :'x-zammad-is-auto-response' ] + + # Skip the auto response checks, if the header already exists. + return if header_is_auto_response_exists # do not send an auto response if one of the following headers exists return if mail[ :'list-unsubscribe' ] && mail[ :'list-unsubscribe' ] =~ %r{...} @@ -28,10 +31,10 @@ module Channel::Filter::AutoResponseCheck return if message_id.match?(%r{@#{Regexp.quote(fqdn)}}i) end - mail[ :'x-zammad-send-auto-response' ] = true + mail[ :'x-zammad-send-auto-response' ] = true if !header_send_auto_response_exists mail[ :'x-zammad-is-auto-response' ] = false - mail[ :'x-zammad-article-preferences' ]['send-auto-response'] = true + mail[ :'x-zammad-article-preferences' ]['send-auto-response'] = mail[ :'x-zammad-send-auto-response' ] mail[ :'x-zammad-article-preferences' ]['is-auto-response'] = false end diff --git a/spec/models/channel/filter/auto_response_check_spec.rb b/spec/models/channel/filter/auto_response_check_spec.rb new file mode 100644 index 000000000..f5f991169 --- /dev/null +++ b/spec/models/channel/filter/auto_response_check_spec.rb @@ -0,0 +1,112 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe Channel::Filter::AutoResponseCheck, type: :channel_filter do + let(:mail) do + { + From: 'me@example.com' + } + end + + shared_examples 'check auto response header' do |send_auto_response_state, is_auto_response_state| + let(:mail_auto_response) do + { + 'x-zammad-send-auto-response': send_auto_response_state, + 'x-zammad-is-auto-response': is_auto_response_state, + 'x-zammad-article-preferences': { + 'send-auto-response' => send_auto_response_state, + 'is-auto-response' => is_auto_response_state + } + } + end + + it 'check filter result' do + filter(mail) + expect(mail).to match(a_hash_including(mail_auto_response)) + end + end + + context 'with no blocking response header' do + include_examples 'check auto response header', true, false + end + + context 'with no blocking response header and x-zammad-send-auto-response:false' do + let(:mail) do + { + 'x-zammad-send-auto-response': 'false' + } + end + + include_examples 'check auto response header', false, false + end + + context 'with blocking auto response header' do + context 'with header precedence:list' do + let(:mail) do + { + precedence: 'list' + } + end + + include_examples 'check auto response header', false, true + end + + context 'with header precedence:list and x-zammad-send-auto-response:true' do + let(:mail) do + { + precedence: 'list', + 'x-zammad-send-auto-response': 'true' + } + end + + include_examples 'check auto response header', true, true + end + + context 'with header precedence:list and x-zammad-send-auto-response:false' do + let(:mail) do + { + precedence: 'list', + 'x-zammad-send-auto-response': 'false' + } + end + + include_examples 'check auto response header', false, true + end + + context 'with header precedence:list and x-zammad-is-auto-response:false' do + let(:mail) do + { + precedence: 'list', + 'x-zammad-is-auto-response': 'false' + } + end + + include_examples 'check auto response header', true, false + end + + context 'with header precedence:list, x-zammad-is-auto-response:false and x-zammad-send-auto-response:false' do + let(:mail) do + { + precedence: 'list', + 'x-zammad-is-auto-response': 'false', + 'x-zammad-send-auto-response': 'false' + } + end + + include_examples 'check auto response header', false, false + end + + context 'with header precedence:list, x-zammad-is-auto-response:true and x-zammad-send-auto-response:true' do + let(:mail) do + { + precedence: 'list', + 'x-zammad-is-auto-response': 'true', + 'x-zammad-send-auto-response': 'true' + } + end + + include_examples 'check auto response header', true, true + end + end +end