feat: Add inbox webhook events (#8006)

- Add webhook events for inbox creation/updation.
- Right now, the feature is added under a feature_flag. It is not available by default on all installations.
This commit is contained in:
Pranav Raj S
2023-09-28 15:28:10 -07:00
committed by GitHub
parent 12a64f1b10
commit fd633e1613
8 changed files with 165 additions and 1 deletions

View File

@@ -217,4 +217,64 @@ describe WebhookListener do
end
end
end
describe '#inbox_created' do
let(:event_name) { :'inbox.created' }
let!(:inbox_created_event) { Events::Base.new(event_name, Time.zone.now, inbox: inbox) }
context 'when webhook is not configured' do
it 'does not trigger webhook' do
expect(WebhookJob).to receive(:perform_later).exactly(0).times
listener.inbox_created(inbox_created_event)
end
end
context 'when webhook is configured' do
it 'triggers webhook' do
inbox_data = Inbox::EventDataPresenter.new(inbox).push_data
webhook = create(:webhook, account: account, subscriptions: ['inbox_created'])
expect(WebhookJob).to receive(:perform_later).with(webhook.url, inbox_data.merge(event: 'inbox_created')).once
listener.inbox_created(inbox_created_event)
end
end
end
describe '#inbox_updated' do
let(:event_name) { :'inbox.updated' }
let!(:inbox_updated_event) { Events::Base.new(event_name, Time.zone.now, inbox: inbox, changed_attributes: changed_attributes) }
let(:changed_attributes) { {} }
context 'when webhook is not configured' do
it 'does not trigger webhook' do
expect(WebhookJob).to receive(:perform_later).exactly(0).times
listener.inbox_updated(inbox_updated_event)
end
end
context 'when webhook is configured and there are no changed attributes' do
it 'triggers webhook' do
create(:webhook, account: account, subscriptions: ['inbox_updated'])
expect(WebhookJob).to receive(:perform_later).exactly(0).times
listener.inbox_updated(inbox_updated_event)
end
end
context 'when webhook is configured' do
let(:changed_attributes) { { 'name' => ['Inbox 1', inbox.name] } }
it 'triggers webhook' do
webhook = create(:webhook, account: account, subscriptions: ['inbox_updated'])
inbox_data = Inbox::EventDataPresenter.new(inbox).push_data
changed_attributes_data = [{ 'name' => { 'previous_value': 'Inbox 1', 'current_value': inbox.name } }]
expect(WebhookJob).to receive(:perform_later).with(
webhook.url,
inbox_data.merge(event: 'inbox_updated', changed_attributes: changed_attributes_data)
).once
listener.inbox_updated(inbox_updated_event)
end
end
end
end

View File

@@ -210,6 +210,34 @@ RSpec.describe Inbox do
expect(inbox.portal).to eq(portal)
end
it 'sends the inbox_created event if ENABLE_INBOX_EVENTS is true' do
with_modified_env ENABLE_INBOX_EVENTS: 'true' do
channel = inbox.channel
channel.update(widget_color: '#fff')
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(
'inbox.updated',
kind_of(Time),
inbox: inbox,
changed_attributes: kind_of(Object)
)
end
end
it 'sends the inbox_created event if ENABLE_INBOX_EVENTS is false' do
channel = inbox.channel
channel.update(widget_color: '#fff')
expect(Rails.configuration.dispatcher).not_to have_received(:dispatch)
.with(
'inbox.updated',
kind_of(Time),
inbox: inbox,
changed_attributes: kind_of(Object)
)
end
it 'resets cache key if there is an update in the channel' do
channel = inbox.channel
channel.update(widget_color: '#fff')