Skip to content

Commit

Permalink
fix: Validations for updating team members (#5384)
Browse files Browse the repository at this point in the history
fixes: chatwoot/product#539

Co-authored-by: Sojan Jose <sojan@pepalo.com>
  • Loading branch information
tejaswinichile and sojan-official committed Sep 2, 2022
1 parent 9525d4f commit 329e8c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/api/v1/accounts/team_members_controller.rb
@@ -1,6 +1,7 @@
class Api::V1::Accounts::TeamMembersController < Api::V1::Accounts::BaseController
before_action :fetch_team
before_action :check_authorization
before_action :validate_member_id_params, only: [:create, :update, :destroy]

def index
@team_members = @team.team_members.map(&:user)
Expand Down Expand Up @@ -45,4 +46,10 @@ def current_members_ids
def fetch_team
@team = Current.account.teams.find(params[:team_id])
end

def validate_member_id_params
invalid_ids = params[:user_ids].map(&:to_i) - @team.account.user_ids

render json: { error: 'Invalid User IDs' }, status: :unauthorized and return if invalid_ids.present?
end
end
15 changes: 15 additions & 0 deletions spec/controllers/api/v1/accounts/team_members_controller_spec.rb
Expand Up @@ -2,6 +2,7 @@

RSpec.describe 'Team Members API', type: :request do
let(:account) { create(:account) }
let(:account_2) { create(:account) }
let!(:team) { create(:team, account: account) }

describe 'GET /api/v1/accounts/{account.id}/teams/{team_id}/team_members' do
Expand Down Expand Up @@ -120,6 +121,7 @@

context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:agent_2) { create(:user, account: account_2, role: :agent) }
let(:administrator) { create(:user, account: account, role: :administrator) }

it 'return unauthorized for agent' do
Expand All @@ -145,6 +147,19 @@
json_response = JSON.parse(response.body)
expect(json_response.count).to eq(user_ids.count)
end

it 'ignores the user ids when its not a valid account user id' do
params = { user_ids: [agent_2.id] }

patch "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
params: params,
headers: administrator.create_new_auth_token,
as: :json

expect(response).to have_http_status(:unauthorized)
json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Invalid User IDs')
end
end
end
end

0 comments on commit 329e8c3

Please sign in to comment.