Commit b694cfae authored by Etienne Baqué's avatar Etienne Baqué

Updated group member search

Added search and sort params.
Updated rspec accordingly.
parent 7757f7e8
......@@ -6,6 +6,7 @@ module EE
module MembersHelpers
extend ActiveSupport::Concern
extend ::Gitlab::Utils::Override
include ::SortingHelper
prepended do
params :optional_filter_params_ee do
......@@ -76,8 +77,10 @@ module EE
).for_member(member).security_event
end
def paginate_billable_from_user_ids(user_ids)
paginated = paginate(::Kaminari.paginate_array(user_ids.sort))
def paginate_billable_from_user_ids(user_ids, params = {})
sorted_ids = params[:search].present? ? user_ids : user_ids.sort
paginated = paginate(::Kaminari.paginate_array(sorted_ids))
users_as_hash = ::User.id_in(paginated).index_by(&:id)
......@@ -85,6 +88,21 @@ module EE
# instead of creating a new non-paginatable array
paginated.map! { |user_id| users_as_hash[user_id] }
end
def group_billed_user_ids_for(group, params)
if params[:search].present?
sorting = params[:sort] || sort_value_name
::GroupMember
.with_user(group.billed_user_ids)
.search(params[:search])
.sort_by_attribute(sorting)
.map(&:user_id)
.uniq
else
group.billed_user_ids
end
end
end
end
end
......
......@@ -47,6 +47,8 @@ module EE
end
params do
use :pagination
optional :search, type: String, desc: 'The name of the subscribed member'
optional :sort, type: String, desc: 'The sorting option'
end
get ":id/billable_members" do
group = find_group!(params[:id])
......@@ -56,7 +58,7 @@ module EE
bad_request!(nil) if group.subgroup?
bad_request!(nil) unless ::Ability.allowed?(current_user, :admin_group_member, group)
users = paginate_billable_from_user_ids(group.billed_user_ids)
users = paginate_billable_from_user_ids(group_billed_user_ids_for(group, params), params)
present users, with: ::API::Entities::UserBasic, current_user: current_user
end
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe EE::API::Helpers::MembersHelpers do
subject(:members_helpers) { Class.new.include(described_class).new }
let(:members_helpers) { Class.new.include(described_class).new }
before do
allow(members_helpers).to receive(:current_user).and_return(create(:user))
......@@ -21,6 +21,8 @@ RSpec.describe EE::API::Helpers::MembersHelpers do
end
describe '#log_audit_event' do
subject { members_helpers }
it_behaves_like 'creates security_event', 'group' do
let(:source) { create(:group) }
let(:member) { create(:group_member, :owner, group: source, user: create(:user)) }
......@@ -65,4 +67,41 @@ RSpec.describe EE::API::Helpers::MembersHelpers do
end
end
end
describe '#group_billed_user_ids_for' do
let_it_be(:group) { create(:group) }
let_it_be(:gm_1) { create(:group_member, group: group, user: create(:user, name: 'Maria Gomez')) }
let_it_be(:gm_2) { create(:group_member, group: group, user: create(:user, name: 'John Smith')) }
let_it_be(:gm_3) { create(:group_member, group: group, user: create(:user, name: 'John Doe')) }
let_it_be(:gm_4) { create(:group_member, group: group, user: create(:user, name: 'Sophie Dupont')) }
let(:params) { {} }
subject { members_helpers.group_billed_user_ids_for(group, params).to_a }
context 'when a search parameter is present' do
let(:params) { { search: 'John', sort: sort } }
context 'when a sorting parameter is provided (eg name descending)' do
let(:sort) { 'name_desc' }
it 'sorts results accordingly' do
expect(subject).to eq([gm_2, gm_3].map(&:user_id))
end
end
context 'when a sorting parameter is not provided' do
let(:sort) { nil }
it 'sorts results by name ascending' do
expect(subject).to eq([gm_3, gm_2].map(&:user_id))
end
end
end
context 'when a search parameter is not present' do
it 'returns the expected group member user ids' do
expect(subject).to eq([gm_1, gm_2, gm_3, gm_4].map(&:user_id))
end
end
end
end
......@@ -339,7 +339,7 @@ RSpec.describe API::Members do
end
end
let_it_be(:nested_user) { create(:user) }
let_it_be(:nested_user) { create(:user, name: 'Scott Anderson') }
let_it_be(:nested_group) do
create(:group, parent: group) do |nested_group|
nested_group.add_developer(nested_user)
......@@ -347,9 +347,10 @@ RSpec.describe API::Members do
end
let(:url) { "/groups/#{group.id}/billable_members" }
let(:params) { {} }
subject do
get api(url, owner)
get api(url, owner), params: params
json_response
end
......@@ -361,7 +362,7 @@ RSpec.describe API::Members do
end
end
let!(:linked_group_user) { create(:user) }
let!(:linked_group_user) { create(:user, name: 'Scott McNeil') }
let!(:linked_group) do
create(:group) do |linked_group|
linked_group.add_developer(linked_group_user)
......@@ -375,6 +376,26 @@ RSpec.describe API::Members do
expect_paginated_array_response(*[owner, maintainer, nested_user, project_user, linked_group_user].map(&:id))
end
context 'with seach params provided' do
let(:params) { { search: nested_user.name } }
it 'returns the relevant billable users' do
subject
expect_paginated_array_response([nested_user.id])
end
end
context 'with search and sort params provided' do
let(:params) { { search: 'Scott', sort: 'name_desc' } }
it 'returns the relevant billable users' do
subject
expect_paginated_array_response(*[linked_group_user, nested_user].map(&:id))
end
end
end
context 'when feature is disabled' do
......
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