diff options
Diffstat (limited to 'spec/lib/api')
-rw-r--r-- | spec/lib/api/entities/merge_request_basic_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/api/entities/ml/mlflow/run_info_spec.rb | 4 | ||||
-rw-r--r-- | spec/lib/api/entities/release_spec.rb | 8 | ||||
-rw-r--r-- | spec/lib/api/entities/user_counts_spec.rb | 24 | ||||
-rw-r--r-- | spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb | 17 | ||||
-rw-r--r-- | spec/lib/api/helpers/packages_helpers_spec.rb | 119 | ||||
-rw-r--r-- | spec/lib/api/helpers_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/api/validations/validators/email_or_email_list_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/validations/validators/git_ref_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/validations/validators/limit_spec.rb | 4 |
10 files changed, 168 insertions, 34 deletions
diff --git a/spec/lib/api/entities/merge_request_basic_spec.rb b/spec/lib/api/entities/merge_request_basic_spec.rb index 40f259b86e2..bb0e25d2613 100644 --- a/spec/lib/api/entities/merge_request_basic_spec.rb +++ b/spec/lib/api/entities/merge_request_basic_spec.rb @@ -18,12 +18,16 @@ RSpec.describe ::API::Entities::MergeRequestBasic do subject { entity.as_json } - it 'includes basic fields' do - is_expected.to include( - draft: merge_request.draft?, - work_in_progress: merge_request.draft?, - merge_user: nil - ) + it 'includes expected fields' do + expected_fields = %i[ + merged_by merge_user merged_at closed_by closed_at target_branch user_notes_count upvotes downvotes + author assignees assignee reviewers source_project_id target_project_id labels draft work_in_progress + milestone merge_when_pipeline_succeeds merge_status detailed_merge_status sha merge_commit_sha + squash_commit_sha discussion_locked should_remove_source_branch force_remove_source_branch + reference references web_url time_stats squash task_completion_status has_conflicts blocking_discussions_resolved + ] + + is_expected.to include(*expected_fields) end context "with :with_api_entity_associations scope" do diff --git a/spec/lib/api/entities/ml/mlflow/run_info_spec.rb b/spec/lib/api/entities/ml/mlflow/run_info_spec.rb index 2a6d0825e5c..d5a37f53e21 100644 --- a/spec/lib/api/entities/ml/mlflow/run_info_spec.rb +++ b/spec/lib/api/entities/ml/mlflow/run_info_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' RSpec.describe API::Entities::Ml::Mlflow::RunInfo do let_it_be(:candidate) { create(:ml_candidates) } - subject { described_class.new(candidate).as_json } + subject { described_class.new(candidate, packages_url: 'http://example.com').as_json } context 'when start_time is nil' do it { expect(subject[:start_time]).to eq(0) } @@ -53,7 +53,7 @@ RSpec.describe API::Entities::Ml::Mlflow::RunInfo do describe 'artifact_uri' do it 'is not implemented' do - expect(subject[:artifact_uri]).to eq('not_implemented') + expect(subject[:artifact_uri]).to eq("http://example.com#{candidate.artifact_root}") end end diff --git a/spec/lib/api/entities/release_spec.rb b/spec/lib/api/entities/release_spec.rb index aa2c5126bb9..d1e5f191614 100644 --- a/spec/lib/api/entities/release_spec.rb +++ b/spec/lib/api/entities/release_spec.rb @@ -16,13 +16,13 @@ RSpec.describe API::Entities::Release do end describe 'evidences' do - context 'when the current user can download code' do + context 'when the current user can read code' do let(:entity_evidence) { entity[:evidences].first } it 'exposes the evidence sha and the json path' do allow(Ability).to receive(:allowed?).and_call_original allow(Ability).to receive(:allowed?) - .with(user, :download_code, project).and_return(true) + .with(user, :read_code, project).and_return(true) expect(entity_evidence[:sha]).to eq(evidence.summary_sha) expect(entity_evidence[:collected_at]).to eq(evidence.collected_at) @@ -36,11 +36,11 @@ RSpec.describe API::Entities::Release do end end - context 'when the current user cannot download code' do + context 'when the current user cannot read code' do it 'does not expose any evidence data' do allow(Ability).to receive(:allowed?).and_call_original allow(Ability).to receive(:allowed?) - .with(user, :download_code, project).and_return(false) + .with(user, :read_code, project).and_return(false) expect(entity.keys).not_to include(:evidences) end diff --git a/spec/lib/api/entities/user_counts_spec.rb b/spec/lib/api/entities/user_counts_spec.rb new file mode 100644 index 00000000000..0ed989ad7e9 --- /dev/null +++ b/spec/lib/api/entities/user_counts_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Entities::UserCounts do + let(:user) { build(:user) } + + subject(:entity) { described_class.new(user).as_json } + + it 'represents user counts', :aggregate_failures do + expect(user).to receive(:assigned_open_merge_requests_count).and_return(1).twice + expect(user).to receive(:assigned_open_issues_count).and_return(2).once + expect(user).to receive(:review_requested_open_merge_requests_count).and_return(3).once + expect(user).to receive(:todos_pending_count).and_return(4).once + + expect(entity).to include( + merge_requests: 1, + assigned_issues: 2, + assigned_merge_requests: 1, + review_requested_merge_requests: 3, + todos: 4 + ) + end +end diff --git a/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb index 66cf06cde20..582795acc4e 100644 --- a/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb +++ b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb @@ -10,7 +10,7 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do include_context 'dependency proxy helpers context' let_it_be(:group) { create(:group) } - let_it_be(:project) { create(:project, group: group) } + let_it_be_with_reload(:project) { create(:project, group: group) } let_it_be_with_reload(:package_setting) { create(:namespace_package_setting, namespace: group) } let(:target) { project } @@ -76,19 +76,6 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do end end - context 'when cascade_package_forwarding_settings is disabled' do - let(:package_type) { forwardable_package_type } - let(:forward_to_registry) { true } - - before do - stub_feature_flags(cascade_package_forwarding_settings: false) - allow_fetch_cascade_application_setting(attribute: "#{forwardable_package_type}_package_requests_forwarding", return_value: true) - package_setting.update!("#{forwardable_package_type}_package_requests_forwarding" => false) - end - - it_behaves_like 'executing redirect' - end - context 'when no target is present' do let(:package_type) { forwardable_package_type } let(:forward_to_registry) { true } @@ -133,7 +120,7 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do end Packages::Package.package_types.keys.without('maven', 'npm', 'pypi').each do |pkg_type| - context "#{pkg_type}" do + context pkg_type.to_s do let(:package_type) { pkg_type.to_sym } it 'raises an error' do diff --git a/spec/lib/api/helpers/packages_helpers_spec.rb b/spec/lib/api/helpers/packages_helpers_spec.rb index d764ed4afff..b9c887b3e16 100644 --- a/spec/lib/api/helpers/packages_helpers_spec.rb +++ b/spec/lib/api/helpers/packages_helpers_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' RSpec.describe API::Helpers::PackagesHelpers do - let_it_be(:helper) { Class.new.include(described_class).new } + let_it_be(:helper) { Class.new.include(API::Helpers).include(described_class).new } let_it_be(:project) { create(:project) } let_it_be(:group) { create(:group) } let_it_be(:package) { create(:package) } @@ -121,4 +121,121 @@ RSpec.describe API::Helpers::PackagesHelpers do expect(subject).to eq nil end end + + describe '#user_project' do + before do + allow(helper).to receive(:params).and_return(id: project.id) + end + + it 'calls find_project! on default action' do + expect(helper).to receive(:find_project!) + + helper.user_project + end + + it 'calls find_project! on read_project action' do + expect(helper).to receive(:find_project!) + + helper.user_project(action: :read_project) + end + + it 'calls user_project_with_read_package on read_package action' do + expect(helper).to receive(:user_project_with_read_package) + + helper.user_project(action: :read_package) + end + + it 'throws ArgumentError on unexpected action' do + expect { helper.user_project(action: :other_action) }.to raise_error(ArgumentError, 'unexpected action: other_action') + end + end + + describe '#user_project_with_read_package' do + before do + helper.clear_memoization(:user_project_with_read_package) + + allow(helper).to receive(:params).and_return(id: params_id) + allow(helper).to receive(:route_authentication_setting).and_return({ authenticate_non_public: true }) + allow(helper).to receive(:current_user).and_return(user) + allow(helper).to receive(:initial_current_user).and_return(user) + end + + subject { helper.user_project_with_read_package } + + context 'with non-existing project' do + let_it_be(:params_id) { non_existing_record_id } + + context 'with current user' do + let_it_be(:user) { create(:user) } + + it 'returns Not Found' do + expect(helper).to receive(:render_api_error!).with('404 Project Not Found', 404) + + is_expected.to be_nil + end + end + + context 'without current user' do + let_it_be(:user) { nil } + + it 'returns Unauthorized' do + expect(helper).to receive(:render_api_error!).with('401 Unauthorized', 401) + + is_expected.to be_nil + end + end + end + + context 'with existing project' do + let_it_be(:params_id) { project.id } + + context 'with current user' do + let_it_be(:user) { create(:user) } + + context 'as developer member' do + before do + project.add_developer(user) + end + + it 'returns project' do + is_expected.to eq(project) + end + end + + context 'as guest member' do + before do + project.add_guest(user) + end + + it 'returns Forbidden' do + expect(helper).to receive(:render_api_error!).with('403 Forbidden', 403) + + is_expected.to be_nil + end + end + end + + context 'without current user' do + let_it_be(:user) { nil } + + it 'returns Unauthorized' do + expect(helper).to receive(:render_api_error!).with('401 Unauthorized', 401) + + is_expected.to be_nil + end + end + end + + context 'if no authorized project scope' do + let_it_be(:params_id) { project.id } + let_it_be(:user) { nil } + + it 'returns Forbidden' do + expect(helper).to receive(:authorized_project_scope?).and_return(false) + expect(helper).to receive(:render_api_error!).with('403 Forbidden', 403) + + is_expected.to be_nil + end + end + end end diff --git a/spec/lib/api/helpers_spec.rb b/spec/lib/api/helpers_spec.rb index 652727f371b..d24a3bd13c0 100644 --- a/spec/lib/api/helpers_spec.rb +++ b/spec/lib/api/helpers_spec.rb @@ -798,7 +798,7 @@ RSpec.describe API::Helpers do context 'with object storage' do let(:artifact) { create(:ci_job_artifact, :zip, :remote_store) } - subject { helper.present_artifacts_file!(artifact.file, project: artifact.job.project) } + subject { helper.present_artifacts_file!(artifact.file) } before do allow(helper).to receive(:env).and_return({}) @@ -830,7 +830,7 @@ RSpec.describe API::Helpers do it 'retrieves a CDN-fronted URL' do expect(artifact.file).to receive(:cdn_enabled_url).and_call_original expect(Gitlab::ApplicationContext).to receive(:push).with(artifact_used_cdn: false).and_call_original - expect(helper.cdn_fronted_url(artifact.file, artifact.job.project)).to be_a(String) + expect(helper.cdn_fronted_url(artifact.file)).to be_a(String) end end @@ -841,7 +841,7 @@ RSpec.describe API::Helpers do file = double(url: url) expect(Gitlab::ApplicationContext).not_to receive(:push) - expect(helper.cdn_fronted_url(file, nil)).to eq(url) + expect(helper.cdn_fronted_url(file)).to eq(url) end end end diff --git a/spec/lib/api/validations/validators/email_or_email_list_spec.rb b/spec/lib/api/validations/validators/email_or_email_list_spec.rb index ac3111c2319..17cfdf93cdc 100644 --- a/spec/lib/api/validations/validators/email_or_email_list_spec.rb +++ b/spec/lib/api/validations/validators/email_or_email_list_spec.rb @@ -14,6 +14,7 @@ RSpec.describe API::Validations::Validators::EmailOrEmailList do expect_no_validation_error('test' => 'test@example.org') expect_no_validation_error('test' => 'test1@example.com,test2@example.org') expect_no_validation_error('test' => 'test1@example.com,test2@example.org,test3@example.co.uk') + expect_no_validation_error('test' => %w[test1@example.com test2@example.org test3@example.co.uk]) end end @@ -23,6 +24,7 @@ RSpec.describe API::Validations::Validators::EmailOrEmailList do expect_validation_error('test' => '@example.com') expect_validation_error('test' => 'test1@example.com,asdf') expect_validation_error('test' => 'asdf,testa1@example.com,asdf') + expect_validation_error('test' => %w[asdf testa1@example.com asdf]) end end end diff --git a/spec/lib/api/validations/validators/git_ref_spec.rb b/spec/lib/api/validations/validators/git_ref_spec.rb index 0d2d9e8f39a..6b9f5dee5fc 100644 --- a/spec/lib/api/validations/validators/git_ref_spec.rb +++ b/spec/lib/api/validations/validators/git_ref_spec.rb @@ -38,7 +38,7 @@ RSpec.describe API::Validations::Validators::GitRef do expect_validation_error('test' => 'heads/f[/bar') expect_validation_error('test' => "heads/foo\t") expect_validation_error('test' => "heads/foo\177") - expect_validation_error('test' => "#{'a' * 1025}") + expect_validation_error('test' => 'a' * 1025) expect_validation_error('test' => nil) expect_validation_error('test' => '') end diff --git a/spec/lib/api/validations/validators/limit_spec.rb b/spec/lib/api/validations/validators/limit_spec.rb index 0c10e2f74d2..11d808d390b 100644 --- a/spec/lib/api/validations/validators/limit_spec.rb +++ b/spec/lib/api/validations/validators/limit_spec.rb @@ -13,13 +13,13 @@ RSpec.describe API::Validations::Validators::Limit do it 'does not raise a validation error' do expect_no_validation_error('test' => '123-456') expect_no_validation_error('test' => '00000000-ffff-0000-ffff-000000000000') - expect_no_validation_error('test' => "#{'a' * 255}") + expect_no_validation_error('test' => 'a' * 255) end end context 'longer than limit param' do it 'raises a validation error' do - expect_validation_error('test' => "#{'a' * 256}") + expect_validation_error('test' => 'a' * 256) end end |