2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-06 11:31:58 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe ProjectAuthorization do
|
2022-03-14 14:07:46 -04:00
|
|
|
describe 'unique user, project authorizations' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project_1) { create(:project) }
|
|
|
|
|
|
|
|
let!(:project_auth) do
|
|
|
|
create(
|
|
|
|
:project_authorization,
|
|
|
|
user: user,
|
|
|
|
project: project_1,
|
|
|
|
access_level: Gitlab::Access::DEVELOPER
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with duplicate user and project authorization' do
|
|
|
|
subject { project_auth.dup }
|
|
|
|
|
|
|
|
it { is_expected.to be_invalid }
|
|
|
|
|
|
|
|
context 'after validation' do
|
|
|
|
before do
|
|
|
|
subject.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains duplicate error' do
|
|
|
|
expect(subject.errors[:user]).to include('has already been taken')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple access levels for the same user and project' do
|
|
|
|
subject do
|
|
|
|
project_auth.dup.tap do |auth|
|
|
|
|
auth.access_level = Gitlab::Access::MAINTAINER
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_invalid }
|
|
|
|
|
|
|
|
context 'after validation' do
|
|
|
|
before do
|
|
|
|
subject.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains duplicate error' do
|
|
|
|
expect(subject.errors[:user]).to include('has already been taken')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-30 10:14:19 -05:00
|
|
|
describe 'relations' do
|
|
|
|
it { is_expected.to belong_to(:user) }
|
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
end
|
2016-12-06 11:31:58 -05:00
|
|
|
|
2021-11-30 10:14:19 -05:00
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:project) }
|
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
|
|
|
it { is_expected.to validate_presence_of(:access_level) }
|
|
|
|
it { is_expected.to validate_inclusion_of(:access_level).in_array(Gitlab::Access.all_values) }
|
|
|
|
end
|
2016-12-06 11:31:58 -05:00
|
|
|
|
2021-11-30 10:14:19 -05:00
|
|
|
describe '.insert_all' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project_1) { create(:project) }
|
|
|
|
let_it_be(:project_2) { create(:project) }
|
|
|
|
let_it_be(:project_3) { create(:project) }
|
2016-12-06 11:31:58 -05:00
|
|
|
|
2021-11-30 10:14:19 -05:00
|
|
|
it 'skips duplicates and inserts the remaining rows without error' do
|
|
|
|
create(:project_authorization, user: user, project: project_1, access_level: Gitlab::Access::MAINTAINER)
|
|
|
|
|
|
|
|
attributes = [
|
|
|
|
{ user_id: user.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
|
|
|
|
{ user_id: user.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
|
|
|
|
{ user_id: user.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER }
|
|
|
|
]
|
2016-12-06 11:31:58 -05:00
|
|
|
|
2021-11-30 10:14:19 -05:00
|
|
|
described_class.insert_all(attributes)
|
|
|
|
|
|
|
|
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
|
2016-12-06 11:31:58 -05:00
|
|
|
end
|
2021-11-30 10:14:19 -05:00
|
|
|
end
|
2021-11-12 07:10:27 -05:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
shared_examples_for 'does not log any detail' do
|
|
|
|
it 'does not log any detail' do
|
|
|
|
expect(Gitlab::AppLogger).not_to receive(:info)
|
|
|
|
|
|
|
|
execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'logs the detail' do
|
|
|
|
it 'logs the detail' do
|
|
|
|
expect(Gitlab::AppLogger).to receive(:info).with(
|
|
|
|
entire_size: 3,
|
|
|
|
message: 'Project authorizations refresh performed with delay'
|
|
|
|
)
|
|
|
|
|
|
|
|
execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-30 10:14:19 -05:00
|
|
|
describe '.insert_all_in_batches' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project_1) { create(:project) }
|
|
|
|
let_it_be(:project_2) { create(:project) }
|
|
|
|
let_it_be(:project_3) { create(:project) }
|
2021-11-12 07:10:27 -05:00
|
|
|
|
2022-09-20 08:11:03 -04:00
|
|
|
let(:attributes) do
|
|
|
|
[
|
2021-11-30 10:14:19 -05:00
|
|
|
{ user_id: user.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
|
|
|
|
{ user_id: user.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
|
|
|
|
{ user_id: user.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER }
|
2021-11-12 07:10:27 -05:00
|
|
|
]
|
2022-09-20 08:11:03 -04:00
|
|
|
end
|
2021-11-12 07:10:27 -05:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
subject(:execute) { described_class.insert_all_in_batches(attributes, per_batch_size) }
|
|
|
|
|
2022-09-20 08:11:03 -04:00
|
|
|
before do
|
2022-09-27 08:10:16 -04:00
|
|
|
# Configure as if a replica database is enabled
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
|
2022-09-20 08:11:03 -04:00
|
|
|
stub_feature_flags(enable_minor_delay_during_project_authorizations_refresh: true)
|
|
|
|
end
|
2021-11-30 10:14:19 -05:00
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
shared_examples_for 'inserts the rows in batches, as per the `per_batch` size, without a delay between each batch' do
|
|
|
|
specify do
|
|
|
|
expect(described_class).not_to receive(:sleep)
|
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
execute
|
2022-09-27 08:10:16 -04:00
|
|
|
|
|
|
|
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-20 08:11:03 -04:00
|
|
|
context 'when the total number of records to be inserted is greater than the batch size' do
|
|
|
|
let(:per_batch_size) { 2 }
|
2021-11-12 07:10:27 -05:00
|
|
|
|
2022-09-20 08:11:03 -04:00
|
|
|
it 'inserts the rows in batches, as per the `per_batch` size, with a delay between each batch' do
|
|
|
|
expect(described_class).to receive(:insert_all).twice.and_call_original
|
|
|
|
expect(described_class).to receive(:sleep).twice
|
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
execute
|
2022-09-20 08:11:03 -04:00
|
|
|
|
|
|
|
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
|
|
|
|
end
|
2022-09-27 08:10:16 -04:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'logs the detail'
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
context 'when the GitLab installation does not have a replica database configured' do
|
|
|
|
before do
|
|
|
|
# Configure as if a replica database is not enabled
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'inserts the rows in batches, as per the `per_batch` size, without a delay between each batch'
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'does not log any detail'
|
2022-09-27 08:10:16 -04:00
|
|
|
end
|
2022-09-20 08:11:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the total number of records to be inserted is less than the batch size' do
|
|
|
|
let(:per_batch_size) { 5 }
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
it_behaves_like 'inserts the rows in batches, as per the `per_batch` size, without a delay between each batch'
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'does not log any detail'
|
2022-09-20 08:11:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.delete_all_in_batches_for_project' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:user_1) { create(:user) }
|
|
|
|
let_it_be(:user_2) { create(:user) }
|
|
|
|
let_it_be(:user_3) { create(:user) }
|
|
|
|
let_it_be(:user_4) { create(:user) }
|
|
|
|
|
|
|
|
let(:user_ids) { [user_1.id, user_2.id, user_3.id] }
|
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
subject(:execute) do
|
|
|
|
described_class.delete_all_in_batches_for_project(
|
|
|
|
project: project,
|
|
|
|
user_ids: user_ids,
|
|
|
|
per_batch: per_batch_size
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-09-20 08:11:03 -04:00
|
|
|
before do
|
2022-09-27 08:10:16 -04:00
|
|
|
# Configure as if a replica database is enabled
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
|
2022-09-20 08:11:03 -04:00
|
|
|
stub_feature_flags(enable_minor_delay_during_project_authorizations_refresh: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
before_all do
|
|
|
|
create(:project_authorization, user: user_1, project: project)
|
|
|
|
create(:project_authorization, user: user_2, project: project)
|
|
|
|
create(:project_authorization, user: user_3, project: project)
|
|
|
|
create(:project_authorization, user: user_4, project: project)
|
|
|
|
end
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
shared_examples_for 'removes the project authorizations of the specified users in the current project, without a delay between each batch' do
|
|
|
|
specify do
|
|
|
|
expect(described_class).not_to receive(:sleep)
|
2022-09-20 08:11:03 -04:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
execute
|
2022-09-20 08:11:03 -04:00
|
|
|
|
|
|
|
expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
context 'when the total number of records to be removed is greater than the batch size' do
|
|
|
|
let(:per_batch_size) { 2 }
|
2022-09-20 08:11:03 -04:00
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
it 'removes the project authorizations of the specified users in the current project, with a delay between each batch' do
|
|
|
|
expect(described_class).to receive(:sleep).twice
|
2022-09-20 08:11:03 -04:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
execute
|
2022-09-20 08:11:03 -04:00
|
|
|
|
|
|
|
expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids)
|
|
|
|
end
|
2022-09-27 08:10:16 -04:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'logs the detail'
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
context 'when the GitLab installation does not have a replica database configured' do
|
|
|
|
before do
|
|
|
|
# Configure as if a replica database is not enabled
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'removes the project authorizations of the specified users in the current project, without a delay between each batch'
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'does not log any detail'
|
2022-09-27 08:10:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the total number of records to be removed is less than the batch size' do
|
|
|
|
let(:per_batch_size) { 5 }
|
|
|
|
|
|
|
|
it_behaves_like 'removes the project authorizations of the specified users in the current project, without a delay between each batch'
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'does not log any detail'
|
2022-09-20 08:11:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.delete_all_in_batches_for_user' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project_1) { create(:project) }
|
|
|
|
let_it_be(:project_2) { create(:project) }
|
|
|
|
let_it_be(:project_3) { create(:project) }
|
|
|
|
let_it_be(:project_4) { create(:project) }
|
|
|
|
|
|
|
|
let(:project_ids) { [project_1.id, project_2.id, project_3.id] }
|
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
subject(:execute) do
|
|
|
|
described_class.delete_all_in_batches_for_user(
|
|
|
|
user: user,
|
|
|
|
project_ids: project_ids,
|
|
|
|
per_batch: per_batch_size
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-09-20 08:11:03 -04:00
|
|
|
before do
|
2022-09-27 08:10:16 -04:00
|
|
|
# Configure as if a replica database is enabled
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false)
|
2022-09-20 08:11:03 -04:00
|
|
|
stub_feature_flags(enable_minor_delay_during_project_authorizations_refresh: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
before_all do
|
|
|
|
create(:project_authorization, user: user, project: project_1)
|
|
|
|
create(:project_authorization, user: user, project: project_2)
|
|
|
|
create(:project_authorization, user: user, project: project_3)
|
|
|
|
create(:project_authorization, user: user, project: project_4)
|
|
|
|
end
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
shared_examples_for 'removes the project authorizations of the specified projects from the current user, without a delay between each batch' do
|
|
|
|
specify do
|
|
|
|
expect(described_class).not_to receive(:sleep)
|
2022-09-20 08:11:03 -04:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
execute
|
2022-09-20 08:11:03 -04:00
|
|
|
|
|
|
|
expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
context 'when the total number of records to be removed is greater than the batch size' do
|
|
|
|
let(:per_batch_size) { 2 }
|
2022-09-20 08:11:03 -04:00
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
it 'removes the project authorizations of the specified projects from the current user, with a delay between each batch' do
|
|
|
|
expect(described_class).to receive(:sleep).twice
|
2022-09-20 08:11:03 -04:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
execute
|
2022-09-20 08:11:03 -04:00
|
|
|
|
|
|
|
expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids)
|
|
|
|
end
|
2022-09-27 08:10:16 -04:00
|
|
|
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'logs the detail'
|
|
|
|
|
2022-09-27 08:10:16 -04:00
|
|
|
context 'when the GitLab installation does not have a replica database configured' do
|
|
|
|
before do
|
|
|
|
# Configure as if a replica database is not enabled
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'removes the project authorizations of the specified projects from the current user, without a delay between each batch'
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'does not log any detail'
|
2022-09-27 08:10:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the total number of records to be removed is less than the batch size' do
|
|
|
|
let(:per_batch_size) { 5 }
|
|
|
|
|
|
|
|
it_behaves_like 'removes the project authorizations of the specified projects from the current user, without a delay between each batch'
|
2022-10-20 11:10:58 -04:00
|
|
|
it_behaves_like 'does not log any detail'
|
2021-11-12 07:10:27 -05:00
|
|
|
end
|
2016-12-06 11:31:58 -05:00
|
|
|
end
|
|
|
|
end
|