diff --git a/app/models/channel/filter/reply_to_based_sender.rb b/app/models/channel/filter/reply_to_based_sender.rb index ecd206e24..b8904f34a 100644 --- a/app/models/channel/filter/reply_to_based_sender.rb +++ b/app/models/channel/filter/reply_to_based_sender.rb @@ -4,7 +4,9 @@ module Channel::Filter::ReplyToBasedSender def self.run(_channel, mail, _transaction_params) - reply_to = mail[:'reply-to'] + return if mail[:'reply-to'].blank? + + reply_to = mail[:'reply-to'].gsub('<>', '').strip return if reply_to.blank? setting = Setting.get('postmaster_sender_based_on_reply_to') diff --git a/spec/models/channel/filter/reply_to_based_sender_spec.rb b/spec/models/channel/filter/reply_to_based_sender_spec.rb new file mode 100644 index 000000000..0aae90550 --- /dev/null +++ b/spec/models/channel/filter/reply_to_based_sender_spec.rb @@ -0,0 +1,66 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe Channel::Filter::ReplyToBasedSender, type: :channel_filter do + describe '.run' do + let(:mail_hash) { Channel::EmailParser.new.parse(<<~RAW.chomp) } + From: daffy.duck@acme.corp + To: batman@marvell.com + Subject: Anvil + Reply-To: #{reply_to} + + I can haz anvil! + RAW + + before do + Setting.set('postmaster_sender_based_on_reply_to', 'as_sender_of_email') + end + + context 'when empty reply-to' do + let(:reply_to) { '' } + + it 'keeps from' do + expect { filter(mail_hash) } + .not_to change { mail_hash[:from] } + end + end + + context 'when empty reply-to realname and invalid address' do + let(:reply_to) { '<>' } + + it 'keeps from' do + expect { filter(mail_hash) } + .not_to change { mail_hash[:from] } + end + end + + context 'when valid reply-to address' do + let(:reply_to) { '' } + + it 'sets from to reply-to address' do + expect { filter(mail_hash) } + .to change { mail_hash[:from] }.to('bugs.bunny@acme.corp') + end + end + + context 'when valid reply-to realname and address' do + let(:reply_to) { 'Bugs Bunny ' } + + it 'sets from to reply-to realname and address' do + expect { filter(mail_hash) } + .to change { mail_hash[:from] }.to('Bugs Bunny ') + end + end + + context 'when valid reply-to realname and invalid address' do + let(:reply_to) { '"Bugs Bunny" <>' } + + it 'sets from to reply-to realname' do + expect { filter(mail_hash) } + .to change { mail_hash[:from] }.to('"Bugs Bunny"') + end + end + + end +end