feat: Add AgentBot APIs (#2323)

This commit is contained in:
Sojan Jose
2021-06-01 22:34:25 +05:30
committed by GitHub
parent 9d16e52e33
commit 22965be6dc
31 changed files with 552 additions and 67 deletions

View File

@@ -0,0 +1,180 @@
require 'rails_helper'
RSpec.describe 'Agent Bot API', type: :request do
let!(:account) { create(:account) }
let!(:agent_bot) { create(:agent_bot, account: account) }
let(:admin) { create(:user, account: account, role: :administrator) }
let(:agent) { create(:user, account: account, role: :agent) }
describe 'GET /api/v1/accounts/{account.id}/agent_bots' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/agent_bots"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'returns all the agent_bots in account along with global agent bots' do
global_bot = create(:agent_bot)
get "/api/v1/accounts/#{account.id}/agent_bots",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(response.body).to include(agent_bot.name)
expect(response.body).to include(global_bot.name)
end
end
end
describe 'GET /api/v1/accounts/{account.id}/agent_bots/:id' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'shows the agent bot' do
get "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(response.body).to include(agent_bot.name)
end
it 'will show a global agent bot' do
global_bot = create(:agent_bot)
get "/api/v1/accounts/#{account.id}/agent_bots/#{global_bot.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(response.body).to include(global_bot.name)
end
end
end
describe 'POST /api/v1/accounts/{account.id}/agent_bots' do
let(:valid_params) { { name: 'test' } }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
expect { post "/api/v1/accounts/#{account.id}/agent_bots", params: valid_params }.to change(Label, :count).by(0)
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'creates the agent bot when administrator' do
expect do
post "/api/v1/accounts/#{account.id}/agent_bots", headers: admin.create_new_auth_token,
params: valid_params
end.to change(AgentBot, :count).by(1)
expect(response).to have_http_status(:success)
end
it 'would not create the agent bot when agent' do
expect do
post "/api/v1/accounts/#{account.id}/agent_bots", headers: agent.create_new_auth_token,
params: valid_params
end.to change(AgentBot, :count).by(0)
expect(response).to have_http_status(:unauthorized)
end
end
end
describe 'PATCH /api/v1/accounts/{account.id}/agent_bots/:id' do
let(:valid_params) { { name: 'test_updated' } }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
patch "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
params: valid_params
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'updates the agent bot' do
patch "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json
expect(response).to have_http_status(:success)
expect(agent_bot.reload.name).to eq('test_updated')
end
it 'would not update the agent bot when agent' do
patch "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
headers: agent.create_new_auth_token,
params: valid_params,
as: :json
expect(response).to have_http_status(:unauthorized)
expect(agent_bot.reload.name).not_to eq('test_updated')
end
it 'would not update a global agent bot' do
global_bot = create(:agent_bot)
patch "/api/v1/accounts/#{account.id}/agent_bots/#{global_bot.id}",
headers: admin.create_new_auth_token,
params: valid_params,
as: :json
expect(response).to have_http_status(:not_found)
expect(agent_bot.reload.name).not_to eq('test_updated')
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/agent_bots/:id' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'deletes an agent bot when administrator' do
delete "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(account.agent_bots.size).to eq(0)
end
it 'would not delete the agent bot when agent' do
delete "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
expect(account.agent_bots.size).not_to eq(0)
end
it 'would not delete a global agent bot' do
global_bot = create(:agent_bot)
delete "/api/v1/accounts/#{account.id}/agent_bots/#{global_bot.id}",
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:not_found)
expect(account.agent_bots.size).not_to eq(0)
end
end
end
end

View File

@@ -2,6 +2,8 @@ require 'rails_helper'
RSpec.describe 'Inboxes API', type: :request do
let(:account) { create(:account) }
let(:agent) { create(:user, account: account, role: :agent) }
let(:admin) { create(:user, account: account, role: :administrator) }
describe 'GET /api/v1/accounts/{account.id}/inboxes' do
context 'when it is an unauthenticated user' do
@@ -15,11 +17,11 @@ RSpec.describe 'Inboxes API', type: :request do
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:admin) { create(:user, account: account, role: :administrator) }
let(:inbox) { create(:inbox, account: account) }
before do
create(:inbox, account: account)
second_inbox = create(:inbox, account: account)
create(:inbox_member, user: agent, inbox: second_inbox)
create(:inbox_member, user: agent, inbox: inbox)
end
it 'returns all inboxes of current_account as administrator' do
@@ -54,9 +56,6 @@ RSpec.describe 'Inboxes API', type: :request do
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:admin) { create(:user, account: account, role: :administrator) }
before do
create(:inbox_member, user: agent, inbox: inbox)
end
@@ -92,7 +91,9 @@ RSpec.describe 'Inboxes API', type: :request do
let!(:campaign) { create(:campaign, account: account, inbox: inbox) }
it 'returns unauthorized for agents' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/campaigns"
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/campaigns",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
@@ -263,6 +264,42 @@ RSpec.describe 'Inboxes API', type: :request do
end
end
describe 'GET /api/v1/accounts/{account.id}/inboxes/{inbox.id}/agent_bot' do
let(:inbox) { create(:inbox, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/agent_bot"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'returns empty when no agent bot is present' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/agent_bot",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
inbox_data = JSON.parse(response.body, symbolize_names: true)
expect(inbox_data[:agent_bot].blank?).to eq(true)
end
it 'returns the agent bot attached to the inbox' do
agent_bot = create(:agent_bot)
create(:agent_bot_inbox, agent_bot: agent_bot, inbox: inbox)
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/agent_bot",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
inbox_data = JSON.parse(response.body, symbolize_names: true)
expect(inbox_data[:agent_bot][:name]).to eq agent_bot.name
end
end
end
describe 'POST /api/v1/accounts/{account.id}/inboxes/:id/set_agent_bot' do
let(:inbox) { create(:inbox, account: account) }
let(:agent_bot) { create(:agent_bot) }

View File

@@ -1,17 +0,0 @@
require 'rails_helper'
RSpec.describe 'Profile API', type: :request do
let!(:agent_bot1) { create(:agent_bot) }
let!(:agent_bot2) { create(:agent_bot) }
describe 'GET /api/v1/agent_bots' do
it 'returns all the agent bots in the system' do
get '/api/v1/agent_bots',
as: :json
expect(response).to have_http_status(:success)
expect(response.body).to include(agent_bot1.name)
expect(response.body).to include(agent_bot2.name)
end
end
end