debian-mirror-gitlab/spec/requests/api/pypi_packages_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

423 lines
18 KiB
Ruby
Raw Permalink Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe API::PypiPackages, feature_category: :package_registry do
2020-07-28 23:09:34 +05:30
include WorkhorseHelpers
include PackagesManagerApiSpecHelpers
2020-10-24 23:57:45 +05:30
include HttpBasicAuthHelpers
2021-03-11 19:13:27 +05:30
using RSpec::Parameterized::TableSyntax
2020-07-28 23:09:34 +05:30
let_it_be(:user) { create(:user) }
2021-09-04 01:27:46 +05:30
let_it_be_with_reload(:group) { create(:group) }
let_it_be_with_reload(:project) { create(:project, :public, group: group) }
2020-07-28 23:09:34 +05:30
let_it_be(:personal_access_token) { create(:personal_access_token, user: user) }
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) }
2021-11-11 11:23:49 +05:30
let_it_be(:job) { create(:ci_build, :running, user: user, project: project) }
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
let(:snowplow_gitlab_standard_context) { snowplow_context }
2021-09-04 01:27:46 +05:30
let(:headers) { {} }
2020-07-28 23:09:34 +05:30
2023-05-27 22:25:52 +05:30
def snowplow_context(user_role: :developer)
if user_role == :anonymous
{ project: project, namespace: project.namespace, property: 'i_package_pypi_user' }
else
{ project: project, namespace: project.namespace, property: 'i_package_pypi_user', user: user }
end
end
2022-07-23 23:45:48 +05:30
context 'simple index API endpoint' do
let_it_be(:package) { create(:pypi_package, project: project) }
let_it_be(:package2) { create(:pypi_package, project: project) }
subject { get api(url), headers: headers }
describe 'GET /api/v4/groups/:id/-/packages/pypi/simple' do
let(:url) { "/groups/#{group.id}/-/packages/pypi/simple" }
it_behaves_like 'pypi simple index API endpoint'
it_behaves_like 'rejects PyPI access with unknown group id'
context 'deploy tokens' do
let_it_be(:group_deploy_token) { create(:group_deploy_token, deploy_token: deploy_token, group: group) }
before do
project.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
group.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
end
it_behaves_like 'deploy token for package GET requests'
context 'with group path as id' do
2022-08-27 11:52:29 +05:30
let(:url) { "/groups/#{CGI.escape(group.full_path)}/-/packages/pypi/simple" }
2022-07-23 23:45:48 +05:30
it_behaves_like 'deploy token for package GET requests'
end
end
context 'job token' do
before do
project.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
group.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
group.add_developer(user)
end
it_behaves_like 'job token for package GET requests'
end
it_behaves_like 'a pypi user namespace endpoint'
end
describe 'GET /api/v4/projects/:id/packages/pypi/simple' do
let(:package_name) { package.name }
let(:url) { "/projects/#{project.id}/packages/pypi/simple" }
2023-03-04 22:38:38 +05:30
let(:snowplow_gitlab_standard_context) { { project: nil, namespace: group, property: 'i_package_pypi_user' } }
2022-07-23 23:45:48 +05:30
it_behaves_like 'pypi simple index API endpoint'
it_behaves_like 'rejects PyPI access with unknown project id'
it_behaves_like 'deploy token for package GET requests'
it_behaves_like 'job token for package GET requests'
2023-01-13 00:05:48 +05:30
it_behaves_like 'allow access for everyone with public package_registry_access_level'
2022-07-23 23:45:48 +05:30
context 'with project path as id' do
let(:url) { "/projects/#{CGI.escape(project.full_path)}/packages/pypi/simple" }
it_behaves_like 'deploy token for package GET requests'
end
end
end
context 'simple package API endpoint' do
2020-07-28 23:09:34 +05:30
let_it_be(:package) { create(:pypi_package, project: project) }
2021-09-04 01:27:46 +05:30
subject { get api(url), headers: headers }
2020-07-28 23:09:34 +05:30
2021-09-04 01:27:46 +05:30
describe 'GET /api/v4/groups/:id/-/packages/pypi/simple/:package_name' do
2021-10-27 15:23:28 +05:30
let(:package_name) { package.name }
let(:url) { "/groups/#{group.id}/-/packages/pypi/simple/#{package_name}" }
2023-05-27 22:25:52 +05:30
let(:snowplow_context) { { project: nil, namespace: project.namespace, property: 'i_package_pypi_user' } }
2020-07-28 23:09:34 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'pypi simple API endpoint'
it_behaves_like 'rejects PyPI access with unknown group id'
2020-07-28 23:09:34 +05:30
2021-09-04 01:27:46 +05:30
context 'deploy tokens' do
let_it_be(:group_deploy_token) { create(:group_deploy_token, deploy_token: deploy_token, group: group) }
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
before do
2021-09-04 01:27:46 +05:30
project.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
group.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'deploy token for package GET requests'
2021-10-27 15:23:28 +05:30
context 'with group path as id' do
2022-08-27 11:52:29 +05:30
let(:url) { "/groups/#{CGI.escape(group.full_path)}/-/packages/pypi/simple/#{package_name}" }
2021-10-27 15:23:28 +05:30
it_behaves_like 'deploy token for package GET requests'
end
2020-07-28 23:09:34 +05:30
end
2021-09-04 01:27:46 +05:30
context 'job token' do
before do
project.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
group.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
group.add_developer(user)
end
2021-01-03 14:25:43 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'job token for package GET requests'
end
2021-01-03 14:25:43 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'a pypi user namespace endpoint'
2021-01-03 14:25:43 +05:30
end
2021-09-04 01:27:46 +05:30
describe 'GET /api/v4/projects/:id/packages/pypi/simple/:package_name' do
2021-10-27 15:23:28 +05:30
let(:package_name) { package.name }
let(:url) { "/projects/#{project.id}/packages/pypi/simple/#{package_name}" }
2023-05-27 22:25:52 +05:30
let(:snowplow_context) { { project: project, namespace: project.namespace, property: 'i_package_pypi_user' } }
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'pypi simple API endpoint'
it_behaves_like 'rejects PyPI access with unknown project id'
it_behaves_like 'deploy token for package GET requests'
it_behaves_like 'job token for package GET requests'
2023-01-13 00:05:48 +05:30
it_behaves_like 'allow access for everyone with public package_registry_access_level'
2021-10-27 15:23:28 +05:30
context 'with project path as id' do
let(:url) { "/projects/#{CGI.escape(project.full_path)}/packages/pypi/simple/#{package.name}" }
it_behaves_like 'deploy token for package GET requests'
end
2021-09-04 01:27:46 +05:30
end
2020-07-28 23:09:34 +05:30
end
describe 'POST /api/v4/projects/:id/packages/pypi/authorize' do
2021-03-11 19:13:27 +05:30
include_context 'workhorse headers'
2020-07-28 23:09:34 +05:30
let(:url) { "/projects/#{project.id}/packages/pypi/authorize" }
let(:headers) { {} }
subject { post api(url), headers: headers }
2020-10-24 23:57:45 +05:30
context 'with valid project' do
2021-09-04 01:27:46 +05:30
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
:public | :developer | true | true | 'process PyPI api request' | :success
:public | :guest | true | true | 'process PyPI api request' | :forbidden
:public | :developer | true | false | 'process PyPI api request' | :unauthorized
:public | :guest | true | false | 'process PyPI api request' | :unauthorized
:public | :developer | false | true | 'process PyPI api request' | :forbidden
:public | :guest | false | true | 'process PyPI api request' | :forbidden
:public | :developer | false | false | 'process PyPI api request' | :unauthorized
:public | :guest | false | false | 'process PyPI api request' | :unauthorized
:public | :anonymous | false | true | 'process PyPI api request' | :unauthorized
:private | :developer | true | true | 'process PyPI api request' | :success
:private | :guest | true | true | 'process PyPI api request' | :forbidden
:private | :developer | true | false | 'process PyPI api request' | :unauthorized
:private | :guest | true | false | 'process PyPI api request' | :unauthorized
:private | :developer | false | true | 'process PyPI api request' | :not_found
:private | :guest | false | true | 'process PyPI api request' | :not_found
:private | :developer | false | false | 'process PyPI api request' | :unauthorized
:private | :guest | false | false | 'process PyPI api request' | :unauthorized
:private | :anonymous | false | true | 'process PyPI api request' | :unauthorized
2020-10-24 23:57:45 +05:30
end
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
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-03-11 19:13:27 +05:30
let(:headers) { user_headers.merge(workhorse_headers) }
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
before do
2021-09-04 01:27:46 +05:30
project.update_column(:visibility_level, Gitlab::VisibilityLevel.level_value(visibility_level.to_s))
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
end
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
it_behaves_like 'deploy token for package uploads'
2020-07-28 23:09:34 +05:30
2021-04-29 21:17:54 +05:30
it_behaves_like 'job token for package uploads', authorize_endpoint: true
2020-11-24 15:15:51 +05:30
2020-10-24 23:57:45 +05:30
it_behaves_like 'rejects PyPI access with unknown project id'
2020-07-28 23:09:34 +05:30
end
describe 'POST /api/v4/projects/:id/packages/pypi' do
2021-03-11 19:13:27 +05:30
include_context 'workhorse headers'
2020-07-28 23:09:34 +05:30
let_it_be(:file_name) { 'package.whl' }
2021-09-30 23:02:18 +05:30
2020-07-28 23:09:34 +05:30
let(:url) { "/projects/#{project.id}/packages/pypi" }
let(:headers) { {} }
2020-11-24 15:15:51 +05:30
let(:requires_python) { '>=3.7' }
2022-08-13 15:12:31 +05:30
let(:base_params) { { requires_python: requires_python, version: '1.0.0', name: 'sample-project', sha256_digest: '1' * 64, md5_digest: '1' * 32 } }
2020-07-28 23:09:34 +05:30
let(:params) { base_params.merge(content: temp_file(file_name)) }
let(:send_rewritten_field) { true }
2023-03-04 22:38:38 +05:30
let(:snowplow_gitlab_standard_context) { { project: project, namespace: project.namespace, user: user, property: 'i_package_pypi_user' } }
2020-07-28 23:09:34 +05:30
subject do
workhorse_finalize(
api(url),
method: :post,
file_key: :content,
params: params,
headers: headers,
send_rewritten_field: send_rewritten_field
)
end
2020-10-24 23:57:45 +05:30
context 'with valid project' do
2021-09-04 01:27:46 +05:30
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
:public | :developer | true | true | 'PyPI package creation' | :created
:public | :guest | true | true | 'process PyPI api request' | :forbidden
:public | :developer | true | false | 'process PyPI api request' | :unauthorized
:public | :guest | true | false | 'process PyPI api request' | :unauthorized
:public | :developer | false | true | 'process PyPI api request' | :forbidden
:public | :guest | false | true | 'process PyPI api request' | :forbidden
:public | :developer | false | false | 'process PyPI api request' | :unauthorized
:public | :guest | false | false | 'process PyPI api request' | :unauthorized
:public | :anonymous | false | true | 'process PyPI api request' | :unauthorized
:private | :developer | true | true | 'process PyPI api request' | :created
:private | :guest | true | true | 'process PyPI api request' | :forbidden
:private | :developer | true | false | 'process PyPI api request' | :unauthorized
:private | :guest | true | false | 'process PyPI api request' | :unauthorized
:private | :developer | false | true | 'process PyPI api request' | :not_found
:private | :guest | false | true | 'process PyPI api request' | :not_found
:private | :developer | false | false | 'process PyPI api request' | :unauthorized
:private | :guest | false | false | 'process PyPI api request' | :unauthorized
:private | :anonymous | false | true | 'process PyPI api request' | :unauthorized
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
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-03-11 19:13:27 +05:30
let(:headers) { user_headers.merge(workhorse_headers) }
2023-05-27 22:25:52 +05:30
let(:snowplow_gitlab_standard_context) do
if user_role == :anonymous || (visibility_level == :public && !user_token)
{ project: project, namespace: project.namespace, property: 'i_package_pypi_user' }
else
{ project: project, namespace: project.namespace, property: 'i_package_pypi_user', user: user }
end
end
2020-07-28 23:09:34 +05:30
before do
2021-09-04 01:27:46 +05:30
project.update_column(:visibility_level, Gitlab::VisibilityLevel.level_value(visibility_level.to_s))
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
2020-07-28 23:09:34 +05:30
end
2022-05-07 20:08:51 +05:30
context 'without requires_python' do
let(:token) { personal_access_token.token }
let(:user_headers) { basic_auth_header(user.username, token) }
let(:headers) { user_headers.merge(workhorse_headers) }
it_behaves_like 'PyPI package creation', :developer, :created, true
2023-03-04 22:38:38 +05:30
context 'with FIPS mode', :fips_mode do
it_behaves_like 'PyPI package creation', :developer, :created, true, false
end
2022-05-07 20:08:51 +05:30
end
2022-08-13 15:12:31 +05:30
2023-03-04 22:38:38 +05:30
context 'without sha256_digest' do
2022-08-13 15:12:31 +05:30
let(:token) { personal_access_token.token }
let(:user_headers) { basic_auth_header(user.username, token) }
let(:headers) { user_headers.merge(workhorse_headers) }
let(:params) { base_params.merge(content: temp_file(file_name)) }
before do
2023-03-04 22:38:38 +05:30
params.delete(:sha256_digest)
2022-08-13 15:12:31 +05:30
end
2023-03-04 22:38:38 +05:30
it_behaves_like 'PyPI package creation', :developer, :created, true, true
context 'with FIPS mode', :fips_mode do
before do
project.add_developer(user)
end
it 'returns 422 and does not create a package' do
expect { subject }.not_to change { project.packages.pypi.count }
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
end
2022-08-13 15:12:31 +05:30
end
2020-10-24 23:57:45 +05:30
end
2020-07-28 23:09:34 +05:30
2020-11-24 15:15:51 +05:30
context 'with required_python too big' do
let(:requires_python) { 'x' * 256 }
let(:token) { personal_access_token.token }
let(:user_headers) { basic_auth_header(user.username, token) }
2021-03-11 19:13:27 +05:30
let(:headers) { user_headers.merge(workhorse_headers) }
2020-11-24 15:15:51 +05:30
before do
2021-09-04 01:27:46 +05:30
project.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
2020-11-24 15:15:51 +05:30
end
2021-01-03 14:25:43 +05:30
it_behaves_like 'process PyPI api request', :developer, :bad_request, true
2020-11-24 15:15:51 +05:30
end
2020-10-24 23:57:45 +05:30
context 'with an invalid package' do
let(:token) { personal_access_token.token }
let(:user_headers) { basic_auth_header(user.username, token) }
2021-03-11 19:13:27 +05:30
let(:headers) { user_headers.merge(workhorse_headers) }
2020-10-24 23:57:45 +05:30
before do
params[:name] = '.$/@!^*'
project.add_developer(user)
2022-05-03 16:02:30 +05:30
end
it_behaves_like 'returning response status', :bad_request
end
context 'with an invalid sha256' do
let(:token) { personal_access_token.token }
let(:user_headers) { basic_auth_header(user.username, token) }
let(:headers) { user_headers.merge(workhorse_headers) }
before do
params[:sha256_digest] = 'a' * 63 + '%'
project.add_developer(user)
2020-10-24 23:57:45 +05:30
end
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
it_behaves_like 'returning response status', :bad_request
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
it_behaves_like 'deploy token for package uploads'
2020-11-24 15:15:51 +05:30
it_behaves_like 'job token for package uploads'
2020-10-24 23:57:45 +05:30
it_behaves_like 'rejects PyPI access with unknown project id'
2020-11-24 15:15:51 +05:30
context 'file size above maximum limit' do
2021-03-11 19:13:27 +05:30
let(:headers) { basic_auth_header(deploy_token.username, deploy_token.token).merge(workhorse_headers) }
2020-11-24 15:15:51 +05:30
before do
allow_next_instance_of(UploadedFile) do |uploaded_file|
allow(uploaded_file).to receive(:size).and_return(project.actual_limits.pypi_max_file_size + 1)
end
end
it_behaves_like 'returning response status', :bad_request
end
2022-03-02 08:16:31 +05:30
context 'with existing package' do
let_it_be_with_reload(:existing_package) { create(:pypi_package, name: 'sample-project', version: '1.0.0', project: project) }
let(:headers) { basic_auth_header(user.username, personal_access_token.token).merge(workhorse_headers) }
before do
project.add_maintainer(user)
end
it 'does not create a new package', :aggregate_failures do
expect { subject }
.to change { project.packages.pypi.count }.by(0)
.and change { Packages::PackageFile.count }.by(1)
.and change { Packages::Pypi::Metadatum.count }.by(0)
expect(response).to have_gitlab_http_status(:created)
end
context 'marked as pending_destruction' do
it 'does create a new package', :aggregate_failures do
existing_package.pending_destruction!
expect { subject }
.to change { project.packages.pypi.count }.by(1)
.and change { Packages::PackageFile.count }.by(1)
.and change { Packages::Pypi::Metadatum.count }.by(1)
expect(response).to have_gitlab_http_status(:created)
end
end
end
2020-07-28 23:09:34 +05:30
end
2021-09-04 01:27:46 +05:30
context 'file download endpoint' do
2020-07-28 23:09:34 +05:30
let_it_be(:package_name) { 'Dummy-Package' }
let_it_be(:package) { create(:pypi_package, project: project, name: package_name, version: '1.0.0') }
2023-05-27 22:25:52 +05:30
let(:snowplow_gitlab_standard_context) do
if user_role == :anonymous || (visibility_level == :public && !user_token)
{ project: project, namespace: project.namespace, property: 'i_package_pypi_user' }
else
{ project: project, namespace: project.namespace, property: 'i_package_pypi_user', user: user }
end
end
2021-09-04 01:27:46 +05:30
subject { get api(url), headers: headers }
2020-07-28 23:09:34 +05:30
2021-09-04 01:27:46 +05:30
describe 'GET /api/v4/groups/:id/-/packages/pypi/files/:sha256/*file_identifier' do
let(:url) { "/groups/#{group.id}/-/packages/pypi/files/#{package.package_files.first.file_sha256}/#{package_name}-1.0.0.tar.gz" }
2020-07-28 23:09:34 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'pypi file download endpoint'
it_behaves_like 'rejects PyPI access with unknown group id'
it_behaves_like 'a pypi user namespace endpoint'
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
2021-09-04 01:27:46 +05:30
describe 'GET /api/v4/projects/:id/packages/pypi/files/:sha256/*file_identifier' do
let(:url) { "/projects/#{project.id}/packages/pypi/files/#{package.package_files.first.file_sha256}/#{package_name}-1.0.0.tar.gz" }
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
it_behaves_like 'pypi file download endpoint'
it_behaves_like 'rejects PyPI access with unknown project id'
2023-01-13 00:05:48 +05:30
it_behaves_like 'allow access for everyone with public package_registry_access_level'
2020-11-24 15:15:51 +05:30
end
2020-07-28 23:09:34 +05:30
end
end