2020-12-04 10:09:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe API::NugetProjectPackages do
|
2020-12-29 07:10:19 -05:00
|
|
|
include_context 'nuget api setup'
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
let_it_be_with_reload(:project) { create(:project, :public) }
|
2020-12-04 10:09:36 -05:00
|
|
|
let_it_be(:deploy_token) { create(:deploy_token, read_package_registry: true, write_package_registry: true) }
|
|
|
|
let_it_be(:project_deploy_token) { create(:project_deploy_token, deploy_token: deploy_token, project: project) }
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
let(:target) { project }
|
|
|
|
let(:target_type) { 'projects' }
|
|
|
|
|
2020-12-04 10:09:36 -05:00
|
|
|
describe 'GET /api/v4/projects/:id/packages/nuget' do
|
|
|
|
it_behaves_like 'handling nuget service requests' do
|
2020-12-29 07:10:19 -05:00
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget/index.json" }
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /api/v4/projects/:id/packages/nuget/metadata/*package_name/index' do
|
|
|
|
it_behaves_like 'handling nuget metadata requests with package name' do
|
2020-12-29 07:10:19 -05:00
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget/metadata/#{package_name}/index.json" }
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /api/v4/projects/:id/packages/nuget/metadata/*package_name/*package_version' do
|
|
|
|
it_behaves_like 'handling nuget metadata requests with package name and package version' do
|
2020-12-29 07:10:19 -05:00
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget/metadata/#{package_name}/#{package.version}.json" }
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /api/v4/projects/:id/packages/nuget/query' do
|
|
|
|
it_behaves_like 'handling nuget search requests' do
|
2020-12-29 07:10:19 -05:00
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget/query?#{query_parameters.to_query}" }
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
describe 'GET /api/v4/projects/:id/packages/nuget/download/*package_name/index' do
|
|
|
|
let_it_be(:package_name) { 'Dummy.Package' }
|
|
|
|
let_it_be(:packages) { create_list(:nuget_package, 5, name: package_name, project: project) }
|
|
|
|
|
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget/download/#{package_name}/index.json" }
|
|
|
|
|
|
|
|
subject { get api(url) }
|
|
|
|
|
|
|
|
context 'with valid target' do
|
|
|
|
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
|
|
|
|
'PUBLIC' | :developer | true | true | 'process nuget download versions request' | :success
|
|
|
|
'PUBLIC' | :guest | true | true | 'process nuget download versions request' | :success
|
2021-01-01 10:10:11 -05:00
|
|
|
'PUBLIC' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
2020-12-29 07:10:19 -05:00
|
|
|
'PUBLIC' | :developer | false | true | 'process nuget download versions request' | :success
|
|
|
|
'PUBLIC' | :guest | false | true | 'process nuget download versions request' | :success
|
2021-01-01 10:10:11 -05:00
|
|
|
'PUBLIC' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
2020-12-29 07:10:19 -05:00
|
|
|
'PUBLIC' | :anonymous | false | true | 'process nuget download versions request' | :success
|
|
|
|
'PRIVATE' | :developer | true | true | 'process nuget download versions request' | :success
|
|
|
|
'PRIVATE' | :guest | true | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PRIVATE' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :guest | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:token) { user_token ? personal_access_token.token : 'wrong' }
|
|
|
|
let(:headers) { user_role == :anonymous ? {} : basic_auth_header(user.username, token) }
|
|
|
|
|
|
|
|
subject { get api(url), headers: headers }
|
|
|
|
|
|
|
|
before do
|
|
|
|
update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'deploy token for package GET requests'
|
|
|
|
|
|
|
|
it_behaves_like 'rejects nuget access with unknown target id'
|
|
|
|
|
|
|
|
it_behaves_like 'rejects nuget access with invalid target id'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /api/v4/projects/:id/packages/nuget/download/*package_name/*package_version/*package_filename' do
|
|
|
|
let_it_be(:package_name) { 'Dummy.Package' }
|
|
|
|
let_it_be(:package) { create(:nuget_package, project: project, name: package_name) }
|
|
|
|
|
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget/download/#{package.name}/#{package.version}/#{package.name}.#{package.version}.nupkg" }
|
|
|
|
|
|
|
|
subject { get api(url) }
|
|
|
|
|
|
|
|
context 'with valid target' do
|
|
|
|
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
|
|
|
|
'PUBLIC' | :developer | true | true | 'process nuget download content request' | :success
|
|
|
|
'PUBLIC' | :guest | true | true | 'process nuget download content request' | :success
|
2021-01-01 10:10:11 -05:00
|
|
|
'PUBLIC' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
2020-12-29 07:10:19 -05:00
|
|
|
'PUBLIC' | :developer | false | true | 'process nuget download content request' | :success
|
|
|
|
'PUBLIC' | :guest | false | true | 'process nuget download content request' | :success
|
2021-01-01 10:10:11 -05:00
|
|
|
'PUBLIC' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
2020-12-29 07:10:19 -05:00
|
|
|
'PUBLIC' | :anonymous | false | true | 'process nuget download content request' | :success
|
|
|
|
'PRIVATE' | :developer | true | true | 'process nuget download content request' | :success
|
|
|
|
'PRIVATE' | :guest | true | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PRIVATE' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :guest | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:token) { user_token ? personal_access_token.token : 'wrong' }
|
|
|
|
let(:headers) { user_role == :anonymous ? {} : basic_auth_header(user.username, token) }
|
|
|
|
|
|
|
|
subject { get api(url), headers: headers }
|
|
|
|
|
|
|
|
before do
|
|
|
|
update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'deploy token for package GET requests' do
|
|
|
|
before do
|
|
|
|
update_visibility_to(Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'rejects nuget access with unknown target id'
|
|
|
|
|
|
|
|
it_behaves_like 'rejects nuget access with invalid target id'
|
|
|
|
end
|
|
|
|
|
2020-12-04 10:09:36 -05:00
|
|
|
describe 'PUT /api/v4/projects/:id/packages/nuget/authorize' do
|
2021-02-10 19:08:50 -05:00
|
|
|
include_context 'workhorse headers'
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget/authorize" }
|
2020-12-04 10:09:36 -05:00
|
|
|
let(:headers) { {} }
|
|
|
|
|
|
|
|
subject { put api(url), headers: headers }
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
context 'with valid project' do
|
|
|
|
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
|
|
|
|
'PUBLIC' | :developer | true | true | 'process nuget workhorse authorization' | :success
|
|
|
|
'PUBLIC' | :guest | true | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PUBLIC' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :developer | false | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PUBLIC' | :guest | false | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PUBLIC' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | true | true | 'process nuget workhorse authorization' | :success
|
|
|
|
'PRIVATE' | :guest | true | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PRIVATE' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :guest | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
|
|
|
|
end
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
with_them do
|
|
|
|
let(:token) { user_token ? personal_access_token.token : 'wrong' }
|
|
|
|
let(:user_headers) { user_role == :anonymous ? {} : basic_auth_header(user.username, token) }
|
2021-02-10 19:08:50 -05:00
|
|
|
let(:headers) { user_headers.merge(workhorse_headers) }
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
before do
|
|
|
|
update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false))
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
2020-12-29 07:10:19 -05:00
|
|
|
|
|
|
|
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
2020-12-29 07:10:19 -05:00
|
|
|
end
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like 'deploy token for package uploads'
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2021-03-23 08:09:33 -04:00
|
|
|
it_behaves_like 'job token for package uploads', authorize_endpoint: true do
|
|
|
|
let_it_be(:job) { create(:ci_build, :running, user: user) }
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like 'rejects nuget access with unknown target id'
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like 'rejects nuget access with invalid target id'
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /api/v4/projects/:id/packages/nuget' do
|
2021-02-10 19:08:50 -05:00
|
|
|
include_context 'workhorse headers'
|
|
|
|
|
2020-12-04 10:09:36 -05:00
|
|
|
let_it_be(:file_name) { 'package.nupkg' }
|
2020-12-29 07:10:19 -05:00
|
|
|
let(:url) { "/projects/#{target.id}/packages/nuget" }
|
2020-12-04 10:09:36 -05:00
|
|
|
let(:headers) { {} }
|
|
|
|
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
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
context 'with valid project' do
|
|
|
|
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
|
|
|
|
'PUBLIC' | :developer | true | true | 'process nuget upload' | :created
|
|
|
|
'PUBLIC' | :guest | true | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PUBLIC' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :developer | false | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PUBLIC' | :guest | false | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PUBLIC' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | true | true | 'process nuget upload' | :created
|
|
|
|
'PRIVATE' | :guest | true | true | 'rejects nuget packages access' | :forbidden
|
|
|
|
'PRIVATE' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :guest | false | true | 'rejects nuget packages access' | :not_found
|
|
|
|
'PRIVATE' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
with_them do
|
|
|
|
let(:token) { user_token ? personal_access_token.token : 'wrong' }
|
|
|
|
let(:user_headers) { user_role == :anonymous ? {} : basic_auth_header(user.username, token) }
|
2021-02-10 19:08:50 -05:00
|
|
|
let(:headers) { user_headers.merge(workhorse_headers) }
|
2020-12-04 10:09:36 -05:00
|
|
|
|
|
|
|
before do
|
2020-12-29 07:10:19 -05:00
|
|
|
update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false))
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like 'deploy token for package uploads'
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2021-03-23 08:09:33 -04:00
|
|
|
it_behaves_like 'job token for package uploads' do
|
|
|
|
let_it_be(:job) { create(:ci_build, :running, user: user) }
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like 'rejects nuget access with unknown target id'
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like 'rejects nuget access with invalid target id'
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
context 'file size above maximum limit' do
|
2021-02-10 19:08:50 -05:00
|
|
|
let(:headers) { basic_auth_header(deploy_token.username, deploy_token.token).merge(workhorse_headers) }
|
2020-12-04 10:09:36 -05:00
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
before do
|
|
|
|
allow_next_instance_of(UploadedFile) do |uploaded_file|
|
|
|
|
allow(uploaded_file).to receive(:size).and_return(project.actual_limits.nuget_max_file_size + 1)
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
it_behaves_like 'returning response status', :bad_request
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-29 07:10:19 -05:00
|
|
|
def update_visibility_to(visibility)
|
|
|
|
project.update!(visibility_level: visibility)
|
2020-12-04 10:09:36 -05:00
|
|
|
end
|
|
|
|
end
|