2020-01-07 17:29:17 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
describe ::Messages::Facebook::MessageBuilder do
|
2020-01-07 17:29:17 +00:00
|
|
|
subject(:message_builder) { described_class.new(incoming_fb_text_message, facebook_channel.inbox).perform }
|
|
|
|
|
2021-11-11 07:33:48 +00:00
|
|
|
before do
|
|
|
|
stub_request(:post, /graph.facebook.com/)
|
|
|
|
end
|
|
|
|
|
2020-01-07 17:29:17 +00:00
|
|
|
let!(:facebook_channel) { create(:channel_facebook_page) }
|
2021-09-13 12:35:14 +00:00
|
|
|
let!(:message_object) { build(:incoming_fb_text_message).to_json }
|
2020-01-07 17:29:17 +00:00
|
|
|
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
|
2021-08-01 12:45:39 +00:00
|
|
|
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,
|
2021-11-11 07:33:48 +00:00
|
|
|
profile_pic: 'https://chatwoot-assets.local/sample.png'
|
2021-08-01 12:45:39 +00:00
|
|
|
}.with_indifferent_access
|
|
|
|
)
|
2020-01-07 17:29:17 +00:00
|
|
|
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
|
2021-08-01 12:45:39 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-06-07 12:20:51 +00:00
|
|
|
expect(facebook_channel.authorization_error_count).to eq(2)
|
2021-08-01 12:45:39 +00:00
|
|
|
end
|
2020-01-07 17:29:17 +00:00
|
|
|
end
|
|
|
|
end
|