feat: APIs for Integration Hooks (#2250)
- Introduces JSON Schema validations via JSONSchemer - Add CRUD APIs for integration hooks
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Integration Hooks API', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:params) { { app_id: 'dialogflow', inbox_id: inbox.id, settings: { project_id: 'xx', credentials: { test: 'test' } } } }
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/integrations/hooks' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post api_v1_account_integrations_hooks_url(account_id: account.id),
|
||||
params: params,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'return unauthorized if agent' do
|
||||
post api_v1_account_integrations_hooks_url(account_id: account.id),
|
||||
params: params,
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it 'creates hooks if admin' do
|
||||
post api_v1_account_integrations_hooks_url(account_id: account.id),
|
||||
params: params,
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
data = JSON.parse(response.body)
|
||||
expect(data['app']['id']).to eq params[:app_id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PATCH /api/v1/accounts/{account.id}/integrations/hooks/{hook_id}' do
|
||||
let(:hook) { create(:integrations_hook, account: account) }
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
patch api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
|
||||
params: params,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'return unauthorized if agent' do
|
||||
patch api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
|
||||
params: params,
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it 'updates hook if admin' do
|
||||
patch api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
|
||||
params: params,
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
data = JSON.parse(response.body)
|
||||
expect(data['app']['id']).to eq 'slack'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /api/v1/accounts/{account.id}/integrations/hooks/{hook_id}' do
|
||||
let(:hook) { create(:integrations_hook, account: account) }
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
delete api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'return unauthorized if agent' do
|
||||
delete api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it 'updates hook if admin' do
|
||||
delete api_v1_account_integrations_hook_url(account_id: account.id, id: hook.id),
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(::Integrations::Hook.exists?(hook.id)).to eq false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,12 +1,16 @@
|
||||
FactoryBot.define do
|
||||
factory :integrations_hook, class: 'Integrations::Hook' do
|
||||
status { Integrations::Hook.statuses['enabled'] }
|
||||
app_id { 'slack' }
|
||||
inbox
|
||||
account
|
||||
app_id { 'slack' }
|
||||
settings { { 'test': 'test' } }
|
||||
hook_type { Integrations::Hook.statuses['account'] }
|
||||
status { Integrations::Hook.statuses['enabled'] }
|
||||
access_token { SecureRandom.hex }
|
||||
reference_id { SecureRandom.hex }
|
||||
|
||||
trait :dialogflow do
|
||||
app_id { 'dialogflow' }
|
||||
settings { { project_id: 'test', credentials: {} } }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -29,7 +29,7 @@ RSpec.describe HookJob, type: :job do
|
||||
end
|
||||
|
||||
it 'calls Integrations::Dialogflow::ProcessorService when its a dialogflow intergation' do
|
||||
hook = create(:integrations_hook, app_id: 'dialogflow', account: account)
|
||||
hook = create(:integrations_hook, :dialogflow, account: account)
|
||||
allow(Integrations::Dialogflow::ProcessorService).to receive(:new).and_return(process_service)
|
||||
expect(Integrations::Dialogflow::ProcessorService).to receive(:new)
|
||||
described_class.perform_now(hook, event_name, event_data)
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'rails_helper'
|
||||
|
||||
describe Integrations::Dialogflow::ProcessorService do
|
||||
let(:account) { create(:account) }
|
||||
let(:hook) { create(:integrations_hook, app_id: 'dialogflow', account: account) }
|
||||
let(:hook) { create(:integrations_hook, :dialogflow, account: account) }
|
||||
let(:conversation) { create(:conversation, account: account, status: :bot) }
|
||||
let(:message) { create(:message, account: account, conversation: conversation) }
|
||||
let(:event_name) { 'message.created' }
|
||||
|
||||
@@ -351,7 +351,7 @@ RSpec.describe Conversation, type: :model do
|
||||
end
|
||||
|
||||
describe '#botintegration: when conversation created in inbox with dialogflow integration' do
|
||||
let(:hook) { create(:integrations_hook, app_id: 'dialogflow') }
|
||||
let(:hook) { create(:integrations_hook, :dialogflow) }
|
||||
let(:conversation) { create(:conversation, inbox: hook.inbox) }
|
||||
|
||||
it 'returns conversation status as bot' do
|
||||
|
||||
@@ -9,4 +9,22 @@ RSpec.describe Integrations::Hook, type: :model do
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
end
|
||||
|
||||
describe 'when trying to create multiple hooks for an app' do
|
||||
let(:account) { create(:account) }
|
||||
|
||||
context 'when app allows multiple hooks' do
|
||||
it 'allows to create succesfully' do
|
||||
create(:integrations_hook, account: account, app_id: 'webhook')
|
||||
expect(build(:integrations_hook, account: account, app_id: 'webhook').valid?).to eq true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when app doesnot allow multiple hooks' do
|
||||
it 'throws invalid error' do
|
||||
create(:integrations_hook, account: account, app_id: 'slack')
|
||||
expect(build(:integrations_hook, account: account, app_id: 'slack').valid?).to eq false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,7 +16,7 @@ RSpec.describe 'Api::V1::Accounts::Integrations::Slacks', type: :request do
|
||||
context 'when it is an authenticated user' do
|
||||
it 'creates hook' do
|
||||
hook_builder = Integrations::Slack::HookBuilder.new(account: account, code: SecureRandom.hex)
|
||||
expect(hook_builder).to receive(:fetch_access_token).and_return(SecureRandom.hex)
|
||||
expect(hook_builder).to receive(:perform).and_return(hook)
|
||||
expect(Integrations::Slack::HookBuilder).to receive(:new).and_return(hook_builder)
|
||||
|
||||
channel_builder = Integrations::Slack::ChannelBuilder.new(hook: hook, channel: 'channel')
|
||||
|
||||
Reference in New Issue
Block a user