debian-mirror-gitlab/spec/finders/members_finder_spec.rb

242 lines
9.7 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2023-03-17 16:20:25 +05:30
RSpec.describe MembersFinder, feature_category: :subgroups do
shared_examples '#execute' do
let_it_be(:group) { create(:group) }
let_it_be(:nested_group) { create(:group, parent: group) }
let_it_be(:project, reload: true) { create(:project, namespace: nested_group) }
let_it_be(:user1) { create(:user) }
let_it_be(:user2) { create(:user) }
let_it_be(:user3) { create(:user) }
let_it_be(:user4) { create(:user) }
let_it_be(:blocked_user) { create(:user, :blocked) }
it 'returns members for project and parent groups' do
nested_group.request_access(user1)
member1 = group.add_maintainer(user2)
member2 = nested_group.add_maintainer(user3)
member3 = project.add_maintainer(user4)
blocked_member = project.add_maintainer(blocked_user)
result = described_class.new(project, user2).execute
expect(result).to contain_exactly(member1, member2, member3, blocked_member)
end
2020-10-24 23:57:45 +05:30
2023-03-17 16:20:25 +05:30
it 'returns owners and maintainers' do
member1 = group.add_owner(user1)
group.add_developer(user2)
member3 = project.add_maintainer(user3)
project.add_developer(user4)
2020-10-24 23:57:45 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2, params: { owners_and_maintainers: true }).execute
2020-10-24 23:57:45 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1, member3)
end
2020-10-24 23:57:45 +05:30
2023-03-17 16:20:25 +05:30
it 'returns active users and excludes invited users' do
member1 = project.add_maintainer(user2)
create(:project_member, :invited, project: project, invite_email: create(:user).email)
project.add_maintainer(blocked_user)
2020-10-24 23:57:45 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2, params: { active_without_invites_and_requests: true }).execute
2020-10-24 23:57:45 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1)
end
2018-11-08 19:23:39 +05:30
2023-03-17 16:20:25 +05:30
it 'does not return members of parent group with minimal access' do
nested_group.request_access(user1)
member1 = group.add_maintainer(user2)
member2 = nested_group.add_maintainer(user3)
member3 = project.add_maintainer(user4)
create(:group_member, :minimal_access, user: create(:user), source: group)
2020-11-24 15:15:51 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2).execute
2020-11-24 15:15:51 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1, member2, member3)
end
2020-11-24 15:15:51 +05:30
2023-03-17 16:20:25 +05:30
it 'includes only non-invite members if user do not have amdin permissions on project' do
create(:project_member, :invited, project: project, invite_email: create(:user).email)
member1 = project.add_maintainer(user1)
member2 = project.add_developer(user2)
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2).execute(include_relations: [:direct])
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1, member2)
end
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
it 'includes invited members if user have admin permissions on project' do
member_invite = create(:project_member, :invited, project: project, invite_email: create(:user).email)
member1 = project.add_maintainer(user1)
member2 = project.add_maintainer(user2)
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2).execute(include_relations: [:direct])
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1, member2, member_invite)
end
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
it 'includes nested group members if asked', :nested_groups do
nested_group.request_access(user1)
member1 = group.add_maintainer(user2)
member2 = nested_group.add_maintainer(user3)
member3 = project.add_maintainer(user4)
2018-11-08 19:23:39 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2).execute(include_relations: [:direct, :descendants])
2018-11-08 19:23:39 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1, member2, member3)
end
2019-12-04 20:38:33 +05:30
2023-03-17 16:20:25 +05:30
it 'returns only members of project if asked' do
nested_group.request_access(user1)
group.add_maintainer(user2)
nested_group.add_maintainer(user3)
member4 = project.add_maintainer(user4)
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2).execute(include_relations: [:direct])
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member4)
end
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
it 'returns only inherited members of project if asked' do
nested_group.request_access(user1)
member2 = group.add_maintainer(user2)
member3 = nested_group.add_maintainer(user3)
project.add_maintainer(user4)
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2).execute(include_relations: [:inherited])
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member2, member3)
end
2020-01-01 13:55:28 +05:30
2023-03-17 16:20:25 +05:30
it 'returns only inherited members of a personal project' do
project = create(:project, namespace: user1.namespace)
member = project.members.first
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user1).execute(include_relations: [:inherited])
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member)
end
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
it 'returns the members.access_level when the user is invited', :nested_groups do
member_invite = create(:project_member, :invited, project: project, invite_email: create(:user).email)
member1 = group.add_maintainer(user2)
2019-12-04 20:38:33 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2).execute(include_relations: [:direct, :descendants])
2019-12-04 20:38:33 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1, member_invite)
expect(result.last.access_level).to eq(member_invite.access_level)
end
2019-12-04 20:38:33 +05:30
2023-03-17 16:20:25 +05:30
it 'returns the highest access_level for the user', :nested_groups do
member1 = project.add_guest(user1)
group.add_developer(user1)
nested_group.add_reporter(user1)
2019-12-04 20:38:33 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user1).execute(include_relations: [:direct, :descendants])
2019-12-04 20:38:33 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member1)
expect(result.first.access_level).to eq(Gitlab::Access::DEVELOPER)
end
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
it 'returns searched members if requested' do
project.add_maintainer(user2)
project.add_maintainer(user3)
member3 = project.add_maintainer(user4)
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2, params: { search: user4.name }).execute
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to contain_exactly(member3)
end
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
it 'returns members sorted by id_desc' do
member1 = project.add_maintainer(user2)
member2 = project.add_maintainer(user3)
member3 = project.add_maintainer(user4)
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
result = described_class.new(project, user2, params: { sort: 'id_desc' }).execute
2020-03-13 15:44:24 +05:30
2023-03-17 16:20:25 +05:30
expect(result).to eq([member3, member2, member1])
end
2020-03-13 15:44:24 +05:30
2023-07-09 08:55:56 +05:30
context 'with :shared_into_ancestors' do
let_it_be(:invited_group) do
create(:group).tap do |invited_group|
create(:group_group_link, shared_group: nested_group, shared_with_group: invited_group)
end
end
let_it_be(:invited_group_member) { create(:group_member, :developer, group: invited_group, user: user1) }
let_it_be(:namespace_parent_member) { create(:group_member, :owner, group: group, user: user2) }
let_it_be(:namespace_member) { create(:group_member, :developer, group: nested_group, user: user3) }
let_it_be(:project_member) { create(:project_member, :developer, project: project, user: user4) }
subject(:result) { described_class.new(project, user4).execute(include_relations: include_relations) }
context 'when :shared_into_ancestors is included in the relations' do
let(:include_relations) { [:inherited, :direct, :invited_groups, :shared_into_ancestors] }
it "includes members of groups invited into ancestors of project's group" do
expect(result).to match_array([namespace_parent_member, namespace_member, invited_group_member, project_member])
end
end
context 'when :shared_into_ancestors is not included in the relations' do
let(:include_relations) { [:inherited, :direct, :invited_groups] }
it "does not include members of groups invited into ancestors of project's group" do
expect(result).to match_array([namespace_parent_member, namespace_member, project_member])
end
end
end
2023-03-17 16:20:25 +05:30
context 'when :invited_groups is passed' do
shared_examples 'with invited_groups param' do
subject { described_class.new(project, user2).execute(include_relations: [:inherited, :direct, :invited_groups]) }
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
let_it_be(:linked_group) { create(:group, :public) }
let_it_be(:nested_linked_group) { create(:group, parent: linked_group) }
let_it_be(:linked_group_member) { linked_group.add_guest(user1) }
let_it_be(:nested_linked_group_member) { nested_linked_group.add_guest(user2) }
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
it 'includes all the invited_groups members including members inherited from ancestor groups' do
create(:project_group_link, project: project, group: nested_linked_group)
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
expect(subject).to contain_exactly(linked_group_member, nested_linked_group_member)
end
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
it 'includes all the invited_groups members' do
create(:project_group_link, project: project, group: linked_group)
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
expect(subject).to contain_exactly(linked_group_member)
end
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
it 'excludes group_members not visible to the user' do
create(:project_group_link, project: project, group: linked_group)
private_linked_group = create(:group, :private)
private_linked_group.add_developer(user3)
create(:project_group_link, project: project, group: private_linked_group)
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
expect(subject).to contain_exactly(linked_group_member)
end
2021-11-18 22:05:49 +05:30
2023-03-17 16:20:25 +05:30
context 'when the user is a member of invited group and ancestor groups' do
it 'returns the highest access_level for the user limited by project_group_link.group_access', :nested_groups do
create(:project_group_link, project: project, group: nested_linked_group, group_access: Gitlab::Access::REPORTER)
nested_linked_group.add_developer(user1)
2021-11-18 22:05:49 +05:30
2023-03-17 16:20:25 +05:30
expect(subject.map(&:user)).to contain_exactly(user1, user2)
expect(subject.max_by(&:access_level).access_level).to eq(Gitlab::Access::REPORTER)
end
2021-11-18 22:05:49 +05:30
end
end
2023-03-17 16:20:25 +05:30
it_behaves_like 'with invited_groups param'
end
end
it_behaves_like '#execute'
2017-08-17 22:00:37 +05:30
end