2020-06-12 17:42:47 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Integrations::Slack::IncomingMessageBuilder do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:message_params) { slack_message_stub }
|
2022-07-28 04:27:39 +00:00
|
|
|
let(:private_message_params) do
|
|
|
|
{
|
|
|
|
team_id: 'TLST3048H',
|
|
|
|
api_app_id: 'A012S5UETV4',
|
|
|
|
event: message_event.merge({ text: 'pRivate: A private note message' }),
|
|
|
|
type: 'event_callback',
|
|
|
|
event_time: 1_588_623_033
|
|
|
|
}
|
|
|
|
end
|
2022-12-18 00:41:11 +00:00
|
|
|
let(:sub_type_message) do
|
|
|
|
{
|
|
|
|
team_id: 'TLST3048H',
|
|
|
|
api_app_id: 'A012S5UETV4',
|
|
|
|
event: message_event.merge({ type: 'message', subtype: 'bot_message' }),
|
|
|
|
type: 'event_callback',
|
|
|
|
event_time: 1_588_623_033
|
|
|
|
}
|
|
|
|
end
|
|
|
|
let(:message_without_user) do
|
|
|
|
{
|
|
|
|
team_id: 'TLST3048H',
|
|
|
|
api_app_id: 'A012S5UETV4',
|
|
|
|
event: message_event.merge({ type: 'message', user: nil }),
|
|
|
|
type: 'event_callback',
|
|
|
|
event_time: 1_588_623_033
|
|
|
|
}
|
|
|
|
end
|
2021-09-17 19:19:01 +00:00
|
|
|
let(:message_with_attachments) { slack_attachment_stub }
|
2020-11-09 16:31:57 +00:00
|
|
|
let(:message_without_thread_ts) { slack_message_stub_without_thread_ts }
|
2020-06-12 17:42:47 +00:00
|
|
|
let(:verification_params) { slack_url_verification_stub }
|
|
|
|
|
2020-06-22 07:49:26 +00:00
|
|
|
let!(:hook) { create(:integrations_hook, account: account, reference_id: message_params[:event][:channel]) }
|
2020-06-12 17:42:47 +00:00
|
|
|
let!(:conversation) { create(:conversation, identifier: message_params[:event][:thread_ts]) }
|
|
|
|
|
2021-11-11 07:33:48 +00:00
|
|
|
before do
|
|
|
|
stub_request(:get, 'https://chatwoot-assets.local/sample.png').to_return(
|
|
|
|
status: 200,
|
|
|
|
body: File.read('spec/assets/sample.png'),
|
|
|
|
headers: {}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-06-12 17:42:47 +00:00
|
|
|
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
|
2020-11-09 16:31:57 +00:00
|
|
|
it 'doesnot create message if thread info is missing' do
|
|
|
|
messages_count = conversation.messages.count
|
|
|
|
builder = described_class.new(message_without_thread_ts)
|
|
|
|
builder.perform
|
|
|
|
expect(conversation.messages.count).to eql(messages_count)
|
|
|
|
end
|
|
|
|
|
2020-06-12 17:42:47 +00:00
|
|
|
it 'creates message' do
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(hook).not_to be_nil
|
2020-06-12 17:42:47 +00:00
|
|
|
messages_count = conversation.messages.count
|
|
|
|
builder = described_class.new(message_params)
|
2020-06-22 07:49:26 +00:00
|
|
|
allow(builder).to receive(:sender).and_return(nil)
|
2020-06-12 17:42:47 +00:00
|
|
|
builder.perform
|
|
|
|
expect(conversation.messages.count).to eql(messages_count + 1)
|
2021-07-09 12:15:02 +00:00
|
|
|
expect(conversation.messages.last.content).to eql('this is test https://chatwoot.com Hey @Sojan Test again')
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 04:27:39 +00:00
|
|
|
it 'creates a private note' do
|
|
|
|
expect(hook).not_to be_nil
|
|
|
|
messages_count = conversation.messages.count
|
|
|
|
builder = described_class.new(private_message_params)
|
|
|
|
allow(builder).to receive(:sender).and_return(nil)
|
|
|
|
builder.perform
|
|
|
|
expect(conversation.messages.count).to eql(messages_count + 1)
|
|
|
|
expect(conversation.messages.last.content).to eql('pRivate: A private note message')
|
|
|
|
expect(conversation.messages.last.private).to be(true)
|
|
|
|
end
|
|
|
|
|
2020-06-12 17:42:47 +00:00
|
|
|
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
|
2021-09-17 19:19:01 +00:00
|
|
|
|
2022-12-18 00:41:11 +00:00
|
|
|
it 'does not create message for message sub type events' do
|
|
|
|
messages_count = conversation.messages.count
|
|
|
|
builder = described_class.new(sub_type_message)
|
|
|
|
builder.perform
|
|
|
|
expect(conversation.messages.count).to eql(messages_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create message if user is missing' do
|
|
|
|
messages_count = conversation.messages.count
|
|
|
|
builder = described_class.new(message_without_user)
|
|
|
|
builder.perform
|
|
|
|
expect(conversation.messages.count).to eql(messages_count)
|
|
|
|
end
|
|
|
|
|
2022-05-09 18:24:45 +00:00
|
|
|
it 'does not create message for invalid event type and event files is not present' do
|
|
|
|
messages_count = conversation.messages.count
|
|
|
|
message_with_attachments[:event][:files] = nil
|
|
|
|
builder = described_class.new(message_with_attachments)
|
|
|
|
allow(builder).to receive(:sender).and_return(nil)
|
|
|
|
builder.perform
|
|
|
|
expect(conversation.messages.count).to eql(messages_count)
|
|
|
|
end
|
|
|
|
|
2021-09-17 19:19:01 +00:00
|
|
|
it 'saves attachment if params files present' do
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(hook).not_to be_nil
|
2021-09-17 19:19:01 +00:00
|
|
|
messages_count = conversation.messages.count
|
|
|
|
builder = described_class.new(message_with_attachments)
|
|
|
|
allow(builder).to receive(:sender).and_return(nil)
|
|
|
|
builder.perform
|
|
|
|
expect(conversation.messages.count).to eql(messages_count + 1)
|
|
|
|
expect(conversation.messages.last.content).to eql('this is test https://chatwoot.com Hey @Sojan Test again')
|
|
|
|
expect(conversation.messages.last.attachments).to be_any
|
|
|
|
end
|
2022-05-09 13:48:30 +00:00
|
|
|
|
|
|
|
it 'ignore message if it is postback of CW attachment message' do
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(hook).not_to be_nil
|
2022-05-09 13:48:30 +00:00
|
|
|
messages_count = conversation.messages.count
|
|
|
|
message_with_attachments[:event][:text] = 'Attached File!'
|
|
|
|
builder = described_class.new(message_with_attachments)
|
|
|
|
|
|
|
|
allow(builder).to receive(:sender).and_return(nil)
|
|
|
|
builder.perform
|
|
|
|
|
|
|
|
expect(conversation.messages.count).to eql(messages_count)
|
|
|
|
end
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|