chatwoot/spec/models/automation_rule_spec.rb
Sojan Jose aee979ee0b
fix: add explicit remove assignment actions to macros and automations (#12172)
This updates macros and automations so agents can explicitly remove
assigned agents or teams, while keeping the existing `Assign -> None`
flow working for backward compatibility.

Fixes: #7551
Closes: #7551

## Why
The original macro change exposed unassignment only through `Assign ->
None`, which made macros behave differently from automations and left
the explicit remove actions inconsistent across the product. This keeps
the lower-risk compatibility path and adds the explicit remove actions
requested in review.

## What this change does
- Adds `Remove Assigned Agent` and `Remove Assigned Team` as explicit
actions in macros.
- Adds the same explicit remove actions in automations.
- Keeps `Assign Agent -> None` and `Assign Team -> None` working for
existing behavior and stored payloads.
- Preserves backward compatibility for existing macro and automation
execution payloads.
- Downmerges the latest `develop` and resolves the conflicts while
keeping both the new remove actions and current `develop` behavior.

## Validation
- Verified both remove actions are available and selectable in the macro
editor.
- Verified both remove actions are available and selectable in the
automation builder.
- Applied a disposable macro with `Remove Assigned Agent` and `Remove
Assigned Team` on a real conversation and confirmed both fields were
cleared.
- Applied a disposable macro with `Assign Agent -> None` and `Assign
Team -> None` on a real conversation and confirmed both fields were
still cleared.
2026-04-16 15:57:41 +05:30

141 lines
4.0 KiB
Ruby

require 'rails_helper'
require Rails.root.join 'spec/models/concerns/reauthorizable_shared.rb'
RSpec.describe AutomationRule do
describe 'concerns' do
it_behaves_like 'reauthorizable'
end
describe 'associations' do
let(:account) { create(:account) }
let(:params) do
{
name: 'Notify Conversation Created and mark priority query',
description: 'Notify all administrator about conversation created and mark priority query',
event_name: 'conversation_created',
account_id: account.id,
conditions: [
{
attribute_key: 'browser_language',
filter_operator: 'equal_to',
values: ['en'],
query_operator: 'AND'
},
{
attribute_key: 'country_code',
filter_operator: 'equal_to',
values: %w[USA UK],
query_operator: nil
}
],
actions: [
{
action_name: :send_message,
action_params: ['Welcome to the chatwoot platform.']
},
{
action_name: :assign_team,
action_params: [1]
},
{
action_name: :remove_assigned_agent
},
{
action_name: :remove_assigned_team
},
{
action_name: :add_label,
action_params: %w[support priority_customer]
},
{
action_name: :assign_agent,
action_params: [1]
}
]
}.with_indifferent_access
end
it 'returns valid record' do
rule = FactoryBot.build(:automation_rule, params)
expect(rule.valid?).to be true
end
it 'returns invalid record' do
params[:conditions][0].delete('query_operator')
rule = FactoryBot.build(:automation_rule, params)
expect(rule.valid?).to be false
expect(rule.errors.messages[:conditions]).to eq(['Automation conditions should have query operator.'])
end
it 'allows labels as a valid condition attribute' do
params[:conditions] = [
{
attribute_key: 'labels',
filter_operator: 'equal_to',
values: ['bug'],
query_operator: nil
}
]
rule = FactoryBot.build(:automation_rule, params)
expect(rule.valid?).to be true
end
it 'validates label condition operators' do
params[:conditions] = [
{
attribute_key: 'labels',
filter_operator: 'is_present',
values: [],
query_operator: nil
}
]
rule = FactoryBot.build(:automation_rule, params)
expect(rule.valid?).to be true
end
it 'allows private_note as a valid condition attribute' do
params[:conditions] = [
{
attribute_key: 'private_note',
filter_operator: 'equal_to',
values: [true],
query_operator: nil
}
]
rule = FactoryBot.build(:automation_rule, params)
expect(rule.valid?).to be true
end
end
describe 'reauthorizable' do
context 'when prompt_reauthorization!' do
it 'marks the rule inactive' do
rule = create(:automation_rule)
expect(rule.active).to be true
rule.prompt_reauthorization!
expect(rule.active).to be false
end
end
context 'when reauthorization_required?' do
it 'unsets the error count if conditions are updated' do
rule = create(:automation_rule)
rule.prompt_reauthorization!
expect(rule.reauthorization_required?).to be true
rule.update!(conditions: [{ attribute_key: 'browser_language', filter_operator: 'equal_to', values: ['en'], query_operator: 'AND' }])
expect(rule.reauthorization_required?).to be false
end
it 'will not unset the error count if conditions are not updated' do
rule = create(:automation_rule)
rule.prompt_reauthorization!
expect(rule.reauthorization_required?).to be true
rule.update!(name: 'Updated name')
expect(rule.reauthorization_required?).to be true
end
end
end
end