2022-01-10 07:11:59 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
let!(:inbox) { create(:inbox, account: account, enable_auto_assignment: false) }
|
|
|
|
let!(:contact) { create(:contact, account: account) }
|
|
|
|
let(:contact_inbox) { create(:contact_inbox, inbox_id: inbox.id, contact_id: contact.id) }
|
|
|
|
|
|
|
|
describe 'GET /api/v1/accounts/{account.id}/automation_rules' do
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'returns all records' do
|
|
|
|
automation_rule = create(:automation_rule, account: account, name: 'Test Automation Rule')
|
|
|
|
|
|
|
|
get "/api/v1/accounts/#{account.id}/automation_rules",
|
|
|
|
headers: administrator.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
2022-01-13 05:51:06 +00:00
|
|
|
expect(body[:payload].first[:id]).to eq(automation_rule.id)
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/accounts/{account.id}/automation_rules' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'name': 'Notify Conversation Created and mark priority query',
|
|
|
|
'description': 'Notify all administrator about conversation created and mark priority query',
|
|
|
|
'event_name': 'conversation_created',
|
|
|
|
'conditions': [
|
2022-01-10 07:11:59 +00:00
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'attribute_key': 'browser_language',
|
|
|
|
'filter_operator': 'equal_to',
|
|
|
|
'values': ['en'],
|
|
|
|
'query_operator': 'AND'
|
2022-01-10 07:11:59 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'attribute_key': 'country_code',
|
|
|
|
'filter_operator': 'equal_to',
|
|
|
|
'values': %w[USA UK],
|
|
|
|
'query_operator': nil
|
2022-01-10 07:11:59 +00:00
|
|
|
}
|
|
|
|
],
|
2022-06-13 06:28:54 +00:00
|
|
|
'actions': [
|
2022-01-10 07:11:59 +00:00
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :send_message,
|
|
|
|
'action_params': ['Welcome to the chatwoot platform.']
|
2022-01-10 07:11:59 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :assign_team,
|
|
|
|
'action_params': [1]
|
2022-01-10 07:11:59 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :add_label,
|
|
|
|
'action_params': %w[support priority_customer]
|
2022-01-10 07:11:59 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :assign_best_administrator,
|
|
|
|
'action_params': [1]
|
2022-01-10 07:11:59 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :update_additional_attributes,
|
|
|
|
'action_params': [{ intiated_at: '2021-12-03 17:25:26.844536 +0530' }]
|
2022-01-10 07:11:59 +00:00
|
|
|
}
|
|
|
|
]
|
2022-06-13 06:28:54 +00:00
|
|
|
}
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 02:38:58 +00:00
|
|
|
it 'throws an error for unknown attributes in condtions' do
|
|
|
|
expect(account.automation_rules.count).to eq(0)
|
|
|
|
params[:conditions] << {
|
2022-06-13 06:28:54 +00:00
|
|
|
'attribute_key': 'unknown_attribute',
|
|
|
|
'filter_operator': 'equal_to',
|
|
|
|
'values': ['en'],
|
|
|
|
'query_operator': 'AND'
|
2022-03-30 02:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
expect(account.automation_rules.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2022-01-10 07:11:59 +00:00
|
|
|
it 'Saves for automation_rules for account with country_code and browser_language conditions' do
|
|
|
|
expect(account.automation_rules.count).to eq(0)
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(account.automation_rules.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Saves for automation_rules for account with status conditions' do
|
|
|
|
params[:conditions] = [
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'attribute_key': 'status',
|
|
|
|
'filter_operator': 'equal_to',
|
|
|
|
'values': ['resolved'],
|
|
|
|
'query_operator': nil
|
|
|
|
}
|
2022-01-10 07:11:59 +00:00
|
|
|
]
|
|
|
|
expect(account.automation_rules.count).to eq(0)
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(account.automation_rules.count).to eq(1)
|
|
|
|
end
|
2022-03-30 02:38:58 +00:00
|
|
|
|
|
|
|
it 'Saves file in the automation actions to send an attachments' do
|
|
|
|
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
2022-04-24 06:32:40 +00:00
|
|
|
|
|
|
|
expect(account.automation_rules.count).to eq(0)
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules/attach_file",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: { attachment: file }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
|
|
|
|
blob = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(blob['blob_key']).to be_present
|
|
|
|
expect(blob['blob_id']).to be_present
|
|
|
|
|
2022-03-30 02:38:58 +00:00
|
|
|
params[:actions] = [
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :send_message,
|
|
|
|
'action_params': ['Welcome to the chatwoot platform.']
|
2022-03-30 02:38:58 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :send_attachment,
|
|
|
|
'action_params': [blob['blob_id']]
|
2022-03-30 02:38:58 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: params
|
|
|
|
|
|
|
|
automation_rule = account.automation_rules.first
|
|
|
|
expect(automation_rule.files.presence).to be_truthy
|
|
|
|
expect(automation_rule.files.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'Saves files in the automation actions to send multiple attachments' do
|
|
|
|
file_1 = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
2022-04-24 06:32:40 +00:00
|
|
|
file_2 = fixture_file_upload(Rails.root.join('spec/assets/sample.png'), 'image/png')
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules/attach_file",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: { attachment: file_1 }
|
|
|
|
|
|
|
|
blob_1 = JSON.parse(response.body)
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules/attach_file",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: { attachment: file_2 }
|
|
|
|
|
|
|
|
blob_2 = JSON.parse(response.body)
|
|
|
|
|
2022-03-30 02:38:58 +00:00
|
|
|
params[:actions] = [
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :send_attachment,
|
|
|
|
'action_params': [blob_1['blob_id']]
|
2022-03-30 02:38:58 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :send_attachment,
|
|
|
|
'action_params': [blob_2['blob_id']]
|
2022-03-30 02:38:58 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: params
|
|
|
|
|
|
|
|
automation_rule = account.automation_rules.first
|
|
|
|
expect(automation_rule.files.count).to eq(2)
|
|
|
|
end
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
end
|
2022-01-20 23:30:21 +00:00
|
|
|
|
|
|
|
describe 'GET /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}' do
|
|
|
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'returns for automation_rule for account' do
|
|
|
|
expect(account.automation_rules.count).to eq(1)
|
|
|
|
|
|
|
|
get "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
|
|
|
|
headers: administrator.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:payload]).to be_present
|
|
|
|
expect(body[:payload][:id]).to eq(automation_rule.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}/clone' do
|
|
|
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}/clone"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'returns for cloned automation_rule for account' do
|
|
|
|
expect(account.automation_rules.count).to eq(1)
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}/clone",
|
|
|
|
headers: administrator.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:payload]).to be_present
|
|
|
|
expect(body[:payload][:id]).not_to eq(automation_rule.id)
|
|
|
|
expect(account.automation_rules.count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}' do
|
|
|
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
patch "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
2022-04-14 08:06:55 +00:00
|
|
|
let(:update_params) do
|
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'description': 'Update description',
|
|
|
|
'name': 'Update name',
|
|
|
|
'conditions': [
|
2022-04-14 08:06:55 +00:00
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'attribute_key': 'browser_language',
|
|
|
|
'filter_operator': 'equal_to',
|
|
|
|
'values': ['en'],
|
|
|
|
'query_operator': 'AND'
|
2022-04-14 08:06:55 +00:00
|
|
|
}
|
|
|
|
],
|
2022-06-13 06:28:54 +00:00
|
|
|
'actions': [
|
2022-04-14 08:06:55 +00:00
|
|
|
{
|
2022-06-13 06:28:54 +00:00
|
|
|
'action_name': :update_additional_attributes,
|
|
|
|
'action_params': [{ intiated_at: '2021-12-03 17:25:26.844536 +0530' }]
|
2022-04-14 08:06:55 +00:00
|
|
|
}
|
|
|
|
]
|
2022-06-13 06:28:54 +00:00
|
|
|
}
|
2022-04-14 08:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns for cloned automation_rule for account' do
|
2022-01-20 23:30:21 +00:00
|
|
|
expect(account.automation_rules.count).to eq(1)
|
2022-04-14 08:06:55 +00:00
|
|
|
expect(account.automation_rules.first.actions.size).to eq(4)
|
2022-01-20 23:30:21 +00:00
|
|
|
|
|
|
|
patch "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
|
|
|
|
headers: administrator.create_new_auth_token,
|
2022-04-14 08:06:55 +00:00
|
|
|
params: update_params
|
2022-01-20 23:30:21 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:payload][:name]).to eq('Update name')
|
2022-04-14 08:06:55 +00:00
|
|
|
expect(body[:payload][:description]).to eq('Update description')
|
|
|
|
expect(body[:payload][:conditions].size).to eq(1)
|
|
|
|
expect(body[:payload][:actions].size).to eq(1)
|
2022-01-20 23:30:21 +00:00
|
|
|
end
|
2022-04-12 14:53:34 +00:00
|
|
|
|
|
|
|
it 'returns for updated active flag for automation_rule' do
|
|
|
|
expect(automation_rule.active).to eq(true)
|
|
|
|
params = { active: false }
|
|
|
|
|
|
|
|
patch "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
params: params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:payload][:active]).to eq(false)
|
|
|
|
expect(automation_rule.reload.active).to eq(false)
|
|
|
|
end
|
2022-01-20 23:30:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}' do
|
|
|
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
it 'delete the automation_rule for account' do
|
|
|
|
expect(account.automation_rules.count).to eq(1)
|
|
|
|
|
|
|
|
delete "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
|
|
|
|
headers: administrator.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(account.automation_rules.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|