Feature: Slack - receive messages, create threads, send replies (#974)

Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
This commit is contained in:
Sojan Jose
2020-06-22 13:19:26 +05:30
committed by GitHub
parent aa8a85b8bd
commit 1ef8d03e18
53 changed files with 815 additions and 188 deletions

View File

@@ -20,9 +20,9 @@ RSpec.describe 'Integration Apps API', type: :request do
as: :json
expect(response).to have_http_status(:success)
app = JSON.parse(response.body).first
expect(app['id']).to eql('cw_slack')
expect(app['name']).to eql('Slack')
app = JSON.parse(response.body)['payload'].first
expect(app['id']).to eql('webhook')
expect(app['name']).to eql('Webhooks')
end
end
end
@@ -30,7 +30,7 @@ RSpec.describe 'Integration Apps API', type: :request do
describe 'GET /api/v1/integrations/apps/:id' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get api_v1_account_integrations_app_url(account_id: account.id, id: 'cw_slack')
get api_v1_account_integrations_app_url(account_id: account.id, id: 'slack')
expect(response).to have_http_status(:unauthorized)
end
end
@@ -39,13 +39,13 @@ RSpec.describe 'Integration Apps API', type: :request do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns details of the app' do
get api_v1_account_integrations_app_url(account_id: account.id, id: 'cw_slack'),
get api_v1_account_integrations_app_url(account_id: account.id, id: 'slack'),
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
app = JSON.parse(response.body)
expect(app['id']).to eql('cw_slack')
expect(app['id']).to eql('slack')
expect(app['name']).to eql('Slack')
end
end

View File

@@ -3,7 +3,7 @@ FactoryBot.define do
status { 1 }
inbox_id { 1 }
account_id { 1 }
app_id { 'cw_slack' }
app_id { 'slack' }
settings { 'MyText' }
hook_type { 1 }
access_token { SecureRandom.hex }

View File

@@ -10,7 +10,7 @@ describe Integrations::Slack::HookBuilder do
hooks_count = account.hooks.count
builder = described_class.new(account: account, code: code)
builder.stub(:fetch_access_token) { token }
allow(builder).to receive(:fetch_access_token).and_return(token)
builder.perform
expect(account.hooks.count).to eql(hooks_count + 1)

View File

@@ -5,7 +5,7 @@ describe Integrations::Slack::IncomingMessageBuilder do
let(:message_params) { slack_message_stub }
let(:verification_params) { slack_url_verification_stub }
let(:hook) { create(:integrations_hook, account: account, reference_id: message_params[:event][:channel]) }
let!(:hook) { create(:integrations_hook, account: account, reference_id: message_params[:event][:channel]) }
let!(:conversation) { create(:conversation, identifier: message_params[:event][:thread_ts]) }
describe '#perform' do
@@ -19,8 +19,10 @@ describe Integrations::Slack::IncomingMessageBuilder do
context 'when message creation' do
it 'creates message' do
expect(hook).not_to eq nil
messages_count = conversation.messages.count
builder = described_class.new(message_params)
allow(builder).to receive(:sender).and_return(nil)
builder.perform
expect(conversation.messages.count).to eql(messages_count + 1)
end

View File

@@ -14,15 +14,16 @@ describe Integrations::Slack::OutgoingMessageBuilder do
builder = described_class.new(hook, message)
stub_request(:post, 'https://slack.com/api/chat.postMessage')
.to_return(status: 200, body: '', headers: {})
slack_client = double
expect(builder).to receive(:slack_client).and_return(slack_client)
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(
expect(slack_client).to receive(:chat_postMessage).with(
channel: hook.reference_id,
text: message.content,
username: contact.name,
thread_ts: conversation.identifier
username: "Contact: #{contact.name}",
thread_ts: conversation.identifier,
icon_url: anything
)
# rubocop:enable RSpec/AnyInstance
builder.perform
end

View File

@@ -3,7 +3,7 @@ require 'rails_helper'
RSpec.describe 'Api::V1::Accounts::Integrations::Slacks', type: :request do
let(:account) { create(:account) }
let(:agent) { create(:user, account: account, role: :agent) }
let(:hook) { create(:integrations_hook, account: account) }
let!(:hook) { create(:integrations_hook, account: account) }
describe 'POST /api/v1/accounts/{account.id}/integrations/slack' do
context 'when it is an unauthenticated user' do
@@ -16,24 +16,27 @@ 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)
hook_builder.stub(:fetch_access_token) { SecureRandom.hex }
expect(hook_builder).to receive(:fetch_access_token).and_return(SecureRandom.hex)
expect(Integrations::Slack::HookBuilder).to receive(:new).and_return(hook_builder)
channel_builder = Integrations::Slack::ChannelBuilder.new(hook: hook, channel: 'channel')
expect(channel_builder).to receive(:perform)
expect(Integrations::Slack::ChannelBuilder).to receive(:new).and_return(channel_builder)
post "/api/v1/accounts/#{account.id}/integrations/slack",
params: { code: SecureRandom.hex },
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['app_id']).to eql('cw_slack')
expect(json_response['id']).to eql('slack')
end
end
describe 'PUT /api/v1/accounts/{account.id}/integrations/slack/{id}' do
describe 'PUT /api/v1/accounts/{account.id}/integrations/slack/' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put "/api/v1/accounts/#{account.id}/integrations/slack/#{hook.id}", params: {}
put "/api/v1/accounts/#{account.id}/integrations/slack/", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
@@ -41,32 +44,32 @@ RSpec.describe 'Api::V1::Accounts::Integrations::Slacks', type: :request do
context 'when it is an authenticated user' do
it 'updates hook' do
channel_builder = Integrations::Slack::ChannelBuilder.new(hook: hook, channel: 'channel')
channel_builder.stub(:perform)
expect(channel_builder).to receive(:perform)
expect(Integrations::Slack::ChannelBuilder).to receive(:new).and_return(channel_builder)
put "/api/v1/accounts/#{account.id}/integrations/slack/#{hook.id}",
put "/api/v1/accounts/#{account.id}/integrations/slack",
params: { channel: SecureRandom.hex },
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['app_id']).to eql('cw_slack')
expect(json_response['app_id']).to eql('slack')
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/integrations/slack/{id}' do
describe 'DELETE /api/v1/accounts/{account.id}/integrations/slack' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/integrations/slack/#{hook.id}", params: {}
delete "/api/v1/accounts/#{account.id}/integrations/slack", params: {}
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'deletes hook' do
delete "/api/v1/accounts/#{account.id}/integrations/slack/#{hook.id}",
delete "/api/v1/accounts/#{account.id}/integrations/slack",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:success)
expect(Integrations::Hook.find_by(id: hook.id)).to be nil

