Commit c4904d57 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '41673-blank-query-members-api' into 'master'

Resolve "Project/#/Members?query= blank causes 500 error"

Closes #41673

See merge request gitlab-org/gitlab-ce!16235
parents 834dee68 580fa6be
...@@ -318,6 +318,8 @@ class User < ActiveRecord::Base ...@@ -318,6 +318,8 @@ class User < ActiveRecord::Base
# #
# Returns an ActiveRecord::Relation. # Returns an ActiveRecord::Relation.
def search(query) def search(query)
return none if query.blank?
query = query.downcase query = query.downcase
order = <<~SQL order = <<~SQL
...@@ -341,6 +343,8 @@ class User < ActiveRecord::Base ...@@ -341,6 +343,8 @@ class User < ActiveRecord::Base
# This method uses ILIKE on PostgreSQL and LIKE on MySQL. # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
def search_with_secondary_emails(query) def search_with_secondary_emails(query)
return none if query.blank?
query = query.downcase query = query.downcase
email_table = Email.arel_table email_table = Email.arel_table
......
---
title: Fix error on empty query for Members API
merge_request: 16235
author:
type: fixed
...@@ -22,7 +22,7 @@ module API ...@@ -22,7 +22,7 @@ module API
source = find_source(source_type, params[:id]) source = find_source(source_type, params[:id])
users = source.users users = source.users
users = users.merge(User.search(params[:query])) if params[:query] users = users.merge(User.search(params[:query])) if params[:query].present?
present paginate(users), with: Entities::Member, source: source present paginate(users), with: Entities::Member, source: source
end end
......
...@@ -23,7 +23,7 @@ module API ...@@ -23,7 +23,7 @@ module API
source = find_source(source_type, params[:id]) source = find_source(source_type, params[:id])
users = source.users users = source.users
users = users.merge(User.search(params[:query])) if params[:query] users = users.merge(User.search(params[:query])) if params[:query].present?
present paginate(users), with: ::API::Entities::Member, source: source present paginate(users), with: ::API::Entities::Member, source: source
end end
......
...@@ -966,6 +966,14 @@ describe User do ...@@ -966,6 +966,14 @@ describe User do
expect(described_class.search(user3.username.upcase)).to eq([user3]) expect(described_class.search(user3.username.upcase)).to eq([user3])
end end
end end
it 'returns no matches for an empty string' do
expect(described_class.search('')).to be_empty
end
it 'returns no matches for nil' do
expect(described_class.search(nil)).to be_empty
end
end end
describe '.search_with_secondary_emails' do describe '.search_with_secondary_emails' do
...@@ -1020,6 +1028,14 @@ describe User do ...@@ -1020,6 +1028,14 @@ describe User do
it 'does not return users with a matching part of secondary email' do it 'does not return users with a matching part of secondary email' do
expect(search_with_secondary_emails(email.email[1..4])).not_to include([email.user]) expect(search_with_secondary_emails(email.email[1..4])).not_to include([email.user])
end end
it 'returns no matches for an empty string' do
expect(search_with_secondary_emails('')).to be_empty
end
it 'returns no matches for nil' do
expect(search_with_secondary_emails(nil)).to be_empty
end
end end
describe '.find_by_ssh_key_id' do describe '.find_by_ssh_key_id' do
......
...@@ -65,6 +65,16 @@ describe API::Members do ...@@ -65,6 +65,16 @@ describe API::Members do
expect(json_response.count).to eq(1) expect(json_response.count).to eq(1)
expect(json_response.first['username']).to eq(master.username) expect(json_response.first['username']).to eq(master.username)
end end
it 'finds all members with no query specified' do
get api("/#{source_type.pluralize}/#{source.id}/members", developer), query: ''
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.count).to eq(2)
expect(json_response.map { |u| u['id'] }).to match_array [master.id, developer.id]
end
end end
end end
......
...@@ -58,6 +58,16 @@ describe API::V3::Members do ...@@ -58,6 +58,16 @@ describe API::V3::Members do
expect(json_response.count).to eq(1) expect(json_response.count).to eq(1)
expect(json_response.first['username']).to eq(master.username) expect(json_response.first['username']).to eq(master.username)
end end
it 'finds all members with no query specified' do
get v3_api("/#{source_type.pluralize}/#{source.id}/members", developer), query: ''
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.count).to eq(2)
expect(json_response.map { |u| u['id'] }).to match_array [master.id, developer.id]
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment