40
spec/builders/contact_builder_spec.rb
Normal file
40
spec/builders/contact_builder_spec.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe ::ContactBuilder do
|
||||
let(:account) { create(:account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:contact) { create(:contact, account: account) }
|
||||
let(:existing_contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
|
||||
|
||||
describe '#perform' do
|
||||
it 'doesnot create contact if it already exist' do
|
||||
contact_inbox = described_class.new(
|
||||
source_id: existing_contact_inbox.source_id,
|
||||
inbox: inbox,
|
||||
contact_attributes: {
|
||||
name: 'Contact',
|
||||
phone_number: '+1234567890',
|
||||
email: 'testemail@example.com'
|
||||
}
|
||||
).perform
|
||||
|
||||
expect(contact_inbox.contact.id).to be(contact.id)
|
||||
end
|
||||
|
||||
it 'creates contact if contact doesnot exist' do
|
||||
contact_inbox = described_class.new(
|
||||
source_id: '123456',
|
||||
inbox: inbox,
|
||||
contact_attributes: {
|
||||
name: 'Contact',
|
||||
phone_number: '+1234567890',
|
||||
email: 'testemail@example.com'
|
||||
}
|
||||
).perform
|
||||
|
||||
expect(contact_inbox.contact.id).not_to eq(contact.id)
|
||||
expect(contact_inbox.contact.name).to eq('Contact')
|
||||
expect(contact_inbox.inbox_id).to eq(inbox.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,76 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe '/api/v1/accounts/{account.id}/channels/twilio_channel', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let(:twilio_client) { instance_double(::Twilio::REST::Client) }
|
||||
let(:message_double) { instance_double('message') }
|
||||
let(:twilio_webhook_setup_service) { instance_double(::Twilio::WebhookSetupService) }
|
||||
|
||||
before do
|
||||
allow(::Twilio::REST::Client).to receive(:new).and_return(twilio_client)
|
||||
allow(::Twilio::WebhookSetupService).to receive(:new).and_return(twilio_webhook_setup_service)
|
||||
allow(twilio_webhook_setup_service).to receive(:perform)
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/channels/twilio_channel' do
|
||||
let(:params) do
|
||||
{
|
||||
twilio_channel: {
|
||||
account_sid: 'sid',
|
||||
auth_token: 'token',
|
||||
phone_number: '+1234567890',
|
||||
name: 'SMS Channel'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
context 'when unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post api_v1_account_channels_twilio_channel_path(account), params: params
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when user is logged in' do
|
||||
context 'with user as administrator' do
|
||||
it 'creates inbox and returns inbox object' do
|
||||
allow(twilio_client).to receive(:messages).and_return(message_double)
|
||||
allow(message_double).to receive(:list).and_return([])
|
||||
|
||||
post api_v1_account_channels_twilio_channel_path(account),
|
||||
params: params,
|
||||
headers: admin.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['name']).to eq('SMS Channel')
|
||||
expect(json_response['phone_number']).to eq('+1234567890')
|
||||
end
|
||||
|
||||
it 'return error if Twilio tokens are incorrect' do
|
||||
allow(twilio_client).to receive(:messages).and_return(message_double)
|
||||
allow(message_double).to receive(:list).and_raise(Twilio::REST::TwilioError)
|
||||
|
||||
post api_v1_account_channels_twilio_channel_path(account),
|
||||
params: params,
|
||||
headers: admin.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with user as agent' do
|
||||
it 'returns unauthorized' do
|
||||
post api_v1_account_channels_twilio_channel_path(account),
|
||||
params: params,
|
||||
headers: agent.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
18
spec/controllers/twilio/callbacks_controller_spec.rb
Normal file
18
spec/controllers/twilio/callbacks_controller_spec.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Twilio::CallbacksController', type: :request do
|
||||
include Rails.application.routes.url_helpers
|
||||
let(:twilio_service) { instance_double(Twilio::IncomingMessageService) }
|
||||
|
||||
before do
|
||||
allow(::Twilio::IncomingMessageService).to receive(:new).and_return(twilio_service)
|
||||
allow(twilio_service).to receive(:perform)
|
||||
end
|
||||
|
||||
describe 'GET /twilio/callback' do
|
||||
it 'calls incoming message service' do
|
||||
post twilio_callback_index_url, params: {}
|
||||
expect(twilio_service).to have_received(:perform)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -21,7 +21,7 @@ RSpec.describe 'Twitter::CallbacksController', type: :request do
|
||||
end
|
||||
|
||||
describe 'GET /twitter/callback' do
|
||||
it 'renders the page correctly when called with website_token' do
|
||||
it 'returns correct response' do
|
||||
get twitter_callback_url
|
||||
account.reload
|
||||
expect(response).to redirect_to app_twitter_inbox_agents_url(account_id: account.id, inbox_id: account.inboxes.last.id)
|
||||
|
||||
9
spec/factories/channel/twilio_sms.rb
Normal file
9
spec/factories/channel/twilio_sms.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
FactoryBot.define do
|
||||
factory :channel_twilio_sms, class: 'Channel::TwilioSms' do
|
||||
auth_token { SecureRandom.uuid }
|
||||
account_sid { SecureRandom.uuid }
|
||||
sequence(:phone_number) { |n| "+123456789#{n}1" }
|
||||
inbox
|
||||
account
|
||||
end
|
||||
end
|
||||
39
spec/services/twilio/incoming_message_service_spec.rb
Normal file
39
spec/services/twilio/incoming_message_service_spec.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Twilio::IncomingMessageService do
|
||||
let!(:account) { create(:account) }
|
||||
let!(:twilio_sms) do
|
||||
create(:channel_twilio_sms, account: account, phone_number: '+1234567890', account_sid: 'ACxxx')
|
||||
end
|
||||
let!(:contact) { create(:contact, account: account, phone_number: '+12345') }
|
||||
let(:contact_inbox) { create(:contact_inbox, source_id: '+12345', contact: contact, inbox: twilio_sms.inbox) }
|
||||
let!(:conversation) { create(:conversation, contact: contact, inbox: twilio_sms.inbox, contact_inbox: contact_inbox) }
|
||||
|
||||
describe '#perform' do
|
||||
it 'creates a new message in existing conversation' do
|
||||
params = {
|
||||
SmsSid: 'SMxx',
|
||||
From: '+12345',
|
||||
AccountSid: 'ACxxx',
|
||||
To: '+1234567890',
|
||||
Body: 'testing3'
|
||||
}
|
||||
|
||||
described_class.new(params: params).perform
|
||||
expect(conversation.reload.messages.last.content).to eq('testing3')
|
||||
end
|
||||
|
||||
it 'creates a new conversation' do
|
||||
params = {
|
||||
SmsSid: 'SMxx',
|
||||
From: '+123456',
|
||||
AccountSid: 'ACxxx',
|
||||
To: '+1234567890',
|
||||
Body: 'new conversation'
|
||||
}
|
||||
|
||||
described_class.new(params: params).perform
|
||||
expect(Conversation.count).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
59
spec/services/twilio/outgoing_message_service_spec.rb
Normal file
59
spec/services/twilio/outgoing_message_service_spec.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Twilio::OutgoingMessageService do
|
||||
subject(:outgoing_message_service) { described_class.new(message: message) }
|
||||
|
||||
let(:twilio_client) { instance_double(::Twilio::REST::Client) }
|
||||
let(:messages_double) { instance_double('messages') }
|
||||
let(:message_record_double) { instance_double('message_record_double') }
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:widget_inbox) { create(:inbox, account: account) }
|
||||
let!(:twilio_sms) { create(:channel_twilio_sms, account: account) }
|
||||
let!(:twilio_inbox) { create(:inbox, channel: twilio_sms, account: account) }
|
||||
let!(:contact) { create(:contact, account: account) }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: twilio_inbox) }
|
||||
let(:conversation) { create(:conversation, contact: contact, inbox: twilio_inbox, contact_inbox: contact_inbox) }
|
||||
|
||||
before do
|
||||
allow(::Twilio::REST::Client).to receive(:new).and_return(twilio_client)
|
||||
allow(twilio_client).to receive(:messages).and_return(messages_double)
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'without reply' do
|
||||
it 'if message is private' do
|
||||
create(:message, message_type: 'outgoing', private: true, inbox: twilio_inbox, account: account)
|
||||
expect(twilio_client).not_to have_received(:messages)
|
||||
end
|
||||
|
||||
it 'if inbox channel is not facebook page' do
|
||||
create(:message, message_type: 'outgoing', inbox: widget_inbox, account: account)
|
||||
expect(twilio_client).not_to have_received(:messages)
|
||||
end
|
||||
|
||||
it 'if message is not outgoing' do
|
||||
create(:message, message_type: 'incoming', inbox: twilio_inbox, account: account)
|
||||
expect(twilio_client).not_to have_received(:messages)
|
||||
end
|
||||
|
||||
it 'if message has an source id' do
|
||||
create(:message, message_type: 'outgoing', inbox: twilio_inbox, account: account, source_id: SecureRandom.uuid)
|
||||
expect(twilio_client).not_to have_received(:messages)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with reply' do
|
||||
it 'if message is sent from chatwoot and is outgoing' do
|
||||
allow(messages_double).to receive(:create).and_return(message_record_double)
|
||||
allow(message_record_double).to receive(:sid).and_return('1234')
|
||||
|
||||
outgoing_message = create(
|
||||
:message, message_type: 'outgoing', inbox: twilio_inbox, account: account, conversation: conversation
|
||||
)
|
||||
|
||||
expect(outgoing_message.reload.source_id).to eq('1234')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
48
spec/services/twilio/webhook_setup_service_spec.rb
Normal file
48
spec/services/twilio/webhook_setup_service_spec.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Twilio::WebhookSetupService do
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
let(:channel_twilio_sms) { create(:channel_twilio_sms) }
|
||||
let(:twilio_client) { instance_double(::Twilio::REST::Client) }
|
||||
let(:phone_double) { instance_double('phone_double') }
|
||||
let(:phone_record_double) { instance_double('phone_record_double') }
|
||||
|
||||
before do
|
||||
allow(::Twilio::REST::Client).to receive(:new).and_return(twilio_client)
|
||||
allow(phone_double).to receive(:update)
|
||||
allow(phone_record_double).to receive(:sid).and_return('1234')
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
it 'logs error if phone_number is not found' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_return([])
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
|
||||
expect(phone_double).not_to have_received(:update)
|
||||
end
|
||||
|
||||
it 'update webhook_url if phone_number is found' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_return([phone_record_double])
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
|
||||
expect(phone_double).to have_received(:update).with(
|
||||
sms_method: 'POST',
|
||||
sms_url: twilio_callback_index_url
|
||||
)
|
||||
end
|
||||
|
||||
it 'doesnot call update if TwilioError is thrown' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_raise(Twilio::REST::TwilioError)
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
|
||||
expect(phone_double).not_to have_received(:update)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user