feat: Macros CRUD api (#5047)
This commit is contained in:
176
spec/controllers/api/v1/accounts/macros_controller_spec.rb
Normal file
176
spec/controllers/api/v1/accounts/macros_controller_spec.rb
Normal file
@@ -0,0 +1,176 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:administrator) { create(:user, account: account, role: :administrator) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let(:agent_1) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
create(:macro, account: account, created_by: administrator, updated_by: administrator, visibility: :global)
|
||||
create(:macro, account: account, created_by: administrator, updated_by: administrator, visibility: :global)
|
||||
create(:macro, account: account, created_by: administrator, updated_by: administrator, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent, updated_by: agent, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent, updated_by: agent, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent_1, updated_by: agent_1, visibility: :personal)
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/macros' do
|
||||
context 'when it is an authenticated administrator' do
|
||||
it 'returns all records in the account' do
|
||||
get "/api/v1/accounts/#{account.id}/macros",
|
||||
headers: administrator.create_new_auth_token
|
||||
|
||||
visible_macros = account.macros
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
body = JSON.parse(response.body)
|
||||
|
||||
expect(body['payload'].length).to eq(visible_macros.count)
|
||||
expect(body['payload'].first['id']).to eq(Macro.first.id)
|
||||
expect(body['payload'].last['id']).to eq(Macro.last.id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated agent' do
|
||||
it 'returns all records in account and created_by the agent' do
|
||||
get "/api/v1/accounts/#{account.id}/macros",
|
||||
headers: agent.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
body = JSON.parse(response.body)
|
||||
visible_macros = account.macros.global.or(account.macros.personal.where(created_by_id: agent.id))
|
||||
|
||||
expect(body['payload'].length).to eq(visible_macros.count)
|
||||
expect(body['payload'].first['id']).to eq(visible_macros.first.id)
|
||||
expect(body['payload'].last['id']).to eq(visible_macros.last.id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post "/api/v1/accounts/#{account.id}/macros"
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/macros' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post "/api/v1/accounts/#{account.id}/macros"
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:params) do
|
||||
{
|
||||
'name': 'Add label, send message and close the chat',
|
||||
'actions': [
|
||||
{
|
||||
'action_name': :add_label,
|
||||
'action_params': %w[support priority_customer]
|
||||
},
|
||||
{
|
||||
'action_name': :send_message,
|
||||
'action_params': ['Welcome to the chatwoot platform.']
|
||||
},
|
||||
{
|
||||
'action_name': :resolved
|
||||
}
|
||||
],
|
||||
visibility: 'global',
|
||||
created_by_id: administrator.id
|
||||
}.with_indifferent_access
|
||||
end
|
||||
|
||||
it 'creates the macro' do
|
||||
post "/api/v1/accounts/#{account.id}/macros",
|
||||
params: params,
|
||||
headers: administrator.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['payload']['name']).to eql(params['name'])
|
||||
expect(json_response['payload']['visibility']).to eql(params['visibility'])
|
||||
expect(json_response['payload']['created_by']['id']).to eql(administrator.id)
|
||||
end
|
||||
|
||||
it 'sets visibility default to personal for agent' do
|
||||
post "/api/v1/accounts/#{account.id}/macros",
|
||||
params: params,
|
||||
headers: agent.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['payload']['name']).to eql(params['name'])
|
||||
expect(json_response['payload']['visibility']).to eql('personal')
|
||||
expect(json_response['payload']['created_by']['id']).to eql(agent.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /api/v1/accounts/{account.id}/macros/{macro.id}' do
|
||||
let!(:macro) { create(:macro, account: account, created_by: administrator, updated_by: administrator) }
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
put "/api/v1/accounts/#{account.id}/macros/#{macro.id}"
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:params) do
|
||||
{
|
||||
'name': 'Add label, send message and close the chat'
|
||||
}
|
||||
end
|
||||
|
||||
it 'Updates the macro' do
|
||||
put "/api/v1/accounts/#{account.id}/macros/#{macro.id}",
|
||||
params: params,
|
||||
headers: administrator.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['name']).to eql(params['name'])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/macros/{macro.id}' do
|
||||
let!(:macro) { create(:macro, account: account, created_by: administrator, updated_by: administrator) }
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/macros/#{macro.id}"
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'fetch the macro' do
|
||||
get "/api/v1/accounts/#{account.id}/macros/#{macro.id}",
|
||||
headers: administrator.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['payload']['name']).to eql(macro.name)
|
||||
expect(json_response['payload']['created_by']['id']).to eql(administrator.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
spec/factories/macros.rb
Normal file
11
spec/factories/macros.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
FactoryBot.define do
|
||||
factory :macro do
|
||||
account
|
||||
name { 'wrong_message_actions' }
|
||||
actions do
|
||||
[
|
||||
{ 'action_name' => 'add_label', 'action_params' => %w[wrong_chat] }
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
85
spec/models/macro_spec.rb
Normal file
85
spec/models/macro_spec.rb
Normal file
@@ -0,0 +1,85 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Macro, type: :model do
|
||||
let(:account) { create(:account) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to belong_to(:created_by) }
|
||||
it { is_expected.to belong_to(:updated_by) }
|
||||
end
|
||||
|
||||
describe '#set_visibility' do
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let(:macro) { create(:macro, account: account, created_by: admin, updated_by: admin) }
|
||||
|
||||
context 'when user is administrator' do
|
||||
it 'set visibility with params' do
|
||||
expect(macro.visibility).to eq('personal')
|
||||
|
||||
macro.set_visibility(admin, { visibility: :global })
|
||||
|
||||
expect(macro.visibility).to eq('global')
|
||||
|
||||
macro.set_visibility(admin, { visibility: :personal })
|
||||
|
||||
expect(macro.visibility).to eq('personal')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when user is agent' do
|
||||
it 'set visibility always to agent' do
|
||||
Current.user = agent
|
||||
Current.account = account
|
||||
|
||||
expect(macro.visibility).to eq('personal')
|
||||
|
||||
macro.set_visibility(agent, { visibility: :global })
|
||||
|
||||
expect(macro.visibility).to eq('personal')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#with_visibility' do
|
||||
let(:agent_1) { create(:user, account: account, role: :agent) }
|
||||
let(:agent_2) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
create(:macro, account: account, created_by: admin, updated_by: admin, visibility: :global)
|
||||
create(:macro, account: account, created_by: admin, updated_by: admin, visibility: :global)
|
||||
create(:macro, account: account, created_by: admin, updated_by: admin, visibility: :personal)
|
||||
create(:macro, account: account, created_by: admin, updated_by: admin, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent_1, updated_by: agent_1, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent_1, updated_by: agent_1, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent_2, updated_by: agent_2, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent_2, updated_by: agent_2, visibility: :personal)
|
||||
create(:macro, account: account, created_by: agent_2, updated_by: agent_2, visibility: :personal)
|
||||
end
|
||||
|
||||
context 'when user is administrator' do
|
||||
it 'return all macros in account' do
|
||||
Current.user = admin
|
||||
Current.account = account
|
||||
|
||||
expect(described_class.with_visibility(admin, {}).count).to eq(account.macros.count)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when user is agent' do
|
||||
it 'return all macros in account and created_by user' do
|
||||
Current.user = agent_2
|
||||
Current.account = account
|
||||
|
||||
macros_for_agent_2 = account.macros.global.count + agent_2.macros.personal.count
|
||||
expect(described_class.with_visibility(agent_2, {}).count).to eq(macros_for_agent_2)
|
||||
|
||||
Current.user = agent_1
|
||||
|
||||
macros_for_agent_1 = account.macros.global.count + agent_1.macros.personal.count
|
||||
expect(described_class.with_visibility(agent_1, {}).count).to eq(macros_for_agent_1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user