diff options
Diffstat (limited to 'spec/support/shared_examples/services')
8 files changed, 90 insertions, 286 deletions
diff --git a/spec/support/shared_examples/services/incident_shared_examples.rb b/spec/support/shared_examples/services/incident_shared_examples.rb index db2b448f567..94467ad53fa 100644 --- a/spec/support/shared_examples/services/incident_shared_examples.rb +++ b/spec/support/shared_examples/services/incident_shared_examples.rb @@ -40,7 +40,7 @@ end RSpec.shared_examples 'incident management label service' do let_it_be(:project) { create(:project, :private) } - let_it_be(:user) { User.alert_bot } + let_it_be(:user) { Users::Internal.alert_bot } let(:service) { described_class.new(project, user) } subject(:execute) { service.execute } diff --git a/spec/support/shared_examples/services/issuable/issuable_update_service_shared_examples.rb b/spec/support/shared_examples/services/issuable/issuable_update_service_shared_examples.rb index 3f95d6060ea..9624f7a4450 100644 --- a/spec/support/shared_examples/services/issuable/issuable_update_service_shared_examples.rb +++ b/spec/support/shared_examples/services/issuable/issuable_update_service_shared_examples.rb @@ -145,6 +145,77 @@ RSpec.shared_examples 'updating issuable labels' do end end +RSpec.shared_examples 'updating merged MR with locked labels' do + context 'when add_label_ids and label_ids are passed' do + let(:params) { { label_ids: [label_a.id], add_label_ids: [label_c.id] } } + + it 'replaces unlocked labels with the ones in label_ids and adds those in add_label_ids' do + issuable.update!(labels: [label_b, label_unlocked]) + update_issuable(params) + + expect(issuable.label_ids).to contain_exactly(label_a.id, label_b.id, label_c.id) + end + end + + context 'when remove_label_ids and label_ids are passed' do + let(:params) { { label_ids: [label_a.id, label_b.id, label_c.id], remove_label_ids: [label_a.id] } } + + it 'replaces unlocked labels with the ones in label_ids and does not remove locked label in remove_label_ids' do + issuable.update!(labels: [label_a, label_c, label_unlocked]) + update_issuable(params) + + expect(issuable.label_ids).to contain_exactly(label_a.id, label_b.id, label_c.id) + end + end + + context 'when add_label_ids and remove_label_ids are passed' do + let(:params) { { add_label_ids: [label_c.id], remove_label_ids: [label_a.id, label_unlocked.id] } } + + before do + issuable.update!(labels: [label_a, label_unlocked]) + update_issuable(params) + end + + it 'adds the passed labels' do + expect(issuable.label_ids).to include(label_c.id) + end + + it 'removes the passed unlocked labels' do + expect(issuable.label_ids).to include(label_a.id) + expect(issuable.label_ids).not_to include(label_unlocked.id) + end + end + + context 'when same id is passed as add_label_ids and remove_label_ids' do + let(:params) { { add_label_ids: [label_a.id], remove_label_ids: [label_a.id] } } + + context 'for a label assigned to an issue' do + it 'does not remove the label' do + issuable.update!(labels: [label_a]) + update_issuable(params) + + expect(issuable.label_ids).to contain_exactly(label_a.id) + end + end + + context 'for a label not assigned to an issue' do + it 'does not add the label' do + expect(issuable.label_ids).to be_empty + end + end + end + + context 'when duplicate label titles are given' do + let(:params) { { labels: [label_c.title, label_c.title] } } + + it 'assigns the label once' do + update_issuable(params) + + expect(issuable.labels).to contain_exactly(label_c) + end + end +end + RSpec.shared_examples 'keeps issuable labels sorted after update' do before do update_issuable(label_ids: [label_b.id]) diff --git a/spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb b/spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb index 1532e870dcc..b955b71a6bb 100644 --- a/spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb +++ b/spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb @@ -1,13 +1,7 @@ # frozen_string_literal: true -RSpec.shared_examples 'a destroyable issuable link' do |required_role: :reporter| +RSpec.shared_examples 'a destroyable issuable link' do context 'when successfully removes an issuable link' do - before do - [issuable_link.target, issuable_link.source].each do |issuable| - issuable.resource_parent.try(:"add_#{required_role}", user) - end - end - it 'removes related issue' do expect { subject }.to change { issuable_link.class.count }.by(-1) end @@ -28,6 +22,9 @@ RSpec.shared_examples 'a destroyable issuable link' do |required_role: :reporter end context 'when failing to remove an issuable link' do + let_it_be(:non_member) { create(:user) } + let(:user) { non_member } + it 'does not remove relation' do expect { subject }.not_to change { issuable_link.class.count }.from(1) end diff --git a/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb b/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb deleted file mode 100644 index 9b2e038a331..00000000000 --- a/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb +++ /dev/null @@ -1,193 +0,0 @@ -# frozen_string_literal: true - -RSpec.shared_examples 'misconfigured dashboard service response' do |status_code, message = nil| - it 'returns an appropriate message and status code', :aggregate_failures do - result = service_call - - expect(result.keys).to contain_exactly(:message, :http_status, :status) - expect(result[:status]).to eq(:error) - expect(result[:http_status]).to eq(status_code) - expect(result[:message]).to eq(message) if message - end -end - -RSpec.shared_examples 'valid dashboard service response for schema' do - it 'returns a json representation of the dashboard' do - result = service_call - - expect(result.keys).to contain_exactly(:dashboard, :status) - expect(result[:status]).to eq(:success) - - schema_path = Rails.root.join('spec/fixtures', dashboard_schema) - validator = JSONSchemer.schema(schema_path) - expect(validator.valid?(result[:dashboard].with_indifferent_access)).to be true - end -end - -RSpec.shared_examples 'valid dashboard service response' do - let(:dashboard_schema) { 'lib/gitlab/metrics/dashboard/schemas/dashboard.json' } - - it_behaves_like 'valid dashboard service response for schema' -end - -RSpec.shared_examples 'caches the unprocessed dashboard for subsequent calls' do - specify do - expect_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| - expect(loader).to receive(:load_raw!).once.and_call_original - end - - described_class.new(*service_params).get_dashboard - described_class.new(*service_params).get_dashboard - end -end - -# This spec is applicable for predefined/out-of-the-box dashboard services. -RSpec.shared_examples 'refreshes cache when dashboard_version is changed' do - specify do - allow_next_instance_of(described_class) do |service| - allow(service).to receive(:dashboard_version).and_return('1', '2') - end - - expect_file_read(Rails.root.join(described_class::DASHBOARD_PATH)).twice.and_call_original - - service = described_class.new(*service_params) - - service.get_dashboard - service.get_dashboard - end -end - -# This spec is applicable for predefined/out-of-the-box dashboard services. -# This shared_example requires the following variables to be defined: -# dashboard_path: Relative path to the dashboard, ex: 'config/prometheus/common_metrics.yml' -# dashboard_version: The version string used in the cache_key. -RSpec.shared_examples 'dashboard_version contains SHA256 hash of dashboard file content' do - specify do - dashboard = File.read(Rails.root.join(dashboard_path)) - expect(dashboard_version).to eq(Digest::SHA256.hexdigest(dashboard)) - end -end - -RSpec.shared_examples 'valid embedded dashboard service response' do - let(:dashboard_schema) { 'lib/gitlab/metrics/dashboard/schemas/embedded_dashboard.json' } - - it_behaves_like 'valid dashboard service response for schema' -end - -RSpec.shared_examples 'raises error for users with insufficient permissions' do - context 'when the user does not have sufficient access' do - let(:user) { build(:user) } - - it_behaves_like 'misconfigured dashboard service response', :unauthorized - end - - context 'when the user is anonymous' do - let(:user) { nil } - - it_behaves_like 'misconfigured dashboard service response', :unauthorized - end -end - -RSpec.shared_examples 'valid dashboard cloning process' do |dashboard_template, sequence| - context "dashboard template: #{dashboard_template}" do - let(:dashboard) { dashboard_template } - let(:dashboard_attrs) do - { - commit_message: commit_message, - branch_name: branch, - start_branch: project.default_branch, - encoding: 'text', - file_path: ".gitlab/dashboards/#{file_name}", - file_content: file_content_hash.to_yaml - } - end - - it 'delegates commit creation to Files::CreateService', :aggregate_failures do - service_instance = instance_double(::Files::CreateService) - allow(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).and_return(double(process: file_content_hash)) - expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance) - expect(service_instance).to receive(:execute).and_return(status: :success) - - service_call - end - - context 'user has defined custom metrics' do - it 'uses external service to includes them into new file content', :aggregate_failures do - service_instance = double(::Gitlab::Metrics::Dashboard::Processor) - expect(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).with(project, file_content_hash, sequence, {}).and_return(service_instance) - expect(service_instance).to receive(:process).and_return(file_content_hash) - expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(double(execute: { status: :success })) - - service_call - end - end - end -end - -RSpec.shared_examples 'misconfigured dashboard service response with stepable' do |status_code, message = nil| - it 'returns an appropriate message and status code', :aggregate_failures do - result = service_call - - expect(result.keys).to contain_exactly(:message, :http_status, :status, :last_step) - expect(result[:status]).to eq(:error) - expect(result[:http_status]).to eq(status_code) - expect(result[:message]).to eq(message) if message - end -end - -RSpec.shared_examples 'updates gitlab_metrics_dashboard_processing_time_ms metric' do - specify :prometheus do - service_call - metric = subject.send(:processing_time_metric) - labels = subject.send(:processing_time_metric_labels) - - expect(metric.get(labels)).to be > 0 - end -end - -RSpec.shared_examples '#raw_dashboard raises error if dashboard loading fails' do - context 'when yaml is too large' do - before do - allow_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| - allow(loader).to receive(:load_raw!) - .and_raise(Gitlab::Config::Loader::Yaml::DataTooLargeError, 'The parsed YAML is too big') - end - end - - it 'raises error' do - expect { subject.raw_dashboard }.to raise_error( - Gitlab::Metrics::Dashboard::Errors::LayoutError, - 'The parsed YAML is too big' - ) - end - end - - context 'when yaml loader returns error' do - before do - allow_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| - allow(loader).to receive(:load_raw!) - .and_raise(Gitlab::Config::Loader::FormatError, 'Invalid configuration format') - end - end - - it 'raises error' do - expect { subject.raw_dashboard }.to raise_error( - Gitlab::Metrics::Dashboard::Errors::LayoutError, - 'Invalid yaml' - ) - end - end - - context 'when yaml is not a hash' do - before do - allow_next_instance_of(::Gitlab::Config::Loader::Yaml) do |loader| - allow(loader).to receive(:load_raw!) - .and_raise(Gitlab::Config::Loader::Yaml::NotHashError, 'Invalid configuration format') - end - end - - it 'returns nil' do - expect(subject.raw_dashboard).to eq({}) - end - end -end diff --git a/spec/support/shared_examples/services/migrate_to_ghost_user_service_shared_examples.rb b/spec/support/shared_examples/services/migrate_to_ghost_user_service_shared_examples.rb index e77d73d1c72..621fb99afe5 100644 --- a/spec/support/shared_examples/services/migrate_to_ghost_user_service_shared_examples.rb +++ b/spec/support/shared_examples/services/migrate_to_ghost_user_service_shared_examples.rb @@ -38,7 +38,7 @@ RSpec.shared_examples "migrating a deleted user's associated records to the ghos migrated_record = record_class.find_by_id(record.id) migrated_fields.each do |field| - expect(migrated_record.public_send(field)).to eq(User.ghost) + expect(migrated_record.public_send(field)).to eq(Users::Internal.ghost) end end @@ -47,7 +47,7 @@ RSpec.shared_examples "migrating a deleted user's associated records to the ghos migrated_record = record_class.find_by_id(record.id) - check_user = always_ghost ? User.ghost : user + check_user = always_ghost ? Users::Internal.ghost : user migrated_fields.each do |field| expect(migrated_record.public_send(field)).to eq(check_user) diff --git a/spec/support/shared_examples/services/pages_size_limit_shared_examples.rb b/spec/support/shared_examples/services/pages_size_limit_shared_examples.rb deleted file mode 100644 index d9e906ebb75..00000000000 --- a/spec/support/shared_examples/services/pages_size_limit_shared_examples.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -RSpec.shared_examples 'pages size limit is' do |size_limit| - context "when size is below the limit" do - before do - allow(metadata).to receive(:total_size).and_return(size_limit - 1.megabyte) - allow(metadata).to receive(:entries).and_return([]) - end - - it 'updates pages correctly' do - subject.execute - - expect(deploy_status.description).not_to be_present - expect(project.pages_metadatum).to be_deployed - end - end - - context "when size is above the limit" do - before do - allow(metadata).to receive(:total_size).and_return(size_limit + 1.megabyte) - allow(metadata).to receive(:entries).and_return([]) - end - - it 'limits the maximum size of gitlab pages' do - subject.execute - - expect(deploy_status.description) - .to match(/artifacts for pages are too large/) - expect(deploy_status).to be_script_failure - end - end -end diff --git a/spec/support/shared_examples/services/protected_branches_shared_examples.rb b/spec/support/shared_examples/services/protected_branches_shared_examples.rb new file mode 100644 index 00000000000..ce607a6b956 --- /dev/null +++ b/spec/support/shared_examples/services/protected_branches_shared_examples.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +RSpec.shared_context 'with scan result policy blocking protected branches' do + before do + create( + :scan_result_policy_read, + :blocking_protected_branches, + project: project) + + stub_licensed_features(security_orchestration_policies: true) + end +end diff --git a/spec/support/shared_examples/services/users/build_service_shared_examples.rb b/spec/support/shared_examples/services/users/build_service_shared_examples.rb index e448f2f874b..45ebc837e6d 100644 --- a/spec/support/shared_examples/services/users/build_service_shared_examples.rb +++ b/spec/support/shared_examples/services/users/build_service_shared_examples.rb @@ -33,57 +33,6 @@ RSpec.shared_examples 'common user build items' do end RSpec.shared_examples_for 'current user not admin build items' do - using RSpec::Parameterized::TableSyntax - - context 'with "user_default_external" application setting' do - where(:user_default_external, :external, :email, :user_default_internal_regex, :result) do - true | nil | 'fl@example.com' | nil | true - true | true | 'fl@example.com' | nil | true - true | false | 'fl@example.com' | nil | true # admin difference - - true | nil | 'fl@example.com' | '' | true - true | true | 'fl@example.com' | '' | true - true | false | 'fl@example.com' | '' | true # admin difference - - true | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - true | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference - true | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - - true | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true - true | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true - true | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true # admin difference - - false | nil | 'fl@example.com' | nil | false - false | true | 'fl@example.com' | nil | false # admin difference - false | false | 'fl@example.com' | nil | false - - false | nil | 'fl@example.com' | '' | false - false | true | 'fl@example.com' | '' | false # admin difference - false | false | 'fl@example.com' | '' | false - - false | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - false | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference - false | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - - false | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false - false | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference - false | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false - end - - with_them do - before do - stub_application_setting(user_default_external: user_default_external) - stub_application_setting(user_default_internal_regex: user_default_internal_regex) - - params.merge!({ external: external, email: email }.compact) - end - - it 'sets the value of Gitlab::CurrentSettings.user_default_external' do - expect(user.external).to eq(result) - end - end - end - context 'when "email_confirmation_setting" application setting is set to `hard`' do before do stub_application_setting_enum('email_confirmation_setting', 'hard') |