diff options
Diffstat (limited to 'spec/lib/api/helpers')
-rw-r--r-- | spec/lib/api/helpers/common_helpers_spec.rb | 51 | ||||
-rw-r--r-- | spec/lib/api/helpers/graphql_helpers_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/helpers/label_helpers_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb | 72 | ||||
-rw-r--r-- | spec/lib/api/helpers/packages_helpers_spec.rb | 104 | ||||
-rw-r--r-- | spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb | 154 | ||||
-rw-r--r-- | spec/lib/api/helpers/pagination_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/helpers/pagination_strategies_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/helpers/related_resources_helpers_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/helpers/version_spec.rb | 2 |
10 files changed, 387 insertions, 6 deletions
diff --git a/spec/lib/api/helpers/common_helpers_spec.rb b/spec/lib/api/helpers/common_helpers_spec.rb new file mode 100644 index 00000000000..5162d2f1000 --- /dev/null +++ b/spec/lib/api/helpers/common_helpers_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Helpers::CommonHelpers do + include Rack::Test::Methods + + subject do + Class.new(Grape::API) do + helpers API::Helpers::CommonHelpers + + before do + coerce_nil_params_to_array! + end + + params do + requires :id, type: String + optional :array, type: Array, coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce + optional :array_of_strings, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce + optional :array_of_ints, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce + end + get ":id" do + params.to_json + end + end + end + + def app + subject + end + + describe '.coerce_nil_params_to_array!' do + let(:json_response) { Gitlab::Json.parse(last_response.body) } + + it 'converts all nil parameters to empty arrays' do + get '/test?array=&array_of_strings=&array_of_ints=' + + expect(json_response['array']).to eq([]) + expect(json_response['array_of_strings']).to eq([]) + expect(json_response['array_of_ints']).to eq([]) + end + + it 'leaves non-nil parameters alone' do + get '/test?array=&array_of_strings=test,me&array_of_ints=1,2' + + expect(json_response['array']).to eq([]) + expect(json_response['array_of_strings']).to eq(%w(test me)) + expect(json_response['array_of_ints']).to eq([1, 2]) + end + end +end diff --git a/spec/lib/api/helpers/graphql_helpers_spec.rb b/spec/lib/api/helpers/graphql_helpers_spec.rb index c775ba6d5e8..678f4f8a3e3 100644 --- a/spec/lib/api/helpers/graphql_helpers_spec.rb +++ b/spec/lib/api/helpers/graphql_helpers_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe API::Helpers::GraphqlHelpers do +RSpec.describe API::Helpers::GraphqlHelpers do describe 'run_graphql!' do let(:query) { '{ metadata { version } }' } diff --git a/spec/lib/api/helpers/label_helpers_spec.rb b/spec/lib/api/helpers/label_helpers_spec.rb index 138e9a22d70..007cb3248e2 100644 --- a/spec/lib/api/helpers/label_helpers_spec.rb +++ b/spec/lib/api/helpers/label_helpers_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe API::Helpers::LabelHelpers do +RSpec.describe API::Helpers::LabelHelpers do describe 'create_service_params' do let(:label_helper) do Class.new do diff --git a/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb new file mode 100644 index 00000000000..ccf96bcbad6 --- /dev/null +++ b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do + let_it_be(:helper) { Class.new.include(described_class).new } + + describe 'redirect_registry_request' do + using RSpec::Parameterized::TableSyntax + + let(:options) { {} } + + subject { helper.redirect_registry_request(forward_to_registry, package_type, options) { helper.fallback } } + + shared_examples 'executing fallback' do + it 'redirects to package registry' do + expect(helper).to receive(:registry_url).never + expect(helper).to receive(:redirect).never + expect(helper).to receive(:fallback).once + + subject + end + end + + shared_examples 'executing redirect' do + it 'redirects to package registry' do + expect(helper).to receive(:registry_url).once + expect(helper).to receive(:redirect).once + expect(helper).to receive(:fallback).never + + subject + end + end + + context 'with npm packages' do + let(:package_type) { :npm } + + where(:application_setting, :forward_to_registry, :example_name) do + true | true | 'executing redirect' + true | false | 'executing fallback' + false | true | 'executing fallback' + false | false | 'executing fallback' + end + + with_them do + before do + stub_application_setting(npm_package_requests_forwarding: application_setting) + end + + it_behaves_like params[:example_name] + end + end + + context 'with non-forwardable packages' do + let(:forward_to_registry) { true } + + before do + stub_application_setting(npm_package_requests_forwarding: true) + end + + Packages::Package.package_types.keys.without('npm').each do |pkg_type| + context "#{pkg_type}" do + let(:package_type) { pkg_type } + + it 'raises an error' do + expect { subject }.to raise_error(ArgumentError, "Can't build registry_url for package_type #{package_type}") + end + end + end + end + end +end diff --git a/spec/lib/api/helpers/packages_helpers_spec.rb b/spec/lib/api/helpers/packages_helpers_spec.rb new file mode 100644 index 00000000000..0c51e25bad9 --- /dev/null +++ b/spec/lib/api/helpers/packages_helpers_spec.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Helpers::PackagesHelpers do + let_it_be(:helper) { Class.new.include(described_class).new } + let_it_be(:project) { create(:project) } + + describe 'authorize_packages_access!' do + subject { helper.authorize_packages_access!(project) } + + it 'authorizes packages access' do + expect(helper).to receive(:require_packages_enabled!) + expect(helper).to receive(:authorize_read_package!).with(project) + + expect(subject).to eq nil + end + end + + %i[read_package create_package destroy_package].each do |action| + describe "authorize_#{action}!" do + subject { helper.send("authorize_#{action}!", project) } + + it 'calls authorize!' do + expect(helper).to receive(:authorize!).with(action, project) + + expect(subject).to eq nil + end + end + end + + describe 'require_packages_enabled!' do + let(:packages_enabled) { true } + + subject { helper.require_packages_enabled! } + + before do + allow(::Gitlab.config.packages).to receive(:enabled).and_return(packages_enabled) + end + + context 'with packages enabled' do + it "doesn't call not_found!" do + expect(helper).to receive(:not_found!).never + + expect(subject).to eq nil + end + end + + context 'with package disabled' do + let(:packages_enabled) { false } + + it 'calls not_found!' do + expect(helper).to receive(:not_found!).once + + subject + end + end + end + + describe '#authorize_workhorse!' do + let_it_be(:headers) { {} } + + subject { helper.authorize_workhorse!(subject: project) } + + before do + allow(helper).to receive(:headers).and_return(headers) + end + + it 'authorizes workhorse' do + expect(helper).to receive(:authorize_upload!).with(project) + expect(helper).to receive(:status).with(200) + expect(helper).to receive(:content_type).with(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE) + expect(Gitlab::Workhorse).to receive(:verify_api_request!).with(headers) + expect(::Packages::PackageFileUploader).to receive(:workhorse_authorize).with(has_length: true) + + expect(subject).to eq nil + end + + context 'without length' do + subject { helper.authorize_workhorse!(subject: project, has_length: false) } + + it 'authorizes workhorse' do + expect(helper).to receive(:authorize_upload!).with(project) + expect(helper).to receive(:status).with(200) + expect(helper).to receive(:content_type).with(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE) + expect(Gitlab::Workhorse).to receive(:verify_api_request!).with(headers) + expect(::Packages::PackageFileUploader).to receive(:workhorse_authorize).with(has_length: false, maximum_size: ::API::Helpers::PackagesHelpers::MAX_PACKAGE_FILE_SIZE) + + expect(subject).to eq nil + end + end + end + + describe '#authorize_upload!' do + subject { helper.authorize_upload!(project) } + + it 'authorizes the upload' do + expect(helper).to receive(:authorize_create_package!).with(project) + expect(helper).to receive(:require_gitlab_workhorse!) + + expect(subject).to eq nil + end + end +end diff --git a/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb b/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb new file mode 100644 index 00000000000..80be5f7d10a --- /dev/null +++ b/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb @@ -0,0 +1,154 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Helpers::PackagesManagerClientsHelpers do + let_it_be(:personal_access_token) { create(:personal_access_token) } + let_it_be(:username) { personal_access_token.user.username } + let_it_be(:helper) { Class.new.include(described_class).new } + let(:password) { personal_access_token.token } + + describe '#find_personal_access_token_from_http_basic_auth' do + let(:headers) { { Authorization: basic_http_auth(username, password) } } + + subject { helper.find_personal_access_token_from_http_basic_auth } + + before do + allow(helper).to receive(:headers).and_return(headers&.with_indifferent_access) + end + + context 'with a valid Authorization header' do + it { is_expected.to eq personal_access_token } + end + + context 'with an invalid Authorization header' do + where(:headers) do + [ + [{ Authorization: 'Invalid' }], + [{}], + [nil] + ] + end + + with_them do + it { is_expected.to be nil } + end + end + + context 'with an unknown Authorization header' do + let(:password) { 'Unknown' } + + it { is_expected.to be nil } + end + end + + describe '#find_job_from_http_basic_auth' do + let_it_be(:user) { personal_access_token.user } + + let(:job) { create(:ci_build, user: user) } + let(:password) { job.token } + let(:headers) { { Authorization: basic_http_auth(username, password) } } + + subject { helper.find_job_from_http_basic_auth } + + before do + allow(helper).to receive(:headers).and_return(headers&.with_indifferent_access) + end + + context 'with a valid Authorization header' do + it { is_expected.to eq job } + end + + context 'with an invalid Authorization header' do + where(:headers) do + [ + [{ Authorization: 'Invalid' }], + [{}], + [nil] + ] + end + + with_them do + it { is_expected.to be nil } + end + end + + context 'with an unknown Authorization header' do + let(:password) { 'Unknown' } + + it { is_expected.to be nil } + end + end + + describe '#find_deploy_token_from_http_basic_auth' do + let_it_be(:deploy_token) { create(:deploy_token) } + let(:token) { deploy_token.token } + let(:headers) { { Authorization: basic_http_auth(deploy_token.username, token) } } + + subject { helper.find_deploy_token_from_http_basic_auth } + + before do + allow(helper).to receive(:headers).and_return(headers&.with_indifferent_access) + end + + context 'with a valid Authorization header' do + it { is_expected.to eq deploy_token } + end + + context 'with an invalid Authorization header' do + where(:headers) do + [ + [{ Authorization: 'Invalid' }], + [{}], + [nil] + ] + end + + with_them do + it { is_expected.to be nil } + end + end + + context 'with an invalid token' do + let(:token) { 'Unknown' } + + it { is_expected.to be nil } + end + end + + describe '#uploaded_package_file' do + let_it_be(:params) { {} } + + subject { helper.uploaded_package_file } + + before do + allow(helper).to receive(:params).and_return(params) + end + + context 'with valid uploaded package file' do + let_it_be(:uploaded_file) { Object.new } + + before do + allow(UploadedFile).to receive(:from_params).and_return(uploaded_file) + end + + it { is_expected.to be uploaded_file } + end + + context 'with invalid uploaded package file' do + before do + allow(UploadedFile).to receive(:from_params).and_return(nil) + end + + it 'fails with bad_request!' do + expect(helper).to receive(:bad_request!) + + expect(subject).to be nil + end + end + end + + def basic_http_auth(username, password) + ActionController::HttpAuthentication::Basic.encode_credentials(username, password) + end +end diff --git a/spec/lib/api/helpers/pagination_spec.rb b/spec/lib/api/helpers/pagination_spec.rb index 796c753d6c4..a008c1adeac 100644 --- a/spec/lib/api/helpers/pagination_spec.rb +++ b/spec/lib/api/helpers/pagination_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe API::Helpers::Pagination do +RSpec.describe API::Helpers::Pagination do subject { Class.new.include(described_class).new } let(:paginator) { double('paginator') } diff --git a/spec/lib/api/helpers/pagination_strategies_spec.rb b/spec/lib/api/helpers/pagination_strategies_spec.rb index eaa71159714..e8a4243b407 100644 --- a/spec/lib/api/helpers/pagination_strategies_spec.rb +++ b/spec/lib/api/helpers/pagination_strategies_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe API::Helpers::PaginationStrategies do +RSpec.describe API::Helpers::PaginationStrategies do subject { Class.new.include(described_class).new } let(:expected_result) { double("result") } diff --git a/spec/lib/api/helpers/related_resources_helpers_spec.rb b/spec/lib/api/helpers/related_resources_helpers_spec.rb index eeeb22abd10..a0dc69536b4 100644 --- a/spec/lib/api/helpers/related_resources_helpers_spec.rb +++ b/spec/lib/api/helpers/related_resources_helpers_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe API::Helpers::RelatedResourcesHelpers do +RSpec.describe API::Helpers::RelatedResourcesHelpers do subject(:helpers) do Class.new.include(described_class).new end diff --git a/spec/lib/api/helpers/version_spec.rb b/spec/lib/api/helpers/version_spec.rb index a9f33962537..a87a3c5a026 100644 --- a/spec/lib/api/helpers/version_spec.rb +++ b/spec/lib/api/helpers/version_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe API::Helpers::Version do +RSpec.describe API::Helpers::Version do describe '.new' do it 'is possible to initialize it with existing API version' do expect(described_class.new('v4').to_s).to eq 'v4' |