Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

Merge commit from fork

Sony Mathewafda727c34b8
diff --git a/app/controllers/api/v1/accounts/inbox_members_controller.rb b/app/controllers/api/v1/accounts/inbox_members_controller.rb
index bf8822c27fc..2fa5de2494a 100644
--- a/app/controllers/api/v1/accounts/inbox_members_controller.rb
+++ b/app/controllers/api/v1/accounts/inbox_members_controller.rb
@@ -9,6 +9,8 @@ class Api::V1::Accounts::InboxMembersController < Api::V1::Accounts::BaseControl
def create
authorize @inbox, :create?
+ return render_could_not_create_error('Invalid User IDs') unless valid_member_ids?
+
ActiveRecord::Base.transaction do
@inbox.add_members(agents_to_be_added_ids)
end
@@ -17,6 +19,8 @@ class Api::V1::Accounts::InboxMembersController < Api::V1::Accounts::BaseControl
def update
authorize @inbox, :update?
+ return render_could_not_create_error('Invalid User IDs') unless valid_member_ids?
+
update_agents_list
fetch_updated_agents
end
@@ -31,6 +35,13 @@ class Api::V1::Accounts::InboxMembersController < Api::V1::Accounts::BaseControl
private
+ def valid_member_ids?
+ user_ids = params[:user_ids]
+ return false unless user_ids.is_a?(Array) && user_ids.all?(Integer)
+
+ (user_ids - Current.account.users.where(id: user_ids).pluck(:id)).empty?
+ end
+
def fetch_updated_agents
@agents = Current.account.users.where(id: @inbox.members.select(:user_id))
end
diff --git a/spec/controllers/api/v1/accounts/inbox_members_controller_spec.rb b/spec/controllers/api/v1/accounts/inbox_members_controller_spec.rb
index 1333d8595a9..9575f0a76eb 100644
--- a/spec/controllers/api/v1/accounts/inbox_members_controller_spec.rb
+++ b/spec/controllers/api/v1/accounts/inbox_members_controller_spec.rb
@@ -113,7 +113,43 @@ RSpec.describe 'Inbox Member API', type: :request do
as: :json
expect(response).to have_http_status(:unprocessable_entity)
- expect(response.body).to include('User must exist')
+ expect(response.parsed_body['error']).to eq('Invalid User IDs')
+ end
+
+ it 'rejects users who only belong to another account' do
+ other_agent = create(:user, account: create(:account))
+
+ post "/api/v1/accounts/#{account.id}/inbox_members",
+ headers: administrator.create_new_auth_token,
+ params: { inbox_id: inbox.id, user_ids: [other_agent.id] },
+ as: :json
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(inbox.inbox_members.pluck(:user_id)).to eq([old_agent.id])
+ end
+
+ it 'rejects mixed account membership without adding any users' do
+ other_agent = create(:user, account: create(:account))
+
+ post "/api/v1/accounts/#{account.id}/inbox_members",
+ headers: administrator.create_new_auth_token,
+ params: { inbox_id: inbox.id, user_ids: [agent_to_add.id, other_agent.id] },
+ as: :json
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(inbox.inbox_members.pluck(:user_id)).to eq([old_agent.id])
+ end
+
+ it 'adds users who belong to this account and another account' do
+ create(:account_user, account: create(:account), user: agent_to_add)
+
+ post "/api/v1/accounts/#{account.id}/inbox_members",
+ headers: administrator.create_new_auth_token,
+ params: { inbox_id: inbox.id, user_ids: [agent_to_add.id] },
+ as: :json
+
+ expect(response).to have_http_status(:success)
+ expect(inbox.inbox_members.pluck(:user_id)).to contain_exactly(old_agent.id, agent_to_add.id)
end
end
end
@@ -188,7 +224,43 @@ RSpec.describe 'Inbox Member API', type: :request do
as: :json
expect(response).to have_http_status(:unprocessable_entity)
- expect(response.body).to include('User must exist')
+ expect(response.parsed_body['error']).to eq('Invalid User IDs')
+ end
+
+ it 'rejects users who only belong to another account' do
+ other_agent = create(:user, account: create(:account))
+
+ patch "/api/v1/accounts/#{account.id}/inbox_members",
+ headers: administrator.create_new_auth_token,
+ params: { inbox_id: inbox.id, user_ids: [other_agent.id] },
+ as: :json
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(inbox.inbox_members.pluck(:user_id)).to eq([old_agent.id])
+ end
+
+ it 'rejects mixed account membership without changing any users' do
+ other_agent = create(:user, account: create(:account))
+
+ patch "/api/v1/accounts/#{account.id}/inbox_members",
+ headers: administrator.create_new_auth_token,
+ params: { inbox_id: inbox.id, user_ids: [agent_to_add.id, other_agent.id] },
+ as: :json
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(inbox.inbox_members.pluck(:user_id)).to eq([old_agent.id])
+ end
+
+ it 'adds users who belong to this account and another account' do
+ create(:account_user, account: create(:account), user: agent_to_add)
+
+ patch "/api/v1/accounts/#{account.id}/inbox_members",
+ headers: administrator.create_new_auth_token,
+ params: { inbox_id: inbox.id, user_ids: [agent_to_add.id] },
+ as: :json
+
+ expect(response).to have_http_status(:success)
+ expect(inbox.inbox_members.pluck(:user_id)).to eq([agent_to_add.id])
end
end
end