@@ -3,6 +3,27 @@ FactoryBot.define do
|
||||
sequence(:phone_number) { |n| "+123456789#{n}1" }
|
||||
account
|
||||
provider_config { { 'api_key' => 'test_key' } }
|
||||
message_templates do
|
||||
[{ 'name' => 'sample_shipping_confirmation',
|
||||
'status' => 'approved',
|
||||
'category' => 'SHIPPING_UPDATE',
|
||||
'language' => 'id',
|
||||
'namespace' => '2342384942_32423423_23423fdsdaf',
|
||||
'components' =>
|
||||
[{ 'text' => 'Paket Anda sudah dikirim. Paket akan sampai dalam {{1}} hari kerja.', 'type' => 'BODY' },
|
||||
{ 'text' => 'Pesan ini berasal dari bisnis yang tidak terverifikasi.', 'type' => 'FOOTER' }],
|
||||
'rejected_reason' => 'NONE' },
|
||||
{ 'name' => 'sample_shipping_confirmation',
|
||||
'status' => 'approved',
|
||||
'category' => 'SHIPPING_UPDATE',
|
||||
'language' => 'en_US',
|
||||
'namespace' => '23423423_2342423_324234234_2343224',
|
||||
'components' =>
|
||||
[{ 'text' => 'Your package has been shipped. It will be delivered in {{1}} business days.', 'type' => 'BODY' },
|
||||
{ 'text' => 'This message is from an unverified business.', 'type' => 'FOOTER' }],
|
||||
'rejected_reason' => 'NONE' }]
|
||||
end
|
||||
message_templates_last_updated { Time.now.utc }
|
||||
|
||||
after(:create) do |channel_whatsapp|
|
||||
create(:inbox, channel: channel_whatsapp, account: channel_whatsapp.account)
|
||||
|
||||
@@ -7,20 +7,54 @@ describe Whatsapp::SendOnWhatsappService do
|
||||
end
|
||||
|
||||
context 'when a valid message' do
|
||||
it 'calls channel.send_message' do
|
||||
it 'calls channel.send_message when with in 24 hour limit' 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)
|
||||
# to handle the case of 24 hour window limit.
|
||||
create(:message, message_type: :incoming, content: 'test',
|
||||
conversation: conversation)
|
||||
message = create(:message, message_type: :outgoing, content: 'test',
|
||||
conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(whatsapp_request)
|
||||
allow(whatsapp_request).to receive(:success?).and_return(true)
|
||||
allow(whatsapp_request).to receive(:[]).with('messages').and_return([{ 'id' => '123456789' }])
|
||||
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
|
||||
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
|
||||
expect(message.reload.source_id).to eq('123456789')
|
||||
end
|
||||
|
||||
it 'calls channel.send_template when after 24 hour limit' 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: 'Your package has been shipped. It will be delivered in 3 business days.',
|
||||
conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(whatsapp_request)
|
||||
allow(whatsapp_request).to receive(:success?).and_return(true)
|
||||
allow(whatsapp_request).to receive(:[]).with('messages').and_return([{ 'id' => '123456789' }])
|
||||
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',
|
||||
template: {
|
||||
name: 'sample_shipping_confirmation',
|
||||
namespace: '23423423_2342423_324234234_2343224',
|
||||
language: { 'policy': 'deterministic', 'code': 'en_US' },
|
||||
components: [{ 'type': 'body', 'parameters': [{ 'type': 'text', 'text': '3' }] }]
|
||||
},
|
||||
type: 'template'
|
||||
}.to_json
|
||||
)
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.source_id).to eq('123456789')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user