2020-02-29 23:26:30 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Conversation Messages API', type: :request do
|
2020-03-10 18:32:15 +00:00
|
|
|
let!(:account) { create(:account) }
|
2020-02-29 23:26:30 +00:00
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/conversations/<id>/messages' do
|
2020-03-10 18:32:15 +00:00
|
|
|
let!(:inbox) { create(:inbox, account: account) }
|
|
|
|
let!(:conversation) { create(:conversation, inbox: inbox, account: account) }
|
2020-02-29 23:26:30 +00:00
|
|
|
|
|
|
|
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_messages_url(account_id: account.id, conversation_id: conversation.display_id)
|
2020-02-29 23:26:30 +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 conversation' do
|
2020-02-29 23:26:30 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
|
|
|
end
|
|
|
|
|
2020-02-29 23:26:30 +00:00
|
|
|
it 'creates a new outgoing message' do
|
2020-04-10 11:12:37 +00:00
|
|
|
params = { content: 'test-message', private: true }
|
2020-02-29 23:26:30 +00:00
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
2020-02-29 23:26:30 +00:00
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.messages.count).to eq(1)
|
2020-04-10 11:12:37 +00:00
|
|
|
expect(conversation.messages.first.content).to eq(params[:content])
|
2020-03-10 18:32:15 +00:00
|
|
|
end
|
2020-03-22 10:24:36 +00:00
|
|
|
|
2022-02-02 22:21:17 +00:00
|
|
|
it 'does not create the message' do
|
|
|
|
params = { content: "#{'h' * 150 * 1000}a", private: true }
|
|
|
|
|
|
|
|
post api_v1_account_conversation_messages_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(:unprocessable_entity)
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json_response['error']).to eq('Validation failed: Content is too long (maximum is 150000 characters)')
|
|
|
|
end
|
|
|
|
|
2021-10-05 18:40:29 +00:00
|
|
|
it 'creates an outgoing text message with a specific bot sender' do
|
2021-06-07 19:41:06 +00:00
|
|
|
agent_bot = create(:agent_bot)
|
|
|
|
time_stamp = Time.now.utc.to_s
|
|
|
|
params = { content: 'test-message', external_created_at: time_stamp, sender_type: 'AgentBot', sender_id: agent_bot.id }
|
|
|
|
|
|
|
|
post api_v1_account_conversation_messages_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)
|
|
|
|
response_data = JSON.parse(response.body)
|
|
|
|
expect(response_data['content_attributes']['external_created_at']).to eq time_stamp
|
|
|
|
expect(conversation.messages.count).to eq(1)
|
|
|
|
expect(conversation.messages.last.sender_id).to eq(agent_bot.id)
|
2021-10-05 18:40:29 +00:00
|
|
|
expect(conversation.messages.last.content_type).to eq('text')
|
2021-06-07 19:41:06 +00:00
|
|
|
end
|
|
|
|
|
2020-03-22 10:24:36 +00:00
|
|
|
it 'creates a new outgoing message with attachment' do
|
|
|
|
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
2020-04-17 15:45:20 +00:00
|
|
|
params = { content: 'test-message', attachments: [file] }
|
2020-03-22 10:24:36 +00:00
|
|
|
|
|
|
|
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2020-04-17 15:45:20 +00:00
|
|
|
expect(conversation.messages.last.attachments.first.file.present?).to eq(true)
|
|
|
|
expect(conversation.messages.last.attachments.first.file_type).to eq('image')
|
2020-03-22 10:24:36 +00:00
|
|
|
end
|
2020-03-10 18:32:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated agent bot' do
|
|
|
|
let!(:agent_bot) { create(:agent_bot) }
|
|
|
|
|
|
|
|
it 'creates a new outgoing message' do
|
|
|
|
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
|
2020-04-10 11:12:37 +00:00
|
|
|
params = { content: 'test-message' }
|
2020-03-10 18:32:15 +00:00
|
|
|
|
|
|
|
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
|
|
|
params: params,
|
|
|
|
headers: { api_access_token: agent_bot.access_token.token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.messages.count).to eq(1)
|
2020-04-10 11:12:37 +00:00
|
|
|
expect(conversation.messages.first.content).to eq(params[:content])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new outgoing input select message' do
|
|
|
|
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
|
|
|
|
select_item1 = build(:bot_message_select)
|
|
|
|
select_item2 = build(:bot_message_select)
|
|
|
|
params = { content_type: 'input_select', content_attributes: { items: [select_item1, select_item2] } }
|
|
|
|
|
|
|
|
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
|
|
|
params: params,
|
|
|
|
headers: { api_access_token: agent_bot.access_token.token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.messages.count).to eq(1)
|
|
|
|
expect(conversation.messages.first.content_type).to eq(params[:content_type])
|
|
|
|
expect(conversation.messages.first.content).to eq nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new outgoing cards message' do
|
|
|
|
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
|
|
|
|
card = build(:bot_message_card)
|
|
|
|
params = { content_type: 'cards', content_attributes: { items: [card] } }
|
|
|
|
|
|
|
|
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
|
|
|
params: params,
|
|
|
|
headers: { api_access_token: agent_bot.access_token.token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.messages.count).to eq(1)
|
|
|
|
expect(conversation.messages.first.content_type).to eq(params[:content_type])
|
2020-02-29 23:26:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-08 16:38:25 +00:00
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/conversations/:id/messages' do
|
2020-03-08 16:38:25 +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}/messages"
|
2020-03-08 16:38:25 +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 conversation' do
|
2020-03-08 16:38:25 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
|
|
|
end
|
|
|
|
|
2020-03-08 16:38:25 +00:00
|
|
|
it 'shows the conversation' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages",
|
2020-03-08 16:38:25 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2020-05-01 11:19:28 +00:00
|
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:meta][:contact][:id]).to eq(conversation.contact_id)
|
2020-03-08 16:38:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-12-28 18:06:50 +00:00
|
|
|
|
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/conversations/:conversation_id/messages/:id' do
|
2022-03-15 16:31:16 +00:00
|
|
|
let(:message) { create(:message, account: account, content_attributes: { bcc_emails: ['hello@chatwoot.com'] }) }
|
2020-12-28 18:06:50 +00:00
|
|
|
let(:conversation) { message.conversation }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages/#{message.id}"
|
|
|
|
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 conversation' do
|
2020-12-28 18:06:50 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
|
|
|
end
|
|
|
|
|
2020-12-28 18:06:50 +00:00
|
|
|
it 'deletes the message' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages/#{message.id}",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(message.reload.content).to eq 'This message was deleted'
|
|
|
|
expect(message.reload.deleted).to eq true
|
2022-03-15 16:31:16 +00:00
|
|
|
expect(message.reload.content_attributes['bcc_emails']).to eq nil
|
2020-12-28 18:06:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the message id is invalid' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before do
|
|
|
|
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
|
|
|
end
|
|
|
|
|
2020-12-28 18:06:50 +00:00
|
|
|
it 'returns not found error' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages/99999",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-02-29 23:26:30 +00:00
|
|
|
end
|