Commit d361844a authored by Max Woolf's avatar Max Woolf

Raise error when using invalid relation type in member finder

Adds a check in the GroupMembersFinder to raise ArgumentError
if using an invalid value in the include_relations argument.

Currently this just fails silently in the case of a typo.
parent 7e3c6e3e
......@@ -3,6 +3,7 @@
class GroupMembersFinder < UnionFinder
RELATIONS = %i(direct inherited descendants).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 = {
direct: 'Members in the group itself',
......@@ -42,6 +43,8 @@ class GroupMembersFinder < UnionFinder
attr_reader :user, :group
def groups_by_relations(include_relations)
check_relation_arguments!(include_relations)
case include_relations.sort
when [:inherited]
group.ancestors
......@@ -86,6 +89,12 @@ class GroupMembersFinder < UnionFinder
def members_of_groups(groups)
GroupMember.non_request.of_groups(groups)
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
GroupMembersFinder.prepend_mod_with('GroupMembersFinder')
......@@ -38,6 +38,12 @@ RSpec.describe GroupMembersFinder, '#execute' do
}
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
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