Specs: Add specs for Facebook::SendReplyService (#396)

This commit is contained in:
Pranav Raj S 2019-12-28 23:14:09 +05:30 committed by Sojan Jose
parent 790877c056
commit e2aeeeca43
4 changed files with 115 additions and 56 deletions

View file

@ -1,5 +1,4 @@
module Facebook
class SendReplyService
class Facebook::SendReplyService
pattr_initialize [:message!]
def perform
@ -64,5 +63,4 @@ module Facebook
# we can send max 1 message after 24 hour window
conversation.messages.outgoing.where('id > ?', last_incoming_message_id).count == 1
end
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
FactoryBot.define do
factory :facebook_page, class: 'Channel::FacebookPage' do
sequence(:page_id) { |n| n }
sequence(:user_access_token) { |n| "random-token-#{n}" }
sequence(:name) { |n| "Facebook Page #{n}" }
sequence(:page_access_token) { |n| "page-access-token-#{n}" }
account
end
end

View file

@ -5,7 +5,6 @@ FactoryBot.define do
content { 'Message' }
status { 'sent' }
message_type { 'incoming' }
fb_id { SecureRandom.uuid }
account
inbox
conversation

View file

@ -0,0 +1,51 @@
require 'rails_helper'
describe Facebook::SendReplyService do
subject(:send_reply_service) { described_class.new(message: message) }
before do
allow(Facebook::Messenger::Subscriptions).to receive(:subscribe).and_return(true)
allow(bot).to receive(:deliver)
end
let!(:account) { create(:account) }
let(:bot) { class_double('Bot').as_stubbed_const }
let!(:widget_inbox) { create(:inbox, account: account) }
let!(:facebook_channel) { create(:facebook_page, account: account) }
let!(:facebook_inbox) { create(:inbox, channel: facebook_channel, account: account) }
let!(:contact) { create(:contact, account: account) }
let(:conversation) { create(:conversation, contact: contact, inbox: facebook_inbox) }
describe '#perform' do
context 'without reply' do
it 'if message is private' do
create(:message, message_type: 'outgoing', private: true, inbox: facebook_inbox, account: account)
expect(bot).not_to have_received(:deliver)
end
it 'if inbox channel is not facebook page' do
create(:message, message_type: 'outgoing', inbox: widget_inbox, account: account)
expect(bot).not_to have_received(:deliver)
end
it 'if message is not outgoing' do
create(:message, message_type: 'incoming', inbox: facebook_inbox, account: account)
expect(bot).not_to have_received(:deliver)
end
it 'if message has an FB ID' do
create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, fb_id: SecureRandom.uuid)
expect(bot).not_to have_received(:deliver)
end
end
context 'with reply' do
it 'if message is sent from chatwoot and is outgoing' do
create(:contact_inbox, contact: contact, inbox: facebook_inbox)
create(:message, message_type: :incoming, inbox: facebook_inbox, account: account, conversation: conversation)
create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation)
expect(bot).to have_received(:deliver)
end
end
end
end