2019-10-16 09:07:51 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-08 15:02:25 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe GroupMembersFinder, '#execute' do
|
2019-12-13 15:08:02 +00:00
|
|
|
let(:group) { create(:group) }
|
2019-10-16 12:06:32 +00:00
|
|
|
let(:nested_group) { create(:group, parent: group) }
|
2019-12-13 15:08:02 +00:00
|
|
|
let(:deeper_nested_group) { create(:group, parent: nested_group) }
|
|
|
|
let(:user1) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
|
|
|
let(:user3) { create(:user) }
|
|
|
|
let(:user4) { create(:user) }
|
2020-01-13 15:07:53 +00:00
|
|
|
let(:user5) { create(:user, :two_factor_via_otp) }
|
2017-02-08 15:02:25 +00:00
|
|
|
|
|
|
|
it 'returns members for top-level group' do
|
2018-07-11 14:36:08 +00:00
|
|
|
member1 = group.add_maintainer(user1)
|
|
|
|
member2 = group.add_maintainer(user2)
|
|
|
|
member3 = group.add_maintainer(user3)
|
2017-02-08 15:02:25 +00:00
|
|
|
|
|
|
|
result = described_class.new(group).execute
|
|
|
|
|
2017-08-15 10:27:37 +00:00
|
|
|
expect(result.to_a).to match_array([member3, member2, member1])
|
2017-02-08 15:02:25 +00:00
|
|
|
end
|
|
|
|
|
2019-12-13 15:08:02 +00:00
|
|
|
it 'returns members & inherited members for nested group by default' do
|
2018-12-06 13:15:29 +00:00
|
|
|
group.add_developer(user2)
|
2017-02-08 15:02:25 +00:00
|
|
|
nested_group.request_access(user4)
|
2018-07-11 14:36:08 +00:00
|
|
|
member1 = group.add_maintainer(user1)
|
|
|
|
member3 = nested_group.add_maintainer(user2)
|
|
|
|
member4 = nested_group.add_maintainer(user3)
|
2017-02-08 15:02:25 +00:00
|
|
|
|
|
|
|
result = described_class.new(nested_group).execute
|
|
|
|
|
2017-08-15 10:27:37 +00:00
|
|
|
expect(result.to_a).to match_array([member1, member3, member4])
|
2017-02-08 15:02:25 +00:00
|
|
|
end
|
2018-06-07 20:54:24 +00:00
|
|
|
|
2019-12-13 15:08:02 +00:00
|
|
|
it 'does not return inherited members for nested group if requested' do
|
|
|
|
group.add_maintainer(user1)
|
|
|
|
group.add_developer(user2)
|
|
|
|
member2 = nested_group.add_maintainer(user2)
|
|
|
|
member3 = nested_group.add_maintainer(user3)
|
|
|
|
|
|
|
|
result = described_class.new(nested_group).execute(include_relations: [:direct])
|
|
|
|
|
|
|
|
expect(result.to_a).to match_array([member2, member3])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only inherited members for nested group if requested' do
|
|
|
|
group.add_developer(user2)
|
|
|
|
nested_group.request_access(user4)
|
|
|
|
member1 = group.add_maintainer(user1)
|
|
|
|
nested_group.add_maintainer(user2)
|
|
|
|
nested_group.add_maintainer(user3)
|
|
|
|
|
|
|
|
result = described_class.new(nested_group).execute(include_relations: [:inherited])
|
|
|
|
|
|
|
|
expect(result.to_a).to match_array([member1])
|
|
|
|
end
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
it 'does not return nil if `inherited only` relation is requested on root group' do
|
|
|
|
group.add_developer(user2)
|
|
|
|
|
|
|
|
result = described_class.new(group).execute(include_relations: [:inherited])
|
|
|
|
|
|
|
|
expect(result).not_to be_nil
|
|
|
|
end
|
|
|
|
|
2019-07-24 09:20:54 +00:00
|
|
|
it 'returns members for descendant groups if requested' do
|
2018-07-11 14:36:08 +00:00
|
|
|
member1 = group.add_maintainer(user2)
|
|
|
|
member2 = group.add_maintainer(user1)
|
|
|
|
nested_group.add_maintainer(user2)
|
|
|
|
member3 = nested_group.add_maintainer(user3)
|
|
|
|
member4 = nested_group.add_maintainer(user4)
|
2018-06-07 20:54:24 +00:00
|
|
|
|
2019-12-13 15:08:02 +00:00
|
|
|
result = described_class.new(group).execute(include_relations: [:direct, :descendants])
|
2018-06-07 20:54:24 +00:00
|
|
|
|
|
|
|
expect(result.to_a).to match_array([member1, member2, member3, member4])
|
|
|
|
end
|
2020-01-13 15:07:53 +00:00
|
|
|
|
|
|
|
it 'returns searched members if requested' do
|
|
|
|
group.add_maintainer(user2)
|
2020-01-16 15:08:41 +00:00
|
|
|
group.add_developer(user3)
|
2020-01-13 15:07:53 +00:00
|
|
|
member = group.add_maintainer(user1)
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
result = described_class.new(group).execute(params: { search: user1.name })
|
2020-01-13 15:07:53 +00:00
|
|
|
|
|
|
|
expect(result.to_a).to match_array([member])
|
|
|
|
end
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
it 'returns nothing if search only in inherited relation' do
|
|
|
|
group.add_maintainer(user2)
|
|
|
|
group.add_developer(user3)
|
|
|
|
group.add_maintainer(user1)
|
|
|
|
|
|
|
|
result = described_class.new(group).execute(include_relations: [:inherited], params: { search: user1.name })
|
|
|
|
|
|
|
|
expect(result.to_a).to match_array([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns searched member only from nested_group if search only in inherited relation' do
|
|
|
|
group.add_maintainer(user2)
|
|
|
|
group.add_developer(user3)
|
|
|
|
nested_group.add_maintainer(create(:user, name: user1.name))
|
|
|
|
member = group.add_maintainer(user1)
|
|
|
|
|
|
|
|
result = described_class.new(nested_group).execute(include_relations: [:inherited], params: { search: member.user.name })
|
|
|
|
|
|
|
|
expect(result.to_a).to contain_exactly(member)
|
|
|
|
end
|
|
|
|
|
2020-01-13 15:07:53 +00:00
|
|
|
it 'returns members with two-factor auth if requested by owner' do
|
|
|
|
group.add_owner(user2)
|
|
|
|
group.add_maintainer(user1)
|
|
|
|
member = group.add_maintainer(user5)
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
result = described_class.new(group, user2).execute(params: { two_factor: 'enabled' })
|
2020-01-13 15:07:53 +00:00
|
|
|
|
|
|
|
expect(result.to_a).to contain_exactly(member)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns members without two-factor auth if requested by owner' do
|
|
|
|
member1 = group.add_owner(user2)
|
|
|
|
member2 = group.add_maintainer(user1)
|
|
|
|
member_with_2fa = group.add_maintainer(user5)
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
result = described_class.new(group, user2).execute(params: { two_factor: 'disabled' })
|
2020-01-13 15:07:53 +00:00
|
|
|
|
|
|
|
expect(result.to_a).not_to include(member_with_2fa)
|
2020-01-16 15:08:41 +00:00
|
|
|
expect(result.to_a).to match_array([member1, member2])
|
2020-01-13 15:07:53 +00:00
|
|
|
end
|
2017-02-08 15:02:25 +00:00
|
|
|
end
|