diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 08:43:02 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 08:43:02 +0000 |
commit | d9ab72d6080f594d0b3cae15f14b3ef2c6c638cb (patch) | |
tree | 2341ef426af70ad1e289c38036737e04b0aa5007 /spec/lib/api | |
parent | d6e514dd13db8947884cd58fe2a9c2a063400a9b (diff) | |
download | gitlab-ce-d9ab72d6080f594d0b3cae15f14b3ef2c6c638cb.tar.gz |
Add latest changes from gitlab-org/gitlab@14-4-stable-eev14.4.0-rc42
Diffstat (limited to 'spec/lib/api')
-rw-r--r-- | spec/lib/api/base_spec.rb | 92 | ||||
-rw-r--r-- | spec/lib/api/ci/helpers/runner_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/api/entities/clusters/agent_authorization_spec.rb | 35 | ||||
-rw-r--r-- | spec/lib/api/entities/user_spec.rb | 13 | ||||
-rw-r--r-- | spec/lib/api/every_api_endpoint_spec.rb | 4 | ||||
-rw-r--r-- | spec/lib/api/helpers_spec.rb | 12 | ||||
-rw-r--r-- | spec/lib/api/validations/validators/project_portable_spec.rb | 33 |
7 files changed, 177 insertions, 28 deletions
diff --git a/spec/lib/api/base_spec.rb b/spec/lib/api/base_spec.rb new file mode 100644 index 00000000000..8513b800273 --- /dev/null +++ b/spec/lib/api/base_spec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# rubocop:disable Rails/HttpPositionalArguments +RSpec.describe ::API::Base do + let(:app_hello) do + route = double(:route, request_method: 'GET', path: '/:version/test/hello') + double(:endpoint, route: route, options: { for: api_handler, path: ["hello"] }, namespace: '/test') + end + + let(:app_hi) do + route = double(:route, request_method: 'GET', path: '/:version//test/hi') + double(:endpoint, route: route, options: { for: api_handler, path: ["hi"] }, namespace: '/test') + end + + describe 'declare feature categories at handler level for all routes' do + let(:api_handler) do + Class.new(described_class) do + feature_category :foo + urgency :medium + + namespace '/test' do + get 'hello' do + end + post 'hi' do + end + end + end + end + + it 'sets feature category for a particular route', :aggregate_failures do + expect(api_handler.feature_category_for_app(app_hello)).to eq(:foo) + expect(api_handler.feature_category_for_app(app_hi)).to eq(:foo) + end + + it 'sets request urgency for a particular route', :aggregate_failures do + expect(api_handler.urgency_for_app(app_hello)).to be_request_urgency(:medium) + expect(api_handler.urgency_for_app(app_hi)).to be_request_urgency(:medium) + end + end + + describe 'declare feature categories at route level' do + let(:api_handler) do + Class.new(described_class) do + namespace '/test' do + get 'hello', feature_category: :foo, urgency: :low do + end + post 'hi', feature_category: :bar, urgency: :medium do + end + end + end + end + + it 'sets feature category for a particular route', :aggregate_failures do + expect(api_handler.feature_category_for_app(app_hello)).to eq(:foo) + expect(api_handler.feature_category_for_app(app_hi)).to eq(:bar) + end + + it 'sets request urgency for a particular route', :aggregate_failures do + expect(api_handler.urgency_for_app(app_hello)).to be_request_urgency(:low) + expect(api_handler.urgency_for_app(app_hi)).to be_request_urgency(:medium) + end + end + + describe 'declare feature categories at both handler level and route level' do + let(:api_handler) do + Class.new(described_class) do + feature_category :foo, ['/test/hello'] + urgency :low, ['/test/hello'] + + namespace '/test' do + get 'hello' do + end + post 'hi', feature_category: :bar, urgency: :medium do + end + end + end + end + + it 'sets feature category for a particular route', :aggregate_failures do + expect(api_handler.feature_category_for_app(app_hello)).to eq(:foo) + expect(api_handler.feature_category_for_app(app_hi)).to eq(:bar) + end + + it 'sets target duration for a particular route', :aggregate_failures do + expect(api_handler.urgency_for_app(app_hello)).to be_request_urgency(:low) + expect(api_handler.urgency_for_app(app_hi)).to be_request_urgency(:medium) + end + end +end +# rubocop:enable Rails/HttpPositionalArguments diff --git a/spec/lib/api/ci/helpers/runner_spec.rb b/spec/lib/api/ci/helpers/runner_spec.rb index 99f2db544a5..cc871d66d40 100644 --- a/spec/lib/api/ci/helpers/runner_spec.rb +++ b/spec/lib/api/ci/helpers/runner_spec.rb @@ -15,8 +15,8 @@ RSpec.describe API::Ci::Helpers::Runner do it 'handles sticking of a build when a build ID is specified' do allow(helper).to receive(:params).and_return(id: build.id) - expect(Gitlab::Database::LoadBalancing::RackMiddleware) - .to receive(:stick_or_unstick) + expect(ApplicationRecord.sticking) + .to receive(:stick_or_unstick_request) .with({}, :build, build.id) helper.current_job @@ -25,8 +25,8 @@ RSpec.describe API::Ci::Helpers::Runner do it 'does not handle sticking if no build ID was specified' do allow(helper).to receive(:params).and_return({}) - expect(Gitlab::Database::LoadBalancing::RackMiddleware) - .not_to receive(:stick_or_unstick) + expect(ApplicationRecord.sticking) + .not_to receive(:stick_or_unstick_request) helper.current_job end @@ -44,8 +44,8 @@ RSpec.describe API::Ci::Helpers::Runner do it 'handles sticking of a runner if a token is specified' do allow(helper).to receive(:params).and_return(token: runner.token) - expect(Gitlab::Database::LoadBalancing::RackMiddleware) - .to receive(:stick_or_unstick) + expect(ApplicationRecord.sticking) + .to receive(:stick_or_unstick_request) .with({}, :runner, runner.token) helper.current_runner @@ -54,8 +54,8 @@ RSpec.describe API::Ci::Helpers::Runner do it 'does not handle sticking if no token was specified' do allow(helper).to receive(:params).and_return({}) - expect(Gitlab::Database::LoadBalancing::RackMiddleware) - .not_to receive(:stick_or_unstick) + expect(ApplicationRecord.sticking) + .not_to receive(:stick_or_unstick_request) helper.current_runner end diff --git a/spec/lib/api/entities/clusters/agent_authorization_spec.rb b/spec/lib/api/entities/clusters/agent_authorization_spec.rb index 101a8af4ac4..3a1deb43bf8 100644 --- a/spec/lib/api/entities/clusters/agent_authorization_spec.rb +++ b/spec/lib/api/entities/clusters/agent_authorization_spec.rb @@ -3,15 +3,34 @@ require 'spec_helper' RSpec.describe API::Entities::Clusters::AgentAuthorization do - let_it_be(:authorization) { create(:agent_group_authorization) } - subject { described_class.new(authorization).as_json } - it 'includes basic fields' do - expect(subject).to include( - id: authorization.agent_id, - config_project: a_hash_including(id: authorization.agent.project_id), - configuration: authorization.config - ) + shared_examples 'generic authorization' do + it 'includes shared fields' do + expect(subject).to include( + id: authorization.agent_id, + config_project: a_hash_including(id: authorization.agent.project_id), + configuration: authorization.config + ) + end + end + + context 'project authorization' do + let(:authorization) { create(:agent_project_authorization) } + + include_examples 'generic authorization' + end + + context 'group authorization' do + let(:authorization) { create(:agent_group_authorization) } + + include_examples 'generic authorization' + end + + context 'implicit authorization' do + let(:agent) { create(:cluster_agent) } + let(:authorization) { Clusters::Agents::ImplicitAuthorization.new(agent: agent) } + + include_examples 'generic authorization' end end diff --git a/spec/lib/api/entities/user_spec.rb b/spec/lib/api/entities/user_spec.rb index 860f007f284..9c9a157d68a 100644 --- a/spec/lib/api/entities/user_spec.rb +++ b/spec/lib/api/entities/user_spec.rb @@ -3,10 +3,13 @@ require 'spec_helper' RSpec.describe API::Entities::User do - let(:user) { create(:user) } + let_it_be(:timezone) { 'America/Los_Angeles' } + + let(:user) { create(:user, timezone: timezone) } let(:current_user) { create(:user) } + let(:entity) { described_class.new(user, current_user: current_user) } - subject { described_class.new(user, current_user: current_user).as_json } + subject { entity.as_json } it 'exposes correct attributes' do expect(subject).to include(:bio, :location, :public_email, :skype, :linkedin, :twitter, :website_url, :organization, :job_title, :work_information, :pronouns) @@ -35,4 +38,10 @@ RSpec.describe API::Entities::User do expect(subject[:bot]).to eq(true) end end + + it 'exposes local_time' do + local_time = '2:30 PM' + expect(entity).to receive(:local_time).with(timezone).and_return(local_time) + expect(subject[:local_time]).to eq(local_time) + end end diff --git a/spec/lib/api/every_api_endpoint_spec.rb b/spec/lib/api/every_api_endpoint_spec.rb index ebf75e733d0..5fe14823a29 100644 --- a/spec/lib/api/every_api_endpoint_spec.rb +++ b/spec/lib/api/every_api_endpoint_spec.rb @@ -5,11 +5,11 @@ require 'spec_helper' RSpec.describe 'Every API endpoint' do context 'feature categories' do let_it_be(:feature_categories) do - YAML.load_file(Rails.root.join('config', 'feature_categories.yml')).map(&:to_sym).to_set + Gitlab::FeatureCategories.default.categories.map(&:to_sym).to_set end let_it_be(:api_endpoints) do - API::API.routes.map do |route| + Gitlab::RequestEndpoints.all_api_endpoints.map do |route| [route.app.options[:for], API::Base.path_for_app(route.app)] end end diff --git a/spec/lib/api/helpers_spec.rb b/spec/lib/api/helpers_spec.rb index 587fe60860a..37e040a422b 100644 --- a/spec/lib/api/helpers_spec.rb +++ b/spec/lib/api/helpers_spec.rb @@ -32,15 +32,11 @@ RSpec.describe API::Helpers do helper end - before do - allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true) - end - it 'handles sticking when a user could be found' do allow_any_instance_of(API::Helpers).to receive(:initial_current_user).and_return(user) - expect(Gitlab::Database::LoadBalancing::RackMiddleware) - .to receive(:stick_or_unstick).with(any_args, :user, 42) + expect(ApplicationRecord.sticking) + .to receive(:stick_or_unstick_request).with(any_args, :user, 42) get 'user' @@ -50,8 +46,8 @@ RSpec.describe API::Helpers do it 'does not handle sticking if no user could be found' do allow_any_instance_of(API::Helpers).to receive(:initial_current_user).and_return(nil) - expect(Gitlab::Database::LoadBalancing::RackMiddleware) - .not_to receive(:stick_or_unstick) + expect(ApplicationRecord.sticking) + .not_to receive(:stick_or_unstick_request) get 'user' diff --git a/spec/lib/api/validations/validators/project_portable_spec.rb b/spec/lib/api/validations/validators/project_portable_spec.rb new file mode 100644 index 00000000000..8c1a49d5214 --- /dev/null +++ b/spec/lib/api/validations/validators/project_portable_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Validations::Validators::ProjectPortable do + include ApiValidatorsHelpers + + let(:portable) { 'labels' } + let(:not_portable) { 'project_members' } + + subject do + described_class.new(['test'], {}, false, scope.new) + end + + context 'valid portable' do + it 'does not raise a validation error' do + expect_no_validation_error('test' => portable) + end + end + + context 'empty params' do + it 'raises a validation error' do + expect_validation_error('test' => nil) + expect_validation_error('test' => '') + end + end + + context 'not portable' do + it 'raises a validation error' do + expect_validation_error('test' => not_portable) # Sha length > 40 + end + end +end |