2020-09-21 17:14:22 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe SupportMailbox, type: :mailbox do
|
|
|
|
include ActionMailbox::TestHelper
|
|
|
|
|
2021-11-15 13:45:51 +00:00
|
|
|
describe 'when a chatwoot notification email is received' do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let!(:channel_email) { create(:channel_email, email: 'sojan@chatwoot.com', account: account) }
|
|
|
|
let(:notification_mail) { create_inbound_email_from_fixture('notification.eml') }
|
|
|
|
let(:described_subject) { described_class.receive notification_mail }
|
|
|
|
let(:conversation) { Conversation.where(inbox_id: channel_email.inbox).last }
|
|
|
|
|
|
|
|
it 'shouldnt create a conversation in the channel' do
|
|
|
|
described_subject
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.present?).to be(false)
|
2021-11-15 13:45:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-21 17:14:22 +00:00
|
|
|
describe 'add mail as a new ticket in the email inbox' do
|
|
|
|
let(:account) { create(:account) }
|
2021-10-12 15:16:00 +00:00
|
|
|
let(:agent) { create(:user, email: 'agent1@example.com', account: account) }
|
2020-09-21 17:14:22 +00:00
|
|
|
let!(:channel_email) { create(:channel_email, account: account) }
|
|
|
|
let(:support_mail) { create_inbound_email_from_fixture('support.eml') }
|
2022-07-02 12:10:28 +00:00
|
|
|
let(:support_in_reply_to_mail) { create_inbound_email_from_fixture('support_in_reply_to.eml') }
|
2020-09-21 17:14:22 +00:00
|
|
|
let(:described_subject) { described_class.receive support_mail }
|
2021-08-09 12:34:34 +00:00
|
|
|
let(:serialized_attributes) do
|
|
|
|
%w[bcc cc content_type date from html_content in_reply_to message_id multipart number_of_attachments subject
|
|
|
|
text_content to]
|
|
|
|
end
|
2020-09-21 17:14:22 +00:00
|
|
|
let(:conversation) { Conversation.where(inbox_id: channel_email.inbox).last }
|
|
|
|
|
|
|
|
before do
|
|
|
|
# this email is hardcoded in the support.eml, that's why we are updating this
|
|
|
|
channel_email.email = 'care@example.com'
|
2022-09-13 12:10:06 +00:00
|
|
|
channel_email.save!
|
2020-09-21 17:14:22 +00:00
|
|
|
end
|
|
|
|
|
2022-07-02 12:10:28 +00:00
|
|
|
describe 'covers email address format' do
|
|
|
|
before do
|
|
|
|
described_class.receive support_in_reply_to_mail
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates contact with proper email address' do
|
|
|
|
expect(support_in_reply_to_mail.mail['reply_to'].try(:value)).to eq('Sony Mathew <sony@chatwoot.com>')
|
|
|
|
expect(conversation.contact.email).to eq('sony@chatwoot.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-21 17:14:22 +00:00
|
|
|
describe 'covers basic ticket creation' do
|
|
|
|
before do
|
|
|
|
described_subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create the conversation in the inbox of the email channel' do
|
|
|
|
expect(conversation.inbox.id).to eq(channel_email.inbox.id)
|
|
|
|
expect(conversation.additional_attributes['source']).to eq('email')
|
|
|
|
expect(conversation.contact.email).to eq(support_mail.mail.from.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create a new contact as the sender of the email' do
|
2021-09-16 07:56:52 +00:00
|
|
|
email_sender = Mail::Address.new(support_mail.mail[:from].value).name
|
2020-09-21 17:14:22 +00:00
|
|
|
expect(conversation.messages.last.sender.email).to eq(support_mail.mail.from.first)
|
2021-09-16 07:56:52 +00:00
|
|
|
expect(conversation.contact.name).to eq(email_sender)
|
2020-09-21 17:14:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'add the mail content as new message on the conversation' do
|
|
|
|
expect(conversation.messages.last.content).to eq("Let's talk about these images:")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'add the attachments' do
|
|
|
|
expect(conversation.messages.last.attachments.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'have proper content_attributes with details of email' do
|
|
|
|
expect(conversation.messages.last.content_attributes[:email].keys).to eq(serialized_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'set proper content_type' do
|
|
|
|
expect(conversation.messages.last.content_type).to eq('incoming_email')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-16 07:56:52 +00:00
|
|
|
describe 'Sender without name' do
|
|
|
|
let(:support_mail_without_sender_name) { create_inbound_email_from_fixture('support_without_sender_name.eml') }
|
|
|
|
let(:described_subject) { described_class.receive support_mail_without_sender_name }
|
|
|
|
|
|
|
|
it 'create a new contact with the email' do
|
|
|
|
described_subject
|
|
|
|
email_sender = support_mail_without_sender_name.mail.from.first.split('@').first
|
|
|
|
expect(conversation.messages.last.sender.email).to eq(support_mail.mail.from.first)
|
|
|
|
expect(conversation.contact.name).to eq(email_sender)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-17 16:44:39 +00:00
|
|
|
describe 'Sender with upcase mail address' do
|
|
|
|
let(:support_mail_without_sender_name) { create_inbound_email_from_fixture('support_without_sender_name.eml') }
|
|
|
|
let(:described_subject) { described_class.receive support_mail_without_sender_name }
|
|
|
|
|
|
|
|
it 'create a new inbox with the email case insensitive' do
|
|
|
|
described_subject
|
|
|
|
expect(conversation.inbox.id).to eq(channel_email.inbox.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-21 17:14:22 +00:00
|
|
|
describe 'handle inbox contacts' do
|
|
|
|
let(:contact) { create(:contact, account: account, email: support_mail.mail.from.first) }
|
|
|
|
let(:contact_inbox) { create(:contact_inbox, inbox: channel_email.inbox, contact: contact) }
|
|
|
|
|
|
|
|
it 'does not create new contact if that contact exists in the inbox' do
|
|
|
|
# making sure we have a contact already present
|
|
|
|
expect(contact_inbox.contact.email).to eq(support_mail.mail.from.first)
|
|
|
|
described_subject
|
|
|
|
expect(conversation.messages.last.sender.id).to eq(contact.id)
|
|
|
|
end
|
|
|
|
end
|
2021-08-24 08:48:08 +00:00
|
|
|
|
|
|
|
describe 'group email sender' do
|
|
|
|
let(:group_sender_support_mail) { create_inbound_email_from_fixture('group_sender_support.eml') }
|
|
|
|
let(:described_subject) { described_class.receive group_sender_support_mail }
|
|
|
|
|
|
|
|
before do
|
|
|
|
# this email is hardcoded eml fixture file that's why we are updating this
|
|
|
|
channel_email.email = 'support@chatwoot.com'
|
2022-09-13 12:10:06 +00:00
|
|
|
channel_email.save!
|
2021-08-24 08:48:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'create new contact with original sender' do
|
|
|
|
described_subject
|
2021-09-16 07:56:52 +00:00
|
|
|
email_sender = Mail::Address.new(group_sender_support_mail.mail[:from].value).name
|
|
|
|
|
2021-08-24 08:48:08 +00:00
|
|
|
expect(conversation.contact.email).to eq(group_sender_support_mail.mail['X-Original-Sender'].value)
|
2021-09-16 07:56:52 +00:00
|
|
|
expect(conversation.contact.name).to eq(email_sender)
|
2021-08-24 08:48:08 +00:00
|
|
|
end
|
|
|
|
end
|
2021-10-12 15:16:00 +00:00
|
|
|
|
|
|
|
describe 'when mail has in reply to email' do
|
|
|
|
let(:reply_mail_without_uuid) { create_inbound_email_from_fixture('reply_mail_without_uuid.eml') }
|
|
|
|
let(:described_subject) { described_class.receive reply_mail_without_uuid }
|
|
|
|
let(:email_channel) { create(:channel_email, email: 'test@example.com', account: account) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
email_channel
|
|
|
|
reply_mail_without_uuid.mail['In-Reply-To'] = 'conversation/6bdc3f4d-0bec-4515-a284-5d916fdde489/messages/123'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create channel with reply to mail' do
|
|
|
|
described_subject
|
|
|
|
conversation_1 = Conversation.last
|
|
|
|
|
|
|
|
expect(conversation_1.messages.last.content).to eq("Let's talk about these images:")
|
|
|
|
expect(conversation_1.additional_attributes['in_reply_to']).to eq('conversation/6bdc3f4d-0bec-4515-a284-5d916fdde489/messages/123')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'append message to email conversation with same in reply to' do
|
|
|
|
described_subject
|
|
|
|
conversation_1 = Conversation.last
|
|
|
|
|
|
|
|
expect(conversation_1.messages.last.content).to eq("Let's talk about these images:")
|
|
|
|
expect(conversation_1.additional_attributes['in_reply_to']).to eq('conversation/6bdc3f4d-0bec-4515-a284-5d916fdde489/messages/123')
|
|
|
|
expect(conversation_1.messages.count).to eq(1)
|
|
|
|
|
|
|
|
reply_mail_without_uuid.mail['In-Reply-To'] = 'conversation/6bdc3f4d-0bec-4515-a284-5d916fdde489/messages/123'
|
2022-04-08 05:50:19 +00:00
|
|
|
reply_mail_without_uuid.mail['Message-Id'] = '0CB459E0-0336-41DA-BC88-E6E28C697SFC@chatwoot.com'
|
2021-10-12 15:16:00 +00:00
|
|
|
|
|
|
|
described_class.receive reply_mail_without_uuid
|
|
|
|
|
|
|
|
expect(conversation_1.messages.last.content).to eq("Let's talk about these images:")
|
|
|
|
expect(conversation_1.additional_attributes['in_reply_to']).to eq('conversation/6bdc3f4d-0bec-4515-a284-5d916fdde489/messages/123')
|
|
|
|
expect(conversation_1.messages.count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
2022-02-14 14:52:58 +00:00
|
|
|
|
2022-02-15 04:58:04 +00:00
|
|
|
describe 'Sender with reply_to email address' do
|
|
|
|
let(:reply_to_mail) { create_inbound_email_from_fixture('reply_to.eml') }
|
|
|
|
let(:email_channel) { create(:channel_email, email: 'test@example.com', account: account) }
|
|
|
|
|
|
|
|
it 'prefer reply-to over from address' do
|
|
|
|
email_channel
|
|
|
|
described_class.receive reply_to_mail
|
|
|
|
|
|
|
|
conversation_1 = Conversation.last
|
|
|
|
email = conversation_1.messages.last.content_attributes['email']
|
|
|
|
|
|
|
|
expect(reply_to_mail.mail['From'].value).to be_present
|
|
|
|
expect(conversation_1.messages.last.content).to eq("Let's talk about these images:")
|
|
|
|
expect(reply_to_mail.mail['Reply-To'].value).to include(email['from'][0])
|
|
|
|
expect(reply_to_mail.mail['Reply-To'].value).to include(conversation_1.contact.email)
|
|
|
|
expect(reply_to_mail.mail['From'].value).not_to include(conversation_1.contact.email)
|
|
|
|
end
|
|
|
|
end
|
2022-03-30 12:34:30 +00:00
|
|
|
|
|
|
|
describe 'when mail part is not present' do
|
|
|
|
let(:support_mail) { create_inbound_email_from_fixture('support_1.eml') }
|
|
|
|
let(:only_text) { create_inbound_email_from_fixture('only_text.eml') }
|
|
|
|
let(:only_html) { create_inbound_email_from_fixture('only_html.eml') }
|
|
|
|
let(:only_attachments) { create_inbound_email_from_fixture('only_attachments.eml') }
|
|
|
|
let(:html_and_attachments) { create_inbound_email_from_fixture('html_and_attachments.eml') }
|
|
|
|
let(:described_subject) { described_class.receive support_mail }
|
|
|
|
|
|
|
|
it 'Considers raw html mail body' do
|
|
|
|
described_subject
|
|
|
|
expect(conversation.inbox.id).to eq(channel_email.inbox.id)
|
|
|
|
|
|
|
|
expect(conversation.messages.last.content_attributes['email']['html_content']['reply']).to include(
|
2022-07-15 02:51:59 +00:00
|
|
|
<<~BODY.chomp
|
2022-03-30 12:34:30 +00:00
|
|
|
Hi,
|
|
|
|
We are providing you platform from here you can sell paid posts on your website.
|
|
|
|
|
|
|
|
Chatwoot | CS team | [C](https://d33wubrfki0l68.cloudfront.net/973467c532160fd8b940300a43fa85fa2d060307/dc9a0/static/brand-73f58cdefae282ae74cebfa74c1d7003.svg)
|
|
|
|
|
|
|
|
Skype: live:.cid.something
|
|
|
|
|
|
|
|
[]
|
|
|
|
BODY
|
|
|
|
)
|
|
|
|
expect(conversation.messages.last.content_attributes['email']['subject']).to eq('Get Paid to post an article')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Considers only text body' do
|
|
|
|
described_class.receive only_text
|
|
|
|
|
|
|
|
expect(conversation.inbox.id).to eq(channel_email.inbox.id)
|
|
|
|
|
|
|
|
expect(conversation.messages.last.content).to eq('text only mail')
|
|
|
|
expect(conversation.messages.last.content_attributes['email']['subject']).to eq('test text only mail')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Considers only html body' do
|
|
|
|
described_class.receive only_html
|
|
|
|
|
|
|
|
expect(conversation.inbox.id).to eq(channel_email.inbox.id)
|
|
|
|
|
|
|
|
expect(conversation.messages.last.content).to eq(
|
2022-07-15 02:51:59 +00:00
|
|
|
<<~BODY.chomp
|
|
|
|
This is html only mail
|
2022-03-30 12:34:30 +00:00
|
|
|
BODY
|
|
|
|
)
|
|
|
|
expect(conversation.messages.last.content_attributes['email']['subject']).to eq('test html only mail')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Considers only attachments' do
|
|
|
|
described_class.receive only_attachments
|
|
|
|
|
|
|
|
expect(conversation.inbox.id).to eq(channel_email.inbox.id)
|
|
|
|
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.messages.last.content).to be_nil
|
2022-03-30 12:34:30 +00:00
|
|
|
expect(conversation.messages.last.attachments.count).to eq(1)
|
|
|
|
expect(conversation.messages.last.content_attributes['email']['subject']).to eq('only attachments')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Considers html and attachments' do
|
|
|
|
described_class.receive html_and_attachments
|
|
|
|
|
|
|
|
expect(conversation.inbox.id).to eq(channel_email.inbox.id)
|
|
|
|
|
|
|
|
expect(conversation.messages.last.content).to eq('This is html and attachments only mail')
|
|
|
|
expect(conversation.messages.last.attachments.count).to eq(1)
|
|
|
|
expect(conversation.messages.last.content_attributes['email']['subject']).to eq('attachment with html')
|
|
|
|
end
|
|
|
|
end
|
2020-09-21 17:14:22 +00:00
|
|
|
end
|
|
|
|
end
|