fix: Check valid params exists in WhatsAapp payload (#6780)

Fixes #6779

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Muhsin Keloth
2023-03-30 12:10:59 +05:30
committed by GitHub
parent 7e3a4d2c20
commit cef44bc557
3 changed files with 44 additions and 32 deletions

View File

@@ -9,9 +9,9 @@ class Whatsapp::IncomingMessageBaseService
def perform
processed_params
if processed_params[:statuses].present?
if processed_params.try(:[], :statuses).present?
process_statuses
elsif processed_params[:messages].present?
elsif processed_params.try(:[], :messages).present?
process_messages
end
end

View File

@@ -5,7 +5,7 @@ class Whatsapp::IncomingMessageWhatsappCloudService < Whatsapp::IncomingMessageB
private
def processed_params
@processed_params ||= params[:entry].first['changes'].first['value']
@processed_params ||= params[:entry].try(:first).try(:[], 'changes').try(:first).try(:[], 'value')
end
def download_attachment_file(attachment_payload)

View File

@@ -27,35 +27,6 @@ describe Whatsapp::IncomingMessageWhatsappCloudService do
}.with_indifferent_access
end
let(:error_params) do
{
phone_number: whatsapp_channel.phone_number,
object: 'whatsapp_business_account',
entry: [{
changes: [{
value: {
contacts: [{ profile: { name: 'Sojan Jose' }, wa_id: '2423423243' }],
messages: [{
from: '2423423243',
image: {
id: 'b1c68f38-8734-4ad3-b4a1-ef0c10d683',
mime_type: 'image/jpeg',
sha256: '29ed500fa64eb55fc19dc4124acb300e5dcca0f822a301ae99944db',
caption: 'Check out my product!'
},
errors: [{
code: 400,
details: 'Last error was: ServerThrottle. Http request error: HTTP response code said error. See logs for details',
title: 'Media download failed: Not retrying as download is not retriable at this time'
}],
timestamp: '1664799904', type: 'image'
}]
}
}]
}]
}.with_indifferent_access
end
context 'when valid attachment message params' do
it 'creates appropriate conversations, message and contacts' do
stub_request(:get, whatsapp_channel.media_url('b1c68f38-8734-4ad3-b4a1-ef0c10d683')).to_return(
@@ -94,6 +65,37 @@ describe Whatsapp::IncomingMessageWhatsappCloudService do
expect(whatsapp_channel.inbox.messages.first.attachments.present?).to be false
expect(whatsapp_channel.authorization_error_count).to eq(1)
end
end
context 'when invalid attachment message params' do
let(:error_params) do
{
phone_number: whatsapp_channel.phone_number,
object: 'whatsapp_business_account',
entry: [{
changes: [{
value: {
contacts: [{ profile: { name: 'Sojan Jose' }, wa_id: '2423423243' }],
messages: [{
from: '2423423243',
image: {
id: 'b1c68f38-8734-4ad3-b4a1-ef0c10d683',
mime_type: 'image/jpeg',
sha256: '29ed500fa64eb55fc19dc4124acb300e5dcca0f822a301ae99944db',
caption: 'Check out my product!'
},
errors: [{
code: 400,
details: 'Last error was: ServerThrottle. Http request error: HTTP response code said error. See logs for details',
title: 'Media download failed: Not retrying as download is not retriable at this time'
}],
timestamp: '1664799904', type: 'image'
}]
}
}]
}]
}.with_indifferent_access
end
it 'with attachment errors' do
described_class.new(inbox: whatsapp_channel.inbox, params: error_params).perform
@@ -102,5 +104,15 @@ describe Whatsapp::IncomingMessageWhatsappCloudService do
expect(whatsapp_channel.inbox.messages.count).to eq(0)
end
end
context 'when invalid params' do
it 'will not throw error' do
described_class.new(inbox: whatsapp_channel.inbox, params: { phone_number: whatsapp_channel.phone_number,
object: 'whatsapp_business_account', entry: {} }).perform
expect(whatsapp_channel.inbox.conversations.count).to eq(0)
expect(Contact.all.first).to be_nil
expect(whatsapp_channel.inbox.messages.count).to eq(0)
end
end
end
end