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) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
conversation = create(:conversation, account: account)
|
|
|
|
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all conversations' 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)
|
|
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:data][:meta][:all_count]).to eq(1)
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
expect(JSON.parse(response.body, symbolize_names: true)[:data][:meta][:all_count]).to eq(1)
|
|
|
|
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) }
|
|
|
|
|
|
|
|
it 'shows the conversation' 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
|
|
|
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
|
|
|
|
|
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) }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
it 'toggles the conversation status to open from bot' do
|
|
|
|
conversation.update!(status: 'bot')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
it 'toggles the conversation status to specific status when parameter is passed' do
|
|
|
|
expect(conversation.status).to eq('open')
|
|
|
|
|
|
|
|
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)
|
|
|
|
expect(conversation.reload.status).to eq('bot')
|
|
|
|
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) }
|
|
|
|
|
|
|
|
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,
|
|
|
|
params: { typing_status: 'on' },
|
|
|
|
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 })
|
|
|
|
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) }
|
|
|
|
|
|
|
|
it 'updates last seen' do
|
|
|
|
params = { agent_last_seen_at: '-1' }
|
|
|
|
|
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,
|
|
|
|
params: params,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.agent_last_seen_at).to eq(DateTime.strptime(params[:agent_last_seen_at].to_s, '%s'))
|
|
|
|
end
|
|
|
|
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) }
|
|
|
|
|
|
|
|
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)
|
|
|
|
expect(conversation.reload.resolved?).to eq(true)
|
|
|
|
expect(conversation.reload.muted?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 10:27:57 +00:00
|
|
|
end
|