chore: Provider API prototype (#3112)

Enabling Support for Whatsapp via 360Dialog as a prototype for the provider APIs. 

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose
2021-10-05 23:35:06 +05:30
committed by GitHub
parent 40d0b2faf3
commit bd7aeba484
31 changed files with 506 additions and 60 deletions

View File

@@ -0,0 +1,12 @@
require 'rails_helper'
RSpec.describe 'Webhooks::WhatsappController', type: :request do
describe 'POST /webhooks/whatsapp/{:phone_number}' do
it 'call the whatsapp events job with the params' do
allow(Webhooks::WhatsappEventsJob).to receive(:perform_later)
expect(Webhooks::WhatsappEventsJob).to receive(:perform_later)
post '/webhooks/whatsapp/123221321', params: { content: 'hello' }
expect(response).to have_http_status(:success)
end
end
end

View File

@@ -0,0 +1,11 @@
FactoryBot.define do
factory :channel_whatsapp, class: 'Channel::Whatsapp' do
sequence(:phone_number) { |n| "+123456789#{n}1" }
account
provider_config { { 'api_key' => 'test_key' } }
after(:create) do |channel_whatsapp|
create(:inbox, channel: channel_whatsapp, account: channel_whatsapp.account)
end
end
end

View File

@@ -64,5 +64,14 @@ RSpec.describe SendReplyJob, type: :job do
expect(process_service).to receive(:perform)
described_class.perform_now(message.id)
end
it 'calls ::Whatsapp:SendOnWhatsappService when its line message' do
whatsapp_channel = create(:channel_whatsapp)
message = create(:message, conversation: create(:conversation, inbox: whatsapp_channel.inbox))
allow(::Whatsapp::SendOnWhatsappService).to receive(:new).with(message: message).and_return(process_service)
expect(::Whatsapp::SendOnWhatsappService).to receive(:new).with(message: message)
expect(process_service).to receive(:perform)
described_class.perform_now(message.id)
end
end
end

View File

@@ -0,0 +1,21 @@
require 'rails_helper'
describe Whatsapp::IncomingMessageService do
let!(:whatsapp_channel) { create(:channel_whatsapp) }
describe '#perform' do
context 'when valid text message params' do
it 'creates appropriate conversations, message and contacts' do
params = {
'contacts' => [{ 'profile' => { 'name' => 'Sojan Jose' }, 'wa_id' => '2423423243' }],
'messages' => [{ 'from' => '2423423243', 'id' => 'SDFADSf23sfasdafasdfa', 'text' => { 'body' => 'Test' },
'timestamp' => '1633034394', 'type' => 'text' }]
}.with_indifferent_access
described_class.new(inbox: whatsapp_channel.inbox, params: params).perform
expect(whatsapp_channel.inbox.conversations.count).not_to eq(0)
expect(Contact.all.first.name).to eq('Sojan Jose')
expect(whatsapp_channel.inbox.messages.first.content).to eq('Test')
end
end
end
end

View File

@@ -0,0 +1,23 @@
require 'rails_helper'
describe Whatsapp::SendOnWhatsappService do
describe '#perform' do
context 'when a valid message' do
it 'calls channel.send_message' do
whatsapp_request = double
whatsapp_channel = create(:channel_whatsapp)
contact_inbox = create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: '123456789')
conversation = create(:conversation, contact_inbox: contact_inbox, inbox: whatsapp_channel.inbox)
message = create(:message, message_type: :outgoing, content: 'test',
conversation: conversation)
allow(HTTParty).to receive(:post).and_return(whatsapp_request)
expect(HTTParty).to receive(:post).with(
'https://waba.360dialog.io/v1/messages',
headers: { 'D360-API-KEY': 'test_key', 'Content-Type': 'application/json' },
body: { to: '123456789', text: { body: 'test' }, type: 'text' }.to_json
)
described_class.new(message: message).perform
end
end
end
end