diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 14:33:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 14:33:21 +0300 |
commit | 7021455bd1ed7b125c55eb1b33c5a01f2bc55ee0 (patch) | |
tree | 5bdc2229f5198d516781f8d24eace62fc7e589e9 /spec/support/shared_examples/lib | |
parent | 185b095e93520f96e9cfc31d9c3e69b498cdab7c (diff) |
Add latest changes from gitlab-org/gitlab@15-6-stable-eev15.6.0-rc42
Diffstat (limited to 'spec/support/shared_examples/lib')
8 files changed, 203 insertions, 58 deletions
diff --git a/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb b/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb index 2e00abe2f8e..6cdd7954b5f 100644 --- a/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb +++ b/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb @@ -129,6 +129,7 @@ RSpec.shared_examples_for 'collection cache helper' do before do allow(::Gitlab::Metrics::WebTransaction).to receive(:current).and_return(transaction) allow(transaction).to receive(:increment) + allow(Gitlab::ApplicationContext).to receive(:current_context_attribute).with(any_args).and_call_original allow(Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return(caller_id) end diff --git a/spec/support/shared_examples/lib/email/email_shared_examples.rb b/spec/support/shared_examples/lib/email/email_shared_examples.rb new file mode 100644 index 00000000000..26655a71fec --- /dev/null +++ b/spec/support/shared_examples/lib/email/email_shared_examples.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +# Set the particular setting as a key-value pair +# Setting method is different depending on klass and must be defined in the calling spec +def stub_email_setting(key_value_pairs) + case setting_name + when :incoming_email + stub_incoming_email_setting(key_value_pairs) + when :service_desk_email + stub_service_desk_email_setting(key_value_pairs) + end +end + +RSpec.shared_examples_for 'enabled? method for email' do + using RSpec::Parameterized::TableSyntax + + subject { described_class.enabled? } + + where(:value, :address, :result) do + false | nil | false + false | 'replies+%{key}@example.com' | false + true | nil | false + true | 'replies+%{key}@example.com' | true + end + + with_them do + before do + stub_email_setting(enabled: value) + stub_email_setting(address: address) + end + + it { is_expected.to eq result } + end +end + +RSpec.shared_examples_for 'supports_wildcard? method for email' do + subject { described_class.supports_wildcard? } + + before do + stub_incoming_email_setting(address: value) + end + + context 'when address contains the wildcard placeholder' do + let(:value) { 'replies+%{key}@example.com' } + + it 'confirms that wildcard is supported' do + expect(subject).to be_truthy + end + end + + context "when address doesn't contain the wildcard placeholder" do + let(:value) { 'replies@example.com' } + + it 'returns that wildcard is not supported' do + expect(subject).to be_falsey + end + end + + context 'when address is nil' do + let(:value) { nil } + + it 'returns that wildcard is not supported' do + expect(subject).to be_falsey + end + end +end + +RSpec.shared_examples_for 'unsubscribe_address method for email' do + before do + stub_incoming_email_setting(address: 'replies+%{key}@example.com') + end + + it 'returns the address with interpolated reply key and unsubscribe suffix' do + expect(described_class.unsubscribe_address('key')) + .to eq("replies+key#{Gitlab::Email::Common::UNSUBSCRIBE_SUFFIX}@example.com") + end +end + +RSpec.shared_examples_for 'key_from_fallback_message_id method for email' do + it 'returns reply key' do + expect(described_class.key_from_fallback_message_id('reply-key@localhost')).to eq('key') + end +end + +RSpec.shared_examples_for 'supports_issue_creation? method for email' do + using RSpec::Parameterized::TableSyntax + + subject { described_class.supports_issue_creation? } + + where(:enabled_value, :supports_wildcard_value, :result) do + false | false | false + false | true | false + true | false | false + true | true | true + end + + with_them do + before do + allow(described_class).to receive(:enabled?).and_return(enabled_value) + allow(described_class).to receive(:supports_wildcard?).and_return(supports_wildcard_value) + end + + it { is_expected.to eq result } + end +end + +RSpec.shared_examples_for 'reply_address method for email' do + before do + stub_incoming_email_setting(address: "replies+%{key}@example.com") + end + + it "returns the address with an interpolated reply key" do + expect(described_class.reply_address("key")).to eq("replies+key@example.com") + end +end + +RSpec.shared_examples_for 'scan_fallback_references method for email' do + let(:references) do + '<issue_1@localhost>' \ + ' <reply-59d8df8370b7e95c5a49fbf86aeb2c93@localhost>' \ + ',<exchange@microsoft.com>' + end + + it 'returns reply key' do + expect(described_class.scan_fallback_references(references)) + .to eq(%w[issue_1@localhost + reply-59d8df8370b7e95c5a49fbf86aeb2c93@localhost + exchange@microsoft.com]) + end +end + +RSpec.shared_examples_for 'common email methods' do + it_behaves_like 'enabled? method for email' + it_behaves_like 'supports_wildcard? method for email' + it_behaves_like 'key_from_fallback_message_id method for email' + it_behaves_like 'supports_issue_creation? method for email' + it_behaves_like 'reply_address method for email' + it_behaves_like 'unsubscribe_address method for email' + it_behaves_like 'scan_fallback_references method for email' +end diff --git a/spec/support/shared_examples/lib/gitlab/database/reestablished_connection_stack_shared_examples.rb b/spec/support/shared_examples/lib/gitlab/database/reestablished_connection_stack_shared_examples.rb index d14216ec5ff..22b4f9f583c 100644 --- a/spec/support/shared_examples/lib/gitlab/database/reestablished_connection_stack_shared_examples.rb +++ b/spec/support/shared_examples/lib/gitlab/database/reestablished_connection_stack_shared_examples.rb @@ -3,9 +3,6 @@ RSpec.shared_context 'reconfigures connection stack' do |db_config_name| before do skip_if_multiple_databases_not_setup - - # Due to lib/gitlab/database/load_balancing/configuration.rb:92 requiring RequestStore - # we cannot use stub_feature_flags(force_no_sharing_primary_model: true) Gitlab::Database.database_base_models.each do |_, model_class| allow(model_class.load_balancer.configuration).to receive(:use_dedicated_connection?).and_return(true) end diff --git a/spec/support/shared_examples/lib/gitlab/event_store_shared_examples.rb b/spec/support/shared_examples/lib/gitlab/event_store_shared_examples.rb index db2f2f2d0f0..e97b9ad969f 100644 --- a/spec/support/shared_examples/lib/gitlab/event_store_shared_examples.rb +++ b/spec/support/shared_examples/lib/gitlab/event_store_shared_examples.rb @@ -15,6 +15,16 @@ RSpec.shared_examples 'subscribes to event' do it_behaves_like 'an idempotent worker' end +RSpec.shared_examples 'ignores the published event' do + include AfterNextHelpers + + it 'does not consume the published event', :sidekiq_inline do + expect_next(described_class).not_to receive(:handle_event) + + ::Gitlab::EventStore.publish(event) + end +end + def consume_event(subscriber:, event:) subscriber.new.perform(event.class.name, event.data) end diff --git a/spec/support/shared_examples/lib/gitlab/experimentation_shared_examples.rb b/spec/support/shared_examples/lib/gitlab/experimentation_shared_examples.rb deleted file mode 100644 index fdca326dbea..00000000000 --- a/spec/support/shared_examples/lib/gitlab/experimentation_shared_examples.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -RSpec.shared_examples 'tracks assignment and records the subject' do |experiment, subject_type| - before do - stub_experiments(experiment => true) - end - - it 'tracks the assignment', :experiment do - expect(experiment(experiment)) - .to track(:assignment) - .with_context(subject_type => subject) - .on_next_instance - - action - end - - it 'records the subject' do - expect(Experiment).to receive(:add_subject).with(experiment.to_s, variant: anything, subject: subject) - - action - end -end diff --git a/spec/support/shared_examples/lib/gitlab/gitaly_client_shared_examples.rb b/spec/support/shared_examples/lib/gitlab/gitaly_client_shared_examples.rb new file mode 100644 index 00000000000..f26b9a4a7bd --- /dev/null +++ b/spec/support/shared_examples/lib/gitlab/gitaly_client_shared_examples.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +def raw_repo_without_container(repository) + Gitlab::Git::Repository.new(repository.shard, + "#{repository.disk_path}.git", + repository.repo_type.identifier_for_container(repository.container), + repository.container.full_path) +end + +RSpec.shared_examples 'Gitaly feature flag actors are inferred from repository' do + it 'captures correct actors' do + service.repository_actor = repository + + expect(service.repository_actor.flipper_id).to eql(repository.flipper_id) + + if expected_project.nil? + expect(service.project_actor).to be(nil) + else + expect(service.project_actor.flipper_id).to eql(expected_project.flipper_id) + end + + if expected_group.nil? + expect(service.group_actor).to be(nil) + else + expect(service.group_actor.flipper_id).to eql(expected_group.flipper_id) + end + end + + it 'does not issues SQL queries after the first invocation' do + service.repository_actor = repository + + service.repository_actor + service.project_actor + service.group_actor + + recorder = ActiveRecord::QueryRecorder.new do + 3.times do + service.repository_actor + service.project_actor + service.group_actor + end + end + + expect(recorder.count).to be(0) + end +end diff --git a/spec/support/shared_examples/lib/gitlab/template/template_shared_examples.rb b/spec/support/shared_examples/lib/gitlab/template/template_shared_examples.rb index 4b4a7f4ce9d..a2a4c57d62e 100644 --- a/spec/support/shared_examples/lib/gitlab/template/template_shared_examples.rb +++ b/spec/support/shared_examples/lib/gitlab/template/template_shared_examples.rb @@ -52,7 +52,7 @@ RSpec.shared_examples 'acts as branch pipeline' do |jobs| context 'when branch pipeline' do let(:pipeline_branch) { default_branch } let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_branch) } - let(:pipeline) { service.execute!(:push).payload } + let(:pipeline) { service.execute(:push).payload } it 'includes a job' do expect(pipeline.builds.pluck(:name)).to match_array(jobs) diff --git a/spec/support/shared_examples/lib/sentry/client_shared_examples.rb b/spec/support/shared_examples/lib/sentry/client_shared_examples.rb index 71b32005c55..e0b411e1e2a 100644 --- a/spec/support/shared_examples/lib/sentry/client_shared_examples.rb +++ b/spec/support/shared_examples/lib/sentry/client_shared_examples.rb @@ -78,8 +78,8 @@ end # Expects to following variables: # - subject # - sentry_api_response -# - sentry_url, token - only if enabled_by_default: false -RSpec.shared_examples 'Sentry API response size limit' do |enabled_by_default: false| +# - sentry_url, token +RSpec.shared_examples 'Sentry API response size limit' do let(:invalid_deep_size) { instance_double(Gitlab::Utils::DeepSize, valid?: false) } before do @@ -89,35 +89,8 @@ RSpec.shared_examples 'Sentry API response size limit' do |enabled_by_default: f .and_return(invalid_deep_size) end - if enabled_by_default - it 'raises an exception when response is too large' do - expect { subject }.to raise_error(ErrorTracking::SentryClient::ResponseInvalidSizeError, - 'Sentry API response is too big. Limit is 1 MB.') - end - else - context 'when guarded by feature flag' do - let(:client) do - ErrorTracking::SentryClient.new(sentry_url, token, validate_size_guarded_by_feature_flag: feature_flag) - end - - context 'with feature flag enabled' do - let(:feature_flag) { true } - - it 'raises an exception when response is too large' do - expect { subject }.to raise_error(ErrorTracking::SentryClient::ResponseInvalidSizeError, - 'Sentry API response is too big. Limit is 1 MB.') - end - end - - context 'with feature flag disabled' do - let(:feature_flag) { false } - - it 'does not check the limit and thus not raise' do - expect { subject }.not_to raise_error - - expect(Gitlab::Utils::DeepSize).not_to have_received(:new) - end - end - end + it 'raises an exception when response is too large' do + expect { subject }.to raise_error(ErrorTracking::SentryClient::ResponseInvalidSizeError, + 'Sentry API response is too big. Limit is 1 MB.') end end |