Feature: Webhooks (#489)

This commit is contained in:
Subin T P
2020-02-14 23:19:17 +05:30
committed by GitHub
parent 79a847aeab
commit 919261d843
29 changed files with 566 additions and 214 deletions

View File

@@ -0,0 +1,104 @@
require 'rails_helper'
RSpec.describe 'Webhooks API', type: :request do
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
let(:webhook) { create(:webhook, account: account, inbox: inbox, urls: ['https://hello.com']) }
let(:administrator) { create(:user, account: account, role: :administrator) }
let(:agent) { create(:user, account: account, role: :agent) }
describe 'GET /api/v1/inbox/webhooks' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
get '/api/v1/inbox/webhooks',
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated admin user' do
it 'gets all webhook' do
get '/api/v1/inbox/webhooks',
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)['payload']['webhooks'].count).to eql account.webhooks.count
end
end
end
describe 'POST /api/v1/inbox/webhooks' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
post '/api/v1/inbox/webhooks',
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated admin user' do
it 'creates webhook' do
post '/api/v1/inbox/webhooks',
params: { account_id: account.id, inbox_id: inbox.id, urls: ['https://hello.com'] },
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)['payload']['webhook']['urls']).to eql ['https://hello.com']
end
end
end
describe 'PUT /api/v1/inbox/webhooks/:id' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
put "/api/v1/inbox/webhooks/#{webhook.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated admin user' do
it 'updates webhook' do
put "/api/v1/inbox/webhooks/#{webhook.id}",
params: { urls: ['https://hello.com', 'https://world.com'] },
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)['payload']['webhook']['urls']).to eql ['https://hello.com', 'https://world.com']
end
end
end
describe 'DELETE /api/v1/inbox/webhooks/:id' do
context 'when it is an authenticated agent' do
it 'returns unauthorized' do
delete "/api/v1/inbox/webhooks/#{webhook.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated admin user' do
it 'deletes webhook' do
delete "/api/v1/inbox/webhooks/#{webhook.id}",
headers: administrator.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(account.webhooks.count).to be 0
end
end
end
end

View File

@@ -0,0 +1,7 @@
FactoryBot.define do
factory :webhook do
account_id { 1 }
inbox_id { 1 }
urls { ['MyString'] }
end
end

View File

@@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe WebhookJob, type: :job do
subject(:job) { described_class.perform_later(url, payload) }
let(:url) { 'https://test.com' }
let(:payload) { { name: 'test' } }
it 'queues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(url, payload)
.on_queue('webhooks')
end
end

View File

@@ -0,0 +1,15 @@
require 'rails_helper'
describe Webhooks::Trigger do
subject(:trigger) { described_class }
describe '#execute' do
it 'triggers webhook' do
params = { hello: 'hello' }
url = 'htpps://test.com'
expect(RestClient).to receive(:post).with(url, params).once
trigger.execute(url, params)
end
end
end

View File

@@ -0,0 +1,33 @@
require 'rails_helper'
describe WebhookListener do
let(:listener) { described_class.instance }
let!(:account) { create(:account) }
let(:report_identity) { Reports::UpdateAccountIdentity.new(account, Time.zone.now) }
let!(:user) { create(:user, account: account) }
let!(:inbox) { create(:inbox, account: account) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: user) }
let!(:message) do
create(:message, message_type: 'outgoing',
account: account, inbox: inbox, conversation: conversation)
end
let!(:event) { Events::Base.new(event_name, Time.zone.now, message: message) }
describe '#message_created' do
let(:event_name) { :'conversation.created' }
context 'when webhook is not configured' do
it 'does not trigger webhook' do
expect(RestClient).to receive(:post).exactly(0).times
listener.message_created(event)
end
end
context 'when webhook is configured' do
it 'triggers webhook' do
create(:webhook, inbox: inbox, account: account)
expect(WebhookJob).to receive(:perform_later).once
listener.message_created(event)
end
end
end
end

View File

@@ -14,4 +14,5 @@ RSpec.describe Account do
it { is_expected.to have_many(:facebook_pages).class_name('::Channel::FacebookPage').dependent(:destroy) }
it { is_expected.to have_many(:web_widgets).class_name('::Channel::WebWidget').dependent(:destroy) }
it { is_expected.to have_one(:subscription).dependent(:destroy) }
it { is_expected.to have_many(:webhooks).dependent(:destroy) }
end

View File

@@ -23,6 +23,7 @@ RSpec.describe Inbox do
it { is_expected.to have_many(:conversations).dependent(:destroy) }
it { is_expected.to have_many(:messages).through(:conversations) }
it { is_expected.to have_one(:webhook) }
end
describe '#add_member' do

View File

@@ -0,0 +1,13 @@
require 'rails_helper'
RSpec.describe Webhook, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:account_id) }
it { is_expected.to validate_presence_of(:inbox_id) }
end
describe 'associations' do
it { is_expected.to belong_to(:account) }
it { is_expected.to belong_to(:inbox) }
end
end