fix: SQL error when rules with missing attributes is triggered (#8673)

This commit is contained in:
Shivam Mishra
2024-01-22 16:09:34 +05:30
committed by GitHub
parent bc04d81a5a
commit 1dc66db516
5 changed files with 181 additions and 4 deletions

View File

@@ -83,6 +83,7 @@ describe AutomationRuleListener do
{
attribute_key: 'customer_type',
filter_operator: 'equal_to',
custom_attribute_type: 'contact_attribute',
values: ['platinum'],
query_operator: 'AND'
}.with_indifferent_access,
@@ -154,6 +155,7 @@ describe AutomationRuleListener do
{
attribute_key: 'customer_type',
filter_operator: 'equal_to',
custom_attribute_type: 'contact_attribute',
values: ['platinum'],
query_operator: nil
}.with_indifferent_access

View File

@@ -0,0 +1,105 @@
require 'rails_helper'
RSpec.describe AutomationRules::ConditionValidationService do
let(:account) { create(:account) }
let(:rule) { create(:automation_rule, account: account) }
describe '#perform' do
context 'with standard attributes' do
before do
rule.conditions = [
{ 'values': ['open'], 'attribute_key': 'status', 'query_operator': nil, 'filter_operator': 'equal_to' },
{ 'values': ['+918484'], 'attribute_key': 'phone_number', 'query_operator': 'OR', 'filter_operator': 'contains' },
{ 'values': ['test'], 'attribute_key': 'email', 'query_operator': nil, 'filter_operator': 'contains' }
]
rule.save
end
it 'returns true' do
expect(described_class.new(rule).perform).to be(true)
end
end
context 'with wrong attribute' do
before do
rule.conditions = [
{ 'values': ['open'], 'attribute_key': 'not-a-standard-attribute-for-sure', 'query_operator': nil, 'filter_operator': 'equal_to' }
]
rule.save
end
it 'returns false' do
expect(described_class.new(rule).perform).to be(false)
end
end
context 'with wrong filter operator' do
before do
rule.conditions = [
{ 'values': ['open'], 'attribute_key': 'status', 'query_operator': nil, 'filter_operator': 'not-a-filter-operator' }
]
rule.save
end
it 'returns false' do
expect(described_class.new(rule).perform).to be(false)
end
end
context 'with "attribute_changed" filter operator' do
before do
rule.conditions = [
{ 'values': ['open'], 'attribute_key': 'status', 'query_operator': nil, 'filter_operator': 'attribute_changed' }
]
rule.save
end
it 'returns true' do
expect(described_class.new(rule).perform).to be(true)
end
end
context 'with correct custom attribute' do
before do
create(:custom_attribute_definition,
attribute_key: 'custom_attr_priority',
account: account,
attribute_model: 'conversation_attribute',
attribute_display_type: 'list',
attribute_values: %w[P0 P1 P2])
rule.conditions = [
{
'values': ['true'],
'attribute_key': 'custom_attr_priority',
'filter_operator': 'equal_to',
'custom_attribute_type': 'conversation_attribute'
}
]
rule.save
end
it 'returns true' do
expect(described_class.new(rule).perform).to be(true)
end
end
context 'with missing custom attribute' do
before do
rule.conditions = [
{
'values': ['true'],
'attribute_key': 'attribute_is_not_present', # the attribute is not present
'filter_operator': 'equal_to',
'custom_attribute_type': 'conversation_attribute'
}
]
rule.save
end
it 'returns false for missing custom attribute' do
expect(described_class.new(rule).perform).to be(false)
end
end
end
end