View File

@@ -4,7 +4,7 @@ RSpec.describe 'Api::V1::Integrations::Webhooks', type: :request do
describe 'POST /api/v1/integrations/webhooks' do
it 'consumes webhook' do
builder = Integrations::Slack::IncomingMessageBuilder.new({})
builder.stub(:perform) { true }
expect(builder).to receive(:perform).and_return(true)
expect(Integrations::Slack::IncomingMessageBuilder).to receive(:new).and_return(builder)

View File

@@ -7,41 +7,12 @@ module SlackStubs
}
end
# rubocop:disable Metrics/MethodLength
def slack_message_stub
{
"token": '[FILTERED]',
"team_id": 'TLST3048H',
"api_app_id": 'A012S5UETV4',
"event": {
"client_msg_id": 'ffc6e64e-6f0c-4a3d-b594-faa6b44e48ab',
"type": 'message',
"text": 'this is test',
"user": 'ULYPAKE5S',
"ts": '1588623033.006000',
"team": 'TLST3048H',
"blocks": [
{
"type": 'rich_text',
"block_id": 'jaIv3',
"elements": [
{
"type": 'rich_text_section',
"elements": [
{
"type": 'text',
"text": 'this is test'
}
]
}
]
}
],
"thread_ts": '1588623023.005900',
"channel": 'G01354F6A6Q',
"event_ts": '1588623033.006000',
"channel_type": 'group'
},
"event": message_event,
"type": 'event_callback',
"event_id": 'Ev013QUX3WV6',
"event_time": 1_588_623_033,
@@ -49,5 +20,38 @@ module SlackStubs
"webhook": {}
}
end
# rubocop:enable Metrics/MethodLength
def message_event
{ "client_msg_id": 'ffc6e64e-6f0c-4a3d-b594-faa6b44e48ab',
"type": 'message',
"text": 'this is test',
"user": 'ULYPAKE5S',
"ts": '1588623033.006000',
"team": 'TLST3048H',
"blocks": message_blocks,
"thread_ts": '1588623023.005900',
"channel": 'G01354F6A6Q',
"event_ts": '1588623033.006000',
"channel_type": 'group' }
end
def message_blocks
[
{
"type": 'rich_text',
"block_id": 'jaIv3',
"elements": [
{
"type": 'rich_text_section',
"elements": [
{
"type": 'text',
"text": 'this is test'
}
]
}
]
}
]
end
end