feat: Add event subscription option to webhooks (#4540)

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Pranav Raj S
2022-04-25 17:44:42 +05:30
committed by GitHub
parent fa51fd1d73
commit 899176a793
25 changed files with 552 additions and 359 deletions

View File

@@ -57,6 +57,36 @@ RSpec.describe 'Webhooks API', type: :request do
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)['message']).to eql 'Url is invalid'
end
it 'throws error if subscription events are invalid' do
post "/api/v1/accounts/#{account.id}/webhooks",
params: { url: 'https://hello.com', subscriptions: ['conversation_random_event'] },
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)['message']).to eql 'Subscriptions Invalid events'
end
it 'throws error if subscription events are empty' do
post "/api/v1/accounts/#{account.id}/webhooks",
params: { url: 'https://hello.com', subscriptions: [] },
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)['message']).to eql 'Subscriptions Invalid events'
end
it 'use default if subscription events are nil' do
post "/api/v1/accounts/#{account.id}/webhooks",
params: { url: 'https://hello.com', subscriptions: nil },
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:ok)
expect(
JSON.parse(response.body)['payload']['webhook']['subscriptions']
).to eql %w[conversation_status_changed conversation_updated conversation_created message_created message_updated
webwidget_triggered]
end
end
end

View File

@@ -3,5 +3,15 @@ FactoryBot.define do
account_id { 1 }
inbox_id { 1 }
url { 'https://api.chatwoot.com' }
subscriptions do
%w[
conversation_status_changed
conversation_updated
conversation_created
message_created
message_updated
webwidget_triggered
]
end
end
end

View File

@@ -23,14 +23,22 @@ describe WebhookListener do
end
end
context 'when webhook is configured' do
it 'triggers webhook' do
context 'when webhook is configured and event is subscribed' do
it 'triggers the webhook event' do
webhook = create(:webhook, inbox: inbox, account: account)
expect(WebhookJob).to receive(:perform_later).with(webhook.url, message.webhook_data.merge(event: 'message_created')).once
listener.message_created(message_created_event)
end
end
context 'when webhook is configured and event is not subscribed' do
it 'does not trigger the webhook event' do
create(:webhook, subscriptions: ['conversation_created'], inbox: inbox, account: account)
expect(WebhookJob).not_to receive(:perform_later)
listener.message_created(message_created_event)
end
end
context 'when inbox is an API Channel' do
it 'triggers webhook if webhook_url is present' do
channel_api = create(:channel_api, account: account)
@@ -106,36 +114,6 @@ describe WebhookListener do
end
end
describe '#conversation_resolved' do
let!(:conversation_resolved_event) do
Events::Base.new(event_name, Time.zone.now, conversation: conversation.reload, changed_attributes: { status: [:open, :resolved] })
end
let(:event_name) { :'conversation.resolved' }
context 'when webhook is not configured' do
it 'does not trigger webhook' do
expect(WebhookJob).to receive(:perform_later).exactly(0).times
listener.conversation_resolved(conversation_resolved_event)
end
end
context 'when webhook is configured' do
it 'triggers webhook' do
webhook = create(:webhook, inbox: inbox, account: account)
conversation.update(status: :resolved)
expect(WebhookJob).to receive(:perform_later).with(webhook.url,
conversation.webhook_data.merge(event: 'conversation_resolved',
changed_attributes: [{ status: {
current_value: :resolved, previous_value: :open
} }])).once
listener.conversation_resolved(conversation_resolved_event)
end
end
end
describe '#conversation_updated' do
let(:custom_attributes) { { test: nil } }
let!(:conversation_updated_event) do