Skip to content

Commit

Permalink
Added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
endSly committed Dec 19, 2023
1 parent 372bd1f commit 315d060
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
7 changes: 5 additions & 2 deletions app/mailers/conversation_reply_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def should_use_conversation_email_address?
@inbox.inbox_type == 'Email' || inbound_email_enabled?
end

def should_use_inbox_from?
ENV['MAILER_SENDER_INBOX_PREFERENCE'] == 'true'
end

def conversation_already_viewed?
# whether contact already saw the message on widget
return unless @conversation.contact_last_seen_at
Expand Down Expand Up @@ -101,8 +105,7 @@ def business_name
end

def from_email
sender_inbox_preference = ENV.fetch('MAILER_SENDER_INBOX_PREFERENCE', 'false') == 'true'
return parse_email(@account.support_email) if should_use_conversation_email_address? && !sender_inbox_preference
return parse_email(@account.support_email) if should_use_conversation_email_address? && !should_use_inbox_from?

parse_email(inbox_from_email_address)
end
Expand Down
7 changes: 5 additions & 2 deletions app/mailers/conversation_reply_mailer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ def email_reply_to

# Use channel email domain or account email domain depending on preference configuration for custom message_id and in_reply_to
def channel_email_domain
use_inbox_email = ENV['MAILER_INBOUND_INBOX_PREFERENCE'] == 'true'
inbox_email_domain = @inbox.channel.try(:email)&.split('@')&.last
return inbox_email_domain if inbox_email_domain.present? && use_inbox_email
return inbox_email_domain if inbox_email_domain.present? && should_use_channel_domain?
return @account.inbound_email_domain if @account.inbound_email_domain.present?
return inbox_email_domain if inbox_email_domain.present?

raise(StandardError, 'Account inbound email domain and channel email domain not present.')
end

def should_use_channel_domain?
ENV['MAILER_INBOUND_INBOX_PREFERENCE'] == 'true'
end
end
32 changes: 32 additions & 0 deletions spec/mailers/conversation_reply_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,5 +383,37 @@
expect(mail.in_reply_to).to eq("account/#{conversation.account.id}/conversation/#{conversation.uuid}@#{domain}")
end
end

context 'when prefer inbox emails configured' do
let(:new_account) { create(:account, domain: 'example.com') }
let!(:email_channel) { create(:channel_email, account: new_account, email: 'testing@channel.com') }
let!(:inbox) { create(:inbox, channel: email_channel, account: new_account, email_address: 'testing@inbox.com') }
let(:inbox_member) { create(:inbox_member, user: agent, inbox: inbox) }
let(:conversation) { create(:conversation, assignee: agent, inbox: inbox_member.inbox, account: new_account) }
let!(:message) { create(:message, conversation: conversation, account: new_account) }
let(:mail) { described_class.reply_with_summary(message.conversation, message.id).deliver_now }
let(:domain) { inbox.channel.email.split('@').last }

before do
allow(class_instance).to receive(:should_use_channel_domain?).and_return(true)
allow(class_instance).to receive(:should_use_inbox_from?).and_return(true)
end

it 'sets the correct custom message id' do
expect(mail.message_id).to eq("conversation/#{conversation.uuid}/messages/#{message.id}@#{domain}")
end

it 'sets the correct in reply to id' do
expect(mail.in_reply_to).to eq("account/#{conversation.account.id}/conversation/#{conversation.uuid}@#{domain}")
end

it 'sets reply to email to be based on the inbox domain' do
expect(mail.reply_to).to eq(["reply+#{message.conversation.uuid}@#{domain}"])
end

it 'sets from email to the inbox email address' do
expect(mail.from).to eq([inbox.email_address])
end
end
end
end

0 comments on commit 315d060

Please sign in to comment.