2020-02-08 18:18:00 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Conversation Assignment API', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/<id>/assignments' do
|
2020-02-08 18:18:00 +00:00
|
|
|
let(:conversation) { create(:conversation, account: account) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id)
|
2020-02-08 18:18:00 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
context 'when it is an authenticated user with access to the inbox' do
|
2020-02-08 18:18:00 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2021-01-31 07:10:02 +00:00
|
|
|
let(:team) { create(:team, account: account) }
|
2020-02-08 18:18:00 +00:00
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
|
|
|
end
|
|
|
|
|
2020-02-08 18:18:00 +00:00
|
|
|
it 'assigns a user to the conversation' do
|
|
|
|
params = { assignee_id: agent.id }
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
|
2020-02-08 18:18:00 +00:00
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.assignee).to eq(agent)
|
|
|
|
end
|
2021-01-31 07:10:02 +00:00
|
|
|
|
|
|
|
it 'assigns a team to the conversation' do
|
|
|
|
params = { team_id: team.id }
|
|
|
|
|
|
|
|
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.team).to eq(team)
|
|
|
|
end
|
2020-02-08 18:18:00 +00:00
|
|
|
end
|
2021-01-14 08:51:41 +00:00
|
|
|
|
|
|
|
context 'when conversation already has an assignee' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
conversation.update!(assignee: agent)
|
2021-06-11 06:14:31 +00:00
|
|
|
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
2021-01-14 08:51:41 +00:00
|
|
|
end
|
|
|
|
|
2021-01-31 07:10:02 +00:00
|
|
|
it 'unassigns the assignee from the conversation' do
|
2021-01-14 08:51:41 +00:00
|
|
|
params = { assignee_id: 0 }
|
|
|
|
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.assignee).to eq(nil)
|
2021-11-12 10:47:59 +00:00
|
|
|
expect(Conversations::ActivityMessageJob)
|
|
|
|
.to(have_been_enqueued.at_least(:once)
|
|
|
|
.with(conversation, { account_id: conversation.account_id, inbox_id: conversation.inbox_id, message_type: :activity,
|
|
|
|
content: "Conversation unassigned by #{agent.name}" }))
|
2021-01-14 08:51:41 +00:00
|
|
|
end
|
|
|
|
end
|
2021-01-31 07:10:02 +00:00
|
|
|
|
|
|
|
context 'when conversation already has a team' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
let(:team) { create(:team, account: account) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
conversation.update!(team: team)
|
2021-06-11 06:14:31 +00:00
|
|
|
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
2021-01-31 07:10:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'unassigns the team from the conversation' do
|
|
|
|
params = { team_id: 0 }
|
|
|
|
post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id),
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.team).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2020-02-08 18:18:00 +00:00
|
|
|
end
|
|
|
|
end
|