diff options
Diffstat (limited to 'spec/services/projects')
9 files changed, 257 insertions, 17 deletions
diff --git a/spec/services/projects/alerting/notify_service_spec.rb b/spec/services/projects/alerting/notify_service_spec.rb index 4b7b7b0b200..4e366fce0d9 100644 --- a/spec/services/projects/alerting/notify_service_spec.rb +++ b/spec/services/projects/alerting/notify_service_spec.rb @@ -36,7 +36,7 @@ RSpec.describe Projects::Alerting::NotifyService do subject { service.execute(token, nil) } - shared_examples 'notifcations are handled correctly' do + shared_examples 'notifications are handled correctly' do context 'with valid token' do let(:token) { integration.token } let(:incident_management_setting) { double(send_email?: email_enabled, create_issue?: issue_enabled, auto_close_incident?: auto_close_enabled) } @@ -85,6 +85,15 @@ RSpec.describe Projects::Alerting::NotifyService do it_behaves_like 'creates an alert management alert' it_behaves_like 'assigns the alert properties' + it 'passes the integration to alert processing' do + expect(Gitlab::AlertManagement::Payload) + .to receive(:parse) + .with(project, payload.to_h, integration: integration) + .and_call_original + + subject + end + it 'creates a system note corresponding to alert creation' do expect { subject }.to change(Note, :count).by(1) expect(Note.last.note).to include(payload_raw.fetch(:monitoring_tool)) @@ -259,7 +268,7 @@ RSpec.describe Projects::Alerting::NotifyService do subject { service.execute(token, integration) } - it_behaves_like 'notifcations are handled correctly' do + it_behaves_like 'notifications are handled correctly' do let(:source) { integration.name } end diff --git a/spec/services/projects/branches_by_mode_service_spec.rb b/spec/services/projects/branches_by_mode_service_spec.rb new file mode 100644 index 00000000000..9199c3e0b3a --- /dev/null +++ b/spec/services/projects/branches_by_mode_service_spec.rb @@ -0,0 +1,136 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Projects::BranchesByModeService do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project, :repository) } + + let(:finder) { described_class.new(project, params) } + let(:params) { { mode: 'all' } } + + subject { finder.execute } + + describe '#execute' do + context 'page is passed' do + let(:params) { { page: 4, mode: 'all', offset: 3 } } + + it 'uses offset pagination' do + expect(finder).to receive(:fetch_branches_via_offset_pagination).and_call_original + + branches, prev_page, next_page = subject + + expect(branches.size).to eq(10) + expect(next_page).to be_nil + expect(prev_page).to eq("/#{project.full_path}/-/branches/all?offset=2&page=3") + end + + context 'but the page does not contain any branches' do + let(:params) { { page: 10, mode: 'all' } } + + it 'uses offset pagination' do + expect(finder).to receive(:fetch_branches_via_offset_pagination).and_call_original + + branches, prev_page, next_page = subject + + expect(branches).to eq([]) + expect(next_page).to be_nil + expect(prev_page).to be_nil + end + end + end + + context 'search is passed' do + let(:params) { { search: 'feature' } } + + it 'uses offset pagination' do + expect(finder).to receive(:fetch_branches_via_offset_pagination).and_call_original + + branches, prev_page, next_page = subject + + expect(branches.map(&:name)).to match_array(%w(feature feature_conflict)) + expect(next_page).to be_nil + expect(prev_page).to be_nil + end + end + + context 'branch_list_keyset_pagination is disabled' do + it 'uses offset pagination' do + stub_feature_flags(branch_list_keyset_pagination: false) + + expect(finder).to receive(:fetch_branches_via_offset_pagination).and_call_original + + branches, prev_page, next_page = subject + + expect(branches.size).to eq(20) + expect(next_page).to eq("/#{project.full_path}/-/branches/all?offset=1&page_token=conflict-resolvable") + expect(prev_page).to be_nil + end + end + + context 'uses gitaly pagination' do + before do + expect(finder).to receive(:fetch_branches_via_gitaly_pagination).and_call_original + end + + it 'returns branches for the first page' do + branches, prev_page, next_page = subject + + expect(branches.size).to eq(20) + expect(next_page).to eq("/#{project.full_path}/-/branches/all?offset=1&page_token=conflict-resolvable") + expect(prev_page).to be_nil + end + + context 'when second page is requested' do + let(:params) { { page_token: 'conflict-resolvable', mode: 'all', sort: 'name_asc', offset: 1 } } + + it 'returns branches for the first page' do + branches, prev_page, next_page = subject + + expect(branches.size).to eq(20) + expect(next_page).to eq("/#{project.full_path}/-/branches/all?offset=2&page_token=improve%2Fawesome&sort=name_asc") + expect(prev_page).to eq("/#{project.full_path}/-/branches/all?offset=0&page=1&sort=name_asc") + end + end + + context 'when last page is requested' do + let(:params) { { page_token: 'signed-commits', mode: 'all', sort: 'name_asc', offset: 4 } } + + it 'returns branches after the specified branch' do + branches, prev_page, next_page = subject + + expect(branches.size).to eq(14) + expect(next_page).to be_nil + expect(prev_page).to eq("/#{project.full_path}/-/branches/all?offset=3&page=4&sort=name_asc") + end + end + end + + context 'filter by mode' do + let(:stale) { double(state: 'stale') } + let(:active) { double(state: 'active') } + + before do + allow_next_instance_of(BranchesFinder) do |instance| + allow(instance).to receive(:execute).and_return([stale, active]) + end + end + + context 'stale' do + let(:params) { { mode: 'stale' } } + + it 'returns stale branches' do + is_expected.to eq([[stale], nil, nil]) + end + end + + context 'active' do + let(:params) { { mode: 'active' } } + + it 'returns active branches' do + is_expected.to eq([[active], nil, nil]) + end + end + end + end +end diff --git a/spec/services/projects/cleanup_service_spec.rb b/spec/services/projects/cleanup_service_spec.rb index 6fd29813d98..f2c052d9397 100644 --- a/spec/services/projects/cleanup_service_spec.rb +++ b/spec/services/projects/cleanup_service_spec.rb @@ -88,7 +88,7 @@ RSpec.describe Projects::CleanupService do end it 'runs garbage collection on the repository' do - expect_next_instance_of(GitGarbageCollectWorker) do |worker| + expect_next_instance_of(Projects::GitGarbageCollectWorker) do |worker| expect(worker).to receive(:perform).with(project.id, :prune, "project_cleanup:gc:#{project.id}") end diff --git a/spec/services/projects/container_repository/cleanup_tags_service_spec.rb b/spec/services/projects/container_repository/cleanup_tags_service_spec.rb index 17c2f0f6c17..eed22416868 100644 --- a/spec/services/projects/container_repository/cleanup_tags_service_spec.rb +++ b/spec/services/projects/container_repository/cleanup_tags_service_spec.rb @@ -284,7 +284,7 @@ RSpec.describe Projects::ContainerRepository::CleanupTagsService do deleted: nil ) - expect(result).to eq(service_response.compact) + expect(result).to eq(service_response) end end @@ -369,6 +369,6 @@ RSpec.describe Projects::ContainerRepository::CleanupTagsService do before_truncate_size: before_truncate_size, after_truncate_size: after_truncate_size, before_delete_size: before_delete_size - }.compact + }.compact.merge(deleted_size: deleted&.size) end end diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb index 6c0e6654622..f7da6f75141 100644 --- a/spec/services/projects/create_service_spec.rb +++ b/spec/services/projects/create_service_spec.rb @@ -40,6 +40,48 @@ RSpec.describe Projects::CreateService, '#execute' do end end + describe 'setting name and path' do + subject(:project) { create_project(user, opts) } + + context 'when both are set' do + let(:opts) { { name: 'one', path: 'two' } } + + it 'keeps them as specified' do + expect(project.name).to eq('one') + expect(project.path).to eq('two') + end + end + + context 'when path is set' do + let(:opts) { { path: 'one.two_three-four' } } + + it 'sets name == path' do + expect(project.path).to eq('one.two_three-four') + expect(project.name).to eq(project.path) + end + end + + context 'when name is a valid path' do + let(:opts) { { name: 'one.two_three-four' } } + + it 'sets path == name' do + expect(project.name).to eq('one.two_three-four') + expect(project.path).to eq(project.name) + end + end + + context 'when name is not a valid path' do + let(:opts) { { name: 'one.two_three-four and five' } } + + # TODO: Retained for backwards compatibility. Remove in API v5. + # See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/52725 + it 'parameterizes the name' do + expect(project.name).to eq('one.two_three-four and five') + expect(project.path).to eq('one-two_three-four-and-five') + end + end + end + context 'user namespace' do it do project = create_project(user, opts) @@ -419,7 +461,7 @@ RSpec.describe Projects::CreateService, '#execute' do context 'when another repository already exists on disk' do let(:opts) do { - name: 'Existing', + name: 'existing', namespace_id: user.namespace.id } end diff --git a/spec/services/projects/fork_service_spec.rb b/spec/services/projects/fork_service_spec.rb index a11f16573f5..df02f8ea15d 100644 --- a/spec/services/projects/fork_service_spec.rb +++ b/spec/services/projects/fork_service_spec.rb @@ -323,6 +323,50 @@ RSpec.describe Projects::ForkService do end end end + + describe 'fork with optional attributes' do + let(:public_project) { create(:project, :public) } + + it 'sets optional attributes to specified values' do + forked_project = fork_project( + public_project, + nil, + namespace: public_project.namespace, + path: 'forked', + name: 'My Fork', + description: 'Description', + visibility: 'internal', + using_service: true + ) + + expect(forked_project.path).to eq('forked') + expect(forked_project.name).to eq('My Fork') + expect(forked_project.description).to eq('Description') + expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL) + end + + it 'sets visibility level to private if an unknown visibility is requested' do + forked_project = fork_project(public_project, nil, using_service: true, visibility: 'unknown') + + expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) + end + + it 'sets visibility level to project visibility level if requested visibility is greater' do + private_project = create(:project, :private) + + forked_project = fork_project(private_project, nil, using_service: true, visibility: 'public') + + expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) + end + + it 'sets visibility level to target namespace visibility level if requested visibility is greater' do + private_group = create(:group, :private) + + forked_project = fork_project(public_project, nil, namespace: private_group, using_service: true, visibility: 'public') + + expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) + end + end end context 'when a project is already forked' do diff --git a/spec/services/projects/prometheus/alerts/notify_service_spec.rb b/spec/services/projects/prometheus/alerts/notify_service_spec.rb index 8ae47ec266c..e196220eabe 100644 --- a/spec/services/projects/prometheus/alerts/notify_service_spec.rb +++ b/spec/services/projects/prometheus/alerts/notify_service_spec.rb @@ -4,6 +4,7 @@ require 'spec_helper' RSpec.describe Projects::Prometheus::Alerts::NotifyService do include PrometheusHelpers + using RSpec::Parameterized::TableSyntax let_it_be(:project, reload: true) { create(:project) } @@ -61,8 +62,6 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do end context 'with project specific cluster' do - using RSpec::Parameterized::TableSyntax - where(:cluster_enabled, :status, :configured_token, :token_input, :result) do true | :installed | token | token | :success true | :installed | nil | nil | :success @@ -104,8 +103,6 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do end context 'with manual prometheus installation' do - using RSpec::Parameterized::TableSyntax - where(:alerting_setting, :configured_token, :token_input, :result) do true | token | token | :success true | token | 'x' | :failure @@ -139,8 +136,6 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do end context 'with HTTP integration' do - using RSpec::Parameterized::TableSyntax - where(:active, :token, :result) do :active | :valid | :success :active | :invalid | :failure diff --git a/spec/services/projects/update_pages_service_spec.rb b/spec/services/projects/update_pages_service_spec.rb index a6730c5de52..6bf2876f640 100644 --- a/spec/services/projects/update_pages_service_spec.rb +++ b/spec/services/projects/update_pages_service_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Projects::UpdatePagesService do subject { described_class.new(project, build) } before do - project.remove_pages + project.legacy_remove_pages end context '::TMP_EXTRACT_PATH' do @@ -55,6 +55,15 @@ RSpec.describe Projects::UpdatePagesService do end end + it "doesn't deploy to legacy storage if it's disabled" do + stub_feature_flags(pages_update_legacy_storage: false) + + expect(execute).to eq(:success) + expect(project.pages_deployed?).to be_truthy + + expect(File.exist?(File.join(project.pages_path, 'public', 'index.html'))).to eq(false) + end + it 'creates pages_deployment and saves it in the metadata' do expect do expect(execute).to eq(:success) diff --git a/spec/services/projects/update_repository_storage_service_spec.rb b/spec/services/projects/update_repository_storage_service_spec.rb index ef8f166cc3f..828667fdfc2 100644 --- a/spec/services/projects/update_repository_storage_service_spec.rb +++ b/spec/services/projects/update_repository_storage_service_spec.rb @@ -59,13 +59,18 @@ RSpec.describe Projects::UpdateRepositoryStorageService do end context 'when the filesystems are the same' do - let(:destination) { project.repository_storage } + before do + expect(Gitlab::GitalyClient).to receive(:filesystem_id).twice.and_return(SecureRandom.uuid) + end - it 'bails out and does nothing' do + it 'updates the database without trying to move the repostory', :aggregate_failures do result = subject.execute + project.reload - expect(result).to be_error - expect(result.message).to match(/SameFilesystemError/) + expect(result).to be_success + expect(project).not_to be_repository_read_only + expect(project.repository_storage).to eq('test_second_storage') + expect(project.project_repository.shard_name).to eq('test_second_storage') end end |