Feature: Slack integration (#783)

- Integrations architecture
- Slack integration
This commit is contained in:
Subin T P
2020-06-12 23:12:47 +05:30
committed by GitHub
parent 4f3b483066
commit ed1c871633
44 changed files with 867 additions and 7 deletions

View File

@@ -0,0 +1,22 @@
require 'rails_helper'
describe Integrations::Slack::HookBuilder do
let(:account) { create(:account) }
let(:code) { SecureRandom.hex }
let(:token) { SecureRandom.hex }
describe '#perform' do
it 'creates hook' do
hooks_count = account.hooks.count
builder = described_class.new(account: account, code: code)
builder.stub(:fetch_access_token) { token }
builder.perform
expect(account.hooks.count).to eql(hooks_count + 1)
hook = account.hooks.last
expect(hook.access_token).to eql(token)
end
end
end

View File

@@ -0,0 +1,45 @@
require 'rails_helper'
describe Integrations::Slack::IncomingMessageBuilder do
let(:account) { create(:account) }
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!(:conversation) { create(:conversation, identifier: message_params[:event][:thread_ts]) }
describe '#perform' do
context 'when url verification' do
it 'return challenge code as response' do
builder = described_class.new(verification_params)
response = builder.perform
expect(response[:challenge]).to eql(verification_params[:challenge])
end
end
context 'when message creation' do
it 'creates message' do
messages_count = conversation.messages.count
builder = described_class.new(message_params)
builder.perform
expect(conversation.messages.count).to eql(messages_count + 1)
end
it 'does not create message for invalid event type' do
messages_count = conversation.messages.count
message_params[:type] = 'invalid_event_type'
builder = described_class.new(message_params)
builder.perform
expect(conversation.messages.count).to eql(messages_count)
end
it 'does not create message for invalid event name' do
messages_count = conversation.messages.count
message_params[:event][:type] = 'invalid_event_name'
builder = described_class.new(message_params)
builder.perform
expect(conversation.messages.count).to eql(messages_count)
end
end
end
end

View File

@@ -0,0 +1,30 @@
require 'rails_helper'
describe Integrations::Slack::OutgoingMessageBuilder do
let(:account) { create(:account) }
let!(:inbox) { create(:inbox, account: account) }
let!(:contact) { create(:contact) }
let!(:hook) { create(:integrations_hook, account: account) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact) }
let!(:message) { create(:message, account: account, inbox: inbox, conversation: conversation) }
describe '#perform' do
it 'sent message to slack' do
builder = described_class.new(hook, message)
stub_request(:post, 'https://slack.com/api/chat.postMessage')
.to_return(status: 200, body: '', headers: {})
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(
channel: hook.reference_id,
text: message.content,
username: contact.name,
thread_ts: conversation.identifier
)
# rubocop:enable RSpec/AnyInstance
builder.perform
end
end
end