feat: Mark the messages as failed if the API channel webhooks fail for any reason. (#8277)
This commit is contained in:
@@ -3,19 +3,20 @@ require 'rails_helper'
|
||||
RSpec.describe AgentBots::WebhookJob do
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
subject(:job) { described_class.perform_later(url, payload) }
|
||||
subject(:job) { described_class.perform_later(url, payload, webhook_type) }
|
||||
|
||||
let(:url) { 'https://test.com' }
|
||||
let(:payload) { { name: 'test' } }
|
||||
let(:webhook_type) { :agent_bot_webhook }
|
||||
|
||||
it 'queues the job' do
|
||||
expect { job }.to have_enqueued_job(described_class)
|
||||
.with(url, payload)
|
||||
.with(url, payload, webhook_type)
|
||||
.on_queue('high')
|
||||
end
|
||||
|
||||
it 'executes perform' do
|
||||
expect(Webhooks::Trigger).to receive(:execute).with(url, payload)
|
||||
expect(Webhooks::Trigger).to receive(:execute).with(url, payload, webhook_type)
|
||||
perform_enqueued_jobs { job }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,19 +3,29 @@ require 'rails_helper'
|
||||
RSpec.describe WebhookJob do
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
subject(:job) { described_class.perform_later(url, payload) }
|
||||
subject(:job) { described_class.perform_later(url, payload, webhook_type) }
|
||||
|
||||
let(:url) { 'https://test.chatwoot.com' }
|
||||
let(:payload) { { name: 'test' } }
|
||||
let(:webhook_type) { :account_webhook }
|
||||
|
||||
it 'queues the job' do
|
||||
expect { job }.to have_enqueued_job(described_class)
|
||||
.with(url, payload)
|
||||
.with(url, payload, webhook_type)
|
||||
.on_queue('medium')
|
||||
end
|
||||
|
||||
it 'executes perform' do
|
||||
expect(Webhooks::Trigger).to receive(:execute).with(url, payload)
|
||||
it 'executes perform with default webhook type' do
|
||||
expect(Webhooks::Trigger).to receive(:execute).with(url, payload, webhook_type)
|
||||
perform_enqueued_jobs { job }
|
||||
end
|
||||
|
||||
context 'with custom webhook type' do
|
||||
let(:webhook_type) { :api_inbox_webhook }
|
||||
|
||||
it 'executes perform with inbox webhook type' do
|
||||
expect(Webhooks::Trigger).to receive(:execute).with(url, payload, webhook_type)
|
||||
perform_enqueued_jobs { job }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,10 +3,17 @@ require 'rails_helper'
|
||||
describe Webhooks::Trigger do
|
||||
subject(:trigger) { described_class }
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:inbox) { create(:inbox, account: account) }
|
||||
let!(:conversation) { create(:conversation, inbox: inbox) }
|
||||
let!(:message) { create(:message, account: account, inbox: inbox, conversation: conversation) }
|
||||
|
||||
let!(:webhook_type) { :api_inbox_webhook }
|
||||
let!(:url) { 'https://test.com' }
|
||||
|
||||
describe '#execute' do
|
||||
it 'triggers webhook' do
|
||||
payload = { hello: :hello }
|
||||
url = 'https://test.com'
|
||||
|
||||
expect(RestClient::Request).to receive(:execute)
|
||||
.with(
|
||||
@@ -16,7 +23,51 @@ describe Webhooks::Trigger do
|
||||
headers: { content_type: :json, accept: :json },
|
||||
timeout: 5
|
||||
).once
|
||||
trigger.execute(url, payload)
|
||||
trigger.execute(url, payload, webhook_type)
|
||||
end
|
||||
|
||||
it 'updates message status if webhook fails for message-created event' do
|
||||
payload = { event: 'message_created', conversation: { id: conversation.id }, id: message.id }
|
||||
|
||||
expect(RestClient::Request).to receive(:execute)
|
||||
.with(
|
||||
method: :post,
|
||||
url: url,
|
||||
payload: payload.to_json,
|
||||
headers: { content_type: :json, accept: :json },
|
||||
timeout: 5
|
||||
).and_raise(RestClient::ExceptionWithResponse.new('error', 500)).once
|
||||
|
||||
expect { trigger.execute(url, payload, webhook_type) }.to change { message.reload.status }.from('sent').to('failed')
|
||||
end
|
||||
|
||||
it 'updates message status if webhook fails for message-updated event' do
|
||||
payload = { event: 'message_updated', conversation: { id: conversation.id }, id: message.id }
|
||||
|
||||
expect(RestClient::Request).to receive(:execute)
|
||||
.with(
|
||||
method: :post,
|
||||
url: url,
|
||||
payload: payload.to_json,
|
||||
headers: { content_type: :json, accept: :json },
|
||||
timeout: 5
|
||||
).and_raise(RestClient::ExceptionWithResponse.new('error', 500)).once
|
||||
expect { trigger.execute(url, payload, webhook_type) }.to change { message.reload.status }.from('sent').to('failed')
|
||||
end
|
||||
end
|
||||
|
||||
it 'does not update message status if webhook fails for other events' do
|
||||
payload = { event: 'conversation_created', conversation: { id: conversation.id }, id: message.id }
|
||||
|
||||
expect(RestClient::Request).to receive(:execute)
|
||||
.with(
|
||||
method: :post,
|
||||
url: url,
|
||||
payload: payload.to_json,
|
||||
headers: { content_type: :json, accept: :json },
|
||||
timeout: 5
|
||||
).and_raise(RestClient::ExceptionWithResponse.new('error', 500)).once
|
||||
|
||||
expect { trigger.execute(url, payload, webhook_type) }.not_to(change { message.reload.status })
|
||||
end
|
||||
end
|
||||
|
||||
@@ -54,7 +54,8 @@ describe WebhookListener do
|
||||
conversation: api_conversation
|
||||
)
|
||||
api_event = Events::Base.new(event_name, Time.zone.now, message: api_message)
|
||||
expect(WebhookJob).to receive(:perform_later).with(channel_api.webhook_url, api_message.webhook_data.merge(event: 'message_created')).once
|
||||
expect(WebhookJob).to receive(:perform_later).with(channel_api.webhook_url, api_message.webhook_data.merge(event: 'message_created'),
|
||||
:api_inbox_webhook).once
|
||||
listener.message_created(api_event)
|
||||
end
|
||||
|
||||
@@ -101,7 +102,8 @@ describe WebhookListener do
|
||||
api_conversation = create(:conversation, account: account, inbox: api_inbox, assignee: user)
|
||||
api_event = Events::Base.new(event_name, Time.zone.now, conversation: api_conversation)
|
||||
expect(WebhookJob).to receive(:perform_later).with(channel_api.webhook_url,
|
||||
api_conversation.webhook_data.merge(event: 'conversation_created')).once
|
||||
api_conversation.webhook_data.merge(event: 'conversation_created'),
|
||||
:api_inbox_webhook).once
|
||||
listener.conversation_created(api_event)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user