diff --git a/app/javascript/dashboard/components-next/message/MessageError.vue b/app/javascript/dashboard/components-next/message/MessageError.vue
index e113ada71..d3a03e746 100644
--- a/app/javascript/dashboard/components-next/message/MessageError.vue
+++ b/app/javascript/dashboard/components-next/message/MessageError.vue
@@ -26,7 +26,7 @@ const { t } = useI18n();
/>
true,
+ :body => { message_id: 'anyrandommessageid1234567890' }.to_json,
+ :parsed_response => { 'message_id' => 'anyrandommessageid1234567890' }
+ )
+ end
+
+ let(:error_body) do
+ {
+ 'error' => {
+ 'message' => 'The Instagram account is restricted.',
+ 'type' => 'OAuthException',
+ 'code' => 400,
+ 'fbtrace_id' => 'anyrandomfbtraceid1234567890'
+ }
+ }
+ end
+
+ let(:error_response) do
+ instance_double(
+ HTTParty::Response,
+ :success? => false,
+ :body => error_body.to_json,
+ :parsed_response => error_body
+ )
+ end
+
+ let(:response_with_error) do
+ instance_double(
+ HTTParty::Response,
+ :success? => true,
+ :body => error_body.to_json,
+ :parsed_response => error_body
+ )
+ end
describe '#perform' do
context 'with reply' do
before do
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
- allow(HTTParty).to receive(:post).and_return(
- {
- 'message_id': 'anyrandommessageid1234567890'
- }
- )
+ allow(HTTParty).to receive(:post).and_return(mock_response)
end
context 'without message_tag HUMAN_AGENT' do
@@ -35,22 +68,8 @@ describe Instagram::SendOnInstagramService do
it 'if message is sent from chatwoot and is outgoing' do
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
- allow(HTTParty).to receive(:post).with(
- {
- recipient: { id: contact.get_source_id(instagram_inbox.id) },
- message: {
- text: message.content
- }
- }
- ).and_return(
- {
- 'message_id': 'anyrandommessageid1234567890'
- }
- )
-
response = described_class.new(message: message).perform
-
- expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
+ expect(response['message_id']).to eq('anyrandommessageid1234567890')
end
it 'if message is sent from chatwoot and is outgoing with multiple attachments' do
@@ -78,22 +97,13 @@ describe Instagram::SendOnInstagramService do
message.save!
response = described_class.new(message: message).perform
- expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
+ expect(response['message_id']).to eq('anyrandommessageid1234567890')
end
it 'if message sent from chatwoot is failed' do
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
- allow(HTTParty).to receive(:post).and_return(
- {
- 'error': {
- 'message': 'The Instagram account is restricted.',
- 'type': 'OAuthException',
- 'code': 400,
- 'fbtrace_id': 'anyrandomfbtraceid1234567890'
- }
- }
- )
+ allow(HTTParty).to receive(:post).and_return(response_with_error)
described_class.new(message: message).perform
expect(HTTParty).to have_received(:post)
expect(message.reload.status).to eq('failed')
@@ -129,5 +139,39 @@ describe Instagram::SendOnInstagramService do
end
end
end
+
+ context 'when handling errors' do
+ before do
+ allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
+ end
+
+ it 'handles HTTP errors' do
+ message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
+ allow(HTTParty).to receive(:post).and_return(error_response)
+
+ described_class.new(message: message).perform
+
+ expect(message.reload.status).to eq('failed')
+ expect(message.reload.external_error).to eq('400 - The Instagram account is restricted.')
+ end
+
+ it 'handles response errors' do
+ message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
+
+ error_response = instance_double(
+ HTTParty::Response,
+ success?: true,
+ body: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }.to_json,
+ parsed_response: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }
+ )
+
+ allow(HTTParty).to receive(:post).and_return(error_response)
+
+ described_class.new(message: message).perform
+
+ expect(message.reload.status).to eq('failed')
+ expect(message.reload.external_error).to eq('100 - Invalid message format')
+ end
+ end
end
end
diff --git a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb
index 7c2b79b8d..134d69fee 100644
--- a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb
+++ b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb
@@ -263,4 +263,56 @@ describe Whatsapp::Providers::WhatsappCloudService do
end
end
end
+
+ describe '#handle_error' do
+ let(:error_message) { 'Invalid message format' }
+ let(:error_response) do
+ {
+ 'error' => {
+ 'message' => error_message,
+ 'code' => 100
+ }
+ }
+ end
+
+ let(:error_response_object) do
+ instance_double(
+ HTTParty::Response,
+ body: error_response.to_json,
+ parsed_response: error_response
+ )
+ end
+
+ before do
+ allow(Rails.logger).to receive(:error)
+ end
+
+ context 'when there is a message' do
+ it 'logs error and updates message status' do
+ service.instance_variable_set(:@message, message)
+ service.send(:handle_error, error_response_object)
+
+ expect(message.reload.status).to eq('failed')
+ expect(message.reload.external_error).to eq(error_message)
+ end
+ end
+
+ context 'when error message is blank' do
+ let(:error_response_object) do
+ instance_double(
+ HTTParty::Response,
+ body: '{}',
+ parsed_response: {}
+ )
+ end
+
+ it 'logs error but does not update message' do
+ service.instance_variable_set(:@message, message)
+ service.send(:handle_error, error_response_object)
+
+ expect(message.reload.status).not_to eq('failed')
+ expect(message.reload.external_error).to be_nil
+ end
+ end
+ end
end
diff --git a/spec/services/whatsapp/send_on_whatsapp_service_spec.rb b/spec/services/whatsapp/send_on_whatsapp_service_spec.rb
index 0df9316f5..88f3f7740 100644
--- a/spec/services/whatsapp/send_on_whatsapp_service_spec.rb
+++ b/spec/services/whatsapp/send_on_whatsapp_service_spec.rb
@@ -12,13 +12,30 @@ describe Whatsapp::SendOnWhatsappService do
describe '#perform' do
before do
stub_request(:post, 'https://waba.360dialog.io/v1/configs/webhook')
+ stub_request(:post, 'https://waba.360dialog.io/v1/messages')
end
context 'when a valid message' do
- let(:whatsapp_request) { double }
+ let(:whatsapp_request) { instance_double(HTTParty::Response) }
let!(:whatsapp_channel) { create(:channel_whatsapp, sync_templates: false) }
let!(:contact_inbox) { create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: '123456789') }
let!(:conversation) { create(:conversation, contact_inbox: contact_inbox, inbox: whatsapp_channel.inbox) }
+ let(:api_key) { 'test_key' }
+ let(:headers) { { 'D360-API-KEY' => api_key, 'Content-Type' => 'application/json' } }
+ let(:template_body) do
+ {
+ 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'
+ }
+ end
+
+ let(:success_response) { { 'messages' => [{ 'id' => '123456789' }] }.to_json }
it 'calls channel.send_message when with in 24 hour limit' do
# to handle the case of 24 hour window limit.
@@ -26,14 +43,14 @@ describe Whatsapp::SendOnWhatsappService do
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
- )
+
+ stub_request(:post, 'https://waba.360dialog.io/v1/messages')
+ .with(
+ headers: headers,
+ body: { 'to' => '123456789', 'text' => { 'body' => 'test' }, 'type' => 'text' }.to_json
+ )
+ .to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
+
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
@@ -41,23 +58,13 @@ describe Whatsapp::SendOnWhatsappService do
it 'calls channel.send_template when after 24 hour limit' do
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
- )
+
+ stub_request(:post, 'https://waba.360dialog.io/v1/messages')
+ .with(
+ headers: headers,
+ body: template_body.to_json
+ ).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
+
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
@@ -65,23 +72,12 @@ describe Whatsapp::SendOnWhatsappService do
it 'calls channel.send_template if template_params are present' do
message = create(:message, additional_attributes: { template_params: template_params },
content: 'Your package will be delivered in 3 business days.', conversation: conversation, message_type: :outgoing)
- 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
- )
+ stub_request(:post, 'https://waba.360dialog.io/v1/messages')
+ .with(
+ headers: headers,
+ body: template_body.to_json
+ ).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
+
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end
@@ -93,23 +89,22 @@ describe Whatsapp::SendOnWhatsappService do
content: 'عميلنا العزيز الرجاء الرد على هذه الرسالة بكلمة *نعم* للرد على إستفساركم من قبل خدمة العملاء.',
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: 'customer_yes_no',
- namespace: '2342384942_32423423_23423fdsdaf23',
- language: { 'policy': 'deterministic', 'code': 'ar' },
- components: [{ 'type': 'body', 'parameters': [] }]
- },
- type: 'template'
- }.to_json
- )
+
+ stub_request(:post, 'https://waba.360dialog.io/v1/messages')
+ .with(
+ headers: headers,
+ body: {
+ to: '123456789',
+ template: {
+ name: 'customer_yes_no',
+ namespace: '2342384942_32423423_23423fdsdaf23',
+ language: { 'policy': 'deterministic', 'code': 'ar' },
+ components: [{ 'type': 'body', 'parameters': [] }]
+ },
+ type: 'template'
+ }.to_json
+ ).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
+
described_class.new(message: message).perform
expect(message.reload.source_id).to eq('123456789')
end