Feature: Introduce bots (#545)
Co-authored-by: Pranav Raj S <pranavrajs@gmail.com> Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
7
spec/factories/agent_bot_inboxes.rb
Normal file
7
spec/factories/agent_bot_inboxes.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
FactoryBot.define do
|
||||
factory :agent_bot_inbox do
|
||||
inbox
|
||||
agent_bot
|
||||
status { 'active' }
|
||||
end
|
||||
end
|
||||
7
spec/factories/agent_bots.rb
Normal file
7
spec/factories/agent_bots.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
FactoryBot.define do
|
||||
factory :agent_bot do
|
||||
name { 'MyString' }
|
||||
description { 'MyString' }
|
||||
outgoing_url { 'MyString' }
|
||||
end
|
||||
end
|
||||
14
spec/jobs/agent_bot_job_spec.rb
Normal file
14
spec/jobs/agent_bot_job_spec.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AgentBotJob, 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('bots')
|
||||
end
|
||||
end
|
||||
33
spec/listeners/agent_bot_listener_spec.rb
Normal file
33
spec/listeners/agent_bot_listener_spec.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
require 'rails_helper'
|
||||
describe AgentBotListener do
|
||||
let(:listener) { described_class.instance }
|
||||
let!(:account) { create(:account) }
|
||||
let!(:user) { create(:user, account: account) }
|
||||
let!(:inbox) { create(:inbox, account: account) }
|
||||
let!(:agent_bot) { create(:agent_bot) }
|
||||
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 agent bot is not configured' do
|
||||
it 'does not send message to agent bot' do
|
||||
expect(AgentBotJob).to receive(:perform_later).exactly(0).times
|
||||
listener.message_created(event)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when agent bot is configured' do
|
||||
it 'sends message to agent bot' do
|
||||
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
|
||||
expect(AgentBotJob).to receive(:perform_later).with(agent_bot.outgoing_url, message.webhook_data.merge(event: 'message_created')).once
|
||||
listener.message_created(event)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
13
spec/models/agent_bot_inbox_spec.rb
Normal file
13
spec/models/agent_bot_inbox_spec.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AgentBotInbox, type: :model do
|
||||
describe 'validations' do
|
||||
it { is_expected.to validate_presence_of(:inbox_id) }
|
||||
it { is_expected.to validate_presence_of(:agent_bot_id) }
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:agent_bot) }
|
||||
it { is_expected.to belong_to(:inbox) }
|
||||
end
|
||||
end
|
||||
8
spec/models/agent_bot_spec.rb
Normal file
8
spec/models/agent_bot_spec.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AgentBot, type: :model do
|
||||
describe 'associations' do
|
||||
it { is_expected.to have_many(:agent_bot_inboxes) }
|
||||
it { is_expected.to have_many(:inboxes) }
|
||||
end
|
||||
end
|
||||
@@ -257,4 +257,13 @@ RSpec.describe Conversation, type: :model do
|
||||
expect(lock_event_data).to eq(id: 505, locked: false)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#botinbox: when conversation created inside inbox with agent bot' do
|
||||
let!(:bot_inbox) { create(:agent_bot_inbox) }
|
||||
let(:conversation) { create(:conversation, inbox: bot_inbox.inbox) }
|
||||
|
||||
it 'returns conversation status as bot' do
|
||||
expect(conversation.status).to eq('bot')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,6 +23,9 @@ 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(:agent_bot_inbox) }
|
||||
|
||||
it { is_expected.to have_many(:webhooks).dependent(:destroy) }
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user