FIX: Better error message in `GroupsController#add_members`.

https://meta.discourse.org/t/cant-add-members-to-a-group/85738?u=tgxworld
This commit is contained in:
Guo Xiang Tan 2018-04-20 10:12:16 +08:00
parent fe3142e22d
commit 70d181bff8
2 changed files with 27 additions and 17 deletions

View File

@ -240,20 +240,20 @@ class GroupsController < ApplicationController
group = Group.find(params[:id])
group.public_admission ? ensure_logged_in : guardian.ensure_can_edit!(group)
users =
if params[:usernames].present?
User.where(username: params[:usernames].split(","))
elsif params[:user_ids].present?
User.find(params[:user_ids].split(","))
elsif params[:user_emails].present?
User.with_email(params[:user_emails].split(","))
else
raise Discourse::InvalidParameters.new(
'user_ids or usernames or user_emails must be present'
)
end
raise Discourse::NotFound if users.blank?
if params[:usernames].present?
users = User.where(username: params[:usernames].split(","))
raise Discourse::InvalidParameters.new(:usernames) if users.blank?
elsif params[:user_ids].present?
users = User.where(id: params[:user_ids].split(","))
raise Discourse::InvalidParameters.new(:user_ids) if users.blank?
elsif params[:user_emails].present?
users = User.with_email(params[:user_emails].split(","))
raise Discourse::InvalidParameters.new(:user_emails) if users.blank?
else
raise Discourse::InvalidParameters.new(
'user_ids or usernames or user_emails must be present'
)
end
if group.public_admission
if !guardian.can_log_group_changes?(group) && current_user != users.first

View File

@ -864,10 +864,20 @@ describe GroupsController do
))
end
it "returns 404 if member is not found" do
put "/groups/#{group.id}/members.json", params: { usernames: 'some donkey' }
it "returns 400 if member is not found" do
[
{ usernames: "some thing" },
{ user_ids: "-5,-6" },
{ user_emails: "some@test.org" }
].each do |params|
put "/groups/#{group.id}/members.json", params: params
expect(response.status).to eq(404)
expect(response.status).to eq(400)
body = JSON.parse(response.body)
expect(body["error_type"]).to eq("invalid_parameters")
end
end
context 'public group' do