diff options
Diffstat (limited to 'spec/services/authorized_project_update')
3 files changed, 77 insertions, 22 deletions
diff --git a/spec/services/authorized_project_update/periodic_recalculate_service_spec.rb b/spec/services/authorized_project_update/periodic_recalculate_service_spec.rb index c776e013fdf..782f6858870 100644 --- a/spec/services/authorized_project_update/periodic_recalculate_service_spec.rb +++ b/spec/services/authorized_project_update/periodic_recalculate_service_spec.rb @@ -17,7 +17,7 @@ RSpec.describe AuthorizedProjectUpdate::PeriodicRecalculateService do end it 'calls AuthorizedProjectUpdate::UserRefreshOverUserRangeWorker' do - (1..User.maximum(:id)).each_slice(batch_size).with_index do |batch, index| + (1..User.maximum(:id)).each_slice(batch_size).with_index(1) do |batch, index| delay = AuthorizedProjectUpdate::PeriodicRecalculateService::DELAY_INTERVAL * index expect(AuthorizedProjectUpdate::UserRefreshOverUserRangeWorker).to( diff --git a/spec/services/authorized_project_update/project_recalculate_service_spec.rb b/spec/services/authorized_project_update/project_recalculate_service_spec.rb new file mode 100644 index 00000000000..c339faaeabf --- /dev/null +++ b/spec/services/authorized_project_update/project_recalculate_service_spec.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AuthorizedProjectUpdate::ProjectRecalculateService, '#execute' do + let_it_be(:project) { create(:project) } + + subject(:execute) { described_class.new(project).execute } + + it 'returns success' do + expect(execute.success?).to eq(true) + end + + context 'when there are no changes to be made' do + it 'does not change authorizations' do + expect { execute }.not_to(change { ProjectAuthorization.count }) + end + end + + context 'when there are changes to be made' do + let(:user) { create(:user) } + + context 'when addition is required' do + before do + project.add_developer(user) + project.project_authorizations.where(user: user).delete_all + end + + it 'adds a new authorization record' do + expect { execute }.to( + change { project.project_authorizations.where(user: user).count } + .from(0).to(1) + ) + end + + it 'adds a new authorization record with the correct access level' do + execute + + project_authorization = project.project_authorizations.where( + user: user, + access_level: Gitlab::Access::DEVELOPER + ) + + expect(project_authorization).to exist + end + end + + context 'when removal is required' do + before do + create(:project_authorization, user: user, project: project) + end + + it 'removes the authorization record' do + expect { execute }.to( + change { project.project_authorizations.where(user: user).count } + .from(1).to(0) + ) + end + end + + context 'when an update in access level is required' do + before do + project.add_developer(user) + project.project_authorizations.where(user: user).delete_all + create(:project_authorization, project: project, user: user, access_level: Gitlab::Access::GUEST) + end + + it 'updates the authorization of the user to the correct access level' do + expect { execute }.to( + change { project.project_authorizations.find_by(user: user).access_level } + .from(Gitlab::Access::GUEST).to(Gitlab::Access::DEVELOPER) + ) + end + end + end +end diff --git a/spec/services/authorized_project_update/recalculate_for_user_range_service_spec.rb b/spec/services/authorized_project_update/recalculate_for_user_range_service_spec.rb deleted file mode 100644 index 95e2c0380bf..00000000000 --- a/spec/services/authorized_project_update/recalculate_for_user_range_service_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe AuthorizedProjectUpdate::RecalculateForUserRangeService do - describe '#execute' do - let_it_be(:users) { create_list(:user, 2) } - - it 'calls Users::RefreshAuthorizedProjectsService' do - user_ids = users.map(&:id) - - User.where(id: user_ids).select(:id).each do |user| - expect(Users::RefreshAuthorizedProjectsService).to( - receive(:new).with(user, source: described_class.name).and_call_original) - end - - range = user_ids.minmax - described_class.new(*range).execute - end - end -end |