Feature: Twitter DM Integration (#451)
An initial version of twitter integration Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
11
spec/factories/channel/twitter_profiles.rb
Normal file
11
spec/factories/channel/twitter_profiles.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :channel_twitter_profile, class: 'Channel::TwitterProfile' do
|
||||
name { Faker::Name.name }
|
||||
twitter_access_token { SecureRandom.uuid }
|
||||
twitter_access_token_secret { SecureRandom.uuid }
|
||||
profile_id { SecureRandom.uuid }
|
||||
account
|
||||
end
|
||||
end
|
||||
37
spec/factories/twitter/twitter_message_create_event.rb
Normal file
37
spec/factories/twitter/twitter_message_create_event.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :twitter_message_create_event, class: Hash do
|
||||
for_user_id { '1' }
|
||||
direct_message_events do
|
||||
[{
|
||||
'type' => 'message_create',
|
||||
'id' => '123',
|
||||
'message_create' => {
|
||||
target: { 'recipient_id' => '1' },
|
||||
'sender_id' => '2',
|
||||
'source_app_id' => '268278',
|
||||
'message_data' => {
|
||||
'text' => 'Blue Bird'
|
||||
}
|
||||
}
|
||||
}]
|
||||
end
|
||||
users do
|
||||
{
|
||||
'1' => {
|
||||
id: '1',
|
||||
name: 'person 1',
|
||||
profile_image_url: 'https://via.placeholder.com/250x250.png'
|
||||
},
|
||||
'2' => {
|
||||
id: '1',
|
||||
name: 'person 1',
|
||||
profile_image_url: 'https://via.placeholder.com/250x250.png'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
initialize_with { attributes }
|
||||
end
|
||||
end
|
||||
23
spec/lib/webhooks/twitter_spec.rb
Normal file
23
spec/lib/webhooks/twitter_spec.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
require 'rails_helper'
|
||||
require 'webhooks/twitter'
|
||||
|
||||
describe Webhooks::Twitter do
|
||||
subject(:process_twitter_event) { described_class.new(params).consume }
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
# FIX ME: recipient id is set to 1 inside event factories
|
||||
let!(:twitter_channel) { create(:channel_twitter_profile, account: account, profile_id: '1') }
|
||||
let!(:twitter_inbox) { create(:inbox, channel: twitter_channel, account: account) }
|
||||
let!(:params) { build(:twitter_message_create_event).with_indifferent_access }
|
||||
|
||||
describe '#perform' do
|
||||
context 'with correct params' do
|
||||
it 'creates incoming message in the twitter inbox' do
|
||||
process_twitter_event
|
||||
expect(twitter_inbox.contacts.count).to be 1
|
||||
expect(twitter_inbox.conversations.count).to be 1
|
||||
expect(twitter_inbox.messages.count).to be 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
44
spec/services/twitter/send_reply_service_spec.rb
Normal file
44
spec/services/twitter/send_reply_service_spec.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Twitter::SendReplyService do
|
||||
subject(:send_reply_service) { described_class.new(message: message) }
|
||||
|
||||
before do
|
||||
allow($twitter).to receive(:send_direct_message).and_return(true)
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:widget_inbox) { create(:inbox, account: account) }
|
||||
let!(:twitter_channel) { create(:channel_twitter_profile, account: account) }
|
||||
let!(:twitter_inbox) { create(:inbox, channel: twitter_channel, account: account) }
|
||||
let!(:contact) { create(:contact, account: account) }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: twitter_inbox) }
|
||||
let(:conversation) { create(:conversation, contact: contact, inbox: twitter_inbox, contact_inbox: contact_inbox) }
|
||||
|
||||
describe '#perform' do
|
||||
context 'without reply' do
|
||||
it 'if message is private' do
|
||||
create(:message, message_type: 'outgoing', private: true, inbox: twitter_inbox, account: account)
|
||||
expect($twitter).not_to have_received(:send_direct_message)
|
||||
end
|
||||
|
||||
it 'if inbox channel is not twitter profile' do
|
||||
create(:message, message_type: 'outgoing', inbox: widget_inbox, account: account)
|
||||
expect($twitter).not_to have_received(:send_direct_message)
|
||||
end
|
||||
|
||||
it 'if message is not outgoing' do
|
||||
create(:message, message_type: 'incoming', inbox: twitter_inbox, account: account)
|
||||
expect($twitter).not_to have_received(:send_direct_message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with reply' do
|
||||
it 'if message is sent from chatwoot and is outgoing' do
|
||||
create(:message, message_type: :incoming, inbox: twitter_inbox, account: account, conversation: conversation)
|
||||
create(:message, message_type: 'outgoing', inbox: twitter_inbox, account: account, conversation: conversation)
|
||||
expect($twitter).to have_received(:send_direct_message)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user