2020-02-17 10:27:57 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Conversations API', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/conversations' do
|
2020-02-17 10:27:57 +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}/conversations"
|
2020-02-17 10:27:57 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2021-01-31 07:35:07 +00:00
|
|
|
let(:conversation) { create(:conversation, account: account) }
|
2022-05-22 07:38:41 +00:00
|
|
|
let(:attended_conversation) { create(:conversation, account: account, first_reply_created_at: Time.now.utc) }
|
|
|
|
let(:unattended_conversation) { create(:conversation, account: account, first_reply_created_at: nil) }
|
2020-02-17 10:27:57 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
2021-01-31 07:35:07 +00:00
|
|
|
it 'returns all conversations with messages' do
|
|
|
|
message = create(:message, conversation: conversation, account: account)
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:data][:meta][:all_count]).to eq(1)
|
2021-07-19 13:40:58 +00:00
|
|
|
expect(body[:data][:meta].keys).to include(:all_count, :mine_count, :assigned_count, :unassigned_count)
|
2021-01-31 07:35:07 +00:00
|
|
|
expect(body[:data][:payload].first[:messages].first[:id]).to eq(message.id)
|
|
|
|
end
|
|
|
|
|
2021-10-12 15:16:00 +00:00
|
|
|
it 'returns conversations with empty messages array for conversations with out messages' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/conversations",
|
2020-02-17 10:27:57 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-01-31 07:35:07 +00:00
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:data][:meta][:all_count]).to eq(1)
|
|
|
|
expect(body[:data][:payload].first[:messages]).to eq([])
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|
2022-05-22 07:38:41 +00:00
|
|
|
|
|
|
|
it 'returns unattended conversations' do
|
|
|
|
agent_1 = create(:user, account: account, role: :agent)
|
|
|
|
create(:inbox_member, user: agent_1, inbox: attended_conversation.inbox)
|
|
|
|
create(:inbox_member, user: agent_1, inbox: unattended_conversation.inbox)
|
|
|
|
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent_1.create_new_auth_token,
|
|
|
|
params: { reply_status: 'unattended' },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:data][:meta][:all_count]).to eq(1)
|
|
|
|
expect(body[:data][:payload].count).to eq(1)
|
|
|
|
end
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-03 06:45:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/conversations/meta' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/meta"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
conversation = create(:conversation, account: account)
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all conversations counts' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/meta",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-07-19 13:40:58 +00:00
|
|
|
body = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(body[:meta].keys).to include(:all_count, :mine_count, :assigned_count, :unassigned_count)
|
|
|
|
expect(body[:meta][:all_count]).to eq(1)
|
2020-05-03 06:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-23 09:57:41 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/conversations/search' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/search", params: { q: 'test' }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
conversation = create(:conversation, account: account)
|
|
|
|
create(:message, conversation: conversation, account: account, content: 'test1')
|
|
|
|
create(:message, conversation: conversation, account: account, content: 'test2')
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all conversations with messages containing the search query' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/search",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { q: 'test1' },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2020-11-07 20:16:45 +00:00
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
2020-09-23 09:57:41 +00:00
|
|
|
expect(response_data[:meta][:all_count]).to eq(1)
|
|
|
|
expect(response_data[:payload].first[:messages].first[:content]).to eq 'test1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/conversations/filter' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2021-11-01 08:27:04 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/filter", params: { q: 'test' }
|
2021-10-20 12:44:56 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
conversation = create(:conversation, account: account)
|
|
|
|
create(:message, conversation: conversation, account: account, content: 'test1')
|
|
|
|
create(:message, conversation: conversation, account: account, content: 'test2')
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all conversations with empty query' do
|
2021-11-01 08:27:04 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/filter",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { payload: [] },
|
|
|
|
as: :json
|
2021-10-20 12:44:56 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
|
2021-11-01 08:27:04 +00:00
|
|
|
expect(response_data.count).to eq(2)
|
2021-10-20 12:44:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/conversations/:id' do
|
2020-02-17 10:27:57 +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
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}"
|
2020-02-17 10:27:57 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2021-06-11 06:14:31 +00:00
|
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
2020-02-17 10:27:57 +00:00
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
it 'does not shows the conversation if you do not have access to it' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows the conversation if you are an administrator' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(conversation.display_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows the conversation if you are an agent with access to inbox' do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}",
|
2020-02-17 10:27:57 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2020-03-08 16:38:25 +00:00
|
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(conversation.display_id)
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-07 08:17:38 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations' do
|
|
|
|
let(:contact) { create(:contact, account: account) }
|
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
|
|
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
params: { source_id: contact_inbox.source_id },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
2022-08-16 11:28:23 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent, auto_offline: false) }
|
2021-10-11 09:47:30 +00:00
|
|
|
let(:team) { create(:team, account: account) }
|
2021-01-07 08:17:38 +00:00
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
it 'will not create a new conversation if agent does not have access to inbox' do
|
2021-01-07 08:17:38 +00:00
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
2021-04-15 09:43:01 +00:00
|
|
|
additional_attributes = { test: 'test' }
|
2021-01-07 08:17:38 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
2021-04-15 09:43:01 +00:00
|
|
|
params: { source_id: contact_inbox.source_id, additional_attributes: additional_attributes },
|
|
|
|
as: :json
|
2021-06-11 06:14:31 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
2021-04-15 09:43:01 +00:00
|
|
|
end
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
context 'when it is an authenticated user who has access to the inbox' do
|
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: inbox)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new conversation' do
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
additional_attributes = { test: 'test' }
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { source_id: contact_inbox.source_id, additional_attributes: additional_attributes },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(response_data[:additional_attributes]).to eq(additional_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a conversation in specificed status' do
|
2021-07-21 16:32:43 +00:00
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { source_id: contact_inbox.source_id, status: 'pending' },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(response_data[:status]).to eq('pending')
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: remove this spec when we remove the condition check in controller
|
|
|
|
# Added for backwards compatibility for bot status
|
|
|
|
it 'creates a conversation as pending if status is specified as bot' do
|
2021-06-11 06:14:31 +00:00
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { source_id: contact_inbox.source_id, status: 'bot' },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
2021-07-21 16:32:43 +00:00
|
|
|
expect(response_data[:status]).to eq('pending')
|
2021-06-11 06:14:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new conversation with message when message is passed' do
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { source_id: contact_inbox.source_id, message: { content: 'hi' } },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(response_data[:additional_attributes]).to eq({})
|
|
|
|
expect(account.conversations.find_by(display_id: response_data[:id]).messages.outgoing.first.content).to eq 'hi'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls contact inbox builder if contact_id and inbox_id is present' do
|
|
|
|
builder = double
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
allow(ContactInboxBuilder).to receive(:new).and_return(builder)
|
|
|
|
allow(builder).to receive(:perform)
|
|
|
|
expect(builder).to receive(:perform)
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { contact_id: contact.id, inbox_id: inbox.id },
|
|
|
|
as: :json
|
|
|
|
end
|
2021-10-11 09:47:30 +00:00
|
|
|
|
|
|
|
it 'creates a new conversation with assignee and team' do
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { source_id: contact_inbox.source_id, contact_id: contact.id, inbox_id: inbox.id, assignee_id: agent.id, team_id: team.id },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_data = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
expect(response_data[:meta][:assignee][:name]).to eq(agent.name)
|
|
|
|
expect(response_data[:meta][:team][:name]).to eq(team.name)
|
|
|
|
end
|
2021-01-07 08:17:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/toggle_status' do
|
2020-02-17 10:27:57 +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/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status"
|
2020-02-17 10:27:57 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2022-02-08 11:09:19 +00:00
|
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
2020-02-17 10:27:57 +00:00
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
2020-02-17 10:27:57 +00:00
|
|
|
it 'toggles the conversation status' do
|
|
|
|
expect(conversation.status).to eq('open')
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
2020-02-17 10:27:57 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.status).to eq('resolved')
|
|
|
|
end
|
2020-03-10 18:32:15 +00:00
|
|
|
|
2021-07-21 16:32:43 +00:00
|
|
|
it 'toggles the conversation status to open from pending' do
|
|
|
|
conversation.update!(status: 'pending')
|
2020-03-10 18:32:15 +00:00
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.status).to eq('open')
|
|
|
|
end
|
2020-06-02 18:20:39 +00:00
|
|
|
|
2022-02-08 11:09:19 +00:00
|
|
|
it 'self assign if agent changes the conversation status to open' do
|
|
|
|
conversation.update!(status: 'pending')
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.status).to eq('open')
|
|
|
|
expect(conversation.reload.assignee_id).to eq(agent.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disbale self assign if admin changes the conversation status to open' do
|
|
|
|
conversation.update!(status: 'pending')
|
|
|
|
conversation.update!(assignee_id: nil)
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.status).to eq('open')
|
|
|
|
expect(conversation.reload.assignee_id).not_to eq(administrator.id)
|
|
|
|
end
|
|
|
|
|
2020-06-02 18:20:39 +00:00
|
|
|
it 'toggles the conversation status to specific status when parameter is passed' do
|
|
|
|
expect(conversation.status).to eq('open')
|
|
|
|
|
2021-07-21 16:32:43 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { status: 'pending' },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.status).to eq('pending')
|
|
|
|
end
|
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
it 'toggles the conversation status to snoozed when parameter is passed' do
|
|
|
|
expect(conversation.status).to eq('open')
|
|
|
|
snoozed_until = (DateTime.now.utc + 2.days).to_i
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { status: 'snoozed', snoozed_until: snoozed_until },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.status).to eq('snoozed')
|
|
|
|
expect(conversation.reload.snoozed_until.to_i).to eq(snoozed_until)
|
|
|
|
end
|
|
|
|
|
2021-07-21 16:32:43 +00:00
|
|
|
# TODO: remove this spec when we remove the condition check in controller
|
|
|
|
# Added for backwards compatibility for bot status
|
|
|
|
it 'toggles the conversation status to pending status when parameter bot is passed' do
|
|
|
|
expect(conversation.status).to eq('open')
|
|
|
|
|
2020-06-02 18:20:39 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_status",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { status: 'bot' },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-07-21 16:32:43 +00:00
|
|
|
expect(conversation.reload.status).to eq('pending')
|
2020-06-02 18:20:39 +00:00
|
|
|
end
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-03 06:47:27 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/toggle_typing_status' do
|
|
|
|
let(:conversation) { create(:conversation, account: account) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_typing_status"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
2020-05-03 06:47:27 +00:00
|
|
|
it 'toggles the conversation status' do
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_typing_status",
|
|
|
|
headers: agent.create_new_auth_token,
|
2021-11-01 08:20:07 +00:00
|
|
|
params: { typing_status: 'on', is_private: false },
|
2020-05-03 06:47:27 +00:00
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
2021-11-01 08:20:07 +00:00
|
|
|
.with(Conversation::CONVERSATION_TYPING_ON, kind_of(Time), { conversation: conversation, user: agent, is_private: false })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'toggles the conversation status for private notes' do
|
|
|
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/toggle_typing_status",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: { typing_status: 'on', is_private: true },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
|
|
|
|
.with(Conversation::CONVERSATION_TYPING_ON, kind_of(Time), { conversation: conversation, user: agent, is_private: true })
|
2020-05-03 06:47:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/update_last_seen' do
|
2020-02-17 10:27:57 +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/accounts/#{account.id}/conversations/#{conversation.display_id}/update_last_seen"
|
2020-02-17 10:27:57 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
2020-02-17 10:27:57 +00:00
|
|
|
it 'updates last seen' do
|
2020-03-09 17:57:10 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/update_last_seen",
|
2020-02-17 10:27:57 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.reload.agent_last_seen_at).not_to be_nil
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|
2021-09-23 15:29:10 +00:00
|
|
|
|
|
|
|
it 'updates assignee last seen' do
|
|
|
|
conversation.update!(assignee_id: agent.id)
|
|
|
|
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.reload.assignee_last_seen_at).to be_nil
|
2021-09-23 15:29:10 +00:00
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/update_last_seen",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.reload.assignee_last_seen_at).not_to be_nil
|
2021-09-23 15:29:10 +00:00
|
|
|
end
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|
|
|
|
end
|
2020-05-26 12:13:59 +00:00
|
|
|
|
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/mute' do
|
|
|
|
let(:conversation) { create(:conversation, account: account) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/mute"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
it 'mutes conversation' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/mute",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.reload.resolved?).to be(true)
|
|
|
|
expect(conversation.reload.muted?).to be(true)
|
2020-05-26 12:13:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-17 05:55:13 +00:00
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/unmute' do
|
|
|
|
let(:conversation) { create(:conversation, account: account).tap(&:mute!) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/unmute"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
it 'unmutes conversation' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/unmute",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.reload.muted?).to be(false)
|
2020-10-08 06:32:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/transcript' do
|
|
|
|
let(:conversation) { create(:conversation, account: account) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/transcript"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
let(:params) { { email: 'test@test.com' } }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
it 'mutes conversation' do
|
2021-06-08 17:15:01 +00:00
|
|
|
mailer = double
|
|
|
|
allow(ConversationReplyMailer).to receive(:with).and_return(mailer)
|
|
|
|
allow(mailer).to receive(:conversation_transcript)
|
2020-08-17 05:55:13 +00:00
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/transcript",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: params,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-06-08 17:15:01 +00:00
|
|
|
expect(mailer).to have_received(:conversation_transcript).with(conversation, 'test@test.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders error when parameter missing' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/transcript",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: {},
|
|
|
|
as: :json
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2020-08-17 05:55:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-09-22 05:16:48 +00:00
|
|
|
|
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/custom_attributes' do
|
|
|
|
let(:conversation) { create(:conversation, account: account) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/custom_attributes"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
let(:custom_attributes) { { user_id: 1001, created_date: '23/12/2012', subscription_id: 12 } }
|
|
|
|
let(:valid_params) { { custom_attributes: custom_attributes } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates last seen' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/custom_attributes",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
params: valid_params,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2022-07-15 02:51:59 +00:00
|
|
|
expect(conversation.reload.custom_attributes).not_to be_nil
|
2021-09-22 05:16:48 +00:00
|
|
|
expect(conversation.reload.custom_attributes.count).to eq 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|