@@ -15,8 +15,8 @@ describe Contacts::ContactableInboxesService do
|
||||
let!(:email_inbox) { create(:inbox, channel: email_channel, account: account) }
|
||||
let!(:api_channel) { create(:channel_api, account: account) }
|
||||
let!(:api_inbox) { create(:inbox, channel: api_channel, account: account) }
|
||||
let!(:website_channel) { create(:channel_widget, account: account) }
|
||||
let!(:website_inbox) { create(:inbox, channel: website_channel, account: account) }
|
||||
let!(:website_inbox) { create(:inbox, channel: create(:channel_widget, account: account), account: account) }
|
||||
let!(:sms_inbox) { create(:inbox, channel: create(:channel_sms, account: account), account: account) }
|
||||
|
||||
describe '#get' do
|
||||
it 'returns the contactable inboxes for the contact' do
|
||||
@@ -25,7 +25,7 @@ describe Contacts::ContactableInboxesService do
|
||||
expect(contactable_inboxes).to include({ source_id: contact.phone_number, inbox: twilio_sms_inbox })
|
||||
expect(contactable_inboxes).to include({ source_id: "whatsapp:#{contact.phone_number}", inbox: twilio_whatsapp_inbox })
|
||||
expect(contactable_inboxes).to include({ source_id: contact.email, inbox: email_inbox })
|
||||
expect(contactable_inboxes.pluck(:inbox)).to include(api_inbox)
|
||||
expect(contactable_inboxes).to include({ source_id: contact.phone_number, inbox: sms_inbox })
|
||||
end
|
||||
|
||||
it 'doest not return the non contactable inboxes for the contact' do
|
||||
|
||||
31
spec/services/sms/incoming_message_service_spec.rb
Normal file
31
spec/services/sms/incoming_message_service_spec.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Sms::IncomingMessageService do
|
||||
describe '#perform' do
|
||||
let!(:sms_channel) { create(:channel_sms) }
|
||||
|
||||
context 'when valid text message params' do
|
||||
it 'creates appropriate conversations, message and contacts' do
|
||||
params = {
|
||||
|
||||
'id': '3232420-2323-234324',
|
||||
'owner': sms_channel.phone_number,
|
||||
'applicationId': '2342349-324234d-32432432',
|
||||
'time': '2022-02-02T23:14:05.262Z',
|
||||
'segmentCount': 1,
|
||||
'direction': 'in',
|
||||
'to': [
|
||||
sms_channel.phone_number
|
||||
],
|
||||
'from': '+14234234234',
|
||||
'text': 'test message'
|
||||
|
||||
}.with_indifferent_access
|
||||
described_class.new(inbox: sms_channel.inbox, params: params).perform
|
||||
expect(sms_channel.inbox.conversations.count).not_to eq(0)
|
||||
expect(Contact.all.first.name).to eq('+1 423-423-4234')
|
||||
expect(sms_channel.inbox.messages.first.content).to eq('test message')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
47
spec/services/sms/oneoff_sms_campaign_service_spec.rb
Normal file
47
spec/services/sms/oneoff_sms_campaign_service_spec.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Sms::OneoffSmsCampaignService do
|
||||
subject(:sms_campaign_service) { described_class.new(campaign: campaign) }
|
||||
|
||||
let(:account) { create(:account) }
|
||||
let!(:sms_channel) { create(:channel_sms) }
|
||||
let!(:sms_inbox) { create(:inbox, channel: sms_channel) }
|
||||
let(:label1) { create(:label, account: account) }
|
||||
let(:label2) { create(:label, account: account) }
|
||||
let!(:campaign) do
|
||||
create(:campaign, inbox: sms_inbox, account: account,
|
||||
audience: [{ type: 'Label', id: label1.id }, { type: 'Label', id: label2.id }])
|
||||
end
|
||||
|
||||
describe 'perform' do
|
||||
before do
|
||||
stub_request(:post, 'https://messaging.bandwidth.com/api/v2/users/1/messages').to_return(
|
||||
status: 200,
|
||||
body: { 'id' => '1' }.to_json,
|
||||
headers: {}
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises error if the campaign is completed' do
|
||||
campaign.completed!
|
||||
|
||||
expect { sms_campaign_service.perform }.to raise_error 'Completed Campaign'
|
||||
end
|
||||
|
||||
it 'raises error invalid campaign when its not a oneoff sms campaign' do
|
||||
campaign = create(:campaign)
|
||||
|
||||
expect { described_class.new(campaign: campaign).perform }.to raise_error "Invalid campaign #{campaign.id}"
|
||||
end
|
||||
|
||||
it 'send messages to contacts in the audience and marks the campaign completed' do
|
||||
contact_with_label1, contact_with_label2, contact_with_both_labels = FactoryBot.create_list(:contact, 3, :with_phone_number, account: account)
|
||||
contact_with_label1.update_labels([label1.title])
|
||||
contact_with_label2.update_labels([label2.title])
|
||||
contact_with_both_labels.update_labels([label1.title, label2.title])
|
||||
sms_campaign_service.perform
|
||||
assert_requested(:post, 'https://messaging.bandwidth.com/api/v2/users/1/messages', times: 3)
|
||||
expect(campaign.reload.completed?).to eq true
|
||||
end
|
||||
end
|
||||
end
|
||||
28
spec/services/sms/send_on_sms_service_spec.rb
Normal file
28
spec/services/sms/send_on_sms_service_spec.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Sms::SendOnSmsService do
|
||||
describe '#perform' do
|
||||
context 'when a valid message' do
|
||||
let(:sms_request) { double }
|
||||
let!(:sms_channel) { create(:channel_sms) }
|
||||
let!(:contact_inbox) { create(:contact_inbox, inbox: sms_channel.inbox, source_id: '+123456789') }
|
||||
let!(:conversation) { create(:conversation, contact_inbox: contact_inbox, inbox: sms_channel.inbox) }
|
||||
|
||||
it 'calls channel.send_message' do
|
||||
message = create(:message, message_type: :outgoing, content: 'test',
|
||||
conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(sms_request)
|
||||
allow(sms_request).to receive(:success?).and_return(true)
|
||||
allow(sms_request).to receive(:parsed_response).and_return({ 'id' => '123456789' })
|
||||
expect(HTTParty).to receive(:post).with(
|
||||
'https://messaging.bandwidth.com/api/v2/users/1/messages',
|
||||
basic_auth: { username: '1', password: '1' },
|
||||
headers: { 'Content-Type' => 'application/json' },
|
||||
body: { 'to' => '+123456789', 'from' => sms_channel.phone_number, 'text' => 'test', 'applicationId' => '1' }.to_json
|
||||
)
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.source_id).to eq('123456789')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user