diff options
author | Mark Fletcher <mark@gitlab.com> | 2018-01-04 20:32:52 +0000 |
---|---|---|
committer | Mark Fletcher <mark@gitlab.com> | 2018-01-21 17:24:50 +0000 |
commit | 580fa6becf28670c77529993a08fcd0f22491153 (patch) | |
tree | 007e68df1c7bf8aab2fcdde3747bd31bf313063e | |
parent | 062f5b7126978e08522361e879d7c1723b15c9da (diff) | |
download | gitlab-ce-580fa6becf28670c77529993a08fcd0f22491153.tar.gz |
Yield no results for blank searches on User name, username and email
Given no search term, the `search` and `search_with_secondary_emails` methods will yield an empty result set
-rw-r--r-- | app/models/user.rb | 4 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 16 |
2 files changed, 20 insertions, 0 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 09aa5a7b318..9403da98268 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -318,6 +318,8 @@ class User < ActiveRecord::Base # # Returns an ActiveRecord::Relation. def search(query) + return none if query.blank? + query = query.downcase order = <<~SQL @@ -341,6 +343,8 @@ class User < ActiveRecord::Base # This method uses ILIKE on PostgreSQL and LIKE on MySQL. def search_with_secondary_emails(query) + return none if query.blank? + query = query.downcase email_table = Email.arel_table diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8d0eaf565a7..762cec9b95e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -966,6 +966,14 @@ describe User do expect(described_class.search(user3.username.upcase)).to eq([user3]) 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 describe '.search_with_secondary_emails' do @@ -1020,6 +1028,14 @@ describe User 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]) 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 describe '.find_by_ssh_key_id' do |