2022-08-29 14:09:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe API::RpmProjectPackages do
|
2022-09-01 11:12:07 -04:00
|
|
|
include HttpBasicAuthHelpers
|
2022-09-16 14:10:35 -04:00
|
|
|
include WorkhorseHelpers
|
|
|
|
|
|
|
|
include_context 'workhorse headers'
|
2022-09-01 11:12:07 -04:00
|
|
|
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
let_it_be_with_reload(:project) { create(:project, :public) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
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) }
|
|
|
|
let_it_be(:job) { create(:ci_build, :running, user: user, project: project) }
|
|
|
|
|
2022-08-29 14:09:53 -04:00
|
|
|
let(:headers) { {} }
|
|
|
|
let(:package_name) { 'rpm-package.0-1.x86_64.rpm' }
|
|
|
|
let(:package_file_id) { 1 }
|
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
shared_examples 'rejects rpm packages access' do |status|
|
|
|
|
it_behaves_like 'returning response status', status
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
if status == :unauthorized
|
|
|
|
it 'has the correct response header' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response.headers['WWW-Authenticate']).to eq 'Basic realm="GitLab Packages Registry"'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'process rpm packages upload/download' do |status|
|
|
|
|
it_behaves_like 'returning response status', status
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a deploy token for RPM requests' do
|
|
|
|
context 'with deploy token headers' do
|
2022-08-29 14:09:53 -04:00
|
|
|
before do
|
2022-09-01 11:12:07 -04:00
|
|
|
project.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
|
2022-08-29 14:09:53 -04:00
|
|
|
end
|
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
let(:headers) { basic_auth_header(deploy_token.username, deploy_token.token) }
|
|
|
|
|
|
|
|
context 'when token is valid' do
|
|
|
|
it_behaves_like 'returning response status', :not_found
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is invalid' do
|
|
|
|
let(:headers) { basic_auth_header(deploy_token.username, 'bar') }
|
|
|
|
|
|
|
|
it_behaves_like 'returning response status', :unauthorized
|
|
|
|
end
|
2022-08-29 14:09:53 -04:00
|
|
|
end
|
2022-09-01 11:12:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a job token for RPM requests' do
|
|
|
|
context 'with job token headers' do
|
|
|
|
let(:headers) { basic_auth_header(::Gitlab::Auth::CI_JOB_USER, job.token) }
|
2022-08-29 14:09:53 -04:00
|
|
|
|
|
|
|
before do
|
2022-09-01 11:12:07 -04:00
|
|
|
project.update_column(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
project.add_developer(user)
|
2022-08-29 14:09:53 -04:00
|
|
|
end
|
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
context 'with valid token' do
|
|
|
|
it_behaves_like 'returning response status', :not_found
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid token' do
|
|
|
|
let(:headers) { basic_auth_header(::Gitlab::Auth::CI_JOB_USER, 'bar') }
|
|
|
|
|
|
|
|
it_behaves_like 'returning response status', :unauthorized
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid user' do
|
|
|
|
let(:headers) { basic_auth_header('foo', job.token) }
|
|
|
|
|
|
|
|
it_behaves_like 'returning response status', :unauthorized
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a user token for RPM requests' do
|
|
|
|
context 'with valid project' do
|
|
|
|
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
|
|
|
|
'PUBLIC' | :developer | true | true | 'process rpm packages upload/download' | :not_found
|
|
|
|
'PUBLIC' | :guest | true | true | 'process rpm packages upload/download' | :forbidden
|
|
|
|
'PUBLIC' | :developer | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :developer | false | true | 'process rpm packages upload/download' | :not_found
|
|
|
|
'PUBLIC' | :guest | false | true | 'process rpm packages upload/download' | :not_found
|
|
|
|
'PUBLIC' | :developer | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :anonymous | false | true | 'process rpm packages upload/download' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | true | true | 'process rpm packages upload/download' | :not_found
|
|
|
|
'PRIVATE' | :guest | true | true | 'rejects rpm packages access' | :forbidden
|
|
|
|
'PRIVATE' | :developer | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | false | true | 'rejects rpm packages access' | :not_found
|
|
|
|
'PRIVATE' | :guest | false | true | 'rejects rpm packages access' | :not_found
|
|
|
|
'PRIVATE' | :developer | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :anonymous | false | true | 'rejects rpm 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
|
|
|
|
project.update_column(:visibility_level, Gitlab::VisibilityLevel.level_value(visibility_level))
|
|
|
|
project.send("add_#{user_role}", user) if member && user_role != :anonymous
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like params[:shared_examples_name], params[:expected_status]
|
|
|
|
end
|
2022-08-29 14:09:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /api/v4/projects/:project_id/packages/rpm/repodata/:filename' do
|
2022-09-01 11:12:07 -04:00
|
|
|
let(:url) { "/projects/#{project.id}/packages/rpm/repodata/#{package_name}" }
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
subject { get api(url), headers: headers }
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
it_behaves_like 'a job token for RPM requests'
|
|
|
|
it_behaves_like 'a deploy token for RPM requests'
|
|
|
|
it_behaves_like 'a user token for RPM requests'
|
2022-08-29 14:09:53 -04:00
|
|
|
end
|
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
describe 'GET /api/v4/projects/:id/packages/rpm/:package_file_id/:filename' do
|
|
|
|
let(:url) { "/projects/#{project.id}/packages/rpm/#{package_file_id}/#{package_name}" }
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
subject { get api(url), headers: headers }
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
it_behaves_like 'a job token for RPM requests'
|
|
|
|
it_behaves_like 'a deploy token for RPM requests'
|
|
|
|
it_behaves_like 'a user token for RPM requests'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v4/projects/:project_id/packages/rpm' do
|
|
|
|
let(:url) { "/projects/#{project.id}/packages/rpm" }
|
2022-09-16 14:10:35 -04:00
|
|
|
let(:file_upload) { fixture_file_upload('spec/fixtures/packages/rpm/hello-0.0.1-1.fc29.x86_64.rpm') }
|
2022-09-01 11:12:07 -04:00
|
|
|
|
2022-09-16 14:10:35 -04:00
|
|
|
subject { post api(url), params: { file: file_upload }, headers: headers }
|
2022-09-01 11:12:07 -04:00
|
|
|
|
|
|
|
context 'with user token' do
|
|
|
|
context 'with valid project' do
|
|
|
|
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
|
|
|
|
'PUBLIC' | :developer | true | true | 'process rpm packages upload/download' | :not_found
|
|
|
|
'PUBLIC' | :guest | true | true | 'rejects rpm packages access' | :forbidden
|
|
|
|
'PUBLIC' | :developer | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :developer | false | true | 'rejects rpm packages access' | :not_found
|
|
|
|
'PUBLIC' | :guest | false | true | 'rejects rpm packages access' | :not_found
|
|
|
|
'PUBLIC' | :developer | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :guest | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PUBLIC' | :anonymous | false | true | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | true | true | 'process rpm packages upload/download' | :not_found
|
|
|
|
'PRIVATE' | :guest | true | true | 'rejects rpm packages access' | :forbidden
|
|
|
|
'PRIVATE' | :developer | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | true | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :developer | false | true | 'rejects rpm packages access' | :not_found
|
|
|
|
'PRIVATE' | :guest | false | true | 'rejects rpm packages access' | :not_found
|
|
|
|
'PRIVATE' | :developer | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :guest | false | false | 'rejects rpm packages access' | :unauthorized
|
|
|
|
'PRIVATE' | :anonymous | false | true | 'rejects rpm 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) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.update_column(:visibility_level, Gitlab::VisibilityLevel.level_value(visibility_level))
|
|
|
|
project.send("add_#{user_role}", user) if member && user_role != :anonymous
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like params[:shared_examples_name], params[:expected_status]
|
|
|
|
end
|
|
|
|
end
|
2022-09-16 14:10:35 -04:00
|
|
|
|
|
|
|
context 'when user can upload file' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:headers) { basic_auth_header(user.username, personal_access_token.token).merge(workhorse_headers) }
|
|
|
|
|
|
|
|
context 'when file size too large' do
|
|
|
|
before do
|
|
|
|
allow_next_instance_of(UploadedFile) do |uploaded_file|
|
|
|
|
allow(uploaded_file).to receive(:size).and_return(project.actual_limits.rpm_max_file_size + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
upload_file(params: { file: file_upload }, request_headers: headers)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(response.body).to match(/File is too large/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_file(params: {}, request_headers: headers)
|
|
|
|
url = "/projects/#{project.id}/packages/rpm"
|
|
|
|
workhorse_finalize(
|
|
|
|
api(url),
|
|
|
|
method: :post,
|
|
|
|
file_key: :file,
|
|
|
|
params: params,
|
|
|
|
headers: request_headers,
|
|
|
|
send_rewritten_field: true
|
|
|
|
)
|
|
|
|
end
|
2022-09-01 11:12:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a deploy token for RPM requests'
|
|
|
|
it_behaves_like 'a job token for RPM requests'
|
2022-08-29 14:09:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v4/projects/:project_id/packages/rpm/authorize' do
|
|
|
|
let(:url) { api("/projects/#{project.id}/packages/rpm/authorize") }
|
|
|
|
|
|
|
|
subject { post(url, headers: headers) }
|
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
it_behaves_like 'returning response status', :not_found
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
context 'when feature flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(rpm_packages: false)
|
|
|
|
end
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
it_behaves_like 'returning response status', :not_found
|
|
|
|
end
|
2022-08-29 14:09:53 -04:00
|
|
|
|
2022-09-01 11:12:07 -04:00
|
|
|
context 'when package feature is disabled' do
|
|
|
|
before do
|
|
|
|
stub_config(packages: { enabled: false })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'returning response status', :not_found
|
|
|
|
end
|
2022-08-29 14:09:53 -04:00
|
|
|
end
|
|
|
|
end
|