Files
chatwoot/spec/services/whatsapp/webhook_teardown_service_spec.rb
7c7459b734 fix(whatsapp): override webhook at phone number level (#13817)
## Description

Move WhatsApp webhook callback override from WABA level to phone number
level, allowing multiple phone numbers on the same WABA to have
independent callback URLs.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

- Connect a WhatsApp Cloud inbox via embedded signup
- Verify webhook setup succeeds and messages are received
- Connect a second phone number on the same WABA — both should receive
messages independently
- Delete an inbox and verify only that phone number's override is
cleared

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes

---------

Co-authored-by: tds-1 <tds-1@users.noreply.github.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-06-25 14:49:09 +05:30

85 lines
2.6 KiB
Ruby

require 'rails_helper'
RSpec.describe Whatsapp::WebhookTeardownService do
describe '#perform' do
let(:channel) { create(:channel_whatsapp, validate_provider_config: false, sync_templates: false) }
let(:service) { described_class.new(channel) }
context 'when channel is whatsapp_cloud with embedded_signup' do
before do
# Stub webhook setup to prevent HTTP calls during channel update
allow(channel).to receive(:setup_webhooks).and_return(true)
channel.update!(
provider: 'whatsapp_cloud',
provider_config: {
'source' => 'embedded_signup',
'phone_number_id' => 'test_phone_id',
'api_key' => 'test_api_key'
}
)
end
it 'calls clear_phone_number_callback_override on Facebook API client' do
api_client = instance_double(Whatsapp::FacebookApiClient)
allow(Whatsapp::FacebookApiClient).to receive(:new).with('test_api_key').and_return(api_client)
allow(api_client).to receive(:clear_phone_number_callback_override).with('test_phone_id')
service.perform
expect(api_client).to have_received(:clear_phone_number_callback_override).with('test_phone_id')
end
it 'handles errors gracefully without raising' do
api_client = instance_double(Whatsapp::FacebookApiClient)
allow(Whatsapp::FacebookApiClient).to receive(:new).and_return(api_client)
allow(api_client).to receive(:clear_phone_number_callback_override).and_raise(StandardError, 'API Error')
expect { service.perform }.not_to raise_error
end
end
context 'when channel is not whatsapp_cloud' do
before do
channel.update!(provider: 'default')
end
it 'does not attempt to unsubscribe webhook' do
expect(Whatsapp::FacebookApiClient).not_to receive(:new)
service.perform
end
end
context 'when channel is whatsapp_cloud but not embedded_signup' do
before do
channel.update!(
provider: 'whatsapp_cloud',
provider_config: { 'source' => 'manual' }
)
end
it 'does not attempt to unsubscribe webhook' do
expect(Whatsapp::FacebookApiClient).not_to receive(:new)
service.perform
end
end
context 'when required config is missing' do
before do
channel.update!(
provider: 'whatsapp_cloud',
provider_config: { 'source' => 'embedded_signup' }
)
end
it 'does not attempt to unsubscribe webhook' do
expect(Whatsapp::FacebookApiClient).not_to receive(:new)
service.perform
end
end
end
end