chore: one off SMS campaign APIs (#2589)

This commit is contained in:
Sojan Jose
2021-07-14 12:24:09 +05:30
committed by GitHub
parent cb44eb2964
commit dfddf9cacc
15 changed files with 337 additions and 23 deletions
@@ -106,6 +106,28 @@ RSpec.describe 'Campaigns API', type: :request do
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body, symbolize_names: true)[:title]).to eq('test')
end
it 'creates a new oneoff campaign' do
twilio_sms = create(:channel_twilio_sms, account: account)
twilio_inbox = create(:inbox, channel: twilio_sms)
label1 = create(:label, account: account)
label2 = create(:label, account: account)
post "/api/v1/accounts/#{account.id}/campaigns",
params: {
inbox_id: twilio_inbox.id, title: 'test', message: 'test message',
scheduled_at: 2.days.from_now,
audience: [{ type: 'Label', id: label1.id }, { type: 'Label', id: label2.id }]
},
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)
expect(response_data[:campaign_type]).to eq('one_off')
expect(response_data[:scheduled_at].present?).to eq true
expect(response_data[:audience].pluck(:id)).to include(label1.id, label2.id)
end
end
end
@@ -0,0 +1,23 @@
require 'rails_helper'
RSpec.describe Campaigns::TriggerOneoffCampaignJob, type: :job do
let(:account) { create(:account) }
let!(:twilio_sms) { create(:channel_twilio_sms) }
let!(:twilio_inbox) { create(:inbox, channel: twilio_sms) }
let(:label1) { create(:label, account: account) }
let(:label2) { create(:label, account: account) }
let!(:campaign) { create(:campaign, inbox: twilio_inbox, audience: [{ type: 'Label', id: label1.id }, { type: 'Label', id: label2.id }]) }
it 'enqueues the job' do
expect { described_class.perform_later(campaign) }.to have_enqueued_job(described_class)
.on_queue('low')
end
context 'when called with a campaign' do
it 'triggers the campaign' do
expect(campaign).to receive(:trigger!)
described_class.perform_now(campaign)
end
end
end
@@ -0,0 +1,24 @@
require 'rails_helper'
RSpec.describe TriggerScheduledItemsJob, type: :job do
subject(:job) { described_class.perform_later }
let(:account) { create(:account) }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.on_queue('scheduled_jobs')
end
context 'when unexecuted Scheduled campaign jobs' do
let!(:twilio_sms) { create(:channel_twilio_sms) }
let!(:twilio_inbox) { create(:inbox, channel: twilio_sms) }
it 'triggers Campaigns::TriggerOneoffCampaignJob' do
campaign = create(:campaign, inbox: twilio_inbox)
create(:campaign, inbox: twilio_inbox, scheduled_at: 10.days.after)
expect(Campaigns::TriggerOneoffCampaignJob).to receive(:perform_later).with(campaign).once
described_class.perform_now
end
end
end
+64
View File
@@ -20,4 +20,68 @@ RSpec.describe Campaign, type: :model do
expect(campaign.display_id).to eq(1)
end
end
context 'when Inbox other then Website or Twilio SMS' do
let!(:facebook_channel) { create(:channel_facebook_page) }
let!(:facebook_inbox) { create(:inbox, channel: facebook_channel) }
let(:campaign) { build(:campaign, inbox: facebook_inbox) }
it 'would not save the campaigns' do
expect(campaign.save).to eq false
expect(campaign.errors.full_messages.first).to eq 'Inbox Unsupported Inbox type'
end
end
context 'when a campaign is completed' do
let!(:campaign) { create(:campaign, campaign_status: :completed) }
it 'would prevent further updates' do
campaign.title = 'new name'
expect(campaign.save).to eq false
expect(campaign.errors.full_messages.first).to eq 'Status The campaign is already completed'
end
it 'can be deleted' do
campaign.destroy!
expect(described_class.exists?(campaign.id)).to eq false
end
it 'cant be triggered' do
expect(Twilio::OneoffSmsCampaignService).not_to receive(:new).with(campaign: campaign)
expect(campaign.trigger!).to eq nil
end
end
describe 'ensure_correct_campaign_attributes' do
context 'when Twilio SMS campaign' do
let!(:twilio_sms) { create(:channel_twilio_sms) }
let!(:twilio_inbox) { create(:inbox, channel: twilio_sms) }
let(:campaign) { build(:campaign, inbox: twilio_inbox) }
it 'only saves campaign type as oneoff and wont leave scheduled_at empty' do
campaign.campaign_type = 'ongoing'
campaign.save!
expect(campaign.reload.campaign_type).to eq 'one_off'
expect(campaign.scheduled_at.present?).to eq true
end
it 'calls twilio service on trigger!' do
sms_service = double
expect(Twilio::OneoffSmsCampaignService).to receive(:new).with(campaign: campaign).and_return(sms_service)
expect(sms_service).to receive(:perform)
campaign.save!
campaign.trigger!
end
end
context 'when Website campaign' do
let(:campaign) { build(:campaign) }
it 'only saves campaign type as ongoing' do
campaign.campaign_type = 'one_off'
campaign.save!
expect(campaign.reload.campaign_type).to eq 'ongoing'
end
end
end
end
@@ -0,0 +1,52 @@
require 'rails_helper'
describe Twilio::OneoffSmsCampaignService do
subject(:sms_campaign_service) { described_class.new(campaign: campaign) }
let(:account) { create(:account) }
let!(:twilio_sms) { create(:channel_twilio_sms) }
let!(:twilio_inbox) { create(:inbox, channel: twilio_sms) }
let(:label1) { create(:label, account: account) }
let(:label2) { create(:label, account: account) }
let!(:campaign) do
create(:campaign, inbox: twilio_inbox, account: account,
audience: [{ type: 'Label', id: label1.id }, { type: 'Label', id: label2.id }])
end
let(:twilio_client) { double }
let(:twilio_messages) { double }
describe 'perform' do
before do
allow(::Twilio::REST::Client).to receive(:new).and_return(twilio_client)
allow(twilio_client).to receive(:messages).and_return(twilio_messages)
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, 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])
expect(twilio_messages).to receive(:create).with(body: campaign.message,
from: twilio_sms.phone_number, to: contact_with_label1.phone_number).once
expect(twilio_messages).to receive(:create).with(body: campaign.message,
from: twilio_sms.phone_number, to: contact_with_label2.phone_number).once
expect(twilio_messages).to receive(:create).with(body: campaign.message,
from: twilio_sms.phone_number, to: contact_with_both_labels.phone_number).once
sms_campaign_service.perform
expect(campaign.reload.completed?).to eq true
end
end
end