2020-02-08 17:35:41 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Agents API', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
2021-09-02 12:59:45 +00:00
|
|
|
let(:admin) { create(:user, custom_attributes: { test: 'test' }, account: account, role: :administrator) }
|
2021-06-11 06:14:31 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2020-02-08 17:35:41 +00:00
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/agents' do
|
2020-02-08 17:35:41 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/agents"
|
2020-02-08 17:35:41 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
2020-03-09 17:57:10 +00:00
|
|
|
let!(:agent) { create(:user, account: account, role: :agent) }
|
2020-02-08 17:35:41 +00:00
|
|
|
|
|
|
|
it 'returns all agents of account' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/agents",
|
2020-02-08 17:35:41 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(JSON.parse(response.body).size).to eq(account.users.count)
|
|
|
|
end
|
2021-09-02 12:59:45 +00:00
|
|
|
|
|
|
|
it 'returns custom fields on agents if present' do
|
|
|
|
agent.update(custom_attributes: { test: 'test' })
|
|
|
|
|
|
|
|
get "/api/v1/accounts/#{account.id}/agents",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
expect(data.first['custom_attributes']['test']).to eq('test')
|
|
|
|
end
|
2020-02-08 17:35:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/agents/:id' do
|
2020-02-08 17:35:41 +00:00
|
|
|
let(:other_agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}"
|
2020-02-08 17:35:41 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
2021-06-11 06:14:31 +00:00
|
|
|
it 'returns unauthorized for agents' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
2020-02-08 17:35:41 +00:00
|
|
|
|
|
|
|
it 'deletes an agent' do
|
2020-03-09 17:57:10 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
|
2020-02-08 17:35:41 +00:00
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(account.users.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'PUT /api/v1/accounts/{account.id}/agents/:id' do
|
2020-02-08 17:35:41 +00:00
|
|
|
let(:other_agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}"
|
2020-02-08 17:35:41 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
params = { name: 'TestUser' }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
it 'returns unauthorized for agents' do
|
|
|
|
put "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
|
2021-10-07 07:51:46 +00:00
|
|
|
it 'modifies an agent name' do
|
2020-03-09 17:57:10 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
|
2020-02-08 17:35:41 +00:00
|
|
|
params: params,
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(other_agent.reload.name).to eq(params[:name])
|
|
|
|
end
|
2021-10-07 07:51:46 +00:00
|
|
|
|
|
|
|
it 'modifies an agents account user attributes' do
|
|
|
|
put "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
|
|
|
|
params: { role: 'administrator', availability: 'busy', auto_offline: false },
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body)
|
|
|
|
expect(response_data['role']).to eq('administrator')
|
|
|
|
expect(response_data['availability_status']).to eq('busy')
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(response_data['auto_offline']).to be(false)
|
2021-10-07 07:51:46 +00:00
|
|
|
expect(other_agent.account_users.first.role).to eq('administrator')
|
|
|
|
end
|
2020-02-08 17:35:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/agents' do
|
2020-02-08 17:35:41 +00:00
|
|
|
let(:other_agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/agents"
|
2020-02-08 17:35:41 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
params = { name: 'NewUser', email: Faker::Internet.email, role: :agent }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
it 'returns unauthorized for agents' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/agents",
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
|
2020-02-08 17:35:41 +00:00
|
|
|
it 'creates a new agent' do
|
2020-03-09 17:57:10 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/agents",
|
2020-02-08 17:35:41 +00:00
|
|
|
params: params,
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(account.users.last.name).to eq('NewUser')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|