feat: Remove restrictions on API channel webhook_url (#2261)

This commit is contained in:
Pranav Raj S 2021-05-13 15:03:25 +05:30 committed by GitHub
parent 35f8d01a0c
commit 836b317b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 8 deletions

View file

@ -56,7 +56,8 @@ import { required } from 'vuelidate/lib/validators';
import router from '../../../../index'; import router from '../../../../index';
import PageHeader from '../../SettingsSubPageHeader'; import PageHeader from '../../SettingsSubPageHeader';
const shouldBeWebhookUrl = (value = '') => value.startsWith('http'); const shouldBeWebhookUrl = (value = '') =>
value ? value.startsWith('http') : true;
export default { export default {
components: { components: {
@ -76,7 +77,7 @@ export default {
}, },
validations: { validations: {
channelName: { required }, channelName: { required },
webhookUrl: { required, shouldBeWebhookUrl }, webhookUrl: { shouldBeWebhookUrl },
}, },
methods: { methods: {
async createChannel() { async createChannel() {

View file

@ -59,7 +59,9 @@ class WebhookListener < BaseListener
WebhookJob.perform_later(webhook.url, payload) WebhookJob.perform_later(webhook.url, payload)
end end
# Deliver for API Inbox return unless inbox.channel_type == 'Channel::Api'
WebhookJob.perform_later(inbox.channel.webhook_url, payload) if inbox.channel_type == 'Channel::Api' return if inbox.channel.webhook_url.blank?
WebhookJob.perform_later(inbox.channel.webhook_url, payload)
end end
end end

View file

@ -3,7 +3,7 @@
# Table name: channel_api # Table name: channel_api
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# webhook_url :string not null # webhook_url :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# account_id :integer not null # account_id :integer not null

View file

@ -0,0 +1,5 @@
class RemoveNotNullFromWebhookUrlChannelApi < ActiveRecord::Migration[6.0]
def change
change_column :channel_api, :webhook_url, :string, null: true
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_04_30_100138) do ActiveRecord::Schema.define(version: 2021_05_13_083044) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
@ -139,7 +139,7 @@ ActiveRecord::Schema.define(version: 2021_04_30_100138) do
create_table "channel_api", force: :cascade do |t| create_table "channel_api", force: :cascade do |t|
t.integer "account_id", null: false t.integer "account_id", null: false
t.string "webhook_url", null: false t.string "webhook_url"
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false
end end

View file

@ -13,7 +13,7 @@ describe WebhookListener do
let!(:event) { Events::Base.new(event_name, Time.zone.now, message: message) } let!(:event) { Events::Base.new(event_name, Time.zone.now, message: message) }
describe '#message_created' do describe '#message_created' do
let(:event_name) { :'conversation.created' } let(:event_name) { :'message.created' }
context 'when webhook is not configured' do context 'when webhook is not configured' do
it 'does not trigger webhook' do it 'does not trigger webhook' do
@ -29,5 +29,39 @@ describe WebhookListener do
listener.message_created(event) listener.message_created(event)
end end
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)
api_inbox = channel_api.inbox
api_conversation = create(:conversation, account: account, inbox: api_inbox, assignee: user)
api_message = create(
:message,
message_type: 'outgoing',
account: account,
inbox: api_inbox,
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
listener.message_created(api_event)
end
it 'does not trigger webhook if webhook_url is not present' do
channel_api = create(:channel_api, webhook_url: nil, account: account)
api_inbox = channel_api.inbox
api_conversation = create(:conversation, account: account, inbox: api_inbox, assignee: user)
api_message = create(
:message,
message_type: 'outgoing',
account: account,
inbox: channel_api.inbox,
conversation: api_conversation
)
api_event = Events::Base.new(event_name, Time.zone.now, message: api_message)
expect(WebhookJob).not_to receive(:perform_later)
listener.message_created(api_event)
end
end
end end
end end