2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe GroupsFinder do
|
|
|
|
describe '#execute' do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:user) { create(:user) }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
describe 'root level groups' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
where(:user_type, :params, :results) do
|
2019-03-02 22:35:43 +05:30
|
|
|
nil | { all_available: true } | %i(public_group user_public_group)
|
|
|
|
nil | { all_available: false } | %i(public_group user_public_group)
|
2018-10-15 14:42:47 +05:30
|
|
|
nil | {} | %i(public_group user_public_group)
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
:regular | { all_available: true } | %i(public_group internal_group user_public_group user_internal_group
|
|
|
|
user_private_group)
|
|
|
|
:regular | { all_available: false } | %i(user_public_group user_internal_group user_private_group)
|
2018-10-15 14:42:47 +05:30
|
|
|
:regular | {} | %i(public_group internal_group user_public_group user_internal_group user_private_group)
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
:external | { all_available: true } | %i(public_group user_public_group user_internal_group user_private_group)
|
|
|
|
:external | { all_available: false } | %i(user_public_group user_internal_group user_private_group)
|
2018-10-15 14:42:47 +05:30
|
|
|
:external | {} | %i(public_group user_public_group user_internal_group user_private_group)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
:admin | { all_available: true } | %i(public_group internal_group private_group user_public_group
|
|
|
|
user_internal_group user_private_group)
|
|
|
|
:admin | { all_available: false } | %i(user_public_group user_internal_group user_private_group)
|
2018-10-15 14:42:47 +05:30
|
|
|
:admin | {} | %i(public_group internal_group private_group user_public_group user_internal_group
|
|
|
|
user_private_group)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
# Fixme: Because of an issue: https://github.com/tomykaira/rspec-parameterized/issues/8#issuecomment-381888428
|
|
|
|
# The groups need to be created here, not with let syntax, and also compared by name and not ids
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
@groups = {
|
|
|
|
private_group: create(:group, :private, name: 'private_group'),
|
|
|
|
internal_group: create(:group, :internal, name: 'internal_group'),
|
|
|
|
public_group: create(:group, :public, name: 'public_group'),
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
user_private_group: create(:group, :private, name: 'user_private_group'),
|
|
|
|
user_internal_group: create(:group, :internal, name: 'user_internal_group'),
|
|
|
|
user_public_group: create(:group, :public, name: 'user_public_group')
|
|
|
|
}
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
if user_type
|
|
|
|
user =
|
|
|
|
case user_type
|
|
|
|
when :regular
|
|
|
|
create(:user)
|
|
|
|
when :external
|
|
|
|
create(:user, external: true)
|
|
|
|
when :admin
|
|
|
|
create(:user, :admin)
|
|
|
|
end
|
|
|
|
@groups.values_at(:user_private_group, :user_internal_group, :user_public_group).each do |group|
|
|
|
|
group.add_developer(user)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
subject { described_class.new(User.last, params).execute.to_a }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
it { is_expected.to match_array(@groups.values_at(*results)) }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'subgroups' do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:user) { create(:user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let!(:parent_group) { create(:group, :public) }
|
|
|
|
let!(:public_subgroup) { create(:group, :public, parent: parent_group) }
|
|
|
|
let!(:internal_subgroup) { create(:group, :internal, parent: parent_group) }
|
|
|
|
let!(:private_subgroup) { create(:group, :private, parent: parent_group) }
|
|
|
|
|
|
|
|
context 'without a user' do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'only returns parent and public subgroups' do
|
|
|
|
expect(described_class.new(nil).execute).to contain_exactly(parent_group, public_subgroup)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a user' do
|
2017-09-10 17:25:29 +05:30
|
|
|
subject { described_class.new(user).execute }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns parent, public, and internal subgroups' do
|
|
|
|
is_expected.to contain_exactly(parent_group, public_subgroup, internal_subgroup)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'being member' do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns parent, public subgroups, internal subgroups, and private subgroups user is member of' do
|
2017-08-17 22:00:37 +05:30
|
|
|
private_subgroup.add_guest(user)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
is_expected.to contain_exactly(parent_group, public_subgroup, internal_subgroup, private_subgroup)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'parent group private' do
|
|
|
|
before do
|
|
|
|
parent_group.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'being member of parent group' do
|
|
|
|
it 'returns all subgroups' do
|
|
|
|
parent_group.add_guest(user)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
is_expected.to contain_exactly(parent_group, public_subgroup, internal_subgroup, private_subgroup)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized to private project' do
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'project one level deep' do
|
|
|
|
let!(:subproject) { create(:project, :private, namespace: private_subgroup) }
|
|
|
|
before do
|
|
|
|
subproject.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the subgroup of the project' do
|
|
|
|
is_expected.to include(private_subgroup)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not include private subgroups deeper down' do
|
|
|
|
subsubgroup = create(:group, :private, parent: private_subgroup)
|
|
|
|
|
|
|
|
is_expected.not_to include(subsubgroup)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'project two levels deep' do
|
|
|
|
let!(:private_subsubgroup) { create(:group, :private, parent: private_subgroup) }
|
|
|
|
let!(:subsubproject) { create(:project, :private, namespace: private_subsubgroup) }
|
|
|
|
before do
|
|
|
|
subsubproject.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all the ancestor groups' do
|
|
|
|
is_expected.to include(private_subsubgroup, private_subgroup, parent_group)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns the groups for a given parent' do
|
|
|
|
expect(described_class.new(user, parent: parent_group).execute).to include(private_subgroup)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|