diff options
Diffstat (limited to 'spec/models/project_authorization_spec.rb')
-rw-r--r-- | spec/models/project_authorization_spec.rb | 186 |
1 files changed, 179 insertions, 7 deletions
diff --git a/spec/models/project_authorization_spec.rb b/spec/models/project_authorization_spec.rb index 14220007966..55fe28ceb6f 100644 --- a/spec/models/project_authorization_spec.rb +++ b/spec/models/project_authorization_spec.rb @@ -92,20 +92,192 @@ RSpec.describe ProjectAuthorization do let_it_be(:project_2) { create(:project) } let_it_be(:project_3) { create(:project) } - let(:per_batch_size) { 2 } - - it 'inserts the rows in batches, as per the `per_batch` size' do - attributes = [ + let(:attributes) do + [ { 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 } ] + end - expect(described_class).to receive(:insert_all).twice.and_call_original + before do + # Configure as if a replica database is enabled + allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false) + stub_feature_flags(enable_minor_delay_during_project_authorizations_refresh: true) + end - described_class.insert_all_in_batches(attributes, per_batch_size) + 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) - expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values)) + described_class.insert_all_in_batches(attributes, per_batch_size) + + expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values)) + end + end + + context 'when the total number of records to be inserted is greater than the batch size' do + let(:per_batch_size) { 2 } + + 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 + + described_class.insert_all_in_batches(attributes, per_batch_size) + + expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values)) + end + + 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' + end + end + + context 'when the total number of records to be inserted is less than the batch size' do + let(:per_batch_size) { 5 } + + it_behaves_like 'inserts the rows in batches, as per the `per_batch` size, without a delay between each batch' + 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] } + + before do + # Configure as if a replica database is enabled + allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false) + 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 + + 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) + + described_class.delete_all_in_batches_for_project( + project: project, + user_ids: user_ids, + per_batch: per_batch_size + ) + + expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids) + end + end + + context 'when the total number of records to be removed is greater than the batch size' do + let(:per_batch_size) { 2 } + + 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 + + described_class.delete_all_in_batches_for_project( + project: project, + user_ids: user_ids, + per_batch: per_batch_size + ) + + expect(project.project_authorizations.pluck(:user_id)).not_to include(*user_ids) + end + + 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' + 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' + 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] } + + before do + # Configure as if a replica database is enabled + allow(::Gitlab::Database::LoadBalancing).to receive(:primary_only?).and_return(false) + 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 + + 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) + + described_class.delete_all_in_batches_for_user( + user: user, + project_ids: project_ids, + per_batch: per_batch_size + ) + + expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids) + end + end + + context 'when the total number of records to be removed is greater than the batch size' do + let(:per_batch_size) { 2 } + + 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 + + described_class.delete_all_in_batches_for_user( + user: user, + project_ids: project_ids, + per_batch: per_batch_size + ) + + expect(user.project_authorizations.pluck(:project_id)).not_to include(*project_ids) + end + + 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' + 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' end end end |