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
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'creates a new outgoing message' do
|
|
|
|
params = { message: 'test-message', private: true }
|
|
|
|
|
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)
|
|
|
|
expect(conversation.messages.first.content).to eq(params[:message])
|
2020-03-10 18:32:15 +00:00
|
|
|
end
|
|
|
|
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)
|
|
|
|
params = { message: 'test-message' }
|
|
|
|
|
|
|
|
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).to eq(params[:message])
|
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
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
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)
|
|
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:meta][:contact_id]).to eq(conversation.contact_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-02-29 23:26:30 +00:00
|
|
|
end
|