add users search results to group scoped search

This commit is contained in:
Alexis Reigel 2018-08-15 15:25:17 +02:00
parent a52d1dbb0f
commit a8818bab76
No known key found for this signature in database
GPG key ID: 55ADA7C7B683B329
4 changed files with 84 additions and 0 deletions

View file

@ -11,6 +11,12 @@ module Search
@group = group @group = group
end end
def execute
Gitlab::GroupSearchResults.new(
current_user, projects, group, params[:search], default_project_filter: default_project_filter
)
end
def projects def projects
return Project.none unless group return Project.none unless group
return @projects if defined? @projects return @projects if defined? @projects

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
module Gitlab
class GroupSearchResults < SearchResults
def initialize(current_user, limit_projects, group, query, default_project_filter: false, per_page: 20)
super(current_user, limit_projects, query, default_project_filter: default_project_filter, per_page: per_page)
@group = group
end
# rubocop:disable CodeReuse/ActiveRecord
def users
super.where(id: @group.users_with_descendants)
end
# rubocop:enable CodeReuse/ActiveRecord
end
end

View file

@ -50,4 +50,34 @@ describe 'User searches for users' do
expect(page).not_to have_content('@gob_2018') expect(page).not_to have_content('@gob_2018')
end end
end end
context 'when on the group page' do
it 'finds the user belonging to the group' do
group = create(:group)
user1 = create(:user, username: 'gob_bluth', name: 'Gob Bluth')
create(:group_member, :developer, user: user1, group: group)
user2 = create(:user, username: 'michael_bluth', name: 'Michael Bluth')
create(:group_member, :developer, user: user2, group: group)
create(:user, username: 'gob_2018', name: 'George Oscar Bluth')
sign_in(user1)
visit group_path(group)
fill_in 'search', with: 'gob'
click_button 'Go'
expect(page).to have_content('Gob Bluth')
expect(page).to have_content('@gob_bluth')
expect(page).not_to have_content('Michael Bluth')
expect(page).not_to have_content('@michael_bluth')
expect(page).not_to have_content('George Oscar Bluth')
expect(page).not_to have_content('@gob_2018')
end
end
end end

View file

@ -0,0 +1,31 @@
require 'spec_helper'
describe Gitlab::GroupSearchResults do
let(:user) { create(:user) }
describe 'user search' do
let(:group) { create(:group) }
it 'returns the users belonging to the group matching the search query' do
user1 = create(:user, username: 'gob_bluth')
create(:group_member, :developer, user: user1, group: group)
user2 = create(:user, username: 'michael_bluth')
create(:group_member, :developer, user: user2, group: group)
create(:user, username: 'gob_2018')
expect(described_class.new(user, anything, group, 'gob').objects('users')).to eq [user1]
end
it 'returns the user belonging to the subgroup matching the search query', :nested_groups do
user1 = create(:user, username: 'gob_bluth')
subgroup = create(:group, parent: group)
create(:group_member, :developer, user: user1, group: subgroup)
create(:user, username: 'gob_2018')
expect(described_class.new(user, anything, group, 'gob').objects('users')).to eq [user1]
end
end
end