diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-20 13:43:29 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-20 13:43:29 +0300 |
commit | 3b1af5cc7ed2666ff18b718ce5d30fa5a2756674 (patch) | |
tree | 3bc4a40e0ee51ec27eabf917c537033c0c5b14d4 /spec/initializers | |
parent | 9bba14be3f2c211bf79e15769cd9b77bc73a13bc (diff) |
Add latest changes from gitlab-org/gitlab@16-1-stable-eev16.1.0-rc42
Diffstat (limited to 'spec/initializers')
-rw-r--r-- | spec/initializers/00_deprecations_spec.rb | 14 | ||||
-rw-r--r-- | spec/initializers/active_record_relation_union_reset_spec.rb | 134 | ||||
-rw-r--r-- | spec/initializers/carrierwave_performance_patch_spec.rb | 87 | ||||
-rw-r--r-- | spec/initializers/carrierwave_s3_encryption_headers_patch_spec.rb (renamed from spec/initializers/carrierwave_patch_spec.rb) | 2 | ||||
-rw-r--r-- | spec/initializers/mail_starttls_patch_spec.rb | 2 | ||||
-rw-r--r-- | spec/initializers/net_http_patch_spec.rb | 2 | ||||
-rw-r--r-- | spec/initializers/net_http_response_patch_spec.rb | 2 | ||||
-rw-r--r-- | spec/initializers/safe_session_store_patch_spec.rb | 2 |
8 files changed, 236 insertions, 9 deletions
diff --git a/spec/initializers/00_deprecations_spec.rb b/spec/initializers/00_deprecations_spec.rb index a12d079082b..ee415861fe8 100644 --- a/spec/initializers/00_deprecations_spec.rb +++ b/spec/initializers/00_deprecations_spec.rb @@ -120,21 +120,27 @@ RSpec.describe '00_deprecations', feature_category: :shared do subject { ActiveSupport::Deprecation.warn('This is disallowed warning 1.') } - it 'raises ActiveSupport::DeprecationException' do - expect { subject }.to raise_error(ActiveSupport::DeprecationException) + it 'raises Exception and warns on stderr' do + expect { subject } + .to raise_error(Exception) + .and output(match(/^DEPRECATION WARNING: This is disallowed warning 1\./)).to_stderr end context 'when in production environment' do let(:rails_env) { 'production' } - it 'does not raise ActiveSupport::DeprecationException' do + it_behaves_like 'does not log to stderr' + + it 'does not raise' do expect { subject }.not_to raise_error end context 'when GITLAB_LOG_DEPRECATIONS is set' do let(:gitlab_log_deprecations) { '1' } - it 'does not raise ActiveSupport::DeprecationException' do + it_behaves_like 'does not log to stderr' + + it 'does not raise' do expect { subject }.not_to raise_error end end diff --git a/spec/initializers/active_record_relation_union_reset_spec.rb b/spec/initializers/active_record_relation_union_reset_spec.rb new file mode 100644 index 00000000000..013dfa1b49b --- /dev/null +++ b/spec/initializers/active_record_relation_union_reset_spec.rb @@ -0,0 +1,134 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# rubocop:disable Database/MultipleDatabases +RSpec.describe ActiveRecordRelationUnionReset, :delete, feature_category: :shared do + let(:test_unioned_model) do + Class.new(ActiveRecord::Base) do + include FromUnion + + self.table_name = '_test_unioned_model' + + def self.name + 'TestUnion' + end + end + end + + before(:context) do + ActiveRecord::Base.connection.execute(<<~SQL) + CREATE TABLE _test_unioned_model ( + id serial NOT NULL PRIMARY KEY, + created_at timestamptz NOT NULL + ); + SQL + end + + after(:context) do + ActiveRecord::Base.connection.execute(<<~SQL) + DROP TABLE _test_unioned_model + SQL + end + + context 'with mismatched columns due to schema cache' do + def load_query + scopes = [ + test_unioned_model.select('*'), + test_unioned_model.select(test_unioned_model.column_names.join(',')) + ] + + test_unioned_model.from_union(scopes).load + end + + before do + load_query + + ActiveRecord::Base.connection.execute(<<~SQL) + ALTER TABLE _test_unioned_model ADD COLUMN _test_new_column int; + SQL + end + + after do + ActiveRecord::Base.connection.execute(<<~SQL) + ALTER TABLE _test_unioned_model DROP COLUMN _test_new_column; + SQL + + test_unioned_model.reset_column_information + end + + it 'resets column information when encountering an UNION error' do + expect do + load_query + end.to raise_error(ActiveRecord::StatementInvalid, /must have the same number of columns/) + .and change { test_unioned_model.column_names }.from(%w[id created_at]).to(%w[id created_at _test_new_column]) + + # Subsequent query load from new schema cache, so no more error + expect do + load_query + end.not_to raise_error + end + + it 'logs when column is reset' do + expect(Gitlab::ErrorTracking::Logger).to receive(:error) + .with(hash_including("extra.reset_model_name" => "TestUnion")) + .and_call_original + + expect do + load_query + end.to raise_error(ActiveRecord::StatementInvalid, /must have the same number of columns/) + end + + context 'when reset_column_information_on_statement_invalid FF is disabled' do + before do + stub_feature_flags(reset_column_information_on_statement_invalid: false) + end + + it 'does not reset column information' do + expect do + load_query + end.to raise_error(ActiveRecord::StatementInvalid, /must have the same number of columns/) + .and not_change { test_unioned_model.column_names } + end + end + end + + context 'with mismatched columns due to coding error' do + def load_mismatched_query + scopes = [ + test_unioned_model.select("id"), + test_unioned_model.select("id, created_at") + ] + + test_unioned_model.from_union(scopes).load + end + + it 'limits reset_column_information calls' do + expect(test_unioned_model).to receive(:reset_column_information).and_call_original + + expect do + load_mismatched_query + end.to raise_error(ActiveRecord::StatementInvalid, /must have the same number of columns/) + + expect(test_unioned_model).not_to receive(:reset_column_information) + + expect do + load_mismatched_query + end.to raise_error(ActiveRecord::StatementInvalid, /must have the same number of columns/) + end + + it 'does reset_column_information after some time has passed' do + expect do + load_mismatched_query + end.to raise_error(ActiveRecord::StatementInvalid, /must have the same number of columns/) + + travel_to(described_class::MAX_RESET_PERIOD.from_now + 1.minute) + expect(test_unioned_model).to receive(:reset_column_information).and_call_original + + expect do + load_mismatched_query + end.to raise_error(ActiveRecord::StatementInvalid, /must have the same number of columns/) + end + end +end +# rubocop:enable Database/MultipleDatabases diff --git a/spec/initializers/carrierwave_performance_patch_spec.rb b/spec/initializers/carrierwave_performance_patch_spec.rb new file mode 100644 index 00000000000..58adfc15668 --- /dev/null +++ b/spec/initializers/carrierwave_performance_patch_spec.rb @@ -0,0 +1,87 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe "CarrierWave::Uploader::Url", feature_category: :shared do + let(:uploader) { MyCoolUploader.new } + + subject(:url) { uploader.url } + + before do + stub_const("MyCoolUploader", Class.new(CarrierWave::Uploader::Base)) + end + + describe "#url" do + let(:file) { Class.new.new } + + before do + allow(uploader).to receive(:file).and_return(file) + end + + context "when file responds to url" do + it "returns nil when the file.url is empty" do + file.define_singleton_method(:url) { nil } + + expect(url).to be_nil + end + + it "returns the given file url" do + file.define_singleton_method(:url) { "url" } + + expect(url).to eq("url") + end + + it "passes any given options to the file url method" do + file.define_singleton_method(:url) { |x = true| x } + expect(file).to receive(:url).once.and_call_original + + options = { options: true } + expect(uploader.url(options)).to eq(options) + end + end + + context "when file responds to path" do + before do + file.define_singleton_method(:path) { "file/path" } + end + + context "when the asset host is a string" do + it "prefix the path with the asset host" do + expect(uploader).to receive(:asset_host).and_return("host/") + + expect(url).to eq("host/file/path") + end + end + + context "when the asset host responds to call" do + it "prefix the path with the asset host" do + expect(uploader).to receive(:asset_host).and_return(proc { |f| "callable/#{f.class.class}/" }) + + expect(url).to eq("callable/Class/file/path") + end + end + + context "when asset_host is empty" do + context "when base_path is empty" do + it "returns the file path" do + expect(url).to eq("file/path") + end + end + + context "when base_path is not empty" do + it "returns the file path prefixed with the base_path" do + expect(uploader).to receive(:base_path).and_return("base/path/") + + expect(url).to eq("base/path/file/path") + end + end + end + end + + context "when file does not respond to either url nor path" do + it "returns nil" do + expect(url).to eq(nil) + end + end + end +end diff --git a/spec/initializers/carrierwave_patch_spec.rb b/spec/initializers/carrierwave_s3_encryption_headers_patch_spec.rb index 0910342f10f..c8a41847d62 100644 --- a/spec/initializers/carrierwave_patch_spec.rb +++ b/spec/initializers/carrierwave_s3_encryption_headers_patch_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'CarrierWave::Storage::Fog::File' do +RSpec.describe 'CarrierWave::Storage::Fog::File', feature_category: :shared do let(:uploader_class) { Class.new(CarrierWave::Uploader::Base) } let(:uploader) { uploader_class.new } let(:storage) { CarrierWave::Storage::Fog.new(uploader) } diff --git a/spec/initializers/mail_starttls_patch_spec.rb b/spec/initializers/mail_starttls_patch_spec.rb index 126ffb98f0e..99c8edddd12 100644 --- a/spec/initializers/mail_starttls_patch_spec.rb +++ b/spec/initializers/mail_starttls_patch_spec.rb @@ -6,7 +6,7 @@ require 'spec_helper' require 'mail' require_relative '../../config/initializers/mail_starttls_patch' -RSpec.describe 'Mail STARTTLS patch', feature_category: :integrations do +RSpec.describe 'Mail STARTTLS patch', feature_category: :shared do using RSpec::Parameterized::TableSyntax let(:message) do diff --git a/spec/initializers/net_http_patch_spec.rb b/spec/initializers/net_http_patch_spec.rb index 82f896e1fa7..b9f5299b58c 100644 --- a/spec/initializers/net_http_patch_spec.rb +++ b/spec/initializers/net_http_patch_spec.rb @@ -11,7 +11,7 @@ RSpec.describe 'Net::HTTP patch proxy user and password encoding' do before do # This file can be removed once Ruby 3.0 is no longer supported: # https://gitlab.com/gitlab-org/gitlab/-/issues/396223 - skip if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(3.1) + skip if Gem::Version.new(Net::HTTP::VERSION) >= Gem::Version.new('0.2.0') end describe '#proxy_user' do diff --git a/spec/initializers/net_http_response_patch_spec.rb b/spec/initializers/net_http_response_patch_spec.rb index eee0747a02a..cd261d7b997 100644 --- a/spec/initializers/net_http_response_patch_spec.rb +++ b/spec/initializers/net_http_response_patch_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'Net::HTTPResponse patch header read timeout', feature_category: :integrations do +RSpec.describe 'Net::HTTPResponse patch header read timeout', feature_category: :shared do describe '.each_response_header' do let(:server_response) do <<~HTTP diff --git a/spec/initializers/safe_session_store_patch_spec.rb b/spec/initializers/safe_session_store_patch_spec.rb index b48aae02e9a..abf86288364 100644 --- a/spec/initializers/safe_session_store_patch_spec.rb +++ b/spec/initializers/safe_session_store_patch_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'safe_sesion_store_patch', feature_category: :integrations do +RSpec.describe 'safe_session_store_patch', feature_category: :shared do shared_examples 'safe session store' do it 'allows storing a String' do session[:good_data] = 'hello world' |