2019-12-28 17:44:09 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2020-06-25 18:05:16 +00:00
|
|
|
describe Facebook::SendOnFacebookService do
|
2019-12-28 17:44:09 +00:00
|
|
|
subject(:send_reply_service) { described_class.new(message: message) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Facebook::Messenger::Subscriptions).to receive(:subscribe).and_return(true)
|
2020-09-29 19:42:32 +00:00
|
|
|
allow(bot).to receive(:deliver).and_return({ recipient_id: '1008372609250235', message_id: 'mid.1456970487936:c34767dfe57ee6e339' }.to_json)
|
2020-07-16 19:02:32 +00:00
|
|
|
create(:message, message_type: :incoming, inbox: facebook_inbox, account: account, conversation: conversation)
|
2019-12-28 17:44:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let!(:account) { create(:account) }
|
2020-05-18 10:02:26 +00:00
|
|
|
let(:bot) { class_double('FacebookBot::Bot').as_stubbed_const }
|
2019-12-28 17:44:09 +00:00
|
|
|
let!(:widget_inbox) { create(:inbox, account: account) }
|
2020-04-19 18:10:28 +00:00
|
|
|
let!(:facebook_channel) { create(:channel_facebook_page, account: account) }
|
2019-12-28 17:44:09 +00:00
|
|
|
let!(:facebook_inbox) { create(:inbox, channel: facebook_channel, account: account) }
|
|
|
|
let!(:contact) { create(:contact, account: account) }
|
2020-01-09 07:36:40 +00:00
|
|
|
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: facebook_inbox) }
|
|
|
|
let(:conversation) { create(:conversation, contact: contact, inbox: facebook_inbox, contact_inbox: contact_inbox) }
|
2019-12-28 17:44:09 +00:00
|
|
|
|
|
|
|
describe '#perform' do
|
|
|
|
context 'without reply' do
|
|
|
|
it 'if message is private' do
|
2020-07-16 19:02:32 +00:00
|
|
|
message = create(:message, message_type: 'outgoing', private: true, inbox: facebook_inbox, account: account)
|
|
|
|
::Facebook::SendOnFacebookService.new(message: message).perform
|
2019-12-28 17:44:09 +00:00
|
|
|
expect(bot).not_to have_received(:deliver)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'if inbox channel is not facebook page' do
|
2020-07-16 19:02:32 +00:00
|
|
|
message = create(:message, message_type: 'outgoing', inbox: widget_inbox, account: account)
|
|
|
|
expect { ::Facebook::SendOnFacebookService.new(message: message).perform }.to raise_error 'Invalid channel service was called'
|
2019-12-28 17:44:09 +00:00
|
|
|
expect(bot).not_to have_received(:deliver)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'if message is not outgoing' do
|
2020-07-16 19:02:32 +00:00
|
|
|
message = create(:message, message_type: 'incoming', inbox: facebook_inbox, account: account)
|
|
|
|
::Facebook::SendOnFacebookService.new(message: message).perform
|
2019-12-28 17:44:09 +00:00
|
|
|
expect(bot).not_to have_received(:deliver)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'if message has an FB ID' do
|
2020-07-16 19:02:32 +00:00
|
|
|
message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, source_id: SecureRandom.uuid)
|
|
|
|
::Facebook::SendOnFacebookService.new(message: message).perform
|
2019-12-28 17:44:09 +00:00
|
|
|
expect(bot).not_to have_received(:deliver)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with reply' do
|
|
|
|
it 'if message is sent from chatwoot and is outgoing' do
|
2020-07-16 19:02:32 +00:00
|
|
|
message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation)
|
|
|
|
::Facebook::SendOnFacebookService.new(message: message).perform
|
2019-12-28 17:44:09 +00:00
|
|
|
expect(bot).to have_received(:deliver)
|
|
|
|
end
|
2020-03-28 06:13:02 +00:00
|
|
|
|
|
|
|
it 'if message with attachment is sent from chatwoot and is outgoing' do
|
|
|
|
message = build(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation)
|
2020-04-17 15:45:20 +00:00
|
|
|
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
|
|
|
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
|
2020-03-28 06:13:02 +00:00
|
|
|
message.save!
|
2020-07-16 19:02:32 +00:00
|
|
|
::Facebook::SendOnFacebookService.new(message: message).perform
|
2021-01-06 12:26:29 +00:00
|
|
|
expect(bot).to have_received(:deliver).with({
|
|
|
|
recipient: { id: contact_inbox.source_id },
|
|
|
|
message: { text: message.content }
|
|
|
|
}, { access_token: facebook_channel.page_access_token })
|
|
|
|
expect(bot).to have_received(:deliver).with({
|
|
|
|
recipient: { id: contact_inbox.source_id },
|
|
|
|
message: {
|
|
|
|
attachment: {
|
|
|
|
type: 'image',
|
|
|
|
payload: {
|
|
|
|
url: attachment.file_url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, { access_token: facebook_channel.page_access_token })
|
2020-03-28 06:13:02 +00:00
|
|
|
end
|
2019-12-28 17:44:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|