diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-19 17:16:28 +0300 |
commit | e4384360a16dd9a19d4d2d25d0ef1f2b862ed2a6 (patch) | |
tree | 2fcdfa7dcdb9db8f5208b2562f4b4e803d671243 /spec/initializers | |
parent | ffda4e7bcac36987f936b4ba515995a6698698f0 (diff) |
Add latest changes from gitlab-org/gitlab@16-2-stable-eev16.2.0-rc42
Diffstat (limited to 'spec/initializers')
-rw-r--r-- | spec/initializers/00_rails_disable_joins_spec.rb | 288 | ||||
-rw-r--r-- | spec/initializers/100_patch_omniauth_saml_spec.rb | 9 | ||||
-rw-r--r-- | spec/initializers/action_dispatch_journey_router_spec.rb | 36 | ||||
-rw-r--r-- | spec/initializers/google_api_client_spec.rb | 2 | ||||
-rw-r--r-- | spec/initializers/grpc_patch_spec.rb | 31 | ||||
-rw-r--r-- | spec/initializers/secret_token_spec.rb | 4 |
6 files changed, 79 insertions, 291 deletions
diff --git a/spec/initializers/00_rails_disable_joins_spec.rb b/spec/initializers/00_rails_disable_joins_spec.rb deleted file mode 100644 index 3b390f1ef17..00000000000 --- a/spec/initializers/00_rails_disable_joins_spec.rb +++ /dev/null @@ -1,288 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'DisableJoins' do - let(:primary_model) do - Class.new(ApplicationRecord) do - self.table_name = '_test_primary_records' - - def self.name - 'TestPrimary' - end - end - end - - let(:bridge_model) do - Class.new(ApplicationRecord) do - self.table_name = '_test_bridge_records' - - def self.name - 'TestBridge' - end - end - end - - let(:secondary_model) do - Class.new(ApplicationRecord) do - self.table_name = '_test_secondary_records' - - def self.name - 'TestSecondary' - end - end - end - - context 'passing disable_joins as an association option' do - context 'when the association is a bare has_one' do - it 'disallows the disable_joins option' do - expect do - primary_model.has_one :test_bridge, disable_joins: true - end.to raise_error(ArgumentError, /Unknown key: :disable_joins/) - end - end - - context 'when the association is a belongs_to' do - it 'disallows the disable_joins option' do - expect do - bridge_model.belongs_to :test_secondary, disable_joins: true - end.to raise_error(ArgumentError, /Unknown key: :disable_joins/) - end - end - - context 'when the association is has_one :through' do - it 'allows the disable_joins option' do - primary_model.has_one :test_bridge - bridge_model.belongs_to :test_secondary - - expect do - primary_model.has_one :test_secondary, through: :test_bridge, disable_joins: true - end.not_to raise_error - end - end - - context 'when the association is a bare has_many' do - it 'disallows the disable_joins option' do - expect do - primary_model.has_many :test_bridges, disable_joins: true - end.to raise_error(ArgumentError, /Unknown key: :disable_joins/) - end - end - - context 'when the association is a has_many :through' do - it 'allows the disable_joins option' do - primary_model.has_many :test_bridges - bridge_model.belongs_to :test_secondary - - expect do - primary_model.has_many :test_secondaries, through: :test_bridges, disable_joins: true - end.not_to raise_error - end - end - end - - context 'querying has_one :through when disable_joins is set' do - before do - create_tables(<<~SQL) - CREATE TABLE _test_primary_records ( - id serial NOT NULL PRIMARY KEY); - - CREATE TABLE _test_bridge_records ( - id serial NOT NULL PRIMARY KEY, - primary_record_id int NOT NULL, - secondary_record_id int NOT NULL); - - CREATE TABLE _test_secondary_records ( - id serial NOT NULL PRIMARY KEY); - SQL - - primary_model.has_one :test_bridge, anonymous_class: bridge_model, foreign_key: :primary_record_id - bridge_model.belongs_to :test_secondary, anonymous_class: secondary_model, foreign_key: :secondary_record_id - primary_model.has_one :test_secondary, - through: :test_bridge, anonymous_class: secondary_model, disable_joins: -> { joins_disabled_flag } - - primary_record = primary_model.create! - secondary_record = secondary_model.create! - bridge_model.create!(primary_record_id: primary_record.id, secondary_record_id: secondary_record.id) - end - - context 'when disable_joins evaluates to true' do - let(:joins_disabled_flag) { true } - - it 'executes separate queries' do - primary_record = primary_model.first - - query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondary }.count - - expect(query_count).to eq(2) - end - end - - context 'when disable_joins evalutes to false' do - let(:joins_disabled_flag) { false } - - it 'executes a single query' do - primary_record = primary_model.first - - query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondary }.count - - expect(query_count).to eq(1) - end - end - end - - context 'querying has_many :through when disable_joins is set' do - before do - create_tables(<<~SQL) - CREATE TABLE _test_primary_records ( - id serial NOT NULL PRIMARY KEY); - - CREATE TABLE _test_bridge_records ( - id serial NOT NULL PRIMARY KEY, - primary_record_id int NOT NULL); - - CREATE TABLE _test_secondary_records ( - id serial NOT NULL PRIMARY KEY, - bridge_record_id int NOT NULL); - SQL - - primary_model.has_many :test_bridges, anonymous_class: bridge_model, foreign_key: :primary_record_id - bridge_model.has_many :test_secondaries, anonymous_class: secondary_model, foreign_key: :bridge_record_id - primary_model.has_many :test_secondaries, through: :test_bridges, anonymous_class: secondary_model, - disable_joins: -> { disabled_join_flag } - - primary_record = primary_model.create! - bridge_record = bridge_model.create!(primary_record_id: primary_record.id) - secondary_model.create!(bridge_record_id: bridge_record.id) - end - - context 'when disable_joins evaluates to true' do - let(:disabled_join_flag) { true } - - it 'executes separate queries' do - primary_record = primary_model.first - - query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondaries.first }.count - - expect(query_count).to eq(2) - end - end - - context 'when disable_joins evalutes to false' do - let(:disabled_join_flag) { false } - - it 'executes a single query' do - primary_record = primary_model.first - - query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondaries.first }.count - - expect(query_count).to eq(1) - end - end - end - - context 'querying STI relationships' do - let(:child_bridge_model) do - Class.new(bridge_model) do - def self.name - 'ChildBridge' - end - end - end - - let(:child_secondary_model) do - Class.new(secondary_model) do - def self.name - 'ChildSecondary' - end - end - end - - before do - create_tables(<<~SQL) - CREATE TABLE _test_primary_records ( - id serial NOT NULL PRIMARY KEY); - - CREATE TABLE _test_bridge_records ( - id serial NOT NULL PRIMARY KEY, - primary_record_id int NOT NULL, - type text); - - CREATE TABLE _test_secondary_records ( - id serial NOT NULL PRIMARY KEY, - bridge_record_id int NOT NULL, - type text); - SQL - - primary_model.has_many :child_bridges, anonymous_class: child_bridge_model, foreign_key: :primary_record_id - child_bridge_model.has_one :child_secondary, anonymous_class: child_secondary_model, foreign_key: :bridge_record_id - primary_model.has_many :child_secondaries, through: :child_bridges, anonymous_class: child_secondary_model, disable_joins: true - - primary_record = primary_model.create! - parent_bridge_record = bridge_model.create!(primary_record_id: primary_record.id) - child_bridge_record = child_bridge_model.create!(primary_record_id: primary_record.id) - - secondary_model.create!(bridge_record_id: child_bridge_record.id) - child_secondary_model.create!(bridge_record_id: parent_bridge_record.id) - child_secondary_model.create!(bridge_record_id: child_bridge_record.id) - end - - it 'filters correctly by the STI type across multiple queries' do - primary_record = primary_model.first - - query_recorder = ActiveRecord::QueryRecorder.new do - expect(primary_record.child_secondaries.count).to eq(1) - end - - expect(query_recorder.count).to eq(2) - end - end - - context 'querying polymorphic relationships' do - before do - create_tables(<<~SQL) - CREATE TABLE _test_primary_records ( - id serial NOT NULL PRIMARY KEY); - - CREATE TABLE _test_bridge_records ( - id serial NOT NULL PRIMARY KEY, - primaryable_id int NOT NULL, - primaryable_type text NOT NULL); - - CREATE TABLE _test_secondary_records ( - id serial NOT NULL PRIMARY KEY, - bridgeable_id int NOT NULL, - bridgeable_type text NOT NULL); - SQL - - primary_model.has_many :test_bridges, anonymous_class: bridge_model, foreign_key: :primaryable_id, as: :primaryable - bridge_model.has_one :test_secondaries, anonymous_class: secondary_model, foreign_key: :bridgeable_id, as: :bridgeable - primary_model.has_many :test_secondaries, through: :test_bridges, anonymous_class: secondary_model, disable_joins: true - - primary_record = primary_model.create! - primary_bridge_record = bridge_model.create!(primaryable_id: primary_record.id, primaryable_type: 'TestPrimary') - nonprimary_bridge_record = bridge_model.create!(primaryable_id: primary_record.id, primaryable_type: 'NonPrimary') - - secondary_model.create!(bridgeable_id: primary_bridge_record.id, bridgeable_type: 'TestBridge') - secondary_model.create!(bridgeable_id: nonprimary_bridge_record.id, bridgeable_type: 'TestBridge') - secondary_model.create!(bridgeable_id: primary_bridge_record.id, bridgeable_type: 'NonBridge') - end - - it 'filters correctly by the polymorphic type across multiple queries' do - primary_record = primary_model.first - - query_recorder = ActiveRecord::QueryRecorder.new do - expect(primary_record.test_secondaries.count).to eq(1) - end - - expect(query_recorder.count).to eq(2) - end - end - - def create_tables(table_sql) - ApplicationRecord.connection.execute(table_sql) - - bridge_model.reset_column_information - secondary_model.reset_column_information - end -end diff --git a/spec/initializers/100_patch_omniauth_saml_spec.rb b/spec/initializers/100_patch_omniauth_saml_spec.rb index de556cfa1e5..886f350ca88 100644 --- a/spec/initializers/100_patch_omniauth_saml_spec.rb +++ b/spec/initializers/100_patch_omniauth_saml_spec.rb @@ -6,6 +6,15 @@ RSpec.describe 'OmniAuth::Strategies::SAML', type: :strategy do let(:idp_sso_target_url) { 'https://login.example.com/idp' } let(:strategy) { [OmniAuth::Strategies::SAML, { idp_sso_target_url: idp_sso_target_url }] } + before do + mock_session = {} + + allow(mock_session).to receive(:enabled?).and_return(true) + allow(mock_session).to receive(:loaded?).and_return(true) + + env('rack.session', mock_session) + end + describe 'POST /users/auth/saml' do it 'redirects to the provider login page', :aggregate_failures do post '/users/auth/saml' diff --git a/spec/initializers/action_dispatch_journey_router_spec.rb b/spec/initializers/action_dispatch_journey_router_spec.rb new file mode 100644 index 00000000000..641a8c6d11f --- /dev/null +++ b/spec/initializers/action_dispatch_journey_router_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# Adds a missing test to provide full coverage for the patch +RSpec.describe 'ActionDispatch::Journey::Router Patch', feature_category: :database do + before do + load Rails.root.join('config/initializers/action_dispatch_journey_router.rb') + end + + describe '#find_routes' do + context 'when a route has additional constrains' do + it 'does not raise an error' do + stub_const('PagesController', Class.new(ApplicationController)) + + set = ActionDispatch::Routing::RouteSet.new + + set.draw do + get "*namespace_id/:project_id/bar", + to: "pages#show", + constraints: { + namespace_id: %r{(?!api/)[a-zA-Z0-9_\\]+}, + project_id: /[a-zA-Z0-9]+/ + } + + get "/api/foo/bar", to: "pages#index" + end + + params = set.recognize_path("/api/foo/bar", method: :get) + + expect(params[:controller]).to eq('pages') + expect(params[:action]).to eq('index') + end + end + end +end diff --git a/spec/initializers/google_api_client_spec.rb b/spec/initializers/google_api_client_spec.rb index b3c4ac5e23b..cd3e3cf0328 100644 --- a/spec/initializers/google_api_client_spec.rb +++ b/spec/initializers/google_api_client_spec.rb @@ -8,7 +8,7 @@ require 'google/apis/core/base_service' RSpec.describe Google::Apis::Core::HttpCommand do # rubocop:disable RSpec/FilePath context('with a successful response') do let(:client) { Google::Apis::Core::BaseService.new('', '').client } - let(:command) { Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals') } + let(:command) { described_class.new(:get, 'https://www.googleapis.com/zoo/animals') } before do stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(body: %(Hello world)) diff --git a/spec/initializers/grpc_patch_spec.rb b/spec/initializers/grpc_patch_spec.rb new file mode 100644 index 00000000000..90d1269e4c1 --- /dev/null +++ b/spec/initializers/grpc_patch_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'GRPC monkey patch', feature_category: :shared do + let(:server) { GRPC::RpcServer.new } + let(:stub) do + Class.new(Gitaly::CommitService::Service) do + def find_all_commits(_request, _call) + sleep 1 + + nil + end + end + end + + it 'raises DeadlineExceeded on a late server streaming response' do + server_port = server.add_http2_port('0.0.0.0:0', :this_port_is_insecure) + host = "localhost:#{server_port}" + server.handle(stub) + thr = Thread.new { server.run } + + stub = Gitaly::CommitService::Stub.new(host, :this_channel_is_insecure) + request = Gitaly::FindAllCommitsRequest.new + response = stub.find_all_commits(request, deadline: Time.now + 0.1) + expect { response.to_a }.to raise_error(GRPC::DeadlineExceeded) + + server.stop + thr.join + end +end diff --git a/spec/initializers/secret_token_spec.rb b/spec/initializers/secret_token_spec.rb index 2c396a18361..5c39bee16b2 100644 --- a/spec/initializers/secret_token_spec.rb +++ b/spec/initializers/secret_token_spec.rb @@ -7,8 +7,8 @@ RSpec.describe 'create_tokens' do include StubENV let(:secrets) { ActiveSupport::OrderedOptions.new } - let(:hex_key) { /\h{128}/.freeze } - let(:rsa_key) { /\A-----BEGIN RSA PRIVATE KEY-----\n.+\n-----END RSA PRIVATE KEY-----\n\Z/m.freeze } + let(:hex_key) { /\h{128}/ } + let(:rsa_key) { /\A-----BEGIN RSA PRIVATE KEY-----\n.+\n-----END RSA PRIVATE KEY-----\n\Z/m } before do allow(Rails).to receive_message_chain(:application, :secrets).and_return(secrets) |