diff options
Diffstat (limited to 'spec/services')
9 files changed, 98 insertions, 278 deletions
diff --git a/spec/services/groups/import_export/export_service_spec.rb b/spec/services/groups/import_export/export_service_spec.rb index e9e356ab4f6..b78e2f0bc40 100644 --- a/spec/services/groups/import_export/export_service_spec.rb +++ b/spec/services/groups/import_export/export_service_spec.rb @@ -49,6 +49,12 @@ describe Groups::ImportExport::ExportService do FileUtils.rm_rf(archive_path) end + it 'saves the version' do + expect(Gitlab::ImportExport::VersionSaver).to receive(:new).and_call_original + + service.execute + end + it 'saves the models using ndjson tree saver' do stub_feature_flags(group_export_ndjson: true) diff --git a/spec/services/metrics/users_starred_dashboards/delete_service_spec.rb b/spec/services/metrics/users_starred_dashboards/delete_service_spec.rb new file mode 100644 index 00000000000..68a2fef5931 --- /dev/null +++ b/spec/services/metrics/users_starred_dashboards/delete_service_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Metrics::UsersStarredDashboards::DeleteService do + subject(:service_instance) { described_class.new(user, project, dashboard_path) } + + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + + describe '#execute' do + let_it_be(:user_starred_dashboard_1) { create(:metrics_users_starred_dashboard, user: user, project: project, dashboard_path: 'dashboard_1') } + let_it_be(:user_starred_dashboard_2) { create(:metrics_users_starred_dashboard, user: user, project: project) } + let_it_be(:other_user_starred_dashboard) { create(:metrics_users_starred_dashboard, project: project) } + let_it_be(:other_project_starred_dashboard) { create(:metrics_users_starred_dashboard, user: user) } + + context 'without dashboard_path' do + let(:dashboard_path) { nil } + + it 'does not scope user starred dashboards by dashboard path' do + result = service_instance.execute + + expect(result.success?).to be_truthy + expect(result.payload[:deleted_rows]).to be(2) + expect(Metrics::UsersStarredDashboard.all).to contain_exactly(other_user_starred_dashboard, other_project_starred_dashboard) + end + end + + context 'with dashboard_path' do + let(:dashboard_path) { 'dashboard_1' } + + it 'does scope user starred dashboards by dashboard path' do + result = service_instance.execute + + expect(result.success?).to be_truthy + expect(result.payload[:deleted_rows]).to be(1) + expect(Metrics::UsersStarredDashboard.all).to contain_exactly(user_starred_dashboard_2, other_user_starred_dashboard, other_project_starred_dashboard) + end + end + end +end diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb index 1feea27eebc..5b0e32dcd67 100644 --- a/spec/services/projects/create_service_spec.rb +++ b/spec/services/projects/create_service_spec.rb @@ -489,6 +489,27 @@ describe Projects::CreateService, '#execute' do end end + it_behaves_like 'measurable service' do + before do + opts.merge!( + current_user: user, + path: 'foo' + ) + end + + let(:base_log_data) do + { + class: Projects::CreateService.name, + current_user: user.name, + project_full_path: "#{user.namespace.full_path}/#{opts[:path]}" + } + end + + after do + create_project(user, opts) + end + end + def create_project(user, opts) Projects::CreateService.new(user, opts).execute end diff --git a/spec/services/projects/import_export/export_service_spec.rb b/spec/services/projects/import_export/export_service_spec.rb index 32d0b52f096..5f496cb1e56 100644 --- a/spec/services/projects/import_export/export_service_spec.rb +++ b/spec/services/projects/import_export/export_service_spec.rb @@ -7,9 +7,10 @@ describe Projects::ImportExport::ExportService do let!(:user) { create(:user) } let(:project) { create(:project) } let(:shared) { project.import_export_shared } - let(:service) { described_class.new(project, user) } let!(:after_export_strategy) { Gitlab::ImportExport::AfterExportStrategies::DownloadNotificationStrategy.new } + subject(:service) { described_class.new(project, user) } + before do project.add_maintainer(user) end @@ -184,7 +185,7 @@ describe Projects::ImportExport::ExportService do end end - context 'when measurable params are provided' do + it_behaves_like 'measurable service' do let(:base_log_data) do { class: described_class.name, @@ -194,44 +195,8 @@ describe Projects::ImportExport::ExportService do } end - subject(:service) { described_class.new(project, user) } - - context 'when measurement is enabled' do - let(:logger) { double(:logger) } - let(:measurable_options) do - { - measurement_enabled: true, - measurement_logger: logger - } - end - - before do - allow(logger).to receive(:info) - end - - it 'measure service execution with Gitlab::Utils::Measuring' do - expect(Gitlab::Utils::Measuring).to receive(:execute_with).with(true, logger, base_log_data).and_call_original - expect_next_instance_of(Gitlab::Utils::Measuring) do |measuring| - expect(measuring).to receive(:with_measuring).and_call_original - end - - service.execute(after_export_strategy, measurable_options) - end - end - - context 'when measurement is disabled' do - let(:measurable_options) do - { - measurement_enabled: false - } - end - - it 'does not measure service execution' do - expect(Gitlab::Utils::Measuring).to receive(:execute_with).with(false, nil, base_log_data).and_call_original - expect(Gitlab::Utils::Measuring).not_to receive(:new) - - service.execute(after_export_strategy, measurable_options) - end + after do + service.execute(after_export_strategy) end end end diff --git a/spec/services/projects/import_service_spec.rb b/spec/services/projects/import_service_spec.rb index af8118f9b11..ca6750b373d 100644 --- a/spec/services/projects/import_service_spec.rb +++ b/spec/services/projects/import_service_spec.rb @@ -264,13 +264,33 @@ describe Projects::ImportService do it 'fails with port 25' do project.import_url = "https://github.com:25/vim/vim.git" - result = described_class.new(project, user).execute + result = subject.execute expect(result[:status]).to eq :error expect(result[:message]).to include('Only allowed ports are 80, 443') end end + it_behaves_like 'measurable service' do + let(:base_log_data) do + { + class: described_class.name, + current_user: user.name, + project_full_path: project.full_path, + import_type: project.import_type, + file_path: project.import_source + } + end + + before do + project.import_type = 'github' + end + + after do + subject.execute + end + end + def stub_github_omniauth_provider provider = OpenStruct.new( 'name' => 'github', diff --git a/spec/services/wiki_pages/base_service_spec.rb b/spec/services/wiki_pages/base_service_spec.rb index 4c44c195ac8..fede86a5192 100644 --- a/spec/services/wiki_pages/base_service_spec.rb +++ b/spec/services/wiki_pages/base_service_spec.rb @@ -10,7 +10,7 @@ describe WikiPages::BaseService do counter = Gitlab::UsageDataCounters::WikiPageCounter error = counter::UnknownEvent - let(:subject) { bad_service_class.new(project, user, {}) } + let(:subject) { bad_service_class.new(container: project, current_user: user) } context 'the class implements usage_counter_action incorrectly' do let(:bad_service_class) do diff --git a/spec/services/wiki_pages/create_service_spec.rb b/spec/services/wiki_pages/create_service_spec.rb index d63d62e9492..2a17805110e 100644 --- a/spec/services/wiki_pages/create_service_spec.rb +++ b/spec/services/wiki_pages/create_service_spec.rb @@ -3,96 +3,5 @@ require 'spec_helper' describe WikiPages::CreateService do - let(:project) { create(:project, :wiki_repo) } - let(:user) { create(:user) } - let(:page_title) { 'Title' } - - let(:opts) do - { - title: page_title, - content: 'Content for wiki page', - format: 'markdown' - } - end - - subject(:service) { described_class.new(project, user, opts) } - - before do - project.add_developer(user) - end - - describe '#execute' do - it 'creates wiki page with valid attributes' do - page = service.execute - - expect(page).to be_valid - expect(page.title).to eq(opts[:title]) - expect(page.content).to eq(opts[:content]) - expect(page.format).to eq(opts[:format].to_sym) - end - - it 'executes webhooks' do - expect(service).to receive(:execute_hooks).once.with(WikiPage) - - service.execute - end - - it 'counts wiki page creation' do - counter = Gitlab::UsageDataCounters::WikiPageCounter - - expect { service.execute }.to change { counter.read(:create) }.by 1 - end - - shared_examples 'correct event created' do - it 'creates appropriate events' do - expect { service.execute }.to change { Event.count }.by 1 - - expect(Event.recent.first).to have_attributes( - action: Event::CREATED, - target: have_attributes(canonical_slug: page_title) - ) - end - end - - context 'the new page is at the top level' do - let(:page_title) { 'root-level-page' } - - include_examples 'correct event created' - end - - context 'the new page is in a subsection' do - let(:page_title) { 'subsection/page' } - - include_examples 'correct event created' - end - - context 'the feature is disabled' do - before do - stub_feature_flags(wiki_events: false) - end - - it 'does not record the activity' do - expect { service.execute }.not_to change(Event, :count) - end - end - - context 'when the options are bad' do - let(:page_title) { '' } - - it 'does not count a creation event' do - counter = Gitlab::UsageDataCounters::WikiPageCounter - - expect { service.execute }.not_to change { counter.read(:create) } - end - - it 'does not record the activity' do - expect { service.execute }.not_to change(Event, :count) - end - - it 'reports the error' do - expect(service.execute).to be_invalid - .and have_attributes(errors: be_present) - end - end - end + it_behaves_like 'WikiPages::CreateService#execute', :project end diff --git a/spec/services/wiki_pages/destroy_service_spec.rb b/spec/services/wiki_pages/destroy_service_spec.rb index e205bedfdb9..b6fee1fd896 100644 --- a/spec/services/wiki_pages/destroy_service_spec.rb +++ b/spec/services/wiki_pages/destroy_service_spec.rb @@ -3,52 +3,5 @@ require 'spec_helper' describe WikiPages::DestroyService do - let(:project) { create(:project) } - let(:user) { create(:user) } - let(:page) { create(:wiki_page) } - - subject(:service) { described_class.new(project, user) } - - before do - project.add_developer(user) - end - - describe '#execute' do - it 'executes webhooks' do - expect(service).to receive(:execute_hooks).once.with(page) - - service.execute(page) - end - - it 'increments the delete count' do - counter = Gitlab::UsageDataCounters::WikiPageCounter - - expect { service.execute(page) }.to change { counter.read(:delete) }.by 1 - end - - it 'creates a new wiki page deletion event' do - expect { service.execute(page) }.to change { Event.count }.by 1 - - expect(Event.recent.first).to have_attributes( - action: Event::DESTROYED, - target: have_attributes(canonical_slug: page.slug) - ) - end - - it 'does not increment the delete count if the deletion failed' do - counter = Gitlab::UsageDataCounters::WikiPageCounter - - expect { service.execute(nil) }.not_to change { counter.read(:delete) } - end - end - - context 'the feature is disabled' do - before do - stub_feature_flags(wiki_events: false) - end - - it 'does not record the activity' do - expect { service.execute(page) }.not_to change(Event, :count) - end - end + it_behaves_like 'WikiPages::DestroyService#execute', :project end diff --git a/spec/services/wiki_pages/update_service_spec.rb b/spec/services/wiki_pages/update_service_spec.rb index ece714ee8e5..ac629a96f9a 100644 --- a/spec/services/wiki_pages/update_service_spec.rb +++ b/spec/services/wiki_pages/update_service_spec.rb @@ -3,100 +3,5 @@ require 'spec_helper' describe WikiPages::UpdateService do - let(:project) { create(:project) } - let(:user) { create(:user) } - let(:page) { create(:wiki_page) } - let(:page_title) { 'New Title' } - - let(:opts) do - { - content: 'New content for wiki page', - format: 'markdown', - message: 'New wiki message', - title: page_title - } - end - - subject(:service) { described_class.new(project, user, opts) } - - before do - project.add_developer(user) - end - - describe '#execute' do - it 'updates the wiki page' do - updated_page = service.execute(page) - - expect(updated_page).to be_valid - expect(updated_page.message).to eq(opts[:message]) - expect(updated_page.content).to eq(opts[:content]) - expect(updated_page.format).to eq(opts[:format].to_sym) - expect(updated_page.title).to eq(page_title) - end - - it 'executes webhooks' do - expect(service).to receive(:execute_hooks).once.with(WikiPage) - - service.execute(page) - end - - it 'counts edit events' do - counter = Gitlab::UsageDataCounters::WikiPageCounter - - expect { service.execute page }.to change { counter.read(:update) }.by 1 - end - - shared_examples 'adds activity event' do - it 'adds a new wiki page activity event' do - expect { service.execute(page) }.to change { Event.count }.by 1 - - expect(Event.recent.first).to have_attributes( - action: Event::UPDATED, - wiki_page: page, - target_title: page.title - ) - end - end - - context 'the page is at the top level' do - let(:page_title) { 'Top level page' } - - include_examples 'adds activity event' - end - - context 'the page is in a subsection' do - let(:page_title) { 'Subsection / secondary page' } - - include_examples 'adds activity event' - end - - context 'the feature is disabled' do - before do - stub_feature_flags(wiki_events: false) - end - - it 'does not record the activity' do - expect { service.execute(page) }.not_to change(Event, :count) - end - end - - context 'when the options are bad' do - let(:page_title) { '' } - - it 'does not count an edit event' do - counter = Gitlab::UsageDataCounters::WikiPageCounter - - expect { service.execute page }.not_to change { counter.read(:update) } - end - - it 'does not record the activity' do - expect { service.execute page }.not_to change(Event, :count) - end - - it 'reports the error' do - expect(service.execute(page)).to be_invalid - .and have_attributes(errors: be_present) - end - end - end + it_behaves_like 'WikiPages::UpdateService#execute', :project end |