Restructure omni services and add Chatwoot research snapshot
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Messages::Facebook::MessageBuilder do
|
||||
subject(:message_builder) { described_class.new(incoming_fb_text_message, facebook_channel.inbox).perform }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
let!(:facebook_channel) { create(:channel_facebook_page) }
|
||||
let!(:message_object) { build(:incoming_fb_text_message).to_json }
|
||||
let!(:incoming_fb_text_message) { Integrations::Facebook::MessageParser.new(message_object) }
|
||||
let(:fb_object) { double }
|
||||
|
||||
describe '#perform' do
|
||||
it 'creates contact and message for the facebook inbox' do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
{
|
||||
first_name: 'Jane',
|
||||
last_name: 'Dae',
|
||||
account_id: facebook_channel.inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
message_builder
|
||||
|
||||
contact = facebook_channel.inbox.contacts.first
|
||||
message = facebook_channel.inbox.messages.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
expect(message.content).to eq('facebook message')
|
||||
end
|
||||
|
||||
it 'increments channel authorization_error_count when error is thrown' do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::AuthenticationError.new(500, 'Error validating access token'))
|
||||
message_builder
|
||||
|
||||
expect(facebook_channel.authorization_error_count).to eq(2)
|
||||
end
|
||||
|
||||
it 'raises exception for non profile account' do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::ClientError.new(400, '',
|
||||
{
|
||||
'type' => 'OAuthException',
|
||||
'message' => '(#100) No profile available for this user.',
|
||||
'error_subcode' => 2_018_218,
|
||||
'code' => 100
|
||||
}))
|
||||
message_builder
|
||||
|
||||
contact = facebook_channel.inbox.contacts.first
|
||||
# Refer: https://github.com/chatwoot/chatwoot/pull/3016 for this check
|
||||
default_name = 'John Doe'
|
||||
|
||||
expect(facebook_channel.inbox.reload.contacts.count).to eq(1)
|
||||
expect(contact.name).to eq(default_name)
|
||||
end
|
||||
|
||||
context 'when lock to single conversation' do
|
||||
subject(:mocked_message_builder) do
|
||||
described_class.new(mocked_incoming_fb_text_message, facebook_channel.inbox).perform
|
||||
end
|
||||
|
||||
let!(:mocked_message_object) { build(:mocked_message_text, sender_id: contact_inbox.source_id).to_json }
|
||||
let!(:mocked_incoming_fb_text_message) { Integrations::Facebook::MessageParser.new(mocked_message_object) }
|
||||
let(:contact) { create(:contact, name: 'Jane Dae') }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: facebook_channel.inbox.id) }
|
||||
|
||||
context 'when lock to single conversation is disabled' do
|
||||
before do
|
||||
facebook_channel.inbox.update!(lock_to_single_conversation: false)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
|
||||
mocked_message_builder
|
||||
|
||||
facebook_channel.inbox.reload
|
||||
|
||||
expect(facebook_channel.inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
|
||||
it 'will not create a new conversation if last conversation is not resolved' do
|
||||
existing_conversation = create(:conversation, account_id: facebook_channel.inbox.account.id, inbox_id: facebook_channel.inbox.id,
|
||||
contact_id: contact.id, contact_inbox_id: contact_inbox.id,
|
||||
status: :open)
|
||||
|
||||
mocked_message_builder
|
||||
|
||||
facebook_channel.inbox.reload
|
||||
|
||||
expect(facebook_channel.inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if last conversation is resolved' do
|
||||
existing_conversation = create(:conversation, account_id: facebook_channel.inbox.account.id, inbox_id: facebook_channel.inbox.id,
|
||||
contact_id: contact.id, contact_inbox_id: contact_inbox.id, status: :resolved)
|
||||
|
||||
inital_count = Conversation.count
|
||||
|
||||
mocked_message_builder
|
||||
|
||||
facebook_channel.inbox.reload
|
||||
|
||||
expect(facebook_channel.inbox.conversations.last.id).not_to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is enabled' do
|
||||
before do
|
||||
facebook_channel.inbox.update!(lock_to_single_conversation: true)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
mocked_message_builder
|
||||
|
||||
facebook_channel.inbox.reload
|
||||
|
||||
expect(facebook_channel.inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
|
||||
it 'reopens last conversation if last conversation exists' do
|
||||
existing_conversation = create(:conversation, account_id: facebook_channel.inbox.account.id, inbox_id: facebook_channel.inbox.id,
|
||||
contact_id: contact.id, contact_inbox_id: contact_inbox.id)
|
||||
|
||||
inital_count = Conversation.count
|
||||
|
||||
mocked_message_builder
|
||||
|
||||
facebook_channel.inbox.reload
|
||||
|
||||
expect(facebook_channel.inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,345 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Messages::Instagram::MessageBuilder do
|
||||
subject(:instagram_direct_message_builder) { described_class }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph\.instagram\.com/)
|
||||
stub_request(:get, 'https://www.example.com/test.jpeg')
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:instagram_channel) { create(:channel_instagram, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) }
|
||||
let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access }
|
||||
let!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access }
|
||||
let!(:shared_reel_params) { build(:instagram_shared_reel_event).with_indifferent_access }
|
||||
let!(:instagram_story_reply_event) { build(:instagram_story_reply_event).with_indifferent_access }
|
||||
let!(:instagram_message_reply_event) { build(:instagram_message_reply_event).with_indifferent_access }
|
||||
|
||||
describe '#perform' do
|
||||
before do
|
||||
instagram_channel.update(access_token: 'valid_instagram_token')
|
||||
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?/Sender-id-.*?\?.*})
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: proc { |request|
|
||||
sender_id = request.uri.path.split('/').last.split('?').first
|
||||
{
|
||||
name: 'Jane',
|
||||
username: 'some_user_name',
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png',
|
||||
id: sender_id,
|
||||
follower_count: 100,
|
||||
is_user_follow_business: true,
|
||||
is_business_follow_user: true,
|
||||
is_verified_user: false
|
||||
}.to_json
|
||||
},
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates contact and message for the instagram direct inbox' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
expect(message.content).to eq('This is the first message from the customer')
|
||||
end
|
||||
|
||||
it 'discard echo message already sent by chatwoot' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact = create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id)
|
||||
create(:message, account_id: account.id, inbox_id: instagram_inbox.id, conversation_id: conversation.id, message_type: 'outgoing',
|
||||
source_id: 'message-id-1')
|
||||
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
|
||||
messaging[:message][:mid] = 'message-id-1' # Set same source_id as the existing message
|
||||
described_class.new(messaging, instagram_inbox, outgoing_echo: true).perform
|
||||
|
||||
instagram_inbox.reload
|
||||
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
end
|
||||
|
||||
it 'discards duplicate messages from webhook events with the same message_id' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
initial_message_count = instagram_inbox.messages.count
|
||||
expect(initial_message_count).to be 1
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.messages.count).to eq initial_message_count
|
||||
end
|
||||
|
||||
it 'creates message for shared reel' do
|
||||
messaging = shared_reel_params[:entry][0]['messaging'][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
expect(message.attachments.first.file_type).to eq('ig_reel')
|
||||
expect(message.attachments.first.external_url).to eq(
|
||||
shared_reel_params[:entry][0]['messaging'][0]['message']['attachments'][0]['payload']['url']
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates message with story id' do
|
||||
messaging = instagram_story_reply_event[:entry][0]['messaging'][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
story_url = messaging['message']['reply_to']['story']['url']
|
||||
|
||||
stub_request(:get, story_url)
|
||||
.to_return(status: 200, body: 'image_data', headers: { 'Content-Type' => 'image/png' })
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
expect(message.content).to eq('This is the story reply')
|
||||
expect(message.content_attributes[:story_sender]).to eq(instagram_inbox.channel.instagram_id)
|
||||
expect(message.content_attributes[:story_id]).to eq('chatwoot-app-user-id-1')
|
||||
expect(message.content_attributes[:image_type]).to eq('ig_story_reply')
|
||||
expect(message.attachments.first.file_type).to eq('ig_story')
|
||||
expect(message.attachments.first.external_url).to eq(story_url)
|
||||
end
|
||||
|
||||
it 'creates message with reply to mid' do
|
||||
# Create first message to ensure reply to is valid
|
||||
first_messaging = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = first_messaging['sender']['id']
|
||||
create_instagram_contact_for_sender(sender_id, instagram_inbox)
|
||||
described_class.new(first_messaging, instagram_inbox).perform
|
||||
|
||||
# Create second message with reply to mid, using same sender_id
|
||||
messaging = instagram_message_reply_event[:entry][0]['messaging'][0]
|
||||
messaging['sender']['id'] = sender_id
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
first_message = instagram_inbox.messages.first
|
||||
reply_message = instagram_inbox.messages.last
|
||||
|
||||
expect(reply_message.content).to eq('This is message with replyto mid')
|
||||
expect(reply_message.content_attributes[:in_reply_to_external_id]).to eq(first_message.source_id)
|
||||
end
|
||||
|
||||
it 'handles deleted story' do
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
story_source_id = messaging['message']['mid']
|
||||
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?/#{story_source_id}\?.*})
|
||||
.to_return(status: 404, body: { error: { message: 'Story not found', code: 1_609_005 } }.to_json)
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
expect(message.content).to eq('This story is no longer available.')
|
||||
expect(message.attachments.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'does not create message for unsupported file type' do
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
contact = create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id)
|
||||
|
||||
# try to create a message with unsupported file type
|
||||
messaging['message']['attachments'][0]['type'] = 'unsupported_type'
|
||||
|
||||
described_class.new(messaging, instagram_inbox, outgoing_echo: false).perform
|
||||
|
||||
# Conversation should exist but no new message should be created
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 0
|
||||
end
|
||||
|
||||
it 'does not create message if the message is already exists' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact = create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id, contact_id: contact.id)
|
||||
create(:message, account_id: account.id, inbox_id: instagram_inbox.id, conversation_id: conversation.id, message_type: 'outgoing',
|
||||
source_id: 'message-id-1')
|
||||
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
messaging[:message][:mid] = 'message-id-1' # Set same source_id as the existing message
|
||||
described_class.new(messaging, instagram_inbox, outgoing_echo: false).perform
|
||||
|
||||
expect(instagram_inbox.conversations.count).to be 1
|
||||
expect(instagram_inbox.messages.count).to be 1
|
||||
end
|
||||
|
||||
it 'handles authorization errors' do
|
||||
instagram_channel.update(access_token: 'invalid_token')
|
||||
|
||||
# Stub the request to return authorization error status
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?/Sender-id-.*?\?.*})
|
||||
.to_return(
|
||||
status: 401,
|
||||
body: { error: { message: 'unauthorized access token', code: 190 } }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
|
||||
# The method should complete without raising an error
|
||||
expect do
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
end.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is disabled' do
|
||||
before do
|
||||
instagram_inbox.update!(lock_to_single_conversation: false)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
initial_count = Conversation.count
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(initial_count + 1)
|
||||
end
|
||||
|
||||
it 'will not create a new conversation if last conversation is not resolved' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact = create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id,
|
||||
contact_id: contact.id, status: :open)
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if last conversation is resolved' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact = create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id,
|
||||
contact_id: contact.id, status: :resolved)
|
||||
|
||||
initial_count = Conversation.count
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.last.id).not_to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(initial_count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is enabled' do
|
||||
before do
|
||||
instagram_inbox.update!(lock_to_single_conversation: true)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
initial_count = Conversation.count
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(initial_count + 1)
|
||||
end
|
||||
|
||||
it 'reopens last conversation if last conversation is resolved' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
contact = create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
existing_conversation = create(:conversation, account_id: account.id, inbox_id: instagram_inbox.id,
|
||||
contact_id: contact.id, status: :resolved)
|
||||
|
||||
initial_count = Conversation.count
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
expect(instagram_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(initial_count)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#fetch_story_link' do
|
||||
let(:story_data) do
|
||||
{
|
||||
'story' => {
|
||||
'mention' => {
|
||||
'link' => 'https://example.com/story-link',
|
||||
'id' => '18094414321535710'
|
||||
}
|
||||
},
|
||||
'from' => {
|
||||
'username' => 'instagram_user',
|
||||
'id' => '2450757355263608'
|
||||
},
|
||||
'id' => 'story-source-id-123'
|
||||
}.with_indifferent_access
|
||||
end
|
||||
|
||||
before do
|
||||
# Stub the HTTP request to Instagram API
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?fields=story,from})
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: story_data.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'saves story information when story mention is processed' do
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
expect(message.content).to include('instagram_user')
|
||||
expect(message.attachments.count).to eq(1)
|
||||
expect(message.content_attributes[:story_sender]).to eq('instagram_user')
|
||||
expect(message.content_attributes[:story_id]).to eq('18094414321535710')
|
||||
expect(message.content_attributes[:image_type]).to eq('story_mention')
|
||||
end
|
||||
|
||||
it 'handles deleted stories' do
|
||||
# Override the stub for this test to return a 404 error
|
||||
stub_request(:get, %r{https://graph\.instagram\.com/.*?fields=story,from})
|
||||
.to_return(
|
||||
status: 404,
|
||||
body: { error: { message: 'Story not found', code: 1_609_005 } }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
create_instagram_contact_for_sender(messaging['sender']['id'], instagram_inbox)
|
||||
described_class.new(messaging, instagram_inbox).perform
|
||||
|
||||
message = instagram_inbox.messages.first
|
||||
|
||||
expect(message.content).to eq('This story is no longer available.')
|
||||
expect(message.attachments.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,399 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Messages::Instagram::Messenger::MessageBuilder do
|
||||
subject(:instagram_message_builder) { described_class }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
stub_request(:get, 'https://www.example.com/test.jpeg')
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:instagram_messenger_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
|
||||
let!(:instagram_messenger_inbox) { create(:inbox, channel: instagram_messenger_channel, account: account, greeting_enabled: false) }
|
||||
let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access }
|
||||
let!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access }
|
||||
let!(:shared_reel_params) { build(:instagram_shared_reel_event).with_indifferent_access }
|
||||
let!(:instagram_story_reply_event) { build(:instagram_story_reply_event).with_indifferent_access }
|
||||
let!(:instagram_message_reply_event) { build(:instagram_message_reply_event).with_indifferent_access }
|
||||
let(:fb_object) { double }
|
||||
|
||||
describe '#perform' do
|
||||
it 'creates contact and message for the facebook inbox' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
{
|
||||
name: 'Jane',
|
||||
id: sender_id,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
contact = instagram_messenger_channel.inbox.contacts.first
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
expect(message.content).to eq('This is the first message from the customer')
|
||||
end
|
||||
|
||||
it 'discard echo message already sent by chatwoot' do
|
||||
messaging = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
contact = create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
conversation = create(:conversation, account_id: account.id, inbox_id: instagram_messenger_inbox.id, contact_id: contact.id,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
|
||||
create(:message, account_id: account.id, inbox_id: instagram_messenger_inbox.id, conversation_id: conversation.id, message_type: 'outgoing',
|
||||
source_id: 'message-id-1')
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
{
|
||||
name: 'Jane',
|
||||
id: sender_id,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
described_class.new(messaging, instagram_messenger_inbox, outgoing_echo: true).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
end
|
||||
|
||||
it 'creates message for shared reel' do
|
||||
messaging = shared_reel_params[:entry][0]['messaging'][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
{
|
||||
name: 'Jane',
|
||||
id: sender_id,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
expect(message.attachments.first.file_type).to eq('ig_reel')
|
||||
expect(message.attachments.first.external_url).to eq(
|
||||
shared_reel_params[:entry][0]['messaging'][0]['message']['attachments'][0]['payload']['url']
|
||||
)
|
||||
end
|
||||
|
||||
it 'creates message with for reply with story id' do
|
||||
messaging = instagram_story_reply_event[:entry][0]['messaging'][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
story_url = messaging['message']['reply_to']['story']['url']
|
||||
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
{
|
||||
name: 'Jane',
|
||||
id: sender_id,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
stub_request(:get, story_url)
|
||||
.to_return(status: 200, body: 'image_data', headers: { 'Content-Type' => 'image/png' })
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
|
||||
expect(message.content).to eq('This is the story reply')
|
||||
expect(message.content_attributes[:story_sender]).to eq(instagram_messenger_inbox.channel.instagram_id)
|
||||
expect(message.content_attributes[:story_id]).to eq('chatwoot-app-user-id-1')
|
||||
expect(message.content_attributes[:story_url]).to eq(story_url)
|
||||
expect(message.content_attributes[:image_type]).to eq('ig_story_reply')
|
||||
expect(message.attachments.first.file_type).to eq('ig_story')
|
||||
expect(message.attachments.first.external_url).to eq(story_url)
|
||||
end
|
||||
|
||||
it 'creates message with for reply with mid' do
|
||||
# create first message to ensure reply to is valid
|
||||
first_message_data = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = first_message_data['sender']['id']
|
||||
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
{
|
||||
name: 'Jane',
|
||||
id: sender_id,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(first_message_data, instagram_messenger_inbox).perform
|
||||
|
||||
# create the second message with the reply to mid set, ensure same sender_id
|
||||
messaging = instagram_message_reply_event[:entry][0]['messaging'][0]
|
||||
messaging['sender']['id'] = sender_id # Use the same sender_id
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
first_message = instagram_messenger_channel.inbox.messages.first
|
||||
message = instagram_messenger_channel.inbox.messages.last
|
||||
|
||||
expect(message.content).to eq('This is message with replyto mid')
|
||||
expect(message.content_attributes[:in_reply_to_external_id]).to eq(first_message.source_id)
|
||||
expect(message.content_attributes[:in_reply_to]).to eq(first_message.id)
|
||||
end
|
||||
|
||||
it 'raises exception on deleted story' do
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::ClientError.new(
|
||||
190,
|
||||
'This Message has been deleted by the user or the business.'
|
||||
))
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(messaging, instagram_messenger_inbox, outgoing_echo: false).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
# we would have contact created, message created but the empty message because the story mention has been deleted later
|
||||
# As they show it in instagram that this story is no longer available
|
||||
# and external attachments link would be reachable
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 1
|
||||
|
||||
contact = instagram_messenger_channel.inbox.contacts.first
|
||||
message = instagram_messenger_channel.inbox.messages.first
|
||||
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
expect(message.content).to eq('This story is no longer available.')
|
||||
expect(message.attachments.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'does not create message for unsupported file type' do
|
||||
# create a message with unsupported file type
|
||||
story_mention_params[:entry][0][:messaging][0]['message']['attachments'][0]['type'] = 'unsupported_type'
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
allow(fb_object).to receive(:get_object).and_return(
|
||||
{
|
||||
name: 'Jane',
|
||||
id: sender_id,
|
||||
account_id: instagram_messenger_inbox.account_id,
|
||||
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
||||
}.with_indifferent_access
|
||||
)
|
||||
|
||||
contact = create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
create(:conversation, account_id: account.id, inbox_id: instagram_messenger_inbox.id, contact_id: contact.id,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' })
|
||||
described_class.new(messaging, instagram_messenger_inbox, outgoing_echo: false).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
# Conversation should exist but no new message should be created
|
||||
expect(instagram_messenger_inbox.conversations.count).to be 1
|
||||
expect(instagram_messenger_inbox.messages.count).to be 0
|
||||
|
||||
contact = instagram_messenger_channel.inbox.contacts.first
|
||||
expect(contact.name).to eq('Jane Dae')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is disabled' do
|
||||
before do
|
||||
instagram_messenger_inbox.update!(lock_to_single_conversation: false)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = message['sender']['id']
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
|
||||
it 'will not create a new conversation if last conversation is not resolved' do
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = message['sender']['id']
|
||||
contact = create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
|
||||
existing_conversation = create(
|
||||
:conversation,
|
||||
account_id: account.id,
|
||||
inbox_id: instagram_messenger_inbox.id,
|
||||
contact_id: contact.id,
|
||||
status: :open,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' }
|
||||
)
|
||||
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if last conversation is resolved' do
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = message['sender']['id']
|
||||
contact = create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
|
||||
existing_conversation = create(
|
||||
:conversation,
|
||||
account_id: account.id,
|
||||
inbox_id: instagram_messenger_inbox.id,
|
||||
contact_id: contact.id,
|
||||
status: :resolved,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' }
|
||||
)
|
||||
|
||||
inital_count = Conversation.count
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.last.id).not_to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lock to single conversation is enabled' do
|
||||
before do
|
||||
instagram_messenger_inbox.update!(lock_to_single_conversation: true)
|
||||
stub_request(:get, /graph.facebook.com/)
|
||||
end
|
||||
|
||||
it 'creates a new conversation if existing conversation is not present' do
|
||||
inital_count = Conversation.count
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = message['sender']['id']
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.count).to eq(1)
|
||||
expect(Conversation.count).to eq(inital_count + 1)
|
||||
end
|
||||
|
||||
it 'reopens last conversation if last conversation is resolved' do
|
||||
message = dm_params[:entry][0]['messaging'][0]
|
||||
sender_id = message['sender']['id']
|
||||
contact = create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
|
||||
existing_conversation = create(
|
||||
:conversation,
|
||||
account_id: account.id,
|
||||
inbox_id: instagram_messenger_inbox.id,
|
||||
contact_id: contact.id,
|
||||
status: :resolved,
|
||||
additional_attributes: { type: 'instagram_direct_message', conversation_language: 'en' }
|
||||
)
|
||||
|
||||
inital_count = Conversation.count
|
||||
|
||||
described_class.new(message, instagram_messenger_inbox).perform
|
||||
|
||||
instagram_messenger_inbox.reload
|
||||
|
||||
expect(instagram_messenger_inbox.conversations.last.id).to eq(existing_conversation.id)
|
||||
expect(Conversation.count).to eq(inital_count)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#fetch_story_link' do
|
||||
before do
|
||||
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
|
||||
end
|
||||
|
||||
let(:story_data) do
|
||||
{
|
||||
'story' => {
|
||||
'mention' => {
|
||||
'link' => 'https://example.com/story-link',
|
||||
'id' => '18094414321535710'
|
||||
}
|
||||
},
|
||||
'from' => {
|
||||
'username' => 'instagram_user',
|
||||
'id' => '2450757355263608'
|
||||
},
|
||||
'id' => 'story-source-id-123'
|
||||
}.with_indifferent_access
|
||||
end
|
||||
|
||||
it 'saves story information when story mention is processed' do
|
||||
allow(fb_object).to receive(:get_object).and_return(story_data)
|
||||
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
builder = described_class.new(messaging, instagram_messenger_inbox)
|
||||
builder.perform
|
||||
|
||||
message = instagram_messenger_inbox.messages.first
|
||||
|
||||
expect(message.content).to include('instagram_user')
|
||||
expect(message.attachments.count).to eq(1)
|
||||
expect(message.content_attributes[:story_sender]).to eq('instagram_user')
|
||||
expect(message.content_attributes[:story_id]).to eq('18094414321535710')
|
||||
expect(message.content_attributes[:image_type]).to eq('story_mention')
|
||||
end
|
||||
|
||||
it 'handles story mentions specifically in the Instagram builder' do
|
||||
messaging = story_mention_params[:entry][0][:messaging][0]
|
||||
sender_id = messaging['sender']['id']
|
||||
|
||||
# First allow contact info fetch
|
||||
allow(fb_object).to receive(:get_object).and_return({
|
||||
name: 'Jane',
|
||||
id: sender_id
|
||||
}.with_indifferent_access)
|
||||
|
||||
# Then allow story data fetch
|
||||
allow(fb_object).to receive(:get_object).with(anything, fields: %w[story from])
|
||||
.and_return(story_data)
|
||||
|
||||
create_instagram_contact_for_sender(sender_id, instagram_messenger_inbox)
|
||||
described_class.new(messaging, instagram_messenger_inbox).perform
|
||||
|
||||
message = instagram_messenger_inbox.messages.first
|
||||
|
||||
expect(message.content_attributes[:story_sender]).to eq('instagram_user')
|
||||
expect(message.content_attributes[:image_type]).to eq('story_mention')
|
||||
end
|
||||
end
|
||||
end
|
||||
291
research/chatwoot/spec/builders/messages/message_builder_spec.rb
Normal file
291
research/chatwoot/spec/builders/messages/message_builder_spec.rb
Normal file
@@ -0,0 +1,291 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Messages::MessageBuilder do
|
||||
subject(:message_builder) { described_class.new(user, conversation, params).perform }
|
||||
|
||||
let(:account) { create(:account) }
|
||||
let(:user) { create(:user, account: account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:inbox_member) { create(:inbox_member, inbox: inbox, account: account) }
|
||||
let(:conversation) { create(:conversation, inbox: inbox, account: account) }
|
||||
let(:message_for_reply) { create(:message, conversation: conversation) }
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test'
|
||||
})
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
it 'creates a message' do
|
||||
message = message_builder
|
||||
expect(message.content).to eq params[:content]
|
||||
end
|
||||
end
|
||||
|
||||
describe '#content_attributes' do
|
||||
context 'when content_attributes is a JSON string' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
content_attributes: "{\"in_reply_to\":#{message_for_reply.id}}"
|
||||
})
|
||||
end
|
||||
|
||||
it 'parses content_attributes from JSON string' do
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
expect(message.content_attributes).to include(in_reply_to: message_for_reply.id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when content_attributes is a hash' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
content_attributes: { in_reply_to: message_for_reply.id }
|
||||
})
|
||||
end
|
||||
|
||||
it 'uses content_attributes as provided' do
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
expect(message.content_attributes).to include(in_reply_to: message_for_reply.id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when content_attributes is absent' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({ content: 'test' })
|
||||
end
|
||||
|
||||
it 'defaults to an empty hash' do
|
||||
message = message_builder
|
||||
expect(message.content_attributes).to eq({})
|
||||
end
|
||||
end
|
||||
|
||||
context 'when content_attributes is nil' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
content_attributes: nil
|
||||
})
|
||||
end
|
||||
|
||||
it 'defaults to an empty hash' do
|
||||
message = message_builder
|
||||
expect(message.content_attributes).to eq({})
|
||||
end
|
||||
end
|
||||
|
||||
context 'when content_attributes is an invalid JSON string' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
content_attributes: 'invalid_json'
|
||||
})
|
||||
end
|
||||
|
||||
it 'defaults to an empty hash' do
|
||||
message = message_builder
|
||||
expect(message.content_attributes).to eq({})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#perform when message_type is incoming' do
|
||||
context 'when channel is not api' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
message_type: 'incoming'
|
||||
})
|
||||
end
|
||||
|
||||
it 'creates throws error when channel is not api' do
|
||||
expect { message_builder }.to raise_error 'Incoming messages are only allowed in Api inboxes'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when channel is api' do
|
||||
let(:channel_api) { create(:channel_api, account: account) }
|
||||
let(:conversation) { create(:conversation, inbox: channel_api.inbox, account: account) }
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
message_type: 'incoming'
|
||||
})
|
||||
end
|
||||
|
||||
it 'creates message when channel is api' do
|
||||
message = message_builder
|
||||
expect(message.message_type).to eq params[:message_type]
|
||||
end
|
||||
end
|
||||
|
||||
context 'when attachment messages' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
attachments: [Rack::Test::UploadedFile.new('spec/assets/avatar.png', 'image/png')]
|
||||
})
|
||||
end
|
||||
|
||||
it 'creates message with attachments' do
|
||||
message = message_builder
|
||||
expect(message.attachments.first.file_type).to eq 'image'
|
||||
end
|
||||
|
||||
context 'when DIRECT_UPLOAD_ENABLED' do
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({
|
||||
content: 'test',
|
||||
attachments: [get_blob_for('spec/assets/avatar.png', 'image/png').signed_id]
|
||||
})
|
||||
end
|
||||
|
||||
it 'creates message with attachments' do
|
||||
message = message_builder
|
||||
expect(message.attachments.first.file_type).to eq 'image'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when email channel messages' do
|
||||
let!(:channel_email) { create(:channel_email, account: account) }
|
||||
let(:inbox_member) { create(:inbox_member, inbox: channel_email.inbox) }
|
||||
let(:conversation) { create(:conversation, inbox: channel_email.inbox, account: account) }
|
||||
let(:params) do
|
||||
ActionController::Parameters.new({ cc_emails: 'test_cc_mail@test.com', bcc_emails: 'test_bcc_mail@test.com' })
|
||||
end
|
||||
|
||||
it 'creates message with content_attributes for cc and bcc email addresses' do
|
||||
message = message_builder
|
||||
|
||||
expect(message.content_attributes[:cc_emails]).to eq [params[:cc_emails]]
|
||||
expect(message.content_attributes[:bcc_emails]).to eq [params[:bcc_emails]]
|
||||
end
|
||||
|
||||
it 'does not create message with wrong cc and bcc email addresses' do
|
||||
params = ActionController::Parameters.new({ cc_emails: 'test.com', bcc_emails: 'test_bcc.com' })
|
||||
expect { described_class.new(user, conversation, params).perform }.to raise_error 'Invalid email address'
|
||||
end
|
||||
|
||||
it 'strips off whitespace before saving cc_emails and bcc_emails' do
|
||||
cc_emails = ' test1@test.com , test2@test.com, test3@test.com'
|
||||
bcc_emails = 'test1@test.com,test2@test.com, test3@test.com '
|
||||
params = ActionController::Parameters.new({ cc_emails: cc_emails, bcc_emails: bcc_emails })
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes[:cc_emails]).to eq ['test1@test.com', 'test2@test.com', 'test3@test.com']
|
||||
expect(message.content_attributes[:bcc_emails]).to eq ['test1@test.com', 'test2@test.com', 'test3@test.com']
|
||||
end
|
||||
|
||||
context 'when custom email content is provided' do
|
||||
before do
|
||||
account.enable_features('quoted_email_reply')
|
||||
end
|
||||
|
||||
it 'creates message with custom HTML email content' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Regular message content',
|
||||
email_html_content: '<p>Custom <strong>HTML</strong> content</p>'
|
||||
})
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes.dig('email', 'html_content', 'full')).to eq '<p>Custom <strong>HTML</strong> content</p>'
|
||||
expect(message.content_attributes.dig('email', 'html_content', 'reply')).to eq '<p>Custom <strong>HTML</strong> content</p>'
|
||||
expect(message.content_attributes.dig('email', 'text_content', 'full')).to eq 'Regular message content'
|
||||
expect(message.content_attributes.dig('email', 'text_content', 'reply')).to eq 'Regular message content'
|
||||
end
|
||||
|
||||
it 'does not process custom email content for private messages' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Regular message content',
|
||||
email_html_content: '<p>Custom HTML content</p>',
|
||||
private: true
|
||||
})
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes.dig('email', 'html_content')).to be_nil
|
||||
expect(message.content_attributes.dig('email', 'text_content')).to be_nil
|
||||
end
|
||||
|
||||
it 'falls back to default behavior when no custom email content is provided' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Regular **markdown** content'
|
||||
})
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes.dig('email', 'html_content', 'full')).to include('<strong>markdown</strong>')
|
||||
expect(message.content_attributes.dig('email', 'text_content', 'full')).to eq 'Regular **markdown** content'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when liquid templates are present in email content' do
|
||||
let(:contact) { create(:contact, name: 'John', email: 'john@example.com') }
|
||||
let(:conversation) { create(:conversation, inbox: channel_email.inbox, account: account, contact: contact) }
|
||||
|
||||
it 'processes liquid variables in email content' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Hello {{contact.name}}, your email is {{contact.email}}'
|
||||
})
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes.dig('email', 'html_content', 'full')).to include('Hello John')
|
||||
expect(message.content_attributes.dig('email', 'html_content', 'full')).to include('john@example.com')
|
||||
expect(message.content_attributes.dig('email', 'text_content', 'full')).to eq 'Hello John, your email is john@example.com'
|
||||
end
|
||||
|
||||
it 'does not process liquid in code blocks' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Hello {{contact.name}}, use this code: `{{contact.email}}`'
|
||||
})
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes.dig('email', 'text_content', 'full')).to eq 'Hello John, use this code: `{{contact.email}}`'
|
||||
end
|
||||
|
||||
it 'handles broken liquid syntax gracefully' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Hello {{contact.name} {{invalid}}'
|
||||
})
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes.dig('email', 'text_content', 'full')).to eq 'Hello {{contact.name} {{invalid}}'
|
||||
end
|
||||
|
||||
it 'does not process liquid for incoming messages' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Hello {{contact.name}}',
|
||||
message_type: 'incoming'
|
||||
})
|
||||
|
||||
api_channel = create(:channel_api, account: account)
|
||||
api_conversation = create(:conversation, inbox: api_channel.inbox, account: account, contact: contact)
|
||||
|
||||
message = described_class.new(user, api_conversation, params).perform
|
||||
|
||||
expect(message.content).to eq 'Hello {{contact.name}}'
|
||||
end
|
||||
|
||||
it 'does not process liquid for private messages' do
|
||||
params = ActionController::Parameters.new({
|
||||
content: 'Hello {{contact.name}}',
|
||||
private: true
|
||||
})
|
||||
|
||||
message = described_class.new(user, conversation, params).perform
|
||||
|
||||
expect(message.content_attributes.dig('email', 'html_content')).to be_nil
|
||||
expect(message.content_attributes.dig('email', 'text_content')).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user