diff --git a/app/views/api/v1/accounts/conversations/participants/show.json.jbuilder b/app/views/api/v1/accounts/conversations/participants/show.json.jbuilder index cce70781c..5de95847d 100644 --- a/app/views/api/v1/accounts/conversations/participants/show.json.jbuilder +++ b/app/views/api/v1/accounts/conversations/participants/show.json.jbuilder @@ -1,3 +1,3 @@ json.array! @participants do |participant| - json.partial! 'api/v1/models/agent.json.jbuilder', resource: participant.user + json.partial! 'api/v1/models/agent', format: :json, resource: participant.user end diff --git a/spec/controllers/api/v1/accounts/conversations/participants_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations/participants_controller_spec.rb new file mode 100644 index 000000000..3a0de7777 --- /dev/null +++ b/spec/controllers/api/v1/accounts/conversations/participants_controller_spec.rb @@ -0,0 +1,124 @@ +require 'rails_helper' + +RSpec.describe 'Conversation Participants API', type: :request do + let(:account) { create(:account) } + let(:conversation) { create(:conversation, account: account) } + let(:agent) { create(:user, account: account, role: :agent) } + + before do + create(:inbox_member, inbox: conversation.inbox, user: agent) + end + + describe 'GET /api/v1/accounts/{account.id}/conversations//paricipants' do + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id) + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user with access to the conversation' do + let(:participant1) { create(:user, account: account, role: :agent) } + let(:participant2) { create(:user, account: account, role: :agent) } + + it 'returns all the partipants for the conversation' do + create(:conversation_participant, conversation: conversation, user: participant1) + create(:conversation_participant, conversation: conversation, user: participant2) + get api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id), + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).to include(participant1.email) + expect(response.body).to include(participant2.email) + end + end + end + + describe 'POST /api/v1/accounts/{account.id}/conversations//participants' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + post api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id) + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:participant) { create(:user, account: account, role: :agent) } + + it 'creates a new participants when its authorized agent' do + params = { user_ids: [participant.id] } + + expect(conversation.conversation_participants.count).to eq(0) + post api_v1_account_conversation_participants_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(response.body).to include(participant.email) + expect(conversation.conversation_participants.count).to eq(1) + end + end + end + + describe 'PUT /api/v1/accounts/{account.id}/conversations//participants' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + put api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id) + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:participant) { create(:user, account: account, role: :agent) } + let(:participant_to_be_added) { create(:user, account: account, role: :agent) } + let(:participant_to_be_removed) { create(:user, account: account, role: :agent) } + + it 'updates participants when its authorized agent' do + params = { user_ids: [participant.id, participant_to_be_added.id] } + create(:conversation_participant, conversation: conversation, user: participant) + create(:conversation_participant, conversation: conversation, user: participant_to_be_removed) + + expect(conversation.conversation_participants.count).to eq(2) + put api_v1_account_conversation_participants_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(response.body).to include(participant.email) + expect(response.body).to include(participant_to_be_added.email) + expect(conversation.conversation_participants.count).to eq(2) + end + end + end + + describe 'DELETE /api/v1/accounts/{account.id}/conversations//participants' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + delete api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id) + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:participant) { create(:user, account: account, role: :agent) } + + it 'deletes participants when its authorized agent' do + params = { user_ids: [participant.id] } + create(:conversation_participant, conversation: conversation, user: participant) + + expect(conversation.conversation_participants.count).to eq(1) + delete api_v1_account_conversation_participants_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.conversation_participants.count).to eq(0) + end + end + end +end \ No newline at end of file diff --git a/spec/factories/conversation_participants.rb b/spec/factories/conversation_participants.rb new file mode 100644 index 000000000..728a8008c --- /dev/null +++ b/spec/factories/conversation_participants.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :conversation_participant do + conversation + user + account + end +end