diff options
Diffstat (limited to 'spec/support/shared_examples/requests/api/nuget_packages_shared_examples.rb')
-rw-r--r-- | spec/support/shared_examples/requests/api/nuget_packages_shared_examples.rb | 120 |
1 files changed, 113 insertions, 7 deletions
diff --git a/spec/support/shared_examples/requests/api/nuget_packages_shared_examples.rb b/spec/support/shared_examples/requests/api/nuget_packages_shared_examples.rb index d6a0055700d..2e66bae26ba 100644 --- a/spec/support/shared_examples/requests/api/nuget_packages_shared_examples.rb +++ b/spec/support/shared_examples/requests/api/nuget_packages_shared_examples.rb @@ -18,7 +18,7 @@ RSpec.shared_examples 'rejects nuget packages access' do |user_type, status, add end end -RSpec.shared_examples 'process nuget service index request' do |user_type, status, add_member = true| +RSpec.shared_examples 'process nuget service index request' do |user_type, status, add_member = true, v2 = false| context "for user type #{user_type}" do before do target.send("add_#{user_type}", user) if add_member && user_type != :anonymous @@ -28,15 +28,22 @@ RSpec.shared_examples 'process nuget service index request' do |user_type, statu it_behaves_like 'a package tracking event', 'API::NugetPackages', 'cli_metadata' - it 'returns a valid json response' do + it 'returns a valid json or xml response' do subject - expect(response.media_type).to eq('application/json') - expect(json_response).to match_schema('public_api/v4/packages/nuget/service_index') - expect(json_response).to be_a(Hash) + if v2 + expect(response.media_type).to eq('application/xml') + expect(body).to have_xpath('//service') + .and have_xpath('//service/workspace') + .and have_xpath('//service/workspace/collection[@href]') + else + expect(response.media_type).to eq('application/json') + expect(json_response).to match_schema('public_api/v4/packages/nuget/service_index') + expect(json_response).to be_a(Hash) + end end - context 'with invalid format' do + context 'with invalid format', unless: v2 do let(:url) { "/#{target_type}/#{target.id}/packages/nuget/index.xls" } it_behaves_like 'rejects nuget packages access', :anonymous, :not_found @@ -44,6 +51,34 @@ RSpec.shared_examples 'process nuget service index request' do |user_type, statu end end +RSpec.shared_examples 'process nuget v2 $metadata service request' do |user_type, status, add_member = true| + context "for user type #{user_type}" do + before do + target.send("add_#{user_type}", user) if add_member && user_type != :anonymous + end + + it_behaves_like 'returning response status', status + + it 'returns a valid xml response' do + api_request + + doc = Nokogiri::XML(body) + + expect(response.media_type).to eq('application/xml') + expect(doc.at_xpath('//edmx:Edmx')).to be_present + expect(doc.at_xpath('//edmx:Edmx/edmx:DataServices')).to be_present + expect(doc.css('*').map(&:name)).to include( + 'Schema', 'EntityType', 'Key', 'PropertyRef', 'EntityContainer', 'EntitySet', 'FunctionImport', 'Parameter' + ) + expect(doc.css('*').select { |el| el.name == 'Property' }.map { |el| el.attribute_nodes.first.value }) + .to match_array(%w[Id Version Authors Dependencies Description DownloadCount IconUrl Published ProjectUrl + Tags Title LicenseUrl] + ) + expect(doc.css('*').detect { |el| el.name == 'FunctionImport' }.attr('Name')).to eq('FindPackagesById') + end + end +end + RSpec.shared_examples 'returning nuget metadata json response with json schema' do |json_schema| it 'returns a valid json response' do subject @@ -320,6 +355,33 @@ RSpec.shared_examples 'process nuget download content request' do |user_type, st expect(response.media_type).to eq('application/octet-stream') end end + + context 'with normalized package version' do + let(:normalized_version) { '0.1.0' } + let(:url) { "/projects/#{target.id}/packages/nuget/download/#{package.name}/#{normalized_version}/#{package.name}.#{package.version}.#{format}" } + + before do + package.nuget_metadatum.update_column(:normalized_version, normalized_version) + end + + it_behaves_like 'returning response status', status + + it 'returns a valid package archive' do + subject + + expect(response.media_type).to eq('application/octet-stream') + end + + it_behaves_like 'bumping the package last downloaded at field' + + context 'when nuget_normalized_version feature flag is disabled' do + before do + stub_feature_flags(nuget_normalized_version: false) + end + + it_behaves_like 'returning response status', :not_found + end + end end end @@ -439,6 +501,13 @@ end RSpec.shared_examples 'nuget authorize upload endpoint' do using RSpec::Parameterized::TableSyntax + include_context 'workhorse headers' + + let(:headers) { {} } + + subject { put api(url), headers: headers } + + it { is_expected.to have_request_urgency(:low) } context 'with valid project' do where(:visibility_level, :user_role, :member, :user_token, :sent_through, :shared_examples_name, :expected_status) do @@ -517,6 +586,26 @@ end RSpec.shared_examples 'nuget upload endpoint' do |symbol_package: false| using RSpec::Parameterized::TableSyntax + include_context 'workhorse headers' + + let(:headers) { {} } + let(:file_name) { symbol_package ? 'package.snupkg' : 'package.nupkg' } + let(:params) { { package: temp_file(file_name) } } + let(:file_key) { :package } + let(:send_rewritten_field) { true } + + subject do + workhorse_finalize( + api(url), + method: :put, + file_key: file_key, + params: params, + headers: headers, + send_rewritten_field: send_rewritten_field + ) + end + + it { is_expected.to have_request_urgency(:low) } context 'with valid project' do where(:visibility_level, :user_role, :member, :user_token, :sent_through, :shared_examples_name, :expected_status) do @@ -573,7 +662,12 @@ RSpec.shared_examples 'nuget upload endpoint' do |symbol_package: false| end let(:headers) { user_headers.merge(workhorse_headers) } - let(:snowplow_gitlab_standard_context) { { project: project, user: user, namespace: project.namespace, property: 'i_package_nuget_user' } } + + let(:snowplow_gitlab_standard_context) do + { project: project, user: user, namespace: project.namespace, property: 'i_package_nuget_user' }.tap do |ctx| + ctx[:feed] = 'v2' if url.include?('nuget/v2') + end + end before do update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false)) @@ -604,4 +698,16 @@ RSpec.shared_examples 'nuget upload endpoint' do |symbol_package: false| it_behaves_like 'returning response status', :bad_request end + + context 'when ObjectStorage::RemoteStoreError is raised' do + let(:headers) { basic_auth_header(deploy_token.username, deploy_token.token).merge(workhorse_headers) } + + before do + allow_next_instance_of(::Packages::CreatePackageFileService) do |instance| + allow(instance).to receive(:execute).and_raise(ObjectStorage::RemoteStoreError) + end + end + + it_behaves_like 'returning response status', :forbidden + end end |