Commit 742e2b29 authored by Dmitriy Zaporozhets (DZ)'s avatar Dmitriy Zaporozhets (DZ)

Merge branch 'enforce-relation-types-group-member-finder' into 'master'

Raise error when using invalid relation type in member finder

See merge request gitlab-org/gitlab!67849
parents 504100bd d361844a
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
class GroupMembersFinder < UnionFinder class GroupMembersFinder < UnionFinder
RELATIONS = %i(direct inherited descendants).freeze RELATIONS = %i(direct inherited descendants).freeze
DEFAULT_RELATIONS = %i(direct inherited).freeze DEFAULT_RELATIONS = %i(direct inherited).freeze
INVALID_RELATION_TYPE_ERROR_MSG = "is not a valid relation type. Valid relation types are #{RELATIONS.join(', ')}."
RELATIONS_DESCRIPTIONS = { RELATIONS_DESCRIPTIONS = {
direct: 'Members in the group itself', direct: 'Members in the group itself',
...@@ -42,6 +43,8 @@ class GroupMembersFinder < UnionFinder ...@@ -42,6 +43,8 @@ class GroupMembersFinder < UnionFinder
attr_reader :user, :group attr_reader :user, :group
def groups_by_relations(include_relations) def groups_by_relations(include_relations)
check_relation_arguments!(include_relations)
case include_relations.sort case include_relations.sort
when [:inherited] when [:inherited]
group.ancestors group.ancestors
...@@ -86,6 +89,12 @@ class GroupMembersFinder < UnionFinder ...@@ -86,6 +89,12 @@ class GroupMembersFinder < UnionFinder
def members_of_groups(groups) def members_of_groups(groups)
GroupMember.non_request.of_groups(groups) GroupMember.non_request.of_groups(groups)
end end
def check_relation_arguments!(include_relations)
unless include_relations & RELATIONS == include_relations
raise ArgumentError, "#{(include_relations - RELATIONS).first} #{INVALID_RELATION_TYPE_ERROR_MSG}"
end
end
end end
GroupMembersFinder.prepend_mod_with('GroupMembersFinder') GroupMembersFinder.prepend_mod_with('GroupMembersFinder')
...@@ -38,6 +38,12 @@ RSpec.describe GroupMembersFinder, '#execute' do ...@@ -38,6 +38,12 @@ RSpec.describe GroupMembersFinder, '#execute' do
} }
end end
it 'raises an error if a non-supported relation type is used' do
expect do
described_class.new(group).execute(include_relations: [:direct, :invalid_relation_type])
end.to raise_error(ArgumentError, "invalid_relation_type is not a valid relation type. Valid relation types are direct, inherited, descendants.")
end
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:subject_relations, :subject_group, :expected_members) do where(:subject_relations, :subject_group, :expected_members) 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