2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Projects::GroupLinks::CreateService, '#execute', feature_category: :subgroups do
|
2020-06-23 00:09:42 +05:30
|
|
|
let_it_be(:user) { create :user }
|
|
|
|
let_it_be(:group) { create :group }
|
2022-07-16 23:28:13 +05:30
|
|
|
let_it_be(:project) { create(:project, namespace: create(:namespace, :with_namespace_settings)) }
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:opts) do
|
|
|
|
{
|
2022-07-16 23:28:13 +05:30
|
|
|
link_group_access: Gitlab::Access::DEVELOPER,
|
2018-03-17 18:26:18 +05:30
|
|
|
expires_at: nil
|
|
|
|
}
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
subject { described_class.new(project, group, user, opts) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
shared_examples_for 'not shareable' do
|
|
|
|
it 'does not share and returns an error' do
|
|
|
|
expect do
|
|
|
|
result = subject.execute
|
2019-03-13 22:55:13 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:http_status]).to eq(404)
|
|
|
|
end.not_to change { project.project_group_links.count }
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
shared_examples_for 'shareable' do
|
|
|
|
it 'adds group to project' do
|
|
|
|
expect do
|
|
|
|
result = subject.execute
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end.to change { project.project_group_links.count }.from(0).to(1)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2019-03-13 22:55:13 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
context 'when user has proper membership to share a group' do
|
|
|
|
before do
|
|
|
|
group.add_guest(user)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
it_behaves_like 'shareable'
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
it 'updates authorization', :sidekiq_inline do
|
|
|
|
expect { subject.execute }.to(
|
|
|
|
change { Ability.allowed?(user, :read_project, project) }
|
|
|
|
.from(false).to(true))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with specialized project_authorization workers' do
|
|
|
|
let_it_be(:other_user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_developer(other_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules authorization update for users with access to group' do
|
2023-04-23 21:23:45 +05:30
|
|
|
stub_feature_flags(do_not_run_safety_net_auth_refresh_jobs: false)
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
expect(AuthorizedProjectsWorker).not_to(
|
|
|
|
receive(:bulk_perform_async)
|
|
|
|
)
|
|
|
|
expect(AuthorizedProjectUpdate::ProjectRecalculateWorker).to(
|
|
|
|
receive(:perform_async)
|
|
|
|
.with(project.id)
|
|
|
|
.and_call_original
|
|
|
|
)
|
|
|
|
expect(AuthorizedProjectUpdate::UserRefreshFromReplicaWorker).to(
|
2023-06-20 00:43:36 +05:30
|
|
|
receive(:bulk_perform_in).with(
|
|
|
|
1.hour,
|
|
|
|
array_including([user.id], [other_user.id]),
|
|
|
|
batch_delay: 30.seconds, batch_size: 100
|
|
|
|
).and_call_original
|
2022-07-16 23:28:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
context 'when sharing outside the hierarchy is disabled' do
|
|
|
|
let_it_be(:shared_group_parent) do
|
2023-06-20 00:43:36 +05:30
|
|
|
create(:group, namespace_settings: create(:namespace_settings, prevent_sharing_groups_outside_hierarchy: true))
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:project, reload: true) { create(:project, group: shared_group_parent) }
|
|
|
|
|
|
|
|
it_behaves_like 'not shareable'
|
|
|
|
|
|
|
|
context 'when group is inside hierarchy' do
|
|
|
|
let(:group) { create(:group, :private, parent: shared_group_parent) }
|
|
|
|
|
|
|
|
it_behaves_like 'shareable'
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
context 'when user does not have permissions for the group' do
|
|
|
|
it_behaves_like 'not shareable'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when group is blank' do
|
|
|
|
let(:group) { nil }
|
|
|
|
|
|
|
|
it_behaves_like 'not shareable'
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|