Commit 969f62a8 authored by Etienne Baqué's avatar Etienne Baqué

Added BilledUsersFinder class

Added rspec accordingly.
parent 77c482e6
# frozen_string_literal: true
class BilledUsersFinder
def initialize(group, search_term: nil, order_by: 'name_asc')
@group = group
@search_term = search_term
@order_by = order_by
end
def execute
return [] unless group_billed_user_ids.any?
users = ::User.id_in(group_billed_user_ids)
users = users.search(search_term) if search_term
users.sort_by_attribute(order_by)
end
private
attr_reader :group, :search_term, :order_by
def group_billed_user_ids
@group_billed_user_ids ||= group.billed_user_ids
end
end
......@@ -83,13 +83,6 @@ module EE
action: :create
).for_member(member).security_event
end
def billed_users_for(group, search_term, order_by: 'name_asc')
users = ::User.id_in(group.billed_user_ids)
users = users.search(search_term) if search_term
users.sort_by_attribute(order_by)
end
end
end
end
......
......@@ -57,7 +57,11 @@ module EE
bad_request!(nil) unless ::Ability.allowed?(current_user, :admin_group_member, group)
sorting = params[:sort] || 'id_asc'
users = paginate(billed_users_for(group, params[:search], order_by: sorting))
users = paginate(
BilledUsersFinder.new(group,
search_term: params[:search],
order_by: sorting).execute
)
present users, with: ::API::Entities::UserBasic, current_user: current_user
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BilledUsersFinder do
let_it_be(:group) { create(:group) }
let(:search_term) { nil }
let(:order_by) { nil }
describe '#execute' do
let_it_be(:maria) { create(:group_member, group: group, user: create(:user, name: 'Maria Gomez')) }
let_it_be(:john_smith) { create(:group_member, group: group, user: create(:user, name: 'John Smith')) }
let_it_be(:john_doe) { create(:group_member, group: group, user: create(:user, name: 'John Doe')) }
let_it_be(:sophie) { create(:group_member, group: group, user: create(:user, name: 'Sophie Dupont')) }
subject { described_class.new(group, search_term: search_term, order_by: order_by) }
context 'when a group does not have any billed users' do
it 'returns an empty array' do
allow(group).to receive(:billed_user_ids).and_return([])
expect(subject.execute).to be_empty
end
end
context 'when a search parameter is provided' do
let(:search_term) { 'John' }
context 'when a sorting parameter is provided (eg name descending)' do
let(:order_by) { 'name_desc' }
it 'sorts results accordingly' do
expect(subject.execute).to eq([john_smith, john_doe].map(&:user))
end
end
end
context 'when a search parameter is not present' do
subject { described_class.new(group) }
it 'returns expected users in name asc order when a sorting is not provided either' do
allow(group).to receive(:billed_user_members).and_return([john_doe, john_smith, sophie, maria])
expect(subject.execute).to eq([john_doe, john_smith, maria, sophie].map(&:user))
end
context 'and when a sorting parameter is provided (eg name descending)' do
let(:order_by) { 'name_desc' }
subject { described_class.new(group, search_term: search_term, order_by: order_by) }
it 'sorts results accordingly' do
expect(subject.execute).to eq([sophie, maria, john_smith, john_doe].map(&:user))
end
end
end
end
end
......@@ -33,56 +33,4 @@ RSpec.describe EE::API::Helpers::MembersHelpers do
let(:member) { create(:project_member, project: source, user: create(:user)) }
end
end
describe '#billed_users_for' do
let_it_be(:group) { create(:group) }
let_it_be(:maria) { create(:group_member, group: group, user: create(:user, name: 'Maria Gomez')) }
let_it_be(:john_smith) { create(:group_member, group: group, user: create(:user, name: 'John Smith')) }
let_it_be(:john_doe) { create(:group_member, group: group, user: create(:user, name: 'John Doe')) }
let_it_be(:sophie) { create(:group_member, group: group, user: create(:user, name: 'Sophie Dupont')) }
let(:search_term) { nil }
let(:order_by) { nil }
subject { members_helpers.billed_users_for(group, search_term, order_by: order_by) }
context 'when a search parameter is present' do
let(:search_term) { 'John' }
context 'when a sorting parameter is provided (eg name descending)' do
let(:order_by) { 'name_desc' }
it 'sorts results accordingly' do
expect(subject).to eq([john_smith, john_doe].map(&:user))
end
end
context 'when a sorting parameter is not provided' do
let(:order_by) { nil }
it 'sorts results by name ascending' do
expect(subject).to eq([john_doe, john_smith].map(&:user))
end
end
end
context 'when a search parameter is not present' do
subject { members_helpers.billed_users_for(group, search_term) }
it 'returns expected users in name asc order' do
allow(group).to receive(:billed_user_members).and_return([john_doe, john_smith, sophie, maria])
expect(subject).to eq([john_doe, john_smith, maria, sophie].map(&:user))
end
context 'and when a sorting parameter is provided (eg name descending)' do
let(:order_by) { 'name_desc' }
subject { members_helpers.billed_users_for(group, search_term, order_by: order_by) }
it 'sorts results accordingly' do
expect(subject).to eq([sophie, maria, john_smith, john_doe].map(&:user))
end
end
end
end
end
......@@ -399,6 +399,16 @@ RSpec.describe API::Members do
expect_paginated_array_response(*[owner, maintainer, nested_user, project_user, linked_group_user].map(&:id))
end
context 'when the current user does not have the :admin_group_member ability' do
it 'is a bad request' do
not_an_owner = create(:user)
get api(url, not_an_owner), params: params
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'with seach params provided' do
let(:params) { { search: nested_user.name } }
......
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