Files
chatwoot/spec/models/concerns/auto_assignment_handler_shared.rb
T
2025-08-01 01:47:47 +05:30

77 lines
2.4 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
shared_examples_for 'auto_assignment_handler' do
describe '#auto assignment' do
before do
# Stub GlobalConfig for assignment_v2 feature flag before any objects are created
allow(GlobalConfig).to receive(:get) do |key, default = nil|
case key
when 'assignment_v2'
nil
when 'assignment_v2_disabled'
false
when 'assignment_v2_disabled_inboxes'
[]
else
default
end
end
create(:inbox_member, inbox: inbox, user: agent)
allow(Redis::Alfred).to receive(:rpoplpush).and_return(agent.id)
end
let(:account) { create(:account) }
let(:agent) { create(:user, email: 'agent1@example.com', account: account, auto_offline: false) }
let(:inbox) { create(:inbox, account: account) }
let(:conversation) do
create(
:conversation,
account: account,
contact: create(:contact, account: account),
inbox: inbox,
assignee: nil
)
end
it 'runs round robin on after_save callbacks' do
expect(conversation.reload.assignee).to eq(agent)
end
it 'will not auto assign agent if enable_auto_assignment is false' do
inbox.update(enable_auto_assignment: false)
expect(conversation.reload.assignee).to be_nil
end
it 'will not auto assign agent if its a bot conversation' do
conversation = create(
:conversation,
account: account,
contact: create(:contact, account: account),
inbox: inbox,
status: 'pending',
assignee: nil
)
expect(conversation.reload.assignee).to be_nil
end
it 'gets triggered on update only when status changes to open' do
conversation.status = 'resolved'
conversation.save!
expect(conversation.reload.assignee).to eq(agent)
inbox.inbox_members.where(user_id: agent.id).first.destroy!
# round robin changes assignee in this case since agent doesn't have access to inbox
agent2 = create(:user, email: 'agent2@example.com', account: account, auto_offline: false)
create(:inbox_member, inbox: inbox, user: agent2)
allow(Redis::Alfred).to receive(:rpoplpush).and_return(agent2.id)
conversation.status = 'open'
conversation.save!
expect(conversation.reload.assignee).to eq(agent2)
end
end
end